Skip to content

Commit 1de595a

Browse files
facelessuserwaylan
authored andcommitted
Fix raw html reference issue (Python-Markdown#585)
Preserve the line which a reference was on to prevent raw HTML indexing issue. Fixes Python-Markdown#584. Prevent raw HTML parsing issue in abbr and footnotes Peserve abbreviation line when stripping and preserve a line for each footnote block. Footnotes should also accumulate the extraneous padding. Test extra lines at the end of references Strip the gathered extraneous whitespace When processing footnotes, we don't actually care to process the extra whitespace at the end of a footnote, but we want it to calculate lines to preserve.
1 parent bbada79 commit 1de595a

File tree

5 files changed

+203
-1
lines changed

5 files changed

+203
-1
lines changed

markdown/extensions/abbr.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ def run(self, lines):
5353
title = m.group('title').strip()
5454
self.markdown.inlinePatterns['abbr-%s' % abbr] = \
5555
AbbrPattern(self._generate_pattern(abbr), title)
56+
# Preserve the line to prevent raw HTML indexing issue.
57+
# https://github.com/Python-Markdown/markdown/issues/584
58+
new_text.append('')
5659
else:
5760
new_text.append(line)
5861
return new_text

markdown/extensions/footnotes.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,12 @@ def run(self, lines):
238238
fn, _i = self.detectTabbed(lines[i+1:])
239239
fn.insert(0, m.group(2))
240240
i += _i-1 # skip past footnote
241-
self.footnotes.setFootnote(m.group(1), "\n".join(fn))
241+
footnote = "\n".join(fn)
242+
self.footnotes.setFootnote(m.group(1), footnote.rstrip())
243+
# Preserve a line for each block to prevent raw HTML indexing issue.
244+
# https://github.com/Python-Markdown/markdown/issues/584
245+
num_blocks = (len(footnote.split('\n\n')) * 2)
246+
newlines.extend([''] * (num_blocks))
242247
else:
243248
newlines.append(lines[i])
244249
if len(lines) > i+1:
@@ -290,6 +295,11 @@ def detab(line):
290295
if lines[j].strip():
291296
next_line = lines[j]
292297
break
298+
else:
299+
# Include extreaneous padding to prevent raw HTML
300+
# parsing issue: https://github.com/Python-Markdown/markdown/issues/584
301+
items.append("")
302+
i += 1
293303
else:
294304
break # There is no more text; we are done.
295305

markdown/preprocessors.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,9 @@ def run(self, lines):
346346
lines.pop(0)
347347
t = tm.group(2) or tm.group(3) or tm.group(4)
348348
self.markdown.references[id] = (link, t)
349+
# Preserve the line to prevent raw HTML indexing issue.
350+
# https://github.com/Python-Markdown/markdown/issues/584
351+
new_text.append('')
349352
else:
350353
new_text.append(line)
351354

tests/extensions/extra/raw-html.html

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,79 @@
4646
<p>foo bar</p>
4747
<p><em>bar</em>
4848
</p>
49+
</div>
50+
<div name="issue584">
51+
<div>
52+
<p><a href="http://example.com">link</a></p>
53+
</div>
54+
</div>
55+
<div name="issue584">
56+
<div>
57+
<p><abbr title="Abbreviation">abbr</abbr></p>
58+
</div>
59+
</div>
60+
<div name="issue584">
61+
<div>
62+
<p>footnote<sup id="fnref:1"><a class="footnote-ref" href="#fn:1" rel="footnote">1</a></sup></p>
63+
</div>
64+
</div>
65+
<div name="issue584">
66+
<div>
67+
<p><a href="http://example.com">link</a></p>
68+
</div>
69+
</div>
70+
<div name="issue584">
71+
<div>
72+
<p><abbr title="Abbreviation">abbr</abbr></p>
73+
</div>
74+
</div>
75+
<div name="issue584">
76+
<div>
77+
<p>footnote<sup id="fnref:2"><a class="footnote-ref" href="#fn:2" rel="footnote">2</a></sup></p>
78+
</div>
79+
</div>
80+
<div class="footnote">
81+
<hr />
82+
<ol>
83+
<li id="fn:1">
84+
<ol>
85+
<li>
86+
<p>The top couple half figure, contrary sides and hands across with bottom couple,</p>
87+
<p>Half figure back on your own sides, and turn partner to places,</p>
88+
<p>Swing partners with right hands into straight line long-ways, as in a reel, and</p>
89+
<p>Set,</p>
90+
<p>Hey and return to places,</p>
91+
<p>The other three couples do the same.</p>
92+
</li>
93+
<li>
94+
<p>Top and bottom couples meet and set,</p>
95+
<p>Then each gentleman leas the opposite lady to the couple on his left, and set,</p>
96+
<p>Aach four right and left,</p>
97+
<p>Swing side couples to places, and turn partners all eight,</p>
98+
<p>The other two couple o the same.</p>
99+
</li>
100+
</ol>
101+
<p><a class="footnote-backref" href="#fnref:1" rev="footnote" title="Jump back to footnote 1 in the text">&#8617;</a></p>
102+
</li>
103+
<li id="fn:2">
104+
<ol>
105+
<li>
106+
<p>The top couple half figure, contrary sides and hands across with bottom couple,</p>
107+
<p>Half figure back on your own sides, and turn partner to places,</p>
108+
<p>Swing partners with right hands into straight line long-ways, as in a reel, and</p>
109+
<p>Set,</p>
110+
<p>Hey and return to places,</p>
111+
<p>The other three couples do the same.</p>
112+
</li>
113+
<li>
114+
<p>Top and bottom couples meet and set,</p>
115+
<p>Then each gentleman leas the opposite lady to the couple on his left, and set,</p>
116+
<p>Aach four right and left,</p>
117+
<p>Swing side couples to places, and turn partners all eight,</p>
118+
<p>The other two couple o the same.</p>
119+
</li>
120+
</ol>
121+
<p><a class="footnote-backref" href="#fnref:2" rev="footnote" title="Jump back to footnote 2 in the text">&#8617;</a></p>
122+
</li>
123+
</ol>
49124
</div>

tests/extensions/extra/raw-html.txt

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,114 @@ foo bar
7171

7272
<em>bar</em>
7373
</div>
74+
75+
<div markdown="1" name="issue584">
76+
77+
[link]: http://example.com
78+
79+
<div markdown="1">
80+
[link][link]
81+
</div>
82+
83+
</div>
84+
85+
<div markdown="1" name="issue584">
86+
87+
*[abbr]: Abbreviation
88+
89+
<div markdown="1">
90+
abbr
91+
</div>
92+
93+
</div>
94+
95+
<div markdown="1" name="issue584">
96+
97+
[^1]:
98+
1. The top couple half figure, contrary sides and hands across with bottom couple,
99+
100+
Half figure back on your own sides, and turn partner to places,
101+
102+
Swing partners with right hands into straight line long-ways, as in a reel, and
103+
104+
Set,
105+
106+
Hey and return to places,
107+
108+
The other three couples do the same.
109+
110+
2. Top and bottom couples meet and set,
111+
112+
Then each gentleman leas the opposite lady to the couple on his left, and set,
113+
114+
Aach four right and left,
115+
116+
Swing side couples to places, and turn partners all eight,
117+
118+
The other two couple o the same.
119+
120+
<div markdown="1">
121+
footnote[^1]
122+
</div>
123+
124+
</div>
125+
126+
<div markdown="1" name="issue584">
127+
128+
[link]: http://example.com
129+
130+
131+
132+
133+
<div markdown="1">
134+
[link][link]
135+
</div>
136+
137+
</div>
138+
139+
<div markdown="1" name="issue584">
140+
141+
*[abbr]: Abbreviation
142+
143+
144+
145+
146+
<div markdown="1">
147+
abbr
148+
</div>
149+
150+
</div>
151+
152+
<div markdown="1" name="issue584">
153+
154+
[^2]:
155+
1. The top couple half figure, contrary sides and hands across with bottom couple,
156+
157+
Half figure back on your own sides, and turn partner to places,
158+
159+
Swing partners with right hands into straight line long-ways, as in a reel, and
160+
161+
Set,
162+
163+
Hey and return to places,
164+
165+
The other three couples do the same.
166+
167+
2. Top and bottom couples meet and set,
168+
169+
Then each gentleman leas the opposite lady to the couple on his left, and set,
170+
171+
Aach four right and left,
172+
173+
Swing side couples to places, and turn partners all eight,
174+
175+
The other two couple o the same.
176+
177+
178+
179+
180+
<div markdown="1">
181+
footnote[^2]
182+
</div>
183+
184+
</div>

0 commit comments

Comments
 (0)