Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
mdhelp: fixes in markdown to C parser
  • Loading branch information
perexg committed Apr 4, 2016
1 parent 2aa055d commit 321668b
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 13 deletions.
75 changes: 63 additions & 12 deletions support/doc/md_to_c.py
Expand Up @@ -21,10 +21,7 @@
HUMAN=False
DEBUG=False

NOLANG=[
'.',
','
]
NOLANG_CHARS=" "

def debug(str):
sys.stderr.write('DEBUG: ' + str + '\n')
Expand All @@ -36,18 +33,67 @@ class Object:
class TVH_C_Renderer(Renderer):

def get_nolang(self, text):
if not text:
return ''
if HUMAN:
return text
else:
return '_' + str(len(text)) + ':' + text

def get_lang(self, text):
if text in NOLANG:
return self.get_nolang(text)
if not text:
return ''

n = ''
while text and text[0] in NOLANG_CHARS:
n += text[0]
text = text[1:]
e = ''
while text and text[-1] in NOLANG_CHARS:
e = text[-1] + e
text = text[:-1]

while text.find(' ') >= 0:
text = text.replace(' ', ' ')

if not HUMAN:
text = 'x' + str(len(text)) + ':' + text
return self.get_nolang(n) + text + self.get_nolang(e)

def get_human(self, text):
if HUMAN:
return text
else:
return 'x' + str(len(text)) + ':' + text
xfound = 0
xlink = []
d = ''
r = ''
while text:
type = text[0]
p = text.find(':')
if p <= 0:
fatal('wrong text entry: ' + repr(text))
break
l = int(text[1:p])
o = text[:p+1+l]
t = text[p+1:p+1+l]
text = text[p+l+1:]
if not t:
continue
if xlink:
xlink.append(o)
if t.find(']') >= 0 and (t.endswith(')') or t.endswith(') ')):
d += xfound and self.get_lang(r) or self.get_nolang(r)
xfound = 0
d += ''.join(xlink)
xlink = []
continue
if type == '_' and t == ('[' or t == '!['):
xlink.append(o)
continue
r += t
if type == 'x':
xfound = 1
return d + (xfound and self.get_lang(r) or self.get_nolang(r))

def get_block(self, text):
type = text[0]
Expand All @@ -67,8 +113,13 @@ def text(self, text):
return ''
if DEBUG: debug('text: ' + repr(text))
text = text.replace('\n', ' ')
text = text.replace('\t', ' ')
return self.get_lang(text)

def doescape(self, text):
if DEBUG: debug('escape: ' + repr(text))
return self.get_nolang(text)

def linebreak(self):
if DEBUG: debug('linebreak')
return '\n'
Expand All @@ -83,7 +134,7 @@ def header(self, text, level, raw=None):

def paragraph(self, text):
if DEBUG: debug('paragraph: ' + repr(text))
return '\n' + text + '\n'
return '\n' + self.get_human(text) + '\n'

def list(self, text, ordered=True):
r = '\n'
Expand All @@ -99,7 +150,7 @@ def list_item(self, text):
text = text[1:]
if DEBUG: debug('list item: ' + repr(text))
a = text.split('\n')
text = a[0] + '\n'
text = self.get_human(a[0]) + '\n'
for t in a[1:]:
if t:
text += self.get_nolang(' ') + t + '\n'
Expand Down Expand Up @@ -238,8 +289,8 @@ def table_cell(self, content, **flags):
if type(v) == type(True):
v = v and 1 or 0
v = str(v) and str(v) or ''
r += self.get_nolang('f' + str(len(fl) + 1 + len(v)) + ':' + fl + '=') + v
return r + self.get_nolang('c' + str(len(content)) + ':') + content
r += 'f' + str(len(fl) + 1 + len(v)) + ':' + fl + '=' + v
return r + 'c' + str(len(content)) + ':' + content

def footnote_ref(self, key, index):
return self.get_nolang('[^' + str(index) + ']')
Expand Down
9 changes: 8 additions & 1 deletion support/doc/mistune.py
Expand Up @@ -537,7 +537,7 @@ def manipulate(text):
return output

def output_escape(self, m):
return m.group(1)
return self.renderer.doescape(m.group(1))

def output_autolink(self, m):
link = m.group(1)
Expand Down Expand Up @@ -785,6 +785,13 @@ def text(self, text):
"""
return escape(text)

def doescape(self, text):
"""Rendering escape text.
:param text: text content.
"""
return escape(text)

def autolink(self, link, is_email=False):
"""Rendering a given link or email address.
Expand Down

0 comments on commit 321668b

Please sign in to comment.