Skip to content

Commit

Permalink
Allow HTML comments inside lists
Browse files Browse the repository at this point in the history
Fixes #21.
  • Loading branch information
domenic committed Jan 23, 2015
1 parent 852ea6f commit 47d9a6a
Show file tree
Hide file tree
Showing 15 changed files with 98 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<ol>`).
**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 `<ol>`). 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 `<p>`).

Expand Down
29 changes: 21 additions & 8 deletions lib/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = $('<ol>');
Expand All @@ -40,16 +36,33 @@ exports.list = function (lines, state) {
});

// Set the start="" attribute from the list item number if this is the first <li> in the <ol>.
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: $('<!--' + content + '-->')
};
};

exports.listItem = function (atoms) {
var li = $('<li>');
atoms.forEach(function (atom) {
Expand Down
19 changes: 15 additions & 4 deletions lib/grammar.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<!--" content:$(!"-->" .)* "-->" "\n"? { return actions.comment(dents, content); }

atom = variable
/ code
/ string
Expand All @@ -41,3 +48,7 @@ nonterminal = "|" name:$[a-z]i+ params:("[" $[^\]]+ "]")? opt:"_opt"? "|" { ret
params, opt); }

normalText = $[^\n]

indentSpaces = " "*

number = $[0-9]+
3 changes: 3 additions & 0 deletions test/list-cases/html-comment-inside-item.ecmarkdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
1. Start
2. Middle <!-- comment -->
3. End
7 changes: 7 additions & 0 deletions test/list-cases/html-comment-inside-item.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<ol>
<li>Start</li>
<li>Middle
<!-- comment -->
</li>
<li>End</li>
</ol>
5 changes: 5 additions & 0 deletions test/list-cases/html-comment-multiline-2.ecmarkdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
1. Start
<!-- 2. Comment out
3. Two lines but with the end marker on the next line
-->
4. End
7 changes: 7 additions & 0 deletions test/list-cases/html-comment-multiline-2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<ol>
<li>Start</li>
<!-- 2. Comment out
3. Two lines but with the end marker on the next line
-->
<li>End</li>
</ol>
4 changes: 4 additions & 0 deletions test/list-cases/html-comment-multiline-3.ecmarkdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
1. Start
2. Comment <!-- out
3. Two lines -->
4. End
6 changes: 6 additions & 0 deletions test/list-cases/html-comment-multiline-3.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<ol>
<li>Start</li>
<li>Comment
<!-- out</li><li>Two lines --></li>
<li>End</li>
</ol>
4 changes: 4 additions & 0 deletions test/list-cases/html-comment-multiline.ecmarkdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
1. Start
<!-- 2. Comment out
3. Two lines -->
4. End
6 changes: 6 additions & 0 deletions test/list-cases/html-comment-multiline.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<ol>
<li>Start</li>
<!-- 2. Comment out
3. Two lines -->
<li>End</li>
</ol>
3 changes: 3 additions & 0 deletions test/list-cases/html-comment-nested-list.ecmarkdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
1. abc
<!-- comment -->
5. def
8 changes: 8 additions & 0 deletions test/list-cases/html-comment-nested-list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<ol>
<li>abc
<ol start="5">
<!-- comment -->
<li>def</li>
</ol>
</li>
</ol>
3 changes: 3 additions & 0 deletions test/list-cases/html-comment-singleline.ecmarkdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
1. Start
<!-- 2. Comment out one line -->
3. End
5 changes: 5 additions & 0 deletions test/list-cases/html-comment-singleline.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<ol>
<li>Start</li>
<!-- 2. Comment out one line -->
<li>End</li>
</ol>

0 comments on commit 47d9a6a

Please sign in to comment.