From 5cf769b1cd1d9e76ff5f543aba8b4a15a90b7d88 Mon Sep 17 00:00:00 2001 From: fiddlosopher Date: Wed, 20 Dec 2006 00:25:54 +0000 Subject: [PATCH] Modified the HTML writer to add invisible anchors to each section heading. The anchors are derived form the text of the section heading as described in README. This makes it easy to insert links that jump from one part of a document to another: for example, '[back to the Introduction](#Introduction)'. git-svn-id: https://pandoc.googlecode.com/svn/trunk@246 788f1e2b-df1e-0410-8736-df70ead52e1b --- README | 20 ++++++++++++++++++++ src/Text/Pandoc/Writers/HTML.hs | 14 ++++++++------ tests/s5.basic.html | 8 ++++++++ tests/s5.fancy.html | 8 ++++++++ tests/s5.fragment.html | 3 +++ tests/s5.inserts.html | 3 +++ tests/writer.html | 29 +++++++++++++++++++++++++++++ tests/writer.smart.html | 29 +++++++++++++++++++++++++++++ 8 files changed, 108 insertions(+), 6 deletions(-) diff --git a/README b/README index 8a75d90da..d64654376 100644 --- a/README +++ b/README @@ -261,6 +261,26 @@ markdown test suite, type `make test-markdown`.) [standard markdown]: http://daringfireball.net/projects/markdown/syntax +## Section Headings + +Pandoc creates an invisible anchor in front of every HTML section +heading. The ID of this anchor is derived from the section heading +itself: spaces are converted to underscores, and formatting, links, +and other markup are removed. Thus, for example, the source + + ## Aristotle's *De Anima* + +gets converted to HTML as follows: + + +

Aristotle's De Anima

+ +This makes it easy to provide internal links that jump to a particular +place in a document. To provide a link to the heading above, for +example, just insert: + + [Back to Aristotle](#Aristotle's_De_Anima) + ## Lists Pandoc behaves differently from standard markdown on some "edge diff --git a/src/Text/Pandoc/Writers/HTML.hs b/src/Text/Pandoc/Writers/HTML.hs index f1bd0f8b5..7ba506acb 100644 --- a/src/Text/Pandoc/Writers/HTML.hs +++ b/src/Text/Pandoc/Writers/HTML.hs @@ -154,12 +154,14 @@ blockToHtml options (OrderedList lst) = let attribs = if (writerIncremental options) then " class=\"incremental\"" else "" in "\n" ++ (concatMap (listItemToHtml options) lst) ++ "\n" blockToHtml options HorizontalRule = "
\n" -blockToHtml options (Header level lst) = if ((level > 0) && (level <= 6)) then - "" ++ - (inlineListToHtml options lst) ++ - "\n" - else - "

" ++ (inlineListToHtml options lst) ++ "

\n" +blockToHtml options (Header level lst) = + let contents = inlineListToHtml options lst in + let simplify = gsub "<[^>]*>" "" . gsub " " "_" in + if ((level > 0) && (level <= 6)) + then "\n" ++ + "" ++ contents ++ + "\n" + else "

" ++ contents ++ "

\n" listItemToHtml options list = "
  • " ++ (concatMap (blockToHtml options) list) ++ "
  • \n" -- | Convert list of Pandoc inline elements to HTML. diff --git a/tests/s5.basic.html b/tests/s5.basic.html index 919b09da6..88510a74c 100644 --- a/tests/s5.basic.html +++ b/tests/s5.basic.html @@ -723,18 +723,24 @@ window.onresize = function(){setTimeout('fontScale()', 50);}
    +

    My S5 Document

    +

    Sam Smith, Jen Jones

    +

    July 15, 2006

    +

    First slide

    • first bullet
    • @@ -742,6 +748,7 @@ window.onresize = function(){setTimeout('fontScale()', 50);}
    +

    Smarty

    • "Hello there"
    • @@ -750,6 +757,7 @@ window.onresize = function(){setTimeout('fontScale()', 50);}
    +

    Math

    • $\frac{d}{dx}f(x)=\lim_{h\to 0}\frac{f(x+h)-f(x)}{h}$
    • diff --git a/tests/s5.fancy.html b/tests/s5.fancy.html index 8686a8542..e015f7b99 100644 --- a/tests/s5.fancy.html +++ b/tests/s5.fancy.html @@ -1635,18 +1635,24 @@ else
    +

    My S5 Document

    +

    Sam Smith, Jen Jones

    +

    July 15, 2006

    +

    First slide

    • first bullet
    • @@ -1654,6 +1660,7 @@ else
    +

    Smarty

    • “Hello there”
    • @@ -1662,6 +1669,7 @@ else
    +

    Math

    • $\frac{d}{dx}f(x)=\lim_{h\to 0}\frac{f(x+h)-f(x)}{h}$
    • diff --git a/tests/s5.fragment.html b/tests/s5.fragment.html index 370b9c111..240bafb35 100644 --- a/tests/s5.fragment.html +++ b/tests/s5.fragment.html @@ -1,8 +1,10 @@ +

      First slide

      • first bullet
      • second bullet
      +

      Smarty

        @@ -11,6 +13,7 @@
      • And 'ellipses'...
      +

      Math

      • $\frac{d}{dx}f(x)=\lim_{h\to 0}\frac{f(x+h)-f(x)}{h}$
      • diff --git a/tests/s5.inserts.html b/tests/s5.inserts.html index 7be33a2c8..ba6ae5f48 100644 --- a/tests/s5.inserts.html +++ b/tests/s5.inserts.html @@ -13,11 +13,13 @@ STUFF INSERTED STUFF INSERTED

        My S5 Document

        +

        First slide

        • first bullet
        • second bullet
        +

        Smarty

          @@ -26,6 +28,7 @@ STUFF INSERTED
        • And 'ellipses'...
        +

        Math

        • $\frac{d}{dx}f(x)=\lim_{h\to 0}\frac{f(x+h)-f(x)}{h}$
        • diff --git a/tests/writer.html b/tests/writer.html index 6fcb949ec..0661fb732 100644 --- a/tests/writer.html +++ b/tests/writer.html @@ -12,18 +12,28 @@

          Pandoc Test Suite

          This is a set of tests for pandoc. Most of them are adapted from John Gruber's markdown test suite.


          +

          Headers

          +

          Level 2 with an embedded link

          +

          Level 3 with emphasis

          +

          Level 4

          +
          Level 5
          +

          Level 1

          +

          Level 2 with emphasis

          +

          Level 3

          with no blank line

          +

          Level 2

          with no blank line


          +

          Paragraphs

          Here's a regular paragraph.

          In Markdown 1.0.0 and earlier. Version 8. This line turns into a list item. Because a hard-wrapped line in the middle of a paragraph looked like a list item.

          @@ -31,6 +41,7 @@

          There should be a hard line break
          here.


          +

          Block Quotes

          E-mail style:

          @@ -79,6 +90,7 @@ here.

          And a following paragraph.


          +

          Code Blocks

          Code:

          ---- (should be four hyphens)
          @@ -95,7 +107,9 @@ this code block is indented by one tab
           These should not be escaped:  \$ \\ \> \[ \{
           

          +

          Lists

          +

          Unordered

          Asterisks tight:

            @@ -142,6 +156,7 @@ These should not be escaped: \$ \\ \> \[ \{
          • Minus 3

          +

          Ordered

          Tight:

            @@ -183,6 +198,7 @@ These should not be escaped: \$ \\ \> \[ \{
          1. Item 3.

          +

          Nested

          • Tab
              @@ -218,6 +234,7 @@ These should not be escaped: \$ \\ \> \[ \{
            • Third

            • +

              Tabs and spaces

              • this is a list item indented with tabs

                @@ -232,6 +249,7 @@ These should not be escaped: \$ \\ \> \[ \{

              +

              HTML Blocks

              Simple block on one line:

              foo
              @@ -307,6 +325,7 @@ Blah

              +

              Inline Markup

              This is emphasized, and so is this.

              This is strong, and so is this.

              @@ -317,6 +336,7 @@ Blah

              So is this word.

              This is code: >, $, \, \$, <html>.


              +

              Smart quotes, ellipses, dashes

              "Hello," said the spider. "'Shelob' is my name."

              'A', 'B', and 'C' are letters.

              @@ -327,6 +347,7 @@ Blah

              Dashes between numbers: 5-7, 255-66, 1987-1999.

              Ellipses...and. . .and . . . .


              +

              LaTeX

              • \cite[22-23]{smith.1899}
              • @@ -352,6 +373,7 @@ Dog & 2 \\ Cat & 1 \\ \hline \end{tabular}


                +

                Special Characters

                Here is some unicode:

                  @@ -383,7 +405,9 @@ Cat & 1 \\ \hline

                  Plus: +

                  Minus: -


                  +

                  Links

                  +

                  Explicit

                  Just a URL.

                  URL and title.

                  @@ -393,6 +417,7 @@ Cat & 1 \\ \hline

                  URL and title

                  Empty.

                  +

                  Reference

                  Foo bar.

                  Foo bar.

                  @@ -407,11 +432,13 @@ Cat & 1 \\ \hline

                  Foo bar.

                  Foo biz.

                  +

                  With ampersands

                  Here's a link with an ampersand in the URL.

                  Here's a link with an amersand in the link text: AT&T.

                  Here's an inline link.

                  Here's an inline link in pointy braces.

                  +

                  Autolinks

                  With an ampersand: http://example.com/?foo=1&bar=2

                    @@ -427,11 +454,13 @@ Cat & 1 \\ \hline
                    or here: <http://example.com/>
                     

                    +

                    Images

                    From "Voyage dans la Lune" by Georges Melies (1902):

                    lalune

                    Here is a movie movie icon.


                    +

                    Footnotes

                    Here is a footnote reference,1 and another.2 This should not be a footnote reference, because it contains a space.[^my note] Here is an inline note.3

                    This paragraph should not be part of the note, as it is not indented.

                    diff --git a/tests/writer.smart.html b/tests/writer.smart.html index 508b0abf5..d5fb3da93 100644 --- a/tests/writer.smart.html +++ b/tests/writer.smart.html @@ -12,18 +12,28 @@

                    Pandoc Test Suite

                    This is a set of tests for pandoc. Most of them are adapted from John Gruber’s markdown test suite.


                    +

                    Headers

                    +

                    Level 2 with an embedded link

                    +

                    Level 3 with emphasis

                    +

                    Level 4

                    +
                    Level 5
                    +

                    Level 1

                    +

                    Level 2 with emphasis

                    +

                    Level 3

                    with no blank line

                    +

                    Level 2

                    with no blank line


                    +

                    Paragraphs

                    Here’s a regular paragraph.

                    In Markdown 1.0.0 and earlier. Version 8. This line turns into a list item. Because a hard-wrapped line in the middle of a paragraph looked like a list item.

                    @@ -31,6 +41,7 @@

                    There should be a hard line break
                    here.


                    +

                    Block Quotes

                    E-mail style:

                    @@ -79,6 +90,7 @@ here.

                    And a following paragraph.


                    +

                    Code Blocks

                    Code:

                    ---- (should be four hyphens)
                    @@ -95,7 +107,9 @@ this code block is indented by one tab
                     These should not be escaped:  \$ \\ \> \[ \{
                     

                    +

                    Lists

                    +

                    Unordered

                    Asterisks tight:

                      @@ -142,6 +156,7 @@ These should not be escaped: \$ \\ \> \[ \{
                    • Minus 3

                    +

                    Ordered

                    Tight:

                      @@ -183,6 +198,7 @@ These should not be escaped: \$ \\ \> \[ \{
                    1. Item 3.

                    +

                    Nested

                    • Tab
                        @@ -218,6 +234,7 @@ These should not be escaped: \$ \\ \> \[ \{
                      • Third

                      • +

                        Tabs and spaces

                        • this is a list item indented with tabs

                          @@ -232,6 +249,7 @@ These should not be escaped: \$ \\ \> \[ \{

                        +

                        HTML Blocks

                        Simple block on one line:

                        foo
                        @@ -307,6 +325,7 @@ Blah

                        +

                        Inline Markup

                        This is emphasized, and so is this.

                        This is strong, and so is this.

                        @@ -317,6 +336,7 @@ Blah

                        So is this word.

                        This is code: >, $, \, \$, <html>.


                        +

                        Smart quotes, ellipses, dashes

                        “Hello,” said the spider. “‘Shelob’ is my name.”

                        ‘A’, ‘B’, and ‘C’ are letters.

                        @@ -327,6 +347,7 @@ Blah

                        Dashes between numbers: 5–7, 255–66, 1987–1999.

                        Ellipses…and…and … .


                        +

                        LaTeX

                        • \cite[22-23]{smith.1899}
                        • @@ -352,6 +373,7 @@ Dog & 2 \\ Cat & 1 \\ \hline \end{tabular}


                          +

                          Special Characters

                          Here is some unicode:

                            @@ -383,7 +405,9 @@ Cat & 1 \\ \hline

                            Plus: +

                            Minus: -


                            +

                            Links

                            +

                            Explicit

                            Just a URL.

                            URL and title.

                            @@ -393,6 +417,7 @@ Cat & 1 \\ \hline

                            URL and title

                            Empty.

                            +

                            Reference

                            Foo bar.

                            Foo bar.

                            @@ -407,11 +432,13 @@ Cat & 1 \\ \hline

                            Foo bar.

                            Foo biz.

                            +

                            With ampersands

                            Here’s a link with an ampersand in the URL.

                            Here’s a link with an amersand in the link text: AT&T.

                            Here’s an inline link.

                            Here’s an inline link in pointy braces.

                            +

                            Autolinks

                            With an ampersand: http://example.com/?foo=1&bar=2

                              @@ -427,11 +454,13 @@ Cat & 1 \\ \hline
                              or here: <http://example.com/>
                               

                              +

                              Images

                              From “Voyage dans la Lune” by Georges Melies (1902):

                              lalune

                              Here is a movie movie icon.


                              +

                              Footnotes

                              Here is a footnote reference,1 and another.2 This should not be a footnote reference, because it contains a space.[^my note] Here is an inline note.3

                              This paragraph should not be part of the note, as it is not indented.