From 47d9a6a69f31eef5614e32ff04f868515ca22a0d Mon Sep 17 00:00:00 2001 From: Domenic Denicola Date: Fri, 23 Jan 2015 16:55:34 -0500 Subject: [PATCH] Allow HTML comments inside lists Fixes #21. --- README.md | 2 +- lib/actions.js | 29 ++++++++++++++----- lib/grammar.pegjs | 19 +++++++++--- .../html-comment-inside-item.ecmarkdown | 3 ++ test/list-cases/html-comment-inside-item.html | 7 +++++ .../html-comment-multiline-2.ecmarkdown | 5 ++++ test/list-cases/html-comment-multiline-2.html | 7 +++++ .../html-comment-multiline-3.ecmarkdown | 4 +++ test/list-cases/html-comment-multiline-3.html | 6 ++++ .../html-comment-multiline.ecmarkdown | 4 +++ test/list-cases/html-comment-multiline.html | 6 ++++ .../html-comment-nested-list.ecmarkdown | 3 ++ test/list-cases/html-comment-nested-list.html | 8 +++++ .../html-comment-singleline.ecmarkdown | 3 ++ test/list-cases/html-comment-singleline.html | 5 ++++ 15 files changed, 98 insertions(+), 13 deletions(-) create mode 100644 test/list-cases/html-comment-inside-item.ecmarkdown create mode 100644 test/list-cases/html-comment-inside-item.html create mode 100644 test/list-cases/html-comment-multiline-2.ecmarkdown create mode 100644 test/list-cases/html-comment-multiline-2.html create mode 100644 test/list-cases/html-comment-multiline-3.ecmarkdown create mode 100644 test/list-cases/html-comment-multiline-3.html create mode 100644 test/list-cases/html-comment-multiline.ecmarkdown create mode 100644 test/list-cases/html-comment-multiline.html create mode 100644 test/list-cases/html-comment-nested-list.ecmarkdown create mode 100644 test/list-cases/html-comment-nested-list.html create mode 100644 test/list-cases/html-comment-singleline.ecmarkdown create mode 100644 test/list-cases/html-comment-singleline.html diff --git a/README.md b/README.md index d4eefd5..f3ee1a0 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ instead of Every Ecmarkdown string is a either a **numeric list**, a **paragraph**, or a **fragment**. These base productions can contain other productions. -**Numeric lists** are written as a series of lines, each starting with `1. `. Lines can be indented by multiples of exactly two spaces to indicate nesting. To convert a numeric list, use `ecmarkdown.list(stringOfText)` to get back HTML for the list (with root element `
    `). +**Numeric lists** are written as a series of lines, each starting with `1. `. Lines can be indented by multiples of exactly two spaces to indicate nesting. To convert a numeric list, use `ecmarkdown.list(stringOfText)` to get back HTML for the list (with root element `
      `). HTML is allowed inside list items, where it is passed through untouched, but HTML tags cannot span multiple list items. The exception is HTML comments (``), which can span multiple lines. **Paragraphs** are a single line of text. To convert a paragraph, use `ecmarkdown.paragraph(stringOfText)` to get back HTML for the paragraph (with root element `

      `). diff --git a/lib/actions.js b/lib/actions.js index 136fd6b..8870ab2 100644 --- a/lib/actions.js +++ b/lib/actions.js @@ -24,11 +24,7 @@ exports.list = function (lines, state) { var ols = state.ols; lines.forEach(function (line) { - var dents = line[0]; - var liNumber = line[1]; - var li = line[3]; - - dents.forEach(function (dent) { + line.dents.forEach(function (dent) { if (dent === INDENT) { var currentLI = ols.current.children(':last-of-type'); var newOL = $('

        '); @@ -40,16 +36,33 @@ exports.list = function (lines, state) { }); // Set the start="" attribute from the list item number if this is the first
      1. in the
          . - if (liNumber !== '1' && ols.current.children().length === 0) { - ols.current.attr('start', liNumber); + if (line.type === 'listLine' && line.number !== '1' && ols.current.children().length === 0) { + ols.current.attr('start', line.number); } - ols.current.append(li); + ols.current.append(line.node); }); return beautify($.html(ols.emptyAndReturnBottom())); }; +exports.listLine = function (dents, number, li) { + return { + type: 'listLine', + dents: dents, + number: number, + node: li + }; +}; + +exports.comment = function (dents, content) { + return { + type: 'comment', + dents: dents, + node: $('') + }; +}; + exports.listItem = function (atoms) { var li = $('
        1. '); atoms.forEach(function (atom) { diff --git a/lib/grammar.pegjs b/lib/grammar.pegjs index 5069bac..6cbcaa4 100644 --- a/lib/grammar.pegjs +++ b/lib/grammar.pegjs @@ -3,20 +3,27 @@ var state = actions.makeInitialState(); } -// Entry points +// ## Entry points -list = lines:(dents $[0-9]+ ". " listItem "\n")+ { return actions.list(lines, state); } +list = lines:(listLine / comment)+ { return actions.list(lines, state); } paragraph = atoms:atom+ { return actions.paragraph(atoms); } fragment = atoms:atom+ { return actions.fragment(atoms); } -// Supporting productions +// ## Supporting productions -dents = spaces:" "* { return actions.dents(spaces, state); } +dents = spaces:indentSpaces { return actions.dents(spaces, state); } + +// The prelude here ensures we don't gobble up any dents unless we are definitely in a list line instead of a comment +listLine = &(indentSpaces number) + dents:dents number:number ". " li:listItem "\n" { return actions.listLine(dents, + number, li); } listItem = atoms:atom+ { return actions.listItem(atoms); } +comment = dents:dents "" .)* "-->" "\n"? { return actions.comment(dents, content); } + atom = variable / code / string @@ -41,3 +48,7 @@ nonterminal = "|" name:$[a-z]i+ params:("[" $[^\]]+ "]")? opt:"_opt"? "|" { ret params, opt); } normalText = $[^\n] + +indentSpaces = " "* + +number = $[0-9]+ diff --git a/test/list-cases/html-comment-inside-item.ecmarkdown b/test/list-cases/html-comment-inside-item.ecmarkdown new file mode 100644 index 0000000..9d0861f --- /dev/null +++ b/test/list-cases/html-comment-inside-item.ecmarkdown @@ -0,0 +1,3 @@ +1. Start +2. Middle +3. End diff --git a/test/list-cases/html-comment-inside-item.html b/test/list-cases/html-comment-inside-item.html new file mode 100644 index 0000000..cf5842f --- /dev/null +++ b/test/list-cases/html-comment-inside-item.html @@ -0,0 +1,7 @@ +
            +
          1. Start
          2. +
          3. Middle + +
          4. +
          5. End
          6. +
          diff --git a/test/list-cases/html-comment-multiline-2.ecmarkdown b/test/list-cases/html-comment-multiline-2.ecmarkdown new file mode 100644 index 0000000..cfa5376 --- /dev/null +++ b/test/list-cases/html-comment-multiline-2.ecmarkdown @@ -0,0 +1,5 @@ +1. Start + +4. End diff --git a/test/list-cases/html-comment-multiline-2.html b/test/list-cases/html-comment-multiline-2.html new file mode 100644 index 0000000..b9d7934 --- /dev/null +++ b/test/list-cases/html-comment-multiline-2.html @@ -0,0 +1,7 @@ +
            +
          1. Start
          2. + +
          3. End
          4. +
          diff --git a/test/list-cases/html-comment-multiline-3.ecmarkdown b/test/list-cases/html-comment-multiline-3.ecmarkdown new file mode 100644 index 0000000..a758742 --- /dev/null +++ b/test/list-cases/html-comment-multiline-3.ecmarkdown @@ -0,0 +1,4 @@ +1. Start +2. Comment +4. End diff --git a/test/list-cases/html-comment-multiline-3.html b/test/list-cases/html-comment-multiline-3.html new file mode 100644 index 0000000..f53bf4d --- /dev/null +++ b/test/list-cases/html-comment-multiline-3.html @@ -0,0 +1,6 @@ +
            +
          1. Start
          2. +
          3. Comment +
          4. +
          5. End
          6. +
          diff --git a/test/list-cases/html-comment-multiline.ecmarkdown b/test/list-cases/html-comment-multiline.ecmarkdown new file mode 100644 index 0000000..cd41f10 --- /dev/null +++ b/test/list-cases/html-comment-multiline.ecmarkdown @@ -0,0 +1,4 @@ +1. Start + +4. End diff --git a/test/list-cases/html-comment-multiline.html b/test/list-cases/html-comment-multiline.html new file mode 100644 index 0000000..916e26c --- /dev/null +++ b/test/list-cases/html-comment-multiline.html @@ -0,0 +1,6 @@ +
            +
          1. Start
          2. + +
          3. End
          4. +
          diff --git a/test/list-cases/html-comment-nested-list.ecmarkdown b/test/list-cases/html-comment-nested-list.ecmarkdown new file mode 100644 index 0000000..9de1960 --- /dev/null +++ b/test/list-cases/html-comment-nested-list.ecmarkdown @@ -0,0 +1,3 @@ +1. abc + + 5. def diff --git a/test/list-cases/html-comment-nested-list.html b/test/list-cases/html-comment-nested-list.html new file mode 100644 index 0000000..7e17ff3 --- /dev/null +++ b/test/list-cases/html-comment-nested-list.html @@ -0,0 +1,8 @@ +
            +
          1. abc +
              + +
            1. def
            2. +
            +
          2. +
          diff --git a/test/list-cases/html-comment-singleline.ecmarkdown b/test/list-cases/html-comment-singleline.ecmarkdown new file mode 100644 index 0000000..9a3fc22 --- /dev/null +++ b/test/list-cases/html-comment-singleline.ecmarkdown @@ -0,0 +1,3 @@ +1. Start + +3. End diff --git a/test/list-cases/html-comment-singleline.html b/test/list-cases/html-comment-singleline.html new file mode 100644 index 0000000..735fb28 --- /dev/null +++ b/test/list-cases/html-comment-singleline.html @@ -0,0 +1,5 @@ +
            +
          1. Start
          2. + +
          3. End
          4. +