Enhance markmin2html.py contrib PEP8
This commit is contained in:
1 parent
99a323c7ad
commit
e48e47beb2
1 file changed
+49
-17
@@ -7,6 +7,7 @@ import re
|
||||
import urllib
|
||||
from cgi import escape
|
||||
from string import maketrans
|
||||
|
||||
try:
|
||||
from ast import parse as ast_parse
|
||||
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_expand_meta = re.compile('(' + META + '|' + DISABLED_META + '|````)')
|
||||
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_del = re.compile(r'~~(?P<t>[^\s*]+( +[^\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_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_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_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")
|
||||
regex_quote = re.compile('(?P<name>\w+?)\s*\=\s*')
|
||||
|
||||
|
||||
def make_dict(b):
|
||||
return '{%s}' % regex_quote.sub("'\g<name>':", b)
|
||||
|
||||
|
||||
def safe_eval(node_or_string, env):
|
||||
"""
|
||||
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')
|
||||
if isinstance(node_or_string, ast.Expression):
|
||||
node_or_string = node_or_string.body
|
||||
|
||||
def _convert(node):
|
||||
if isinstance(node, ast.Str):
|
||||
return node.s
|
||||
@@ -606,24 +614,30 @@ def safe_eval(node_or_string, env):
|
||||
else:
|
||||
return left - right
|
||||
raise ValueError('malformed string')
|
||||
|
||||
return _convert(node_or_string)
|
||||
|
||||
|
||||
def markmin_escape(text):
|
||||
""" insert \\ before markmin control characters: '`:*~[]{}@$ """
|
||||
return regex_markmin_escape.sub(
|
||||
lambda m: '\\' + m.group(0).replace('\\', '\\\\'), text)
|
||||
|
||||
|
||||
def replace_autolinks(text, autolinks):
|
||||
return regex_auto.sub(lambda m: autolinks(m.group('k')), text)
|
||||
|
||||
|
||||
def replace_at_urls(text, url):
|
||||
# this is experimental @{function/args}
|
||||
def u1(match, url=url):
|
||||
a, c, f, args = match.group('a', 'c', 'f', 'args')
|
||||
return url(a=a or None, c=c or None, f=f or None,
|
||||
args=(args or '').split('/'), scheme=True, host=True)
|
||||
|
||||
return regex_URL.sub(u1, text)
|
||||
|
||||
|
||||
def replace_components(text, env):
|
||||
# not perfect but acceptable
|
||||
def u2(match, env=env):
|
||||
@@ -639,16 +653,18 @@ def replace_components(text,env):
|
||||
except Exception, e:
|
||||
f = 'ERROR: %s' % e
|
||||
return str(f)
|
||||
|
||||
text = regex_env2.sub(u2, text)
|
||||
return text
|
||||
|
||||
|
||||
def autolinks_simple(url):
|
||||
"""
|
||||
it automatically converts the url to link,
|
||||
image, video or audio tag
|
||||
"""
|
||||
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)
|
||||
elif u_url.endswith(('.jpg', '.jpeg', '.gif', '.png')):
|
||||
return '<img src="%s" controls />' % url
|
||||
@@ -658,6 +674,7 @@ def autolinks_simple(url):
|
||||
return '<audio src="%s" controls></audio>' % url
|
||||
return '<a href="%s">%s</a>' % (url, url)
|
||||
|
||||
|
||||
def protolinks_simple(proto, url):
|
||||
"""
|
||||
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 proto + ':' + url
|
||||
|
||||
|
||||
def email_simple(email):
|
||||
return '<a href="mailto:%s">%s</a>' % (email, email)
|
||||
|
||||
|
||||
def render(text,
|
||||
extra={},
|
||||
allowed={},
|
||||
@@ -925,8 +944,10 @@ def render(text,
|
||||
>>> render("anchor with name 'NEWLINE': [[NEWLINE [newline] ]]")
|
||||
'<p>anchor with name \\'NEWLINE\\': <span class="anchor" id="markmin_NEWLINE">newline</span></p>'
|
||||
"""
|
||||
if autolinks=="default": autolinks = autolinks_simple
|
||||
if protolinks=="default": protolinks = protolinks_simple
|
||||
if autolinks == "default":
|
||||
autolinks = autolinks_simple
|
||||
if protolinks == "default":
|
||||
protolinks = protolinks_simple
|
||||
pp = '\n' if pretty_print else ''
|
||||
if isinstance(text, unicode):
|
||||
text = text.encode('utf8')
|
||||
@@ -945,6 +966,7 @@ def render(text,
|
||||
# store them into segments they will be treated as code
|
||||
#############################################################
|
||||
segments = []
|
||||
|
||||
def mark_code(m):
|
||||
g = m.group(0)
|
||||
if g in (META, DISABLED_META):
|
||||
@@ -956,10 +978,12 @@ def render(text,
|
||||
else:
|
||||
c = m.group('c') 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('!`!', '`')
|
||||
segments.append((code, c, p, m.group(0)))
|
||||
return META
|
||||
|
||||
text = regex_code.sub(mark_code, text)
|
||||
|
||||
#############################################################
|
||||
@@ -967,10 +991,12 @@ def render(text,
|
||||
# store them into links they will be treated as link
|
||||
#############################################################
|
||||
links = []
|
||||
|
||||
def mark_link(m):
|
||||
links.append(None if m.group() == LINK
|
||||
else m.group('s'))
|
||||
return LINK
|
||||
|
||||
text = regex_link.sub(mark_link, text)
|
||||
text = escape(text)
|
||||
|
||||
@@ -1264,9 +1290,11 @@ def render(text,
|
||||
ltags = []
|
||||
tlev = []
|
||||
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 pend: etags=[pend]
|
||||
if pend:
|
||||
etags = [pend]
|
||||
out.append(pbeg)
|
||||
mtag = 'p'
|
||||
else:
|
||||
@@ -1336,7 +1364,7 @@ def render(text,
|
||||
t = t or ''
|
||||
a = escape(a) if a else ''
|
||||
if k:
|
||||
if '#' in k and not ':' in k.split('#')[0]:
|
||||
if '#' in k and ':' not in k.split('#')[0]:
|
||||
# wikipage, not external url
|
||||
k = k.replace('#', '#' + id_prefix)
|
||||
k = escape(k)
|
||||
@@ -1358,7 +1386,7 @@ def render(text,
|
||||
parts = text.split(LINK)
|
||||
text = parts[0]
|
||||
for i, s in enumerate(links):
|
||||
if s == None:
|
||||
if s is None:
|
||||
html = LINK
|
||||
else:
|
||||
html = regex_media_level2.sub(sub_media, s)
|
||||
@@ -1374,19 +1402,20 @@ def render(text,
|
||||
#############################################################
|
||||
def expand_meta(m):
|
||||
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)
|
||||
if b in extra:
|
||||
if code[:1]=='\n': code=code[1:]
|
||||
if code[-1:]=='\n': code=code[:-1]
|
||||
if code[:1] == '\n':
|
||||
code = code[1:]
|
||||
if code[-1:] == '\n':
|
||||
code = code[:-1]
|
||||
if p:
|
||||
return str(extra[b](code, p))
|
||||
else:
|
||||
return str(extra[b](code))
|
||||
elif b == 'cite':
|
||||
return '['+','.join('<a href="#%s" class="%s">%s</a>' \
|
||||
% (id_prefix+d,b,d) \
|
||||
for d in escape(code).split(','))+']'
|
||||
return '[' + ','.join('<a href="#%s" class="%s">%s</a>' %
|
||||
(id_prefix + d, b, d) for d in escape(code).split(',')) + ']'
|
||||
elif b == 'latex':
|
||||
return LATEX % urllib.quote(code)
|
||||
elif b in html_colors:
|
||||
@@ -1407,6 +1436,7 @@ def render(text,
|
||||
if beg and end:
|
||||
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]))
|
||||
|
||||
text = regex_expand_meta.sub(expand_meta, text)
|
||||
|
||||
if environment:
|
||||
@@ -1423,10 +1453,12 @@ def markmin2html(text, extra={}, allowed={}, sep='p',
|
||||
class_prefix=class_prefix, id_prefix=id_prefix,
|
||||
pretty_print=pretty_print)
|
||||
|
||||
|
||||
def run_doctests():
|
||||
import doctest
|
||||
doctest.testmod()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import sys
|
||||
import doctest
|
||||
@@ -1467,6 +1499,7 @@ if __name__ == '__main__':
|
||||
body=markmin2html(__doc__, pretty_print=True))
|
||||
elif sys.argv[1:2] == ['-t']:
|
||||
from timeit import Timer
|
||||
|
||||
loops = 1000
|
||||
ts = Timer("markmin2html(__doc__)", "from markmin2html import markmin2html")
|
||||
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 [@path_to/css] - process file.markmin + link path_to/css (optional)"
|
||||
run_doctests()
|
||||
|
||||
Reference in new issue
Block a user