Skip to content

Instantly share code, notes, and snippets.

@pansila
Last active December 14, 2018 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pansila/8d4f2869ccae891326959c947571ea67 to your computer and use it in GitHub Desktop.
Save pansila/8d4f2869ccae891326959c947571ea67 to your computer and use it in GitHub Desktop.
An enhanced patch to enable support of markdown for robotframework
from io import BytesIO
from io import StringIO
from .txtreader import TxtReader
import mistune
def MarkDownReader():
class MarkDownReader(object):
keywords = ["setting", "settings", "variable", "variables", "test case", "test cases", "keyword", "keywords"]
def __init__(self):
self.robot_lines = []
self.robot_data = ''
def robotize(self, md_file):
#print('\n========== INPUT :\n', md_file,':')
# uncomment next two lines if want to see raw input in console
# print('\n', md_file.read())
# md_file.seek(0)
parser = mistune.BlockLexer()
text = md_file.read().decode('UTF-8')
parser.parse(mistune.preprocessing(text))
for t in parser.tokens:
if t["type"] == "table":
#print(t)
if t["header"][0].lower() in self.keywords:
robot_data = "| *" + "* | *".join(t["header"]) + "* |\n"
for l in t["cells"]:
robot_data += "| " + " | ".join(l) + " |\n"
#print(robot_data)
self.robot_data += robot_data
f = StringIO(text)
#print('\n========== TEMP :\n', f)
try:
include_line = False
for line in f.readlines():
if not include_line:
include_line = line.strip().lower() == "```robotframework"
elif line.strip() == "```":
include_line = False
else:
self.robot_lines.append(line)
self.robot_data += str(''.join(self.robot_lines))
finally:
f.close()
#print('\n========== OUTPUT :\n', self.robot_data)
return self.robot_data
def read(self, md_file, rawdata):
return self._read_text(self.robotize(md_file), rawdata)
def _read_text(self, data, rawdata):
txtfile = BytesIO(data.encode('UTF-8'))
return TxtReader().read(txtfile, rawdata)
return MarkDownReader()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment