Skip to content

Commit

Permalink
Better commenting for LaTeXRender module
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexNisnevich committed Apr 20, 2012
1 parent d37cacd commit e02e949
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 25 deletions.
56 changes: 32 additions & 24 deletions MarkdownPP/Modules/LaTeXRender.py
@@ -1,4 +1,4 @@
# Copyright (C) 2010 John Reese # Copyright (C) 2012 Alex Nisnevich
# Licensed under the MIT license # Licensed under the MIT license


import re import re
Expand All @@ -18,24 +18,6 @@ class LaTeXRender(Module):
Rendering is performed using QuickLaTeX via ProblemSetMarmoset. Rendering is performed using QuickLaTeX via ProblemSetMarmoset.
""" """


def render(self, formula):
formula = formula.replace("$", "")
encoded_formula = formula.replace("%","[comment]").replace("+","%2B")
display_formula = formula.replace("\n","")
print 'Rendering: %s ...' % display_formula

params = urllib.urlencode({'engine': 'quicklatex', 'input': encoded_formula})
headers = {"Content-type": "application/x-www-form-urlencoded",
"Accept": "text/plain"}
conn = httplib.HTTPConnection("www.problemsetmarmoset.com")

conn.request("POST", "/latex/render.php", params, headers)
response = conn.getresponse()
img_url = response.read()

rendered_tex = '![{}]({})\n'.format(display_formula, img_url)
return rendered_tex

def transform(self, data): def transform(self, data):
transforms = [] transforms = []
linenum = 0 linenum = 0
Expand All @@ -44,37 +26,63 @@ def transform(self, data):
in_fenced_code_block = False in_fenced_code_block = False


for line in data: for line in data:
# Fenced code blocks (Github-flavored markdown) # Handling fenced code blocks (for Github-flavored markdown)
match = fencedcodere.search(line) if fencedcodere.search(line):
if match:
if in_fenced_code_block: if in_fenced_code_block:
in_fenced_code_block = False in_fenced_code_block = False
else: else:
in_fenced_code_block = True in_fenced_code_block = True


is_code_block = codere.search(line) # Are we in a code block?
if not in_fenced_code_block and not is_code_block: if not in_fenced_code_block and not codere.search(line):
# Is this line part of an existing LaTeX block?
if in_block: if in_block:
transforms.append(Transform(linenum, "drop")) transforms.append(Transform(linenum, "drop"))
current_block += "\n" + line current_block += "\n" + line


match = singlelinere.search(line) match = singlelinere.search(line)
if match: if match:
# Single LaTeX line
tex = match.group(0) tex = match.group(0)
before_tex = line[0:line.find(tex)] before_tex = line[0:line.find(tex)]
after_tex = line[(line.find(tex) + len(tex)) : len(line)] after_tex = line[(line.find(tex) + len(tex)) : len(line)]
transforms.append(Transform(linenum, "swap", before_tex + self.render(tex) + after_tex)) transforms.append(Transform(linenum, "swap", before_tex + self.render(tex) + after_tex))
else: else:
match = startorendre.search(line) match = startorendre.search(line)
if match: if match:
# Starting or ending a multi-line LaTeX block
if in_block: if in_block:
# Ending a LaTeX block
transforms.pop() # undo last drop transforms.pop() # undo last drop
transforms.append(Transform(linenum, "swap", self.render(current_block))) transforms.append(Transform(linenum, "swap", self.render(current_block)))
else: else:
# Starting a LaTeX block
current_block = line current_block = line
transforms.append(Transform(linenum, "drop")) transforms.append(Transform(linenum, "drop"))
in_block = not in_block in_block = not in_block


linenum += 1 linenum += 1


return transforms return transforms

def render(self, formula):
# Prepare the formula
formula = formula.replace("$", "")
encoded_formula = formula.replace("%","[comment]").replace("+","%2B")
display_formula = formula.replace("\n","")
print 'Rendering: %s ...' % display_formula

# Prepare POST request to QuickLaTeX via ProblemSetMarmoset (for added processing)
params = urllib.urlencode({'engine': 'quicklatex', 'input': encoded_formula})
headers = {"Content-type": "application/x-www-form-urlencoded",
"Accept": "text/plain"}
conn = httplib.HTTPConnection("www.problemsetmarmoset.com")

# Make the request
conn.request("POST", "/latex/render.php", params, headers)
response = conn.getresponse()
img_url = response.read()

# Display as Markdown image
rendered_tex = '![{0}]({1} "{0}")\n'.format(display_formula, img_url)
return rendered_tex
2 changes: 1 addition & 1 deletion readme.md
Expand Up @@ -117,7 +117,7 @@ For example,


becomes becomes


![\displaystyle \int x^2 = \frac{x^3}{3} + C](http://quicklatex.com/cache3/ql_0fa1a137ae5e89171b4ecb24ce76998f_l3.png) ![\displaystyle \int x^2 = \frac{x^3}{3} + C](http://quicklatex.com/cache3/ql_0fa1a137ae5e89171b4ecb24ce76998f_l3.png "\displaystyle \int x^2 = \frac{x^3}{3} + C")




<a name="examples"></a> <a name="examples"></a>
Expand Down

0 comments on commit e02e949

Please sign in to comment.