Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- tag_in = lambda x, v: f'<{x}>{v}</{x}>'
- def replace(matchobj):
- blocks = matchobj.groupdict()
- md_header = blocks['header']
- md_lines = blocks['lines']
- header = ''.join(tag_in('th', v) for v in md_header.split('|') if v)
- lines = ''
- for line in md_lines.strip().split('\n'):
- lines += tag_in('tr', ''.join(tag_in('td', v) for v in line.split('|')))
- return tag_in('table', tag_in('tr', header) + tag_in('tbody', lines))
- def main():
- with open('md.table.txt', 'r', encoding="utf-8") as eyes:
- text = eyes.read()
- reg = r'\|{0,1}(?P<header>([ \w\d\-|_]+\|)+[ \w\d\-|_]+\|{0,1})(\r\n|\r|\n)' + \
- r'\|{0,1}((:){0,1}\--*(:){0,1}\|)+(:){0,1}\--*(:){0,1}\|{0,1}(\r\n|\r|\n)' + \
- r'(?P<lines>(\|{0,1}([ \w\d\-|_]+\|{0,1})+(\r\n|\r|\n))*)'
- r = re.search(reg, text)
- s = re.sub(reg, replace, text)
- print(s)
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement