import os import re import codecs import cmarkgfm # pip3 install cmarkgfm from .builder import Builder class HTMLBuilder(Builder): """Traintastic HTML manual builder base class""" def _file_to_html(self, page): with codecs.open(os.path.join(self._language_dir, page['markdown']), 'r', 'utf-8') as md: html = cmarkgfm.github_flavored_markdown_to_html(md.read()) # parse id html = re.sub(r']*)>(.*) {#([a-z0-9-]+)}', r'\3', html) # replace -> by html = re.sub(r'->', '\u2794', html) # set target="_blank" for external links: html = re.sub(r']+href="http(s|)://)', r'(.+)', self._highlight_lua, html) # change img title attribute to figcaption html = re.sub(r'(]+)title="([^">]*)"([^>]*>)', lambda m: '
' + m.group(1) + m.group(3) + '
' + m.group(2) + '
', html) return html def _highlight_lua(self, m): code = m.group(1) code = re.sub(r'\b([A-Z_][A-Z0-9_]*)\b', r'\1', code) # CONSTANTS code = re.sub(r'\b(and|break|do|else|elseif|end|false|for|function|goto|if|in|local|nil|not|or|repeat|return|then|true|until|while)\b', r'\1', code) # keywords return '' + code + ''