Thanks to the work from HOW-TO enable MarkDown support in RobotFramework
I went a bit further by enabling markdown support with test cases written in the tables.
Thanks to the work from HOW-TO enable MarkDown support in RobotFramework
I went a bit further by enabling markdown support with test cases written in the tables.
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() |