Skip to content

Commit

Permalink
Fixed Python-Markdown#78. Added support for two line link refs.
Browse files Browse the repository at this point in the history
Also refactored the reference preprocessor to make this a little easier to
implement. Regex does more now.
  • Loading branch information
Waylan Limberg committed Feb 2, 2012
1 parent 3b6c63f commit 9834966
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
27 changes: 14 additions & 13 deletions markdown/preprocessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,25 +257,26 @@ def run(self, lines):
class ReferencePreprocessor(Preprocessor):
""" Remove reference definitions from text and store for later use. """

RE = re.compile(r'^(\ ?\ ?\ ?)\[([^\]]*)\]:\s*([^ ]*)(.*)$', re.DOTALL)
TITLE = r'[ ]*(\"(.*)\"|\'(.*)\'|\((.*)\))[ ]*'
RE = re.compile(r'^[ ]{0,3}\[([^\]]*)\]:\s*([^ ]*)[ ]*(%s)?$' % TITLE, re.DOTALL)
TITLE_RE = re.compile(r'^%s$' % TITLE)

def run (self, lines):
new_text = [];
for line in lines:
while lines:
line = lines.pop(0)
m = self.RE.match(line)
if m:
id = m.group(2).strip().lower()
link = m.group(3).lstrip('<').rstrip('>')
t = m.group(4).strip() # potential title
id = m.group(1).strip().lower()
link = m.group(2).lstrip('<').rstrip('>')
t = m.group(5) or m.group(6) or m.group(7)
if not t:
self.markdown.references[id] = (link, t)
elif (len(t) >= 2
and (t[0] == t[-1] == "\""
or t[0] == t[-1] == "\'"
or (t[0] == "(" and t[-1] == ")") ) ):
self.markdown.references[id] = (link, t[1:-1])
else:
new_text.append(line)
# Check next line for title
tm = self.TITLE_RE.match(lines[0])
if tm:
lines.pop(0)
t = tm.group(2) or tm.group(3) or tm.group(4)
self.markdown.references[id] = (link, t)
else:
new_text.append(line)

Expand Down
3 changes: 2 additions & 1 deletion tests/basic/links-reference.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@
breaks[] with two spaces.</p>
<p><a href="http://example.com" title="No more hanging empty bracket!">short ref</a></p>
<p><a href="http://example.com" title="No more hanging empty bracket!">short
ref</a></p>
ref</a></p>
<p><a href="http://example.com" title="Title on next line.">a ref</a></p>
5 changes: 5 additions & 0 deletions tests/basic/links-reference.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,8 @@ breaks[] with two spaces.
ref]

[short ref]: http://example.com "No more hanging empty bracket!"

[a ref]

[a ref]: http://example.com
"Title on next line."

0 comments on commit 9834966

Please sign in to comment.