From 9b43efbca60057eb3400f5d32f287f8e340fbcd8 Mon Sep 17 00:00:00 2001 From: facelessuser Date: Fri, 6 Oct 2017 21:24:11 -0600 Subject: [PATCH 1/4] Fix raw html reference issue Preserve the line which a reference was on to prevent raw HTML indexing issue. Ref #584. --- markdown/preprocessors.py | 4 ++++ tests/extensions/extra/raw-html.html | 5 +++++ tests/extensions/extra/raw-html.txt | 10 ++++++++++ 3 files changed, 19 insertions(+) diff --git a/markdown/preprocessors.py b/markdown/preprocessors.py index 94f983005..810b4c81d 100644 --- a/markdown/preprocessors.py +++ b/markdown/preprocessors.py @@ -346,6 +346,10 @@ def run(self, lines): lines.pop(0) t = tm.group(2) or tm.group(3) or tm.group(4) self.markdown.references[id] = (link, t) + # Preserve the line which the reference was on + # to prevent raw HTML indexing issue. + # https://github.com/Python-Markdown/markdown/issues/584 + new_text.append('') else: new_text.append(line) diff --git a/tests/extensions/extra/raw-html.html b/tests/extensions/extra/raw-html.html index 9c0222f56..78ea203ec 100644 --- a/tests/extensions/extra/raw-html.html +++ b/tests/extensions/extra/raw-html.html @@ -46,4 +46,9 @@

foo bar

bar

+ +
+
+

bold

+
\ No newline at end of file diff --git a/tests/extensions/extra/raw-html.txt b/tests/extensions/extra/raw-html.txt index da24af03f..882a4010d 100644 --- a/tests/extensions/extra/raw-html.txt +++ b/tests/extensions/extra/raw-html.txt @@ -71,3 +71,13 @@ foo bar bar + +
+ +[link]: http://example.com + +
+**bold** +
+ +
From cfe3376bb1c76688b3ea63698f90052f41639a7b Mon Sep 17 00:00:00 2001 From: facelessuser Date: Sat, 7 Oct 2017 08:30:55 -0600 Subject: [PATCH 2/4] 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. --- markdown/extensions/abbr.py | 3 ++ markdown/extensions/footnotes.py | 12 +++++++- markdown/preprocessors.py | 3 +- tests/extensions/extra/raw-html.html | 37 +++++++++++++++++++++++- tests/extensions/extra/raw-html.txt | 43 +++++++++++++++++++++++++++- 5 files changed, 93 insertions(+), 5 deletions(-) diff --git a/markdown/extensions/abbr.py b/markdown/extensions/abbr.py index 353d126f6..fc858b65b 100644 --- a/markdown/extensions/abbr.py +++ b/markdown/extensions/abbr.py @@ -53,6 +53,9 @@ def run(self, lines): title = m.group('title').strip() self.markdown.inlinePatterns['abbr-%s' % abbr] = \ AbbrPattern(self._generate_pattern(abbr), title) + # Preserve the line to prevent raw HTML indexing issue. + # https://github.com/Python-Markdown/markdown/issues/584 + new_text.append('') else: new_text.append(line) return new_text diff --git a/markdown/extensions/footnotes.py b/markdown/extensions/footnotes.py index 8bd85959c..580dfa0e3 100644 --- a/markdown/extensions/footnotes.py +++ b/markdown/extensions/footnotes.py @@ -233,7 +233,12 @@ def run(self, lines): fn, _i = self.detectTabbed(lines[i+1:]) fn.insert(0, m.group(2)) i += _i-1 # skip past footnote - self.footnotes.setFootnote(m.group(1), "\n".join(fn)) + footnote = "\n".join(fn) + self.footnotes.setFootnote(m.group(1), footnote) + # Preserve a line for each block to prevent raw HTML indexing issue. + # https://github.com/Python-Markdown/markdown/issues/584 + num_blocks = (len(footnote.split('\n\n')) * 2) + newlines.extend([''] * (num_blocks)) else: newlines.append(lines[i]) if len(lines) > i+1: @@ -285,6 +290,11 @@ def detab(line): if lines[j].strip(): next_line = lines[j] break + else: + # Include extreaneous padding to prevent raw HTML + # parsing issue: https://github.com/Python-Markdown/markdown/issues/584 + items.append("") + i += 1 else: break # There is no more text; we are done. diff --git a/markdown/preprocessors.py b/markdown/preprocessors.py index 810b4c81d..1e99afa85 100644 --- a/markdown/preprocessors.py +++ b/markdown/preprocessors.py @@ -346,8 +346,7 @@ def run(self, lines): lines.pop(0) t = tm.group(2) or tm.group(3) or tm.group(4) self.markdown.references[id] = (link, t) - # Preserve the line which the reference was on - # to prevent raw HTML indexing issue. + # Preserve the line to prevent raw HTML indexing issue. # https://github.com/Python-Markdown/markdown/issues/584 new_text.append('') else: diff --git a/tests/extensions/extra/raw-html.html b/tests/extensions/extra/raw-html.html index 78ea203ec..158e9238f 100644 --- a/tests/extensions/extra/raw-html.html +++ b/tests/extensions/extra/raw-html.html @@ -49,6 +49,41 @@
-

bold

+

link

+
+
+
+

abbr

+
+
+
+
+

footnote1

+
+
+
+
+
    +
  1. +
      +
    1. +

      The top couple half figure, contrary sides and hands across with bottom couple,

      +

      Half figure back on your own sides, and turn partner to places,

      +

      Swing partners with right hands into straight line long-ways, as in a reel, and

      +

      Set,

      +

      Hey and return to places,

      +

      The other three couples do the same.

      +
    2. +
    3. +

      Top and bottom couples meet and set,

      +

      Then each gentleman leas the opposite lady to the couple on his left, and set,

      +

      Aach four right and left,

      +

      Swing side couples to places, and turn partners all eight,

      +

      The other two couple o the same.

      +
    4. +
    +

    ↩

    +
  2. +
\ No newline at end of file diff --git a/tests/extensions/extra/raw-html.txt b/tests/extensions/extra/raw-html.txt index 882a4010d..7b560a5cc 100644 --- a/tests/extensions/extra/raw-html.txt +++ b/tests/extensions/extra/raw-html.txt @@ -77,7 +77,48 @@ foo bar [link]: http://example.com
-**bold** +[link][link] +
+ + + +
+ +*[abbr]: Abbreviation + +
+abbr +
+ +
+ +
+ +[^1]: + 1. The top couple half figure, contrary sides and hands across with bottom couple, + + Half figure back on your own sides, and turn partner to places, + + Swing partners with right hands into straight line long-ways, as in a reel, and + + Set, + + Hey and return to places, + + The other three couples do the same. + + 2. Top and bottom couples meet and set, + + Then each gentleman leas the opposite lady to the couple on his left, and set, + + Aach four right and left, + + Swing side couples to places, and turn partners all eight, + + The other two couple o the same. + +
+footnote[^1]
From 08abeb528fd5345e8d6cedc3b177bd812f441dd6 Mon Sep 17 00:00:00 2001 From: facelessuser Date: Sat, 7 Oct 2017 08:40:40 -0600 Subject: [PATCH 3/4] Test extra lines at the end of references --- tests/extensions/extra/raw-html.html | 35 ++++++++++++++++ tests/extensions/extra/raw-html.txt | 60 ++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) diff --git a/tests/extensions/extra/raw-html.html b/tests/extensions/extra/raw-html.html index 158e9238f..717bd7355 100644 --- a/tests/extensions/extra/raw-html.html +++ b/tests/extensions/extra/raw-html.html @@ -62,6 +62,21 @@

footnote1

+
+
+

link

+
+
+
+
+

abbr

+
+
+
+
+

footnote2

+
+

    @@ -85,5 +100,25 @@

↩

+
  • +
      +
    1. +

      The top couple half figure, contrary sides and hands across with bottom couple,

      +

      Half figure back on your own sides, and turn partner to places,

      +

      Swing partners with right hands into straight line long-ways, as in a reel, and

      +

      Set,

      +

      Hey and return to places,

      +

      The other three couples do the same.

      +
    2. +
    3. +

      Top and bottom couples meet and set,

      +

      Then each gentleman leas the opposite lady to the couple on his left, and set,

      +

      Aach four right and left,

      +

      Swing side couples to places, and turn partners all eight,

      +

      The other two couple o the same.

      +
    4. +
    +

    ↩

    +
  • \ No newline at end of file diff --git a/tests/extensions/extra/raw-html.txt b/tests/extensions/extra/raw-html.txt index 7b560a5cc..c82ddbbc0 100644 --- a/tests/extensions/extra/raw-html.txt +++ b/tests/extensions/extra/raw-html.txt @@ -122,3 +122,63 @@ footnote[^1] + +
    + +[link]: http://example.com + + + + +
    +[link][link] +
    + +
    + +
    + +*[abbr]: Abbreviation + + + + +
    +abbr +
    + +
    + +
    + +[^2]: + 1. The top couple half figure, contrary sides and hands across with bottom couple, + + Half figure back on your own sides, and turn partner to places, + + Swing partners with right hands into straight line long-ways, as in a reel, and + + Set, + + Hey and return to places, + + The other three couples do the same. + + 2. Top and bottom couples meet and set, + + Then each gentleman leas the opposite lady to the couple on his left, and set, + + Aach four right and left, + + Swing side couples to places, and turn partners all eight, + + The other two couple o the same. + + + + +
    +footnote[^2] +
    + +
    From c55c50ca3af1fcf599c7d7041be840e11e932c67 Mon Sep 17 00:00:00 2001 From: facelessuser Date: Sat, 7 Oct 2017 08:56:58 -0600 Subject: [PATCH 4/4] 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. --- markdown/extensions/footnotes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/markdown/extensions/footnotes.py b/markdown/extensions/footnotes.py index 580dfa0e3..aa11cbfa4 100644 --- a/markdown/extensions/footnotes.py +++ b/markdown/extensions/footnotes.py @@ -234,7 +234,7 @@ def run(self, lines): fn.insert(0, m.group(2)) i += _i-1 # skip past footnote footnote = "\n".join(fn) - self.footnotes.setFootnote(m.group(1), footnote) + self.footnotes.setFootnote(m.group(1), footnote.rstrip()) # Preserve a line for each block to prevent raw HTML indexing issue. # https://github.com/Python-Markdown/markdown/issues/584 num_blocks = (len(footnote.split('\n\n')) * 2)