Enhance markmin2html.py contrib PEP8

This commit is contained in:
Hardirc
2016-04-08 22:12:43 -04:00
parent 99a323c7ad
commit e48e47beb2
+49 -17
View File
@@ -7,6 +7,7 @@ import re
import urllib import urllib
from cgi import escape from cgi import escape
from string import maketrans from string import maketrans
try: try:
from ast import parse as ast_parse from ast import parse as ast_parse
import ast import ast
@@ -542,7 +543,9 @@ regex_URL=re.compile(r'@/(?P<a>\w*)/(?P<c>\w*)/(?P<f>\w*(\.\w+)?)(/(?P<args>[\w\
regex_env2 = re.compile(r'@\{(?P<a>[\w\-\.]+?)(\:(?P<b>.*?))?\}') regex_env2 = re.compile(r'@\{(?P<a>[\w\-\.]+?)(\:(?P<b>.*?))?\}')
regex_expand_meta = re.compile('(' + META + '|' + DISABLED_META + '|````)') regex_expand_meta = re.compile('(' + META + '|' + DISABLED_META + '|````)')
regex_dd = re.compile(r'\$\$(?P<latex>.*?)\$\$') regex_dd = re.compile(r'\$\$(?P<latex>.*?)\$\$')
regex_code = re.compile('('+META+'|'+DISABLED_META+r'|````)|(``(?P<t>.+?)``(?::(?P<c>[a-zA-Z][_a-zA-Z\-\d]*)(?:\[(?P<p>[^\]]*)\])?)?)',re.S) regex_code = re.compile(
'(' + META + '|' + DISABLED_META + r'|````)|(``(?P<t>.+?)``(?::(?P<c>[a-zA-Z][_a-zA-Z\-\d]*)(?:\[(?P<p>[^\]]*)\])?)?)',
re.S)
regex_strong = re.compile(r'\*\*(?P<t>[^\s*]+( +[^\s*]+)*)\*\*') regex_strong = re.compile(r'\*\*(?P<t>[^\s*]+( +[^\s*]+)*)\*\*')
regex_del = re.compile(r'~~(?P<t>[^\s*]+( +[^\s*]+)*)~~') regex_del = re.compile(r'~~(?P<t>[^\s*]+( +[^\s*]+)*)~~')
regex_em = re.compile(r"''(?P<t>([^\s']| |'(?!'))+)''") regex_em = re.compile(r"''(?P<t>([^\s']| |'(?!'))+)''")
@@ -554,7 +557,9 @@ regex_proto = re.compile(r'(?<!["\w>/=])(?P<p>\w+):(?P<k>\w+://[\w\d\-+=?%&/:.]+
regex_auto = re.compile(r'(?<!["\w>/=])(?P<k>\w+://[\w\d\-+_=?%&/:.,;#]+\w|[\w\-.]+@[\w\-.]+)', re.M) regex_auto = re.compile(r'(?<!["\w>/=])(?P<k>\w+://[\w\d\-+_=?%&/:.,;#]+\w|[\w\-.]+@[\w\-.]+)', re.M)
regex_link = re.compile(r'(' + LINK + r')|\[\[(?P<s>.+?)\]\]', re.S) regex_link = re.compile(r'(' + LINK + r')|\[\[(?P<s>.+?)\]\]', re.S)
regex_link_level2 = re.compile(r'^(?P<t>\S.*?)?(?:\s+\[(?P<a>.+?)\])?(?:\s+(?P<k>\S+))?(?:\s+(?P<p>popup))?\s*$', re.S) regex_link_level2 = re.compile(r'^(?P<t>\S.*?)?(?:\s+\[(?P<a>.+?)\])?(?:\s+(?P<k>\S+))?(?:\s+(?P<p>popup))?\s*$', re.S)
regex_media_level2=re.compile(r'^(?P<t>\S.*?)?(?:\s+\[(?P<a>.+?)\])?(?:\s+(?P<k>\S+))?\s+(?P<p>img|IMG|left|right|center|video|audio|blockleft|blockright)(?:\s+(?P<w>\d+px))?\s*$',re.S) regex_media_level2 = re.compile(
r'^(?P<t>\S.*?)?(?:\s+\[(?P<a>.+?)\])?(?:\s+(?P<k>\S+))?\s+(?P<p>img|IMG|left|right|center|video|audio|blockleft|blockright)(?:\s+(?P<w>\d+px))?\s*$',
re.S)
regex_markmin_escape = re.compile(r"(\\*)(['`:*~\\[\]{}@\$+\-.#\n])") regex_markmin_escape = re.compile(r"(\\*)(['`:*~\\[\]{}@\$+\-.#\n])")
regex_backslash = re.compile(r"\\(['`:*~\\[\]{}@\$+\-.#\n])") regex_backslash = re.compile(r"\\(['`:*~\\[\]{}@\$+\-.#\n])")
@@ -562,9 +567,11 @@ ttab_in = maketrans("'`:*~\\[]{}@$+-.#\n", '\x0b\x0c\x0e\x0f\x10\x11\x12\x13\x1
ttab_out = maketrans('\x0b\x0c\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x05', "'`:*~\\[]{}@$+-.#\n") ttab_out = maketrans('\x0b\x0c\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x05', "'`:*~\\[]{}@$+-.#\n")
regex_quote = re.compile('(?P<name>\w+?)\s*\=\s*') regex_quote = re.compile('(?P<name>\w+?)\s*\=\s*')
def make_dict(b): def make_dict(b):
return '{%s}' % regex_quote.sub("'\g<name>':", b) return '{%s}' % regex_quote.sub("'\g<name>':", b)
def safe_eval(node_or_string, env): def safe_eval(node_or_string, env):
""" """
Safely evaluate an expression node or a string containing a Python Safely evaluate an expression node or a string containing a Python
@@ -578,6 +585,7 @@ def safe_eval(node_or_string, env):
node_or_string = ast_parse(node_or_string, mode='eval') node_or_string = ast_parse(node_or_string, mode='eval')
if isinstance(node_or_string, ast.Expression): if isinstance(node_or_string, ast.Expression):
node_or_string = node_or_string.body node_or_string = node_or_string.body
def _convert(node): def _convert(node):
if isinstance(node, ast.Str): if isinstance(node, ast.Str):
return node.s return node.s
@@ -606,24 +614,30 @@ def safe_eval(node_or_string, env):
else: else:
return left - right return left - right
raise ValueError('malformed string') raise ValueError('malformed string')
return _convert(node_or_string) return _convert(node_or_string)
def markmin_escape(text): def markmin_escape(text):
""" insert \\ before markmin control characters: '`:*~[]{}@$ """ """ insert \\ before markmin control characters: '`:*~[]{}@$ """
return regex_markmin_escape.sub( return regex_markmin_escape.sub(
lambda m: '\\' + m.group(0).replace('\\', '\\\\'), text) lambda m: '\\' + m.group(0).replace('\\', '\\\\'), text)
def replace_autolinks(text, autolinks): def replace_autolinks(text, autolinks):
return regex_auto.sub(lambda m: autolinks(m.group('k')), text) return regex_auto.sub(lambda m: autolinks(m.group('k')), text)
def replace_at_urls(text, url): def replace_at_urls(text, url):
# this is experimental @{function/args} # this is experimental @{function/args}
def u1(match, url=url): def u1(match, url=url):
a, c, f, args = match.group('a', 'c', 'f', 'args') a, c, f, args = match.group('a', 'c', 'f', 'args')
return url(a=a or None, c=c or None, f=f or None, return url(a=a or None, c=c or None, f=f or None,
args=(args or '').split('/'), scheme=True, host=True) args=(args or '').split('/'), scheme=True, host=True)
return regex_URL.sub(u1, text) return regex_URL.sub(u1, text)
def replace_components(text, env): def replace_components(text, env):
# not perfect but acceptable # not perfect but acceptable
def u2(match, env=env): def u2(match, env=env):
@@ -639,16 +653,18 @@ def replace_components(text,env):
except Exception, e: except Exception, e:
f = 'ERROR: %s' % e f = 'ERROR: %s' % e
return str(f) return str(f)
text = regex_env2.sub(u2, text) text = regex_env2.sub(u2, text)
return text return text
def autolinks_simple(url): def autolinks_simple(url):
""" """
it automatically converts the url to link, it automatically converts the url to link,
image, video or audio tag image, video or audio tag
""" """
u_url = url.lower() u_url = url.lower()
if '@' in url and not '://' in url: if '@' in url and '://' not in url:
return '<a href="mailto:%s">%s</a>' % (url, url) return '<a href="mailto:%s">%s</a>' % (url, url)
elif u_url.endswith(('.jpg', '.jpeg', '.gif', '.png')): elif u_url.endswith(('.jpg', '.jpeg', '.gif', '.png')):
return '<img src="%s" controls />' % url return '<img src="%s" controls />' % url
@@ -658,6 +674,7 @@ def autolinks_simple(url):
return '<audio src="%s" controls></audio>' % url return '<audio src="%s" controls></audio>' % url
return '<a href="%s">%s</a>' % (url, url) return '<a href="%s">%s</a>' % (url, url)
def protolinks_simple(proto, url): def protolinks_simple(proto, url):
""" """
it converts url to html-string using appropriate proto-prefix: it converts url to html-string using appropriate proto-prefix:
@@ -675,9 +692,11 @@ def protolinks_simple(proto, url):
return '<img style="width:100px" src="http://chart.apis.google.com/chart?cht=qr&chs=100x100&chl=%s&choe=UTF-8&chld=H" alt="QR Code" title="QR Code" />' % url return '<img style="width:100px" src="http://chart.apis.google.com/chart?cht=qr&chs=100x100&chl=%s&choe=UTF-8&chld=H" alt="QR Code" title="QR Code" />' % url
return proto + ':' + url return proto + ':' + url
def email_simple(email): def email_simple(email):
return '<a href="mailto:%s">%s</a>' % (email, email) return '<a href="mailto:%s">%s</a>' % (email, email)
def render(text, def render(text,
extra={}, extra={},
allowed={}, allowed={},
@@ -925,8 +944,10 @@ def render(text,
>>> render("anchor with name 'NEWLINE': [[NEWLINE [newline] ]]") >>> render("anchor with name 'NEWLINE': [[NEWLINE [newline] ]]")
'<p>anchor with name \\'NEWLINE\\': <span class="anchor" id="markmin_NEWLINE">newline</span></p>' '<p>anchor with name \\'NEWLINE\\': <span class="anchor" id="markmin_NEWLINE">newline</span></p>'
""" """
if autolinks=="default": autolinks = autolinks_simple if autolinks == "default":
if protolinks=="default": protolinks = protolinks_simple autolinks = autolinks_simple
if protolinks == "default":
protolinks = protolinks_simple
pp = '\n' if pretty_print else '' pp = '\n' if pretty_print else ''
if isinstance(text, unicode): if isinstance(text, unicode):
text = text.encode('utf8') text = text.encode('utf8')
@@ -945,6 +966,7 @@ def render(text,
# store them into segments they will be treated as code # store them into segments they will be treated as code
############################################################# #############################################################
segments = [] segments = []
def mark_code(m): def mark_code(m):
g = m.group(0) g = m.group(0)
if g in (META, DISABLED_META): if g in (META, DISABLED_META):
@@ -956,10 +978,12 @@ def render(text,
else: else:
c = m.group('c') or '' c = m.group('c') or ''
p = m.group('p') or '' p = m.group('p') or ''
if 'code' in allowed and not c in allowed['code']: c = '' if 'code' in allowed and c not in allowed['code']:
c = ''
code = m.group('t').replace('!`!', '`') code = m.group('t').replace('!`!', '`')
segments.append((code, c, p, m.group(0))) segments.append((code, c, p, m.group(0)))
return META return META
text = regex_code.sub(mark_code, text) text = regex_code.sub(mark_code, text)
############################################################# #############################################################
@@ -967,10 +991,12 @@ def render(text,
# store them into links they will be treated as link # store them into links they will be treated as link
############################################################# #############################################################
links = [] links = []
def mark_link(m): def mark_link(m):
links.append(None if m.group() == LINK links.append(None if m.group() == LINK
else m.group('s')) else m.group('s'))
return LINK return LINK
text = regex_link.sub(mark_link, text) text = regex_link.sub(mark_link, text)
text = escape(text) text = escape(text)
@@ -1264,9 +1290,11 @@ def render(text,
ltags = [] ltags = []
tlev = [] tlev = []
lev = 0 lev = 0
if br and mtag == 'p': out.append(br) if br and mtag == 'p':
out.append(br)
if mtag != 'q' and s != META: if mtag != 'q' and s != META:
if pend: etags=[pend] if pend:
etags = [pend]
out.append(pbeg) out.append(pbeg)
mtag = 'p' mtag = 'p'
else: else:
@@ -1336,7 +1364,7 @@ def render(text,
t = t or '' t = t or ''
a = escape(a) if a else '' a = escape(a) if a else ''
if k: if k:
if '#' in k and not ':' in k.split('#')[0]: if '#' in k and ':' not in k.split('#')[0]:
# wikipage, not external url # wikipage, not external url
k = k.replace('#', '#' + id_prefix) k = k.replace('#', '#' + id_prefix)
k = escape(k) k = escape(k)
@@ -1358,7 +1386,7 @@ def render(text,
parts = text.split(LINK) parts = text.split(LINK)
text = parts[0] text = parts[0]
for i, s in enumerate(links): for i, s in enumerate(links):
if s == None: if s is None:
html = LINK html = LINK
else: else:
html = regex_media_level2.sub(sub_media, s) html = regex_media_level2.sub(sub_media, s)
@@ -1374,19 +1402,20 @@ def render(text,
############################################################# #############################################################
def expand_meta(m): def expand_meta(m):
code, b, p, s = segments.pop(0) code, b, p, s = segments.pop(0)
if code==None or m.group() == DISABLED_META: if code is None or m.group() == DISABLED_META:
return escape(s) return escape(s)
if b in extra: if b in extra:
if code[:1]=='\n': code=code[1:] if code[:1] == '\n':
if code[-1:]=='\n': code=code[:-1] code = code[1:]
if code[-1:] == '\n':
code = code[:-1]
if p: if p:
return str(extra[b](code, p)) return str(extra[b](code, p))
else: else:
return str(extra[b](code)) return str(extra[b](code))
elif b == 'cite': elif b == 'cite':
return '['+','.join('<a href="#%s" class="%s">%s</a>' \ return '[' + ','.join('<a href="#%s" class="%s">%s</a>' %
% (id_prefix+d,b,d) \ (id_prefix + d, b, d) for d in escape(code).split(',')) + ']'
for d in escape(code).split(','))+']'
elif b == 'latex': elif b == 'latex':
return LATEX % urllib.quote(code) return LATEX % urllib.quote(code)
elif b in html_colors: elif b in html_colors:
@@ -1407,6 +1436,7 @@ def render(text,
if beg and end: if beg and end:
return '<pre><code%s%s>%s</code></pre>%s' % (cls, id, escape(code[1:-1]), pp) return '<pre><code%s%s>%s</code></pre>%s' % (cls, id, escape(code[1:-1]), pp)
return '<code%s%s>%s</code>' % (cls, id, escape(code[beg:end])) return '<code%s%s>%s</code>' % (cls, id, escape(code[beg:end]))
text = regex_expand_meta.sub(expand_meta, text) text = regex_expand_meta.sub(expand_meta, text)
if environment: if environment:
@@ -1423,10 +1453,12 @@ def markmin2html(text, extra={}, allowed={}, sep='p',
class_prefix=class_prefix, id_prefix=id_prefix, class_prefix=class_prefix, id_prefix=id_prefix,
pretty_print=pretty_print) pretty_print=pretty_print)
def run_doctests(): def run_doctests():
import doctest import doctest
doctest.testmod() doctest.testmod()
if __name__ == '__main__': if __name__ == '__main__':
import sys import sys
import doctest import doctest
@@ -1467,6 +1499,7 @@ if __name__ == '__main__':
body=markmin2html(__doc__, pretty_print=True)) body=markmin2html(__doc__, pretty_print=True))
elif sys.argv[1:2] == ['-t']: elif sys.argv[1:2] == ['-t']:
from timeit import Timer from timeit import Timer
loops = 1000 loops = 1000
ts = Timer("markmin2html(__doc__)", "from markmin2html import markmin2html") ts = Timer("markmin2html(__doc__)", "from markmin2html import markmin2html")
print 'timeit "markmin2html(__doc__)":' print 'timeit "markmin2html(__doc__)":'
@@ -1502,4 +1535,3 @@ if __name__ == '__main__':
print " file.markmin [file.css] - process file.markmin + built in file.css (optional)" print " file.markmin [file.css] - process file.markmin + built in file.css (optional)"
print " file.markmin [@path_to/css] - process file.markmin + link path_to/css (optional)" print " file.markmin [@path_to/css] - process file.markmin + link path_to/css (optional)"
run_doctests() run_doctests()