Skip to content

Commit

Permalink
Add template to the Example parser module
Browse files Browse the repository at this point in the history
  • Loading branch information
cbonte committed Apr 21, 2012
1 parent 72e6003 commit c864bd5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 17 deletions.
45 changes: 28 additions & 17 deletions parser/example.py
@@ -1,63 +1,74 @@
import re
import parser

# Detect examples blocks
class Parser(parser.Parser):
def __init__(self, pctxt):
parser.Parser.__init__(self, pctxt)
template = pctxt.templates.get_template("parser/example/comment.tpl")
self.comment = template.render().strip()


def parse(self, line):
pctxt = self.pctxt
res = ""
if re.search(r'Examples? *:', line):
# Detect examples blocks
result = re.search(r'(Examples? *:)', line)
if result:
label = result.group(0)

desc_indent = False
desc = re.sub(r'.*Examples? *:', '', line).strip()

# Some examples have a description
if desc:
desc_indent = len(line) - len(desc)

line = re.sub(r'(Examples? *:)', r'<span class="label label-success">\1</span>', line)
indent = self.get_indent(line)

if desc:
res += line[:len(line) - len(desc)]
# And some description are on multiple lines
while pctxt.get_line(1) and self.get_indent(pctxt.get_line(1)) == desc_indent:
desc += " " + pctxt.get_line(1).strip()
pctxt.next()
else:
res += line

pctxt.next()
add_empty_line = pctxt.eat_empty_lines()

content = []

if self.get_indent(pctxt.get_line()) > indent:
res += '<pre class="prettyprint">'
if desc:
desc = desc[0].upper() + desc[1:]
res += '<div class="example-desc">%s</div>' % desc
add_empty_line = 0
while pctxt.has_more_lines() and ((not pctxt.get_line()) or (self.get_indent(pctxt.get_line()) > indent)):
if pctxt.get_line():
for j in xrange(0, add_empty_line):
res += "\n"
line = re.sub(r'(#.*)$', r'<span class="comment">\1</span>', pctxt.get_line())
res += line + '\n'
content.append("")

content.append(re.sub(r'(#.*)$', self.comment, pctxt.get_line()))
add_empty_line = 0
else:
add_empty_line += 1
pctxt.next()
res += "</pre>"
elif self.get_indent(pctxt.get_line()) == indent:
# Simple example that can't have empty lines
res += '<pre class="prettyprint">'
if add_empty_line:
# This means that the example was on the same line as the 'Example' tag
res += " " * indent + desc
# This means that the example was on the same line as the 'Example' tag
content.append(" " * indent + desc)
desc = False
else:
while pctxt.has_more_lines() and (self.get_indent(pctxt.get_line()) == indent):
res += pctxt.get_line()
content.append(pctxt.get_line())
pctxt.next()
pctxt.eat_empty_lines() # Skip empty remaining lines
res += "</pre>"

template = pctxt.templates.get_template("parser/example.tpl")
res = template.render(
label=label,
desc=desc,
content=content
)

pctxt.stop = True
else:
res = line
Expand Down
9 changes: 9 additions & 0 deletions templates/parser/example.tpl
@@ -0,0 +1,9 @@
<span class="label label-success">${label}</span>
<pre class="prettyprint">
% if desc:
<div class="example-desc">${desc}</div>
% endif
% for line in content:
${line}
% endfor
</pre>
1 change: 1 addition & 0 deletions templates/parser/example/comment.tpl
@@ -0,0 +1 @@
<span class="comment">\1</span>

0 comments on commit c864bd5

Please sign in to comment.