Browse Source

Added html block draft + minor fixes

pull/14/head
Vitaly Puzrin 10 years ago
parent
commit
350d0174df
  1. 1
      lib/lexer_block.js
  2. 98
      lib/lexer_block/html.js
  3. 1
      lib/lexer_block/paragraph.js
  4. 17
      lib/renderer.js
  5. 482
      test/fixtures/stmd/bad.txt
  6. 270
      test/fixtures/stmd/good.txt

1
lib/lexer_block.js

@ -18,6 +18,7 @@ rules.push(require('./lexer_block/hr'));
rules.push(require('./lexer_block/list'));
rules.push(require('./lexer_block/heading'));
rules.push(require('./lexer_block/lheading'));
rules.push(require('./lexer_block/html'));
rules.push(require('./lexer_block/table'));
rules.push(require('./lexer_block/paragraph'));

98
lib/lexer_block/html.js

@ -0,0 +1,98 @@
// HTML block
'use strict';
var isEmpty = require('../helpers').isEmpty;
var getLines = require('../helpers').getLines;
function replace(regex, options) {
regex = regex.source;
options = options || '';
return function self(name, val) {
if (!name) {
return new RegExp(regex, options);
}
val = val.source || val;
val = val.replace(/(^|[^\[])\^/g, '$1');
regex = regex.replace(name, val);
return self;
};
}
var attr_name = /[a-zA-Z_:][a-zA-Z0-9:._-]*/;
var unquoted = /[^"'=<>`\x00-\x20]+/;
var single_quoted = /'[^']*'/;
var double_quoted = /"[^"]*"/;
/*eslint no-spaced-func:0*/
var attr_value = replace(/(?:unquoted|single_quoted|double_quoted)/)
('unquoted', unquoted)
('single_quoted', single_quoted)
('double_quoted', double_quoted)
();
var attribute = replace(/(?:\s+attr_name(?:\s*=\s*attr_value)?)/)
('attr_name', attr_name)
('attr_value', attr_value)
();
var open_tag = replace(/<[A-Za-z][A-Za-z0-9]*attribute*\s*\/?>/)
('attribute', attribute)
();
var close_tag = /<\/[A-Za-z][A-Za-z0-9]*\s*>/;
var comment = /<!--([^-]+|[-][^-]+)*-->/;
var processing = /<[?].*?[?]>/;
var declaration = /<![A-Z]\s+[^>]*>/;
var cdata = /<!\[CDATA\[([^\]]+|\][^\]]|\]\][^>])*\]\]>/;
var html_tag = replace(/^(?:open_tag|close_tag|comment|processing|declaration|cdata)/, 'i')
('open_tag', open_tag)
('close_tag', close_tag)
('comment', comment)
('processing', processing)
('declaration', declaration)
('cdata', cdata)
();
module.exports = function html(state, startLine, endLine, silent) {
var nextLine,
pos = state.bMarks[startLine],
max = state.eMarks[startLine],
shift = state.tShift[startLine];
pos += shift;
if (pos + 3 >= max ||
shift > 3 ||
state.blkLevel > 0) { return false; }
if (state.src.charCodeAt(pos) !== 0x3C/* < */) { return false; }
// TODO: (?) optimize check.
nextLine = startLine + 1;
while (nextLine < state.lineMax && !isEmpty(state, nextLine)) {
nextLine++;
}
if (!html_tag.test(state.src.slice(pos, state.eMarks[nextLine - 1]).replace(/\n/g,' '))) {
return false;
}
if (silent) { return true; }
state.tokens.push({
type: 'html',
content: getLines(state, startLine, nextLine, 0, true)
});
state.line = nextLine;
return true;
};

1
lib/lexer_block/paragraph.js

@ -28,6 +28,7 @@ module.exports = function paragraph(state, startLine/*, endLine*/) {
// setex header can't interrupt paragraph
// if (rules_named.lheading(state, nextLine, endLine, true)) { break; }
if (rules_named.blockquote(state, nextLine, endLine, true)) { break; }
if (rules_named.html(state, nextLine, endLine, true)) { break; }
if (rules_named.table(state, nextLine, endLine, true)) { break; }
//if (rules_named.tag(state, nextLine, endLine, true)) { break; }
//if (rules_named.def(state, nextLine, endLine, true)) { break; }

17
lib/renderer.js

@ -137,6 +137,11 @@ rules.text = function (state, token /*, idx*/) {
};
rules.html = function (state, token /*, idx*/) {
state.result += token.content;
};
// Renderer class
function Renderer() {
// Clone rules object to allow local modifications
@ -169,6 +174,14 @@ Renderer.prototype.render = function (state) {
if (name === 'ordered_list_close' || name === 'bullet_list_close') {
state.tight = tightStack.pop();
}
if (name === 'blockquote_open') {
tightStack.push(state.tight);
state.tight = false;
}
if (name === 'blockquote_close') {
state.tight = tightStack.pop();
}
// in tight mode just ignore paragraphs for lists
// TODO - track right nesting to blockquotes
@ -179,7 +192,9 @@ Renderer.prototype.render = function (state) {
// Quick hack - texts should have LF if followed by blocks
if (i + 1 < state.tokens.length) {
next = state.tokens[i + 1].type;
if (next === 'bullet_list_open' || next === 'ordered_list_open') {
if (next === 'bullet_list_open' ||
next === 'ordered_list_open' ||
next === 'blockquote_open') {
state.result += '\n';
}
}

482
test/fixtures/stmd/bad.txt

@ -80,62 +80,6 @@ error:
foo</p>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 1373
.
<table>
<tr>
<td>
hi
</td>
</tr>
</table>
okay.
.
<table>
<tr>
<td>
hi
</td>
</tr>
</table>
<p>okay.</p>
.
error:
<p>&lt;table&gt;
&lt;tr&gt;
&lt;td&gt;
hi
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;</p>
<p>okay.</p>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 1394
.
<div>
*hello*
<foo><a>
.
<div>
*hello*
<foo><a>
.
error:
<p>&lt;div&gt;
*hello*
&lt;foo&gt;&lt;a&gt;</p>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 1406
@ -153,187 +97,9 @@ src line: 1406
error:
<p>&lt;DIV CLASS=&quot;foo&quot;&gt;</p>
<DIV CLASS="foo">
<p>*Markdown*</p>
<p>&lt;/DIV&gt;</p>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 1422
.
<div></div>
``` c
int x = 33;
```
.
<div></div>
``` c
int x = 33;
```
.
error:
<p>&lt;div&gt;&lt;/div&gt;</p>
<pre><code class="language-c">int x = 33;
</code></pre>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 1436
.
<!-- Foo
bar
baz -->
.
<!-- Foo
bar
baz -->
.
error:
<p>&lt;!-- Foo
bar
baz --&gt;</p>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 1448
.
<?php
echo 'foo'
?>
.
<?php
echo 'foo'
?>
.
error:
<p>&lt;?php
echo 'foo'
?&gt;</p>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 1460
.
<![CDATA[
function matchwo(a,b)
{
if (a < b && a < 0) then
{
return 1;
}
else
{
return 0;
}
}
]]>
.
<![CDATA[
function matchwo(a,b)
{
if (a < b && a < 0) then
{
return 1;
}
else
{
return 0;
}
}
]]>
.
error:
<p>&lt;![CDATA[
function matchwo(a,b)
{
if (a &lt; b &amp;&amp; a &lt; 0) then
{
return 1;
}
else
{
return 0;
}
}
]]&gt;</p>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 1492
.
<!-- foo -->
<!-- foo -->
.
<!-- foo -->
<pre><code>&lt;!-- foo --&gt;
</code></pre>
.
error:
<p>&lt;!-- foo --&gt;</p>
<pre><code>&lt;!-- foo --&gt;
</code></pre>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 1505
.
Foo
<div>
bar
</div>
.
<p>Foo</p>
<div>
bar
</div>
.
error:
<p>Foo
&lt;div&gt;
bar
&lt;/div&gt;</p>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 1520
.
<div>
bar
</div>
*foo*
.
<div>
bar
</div>
*foo*
.
error:
<p>&lt;div&gt;
bar
&lt;/div&gt;
*foo*</p>
</DIV>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@ -370,65 +136,9 @@ src line: 1570
error:
<p>&lt;div&gt;</p>
<p>*Emphasized* text.</p>
<p>&lt;/div&gt;</p>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 1584
.
<div>
*Emphasized* text.
</div>
.
<div>
*Emphasized* text.
<p>*Emphasized* text.</p>
</div>
.
error:
<p>&lt;div&gt;
*Emphasized* text.
&lt;/div&gt;</p>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 1605
.
<table>
<tr>
<td>
Hi
</td>
</tr>
</table>
.
<table>
<tr>
<td>
Hi
</td>
</tr>
</table>
.
error:
<p>&lt;table&gt;</p>
<p>&lt;tr&gt;</p>
<p>&lt;td&gt;
Hi
&lt;/td&gt;</p>
<p>&lt;/tr&gt;</p>
<p>&lt;/table&gt;</p>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@ -501,9 +211,9 @@ src line: 1678
error:
<p>[Foo bar]:
&lt;my url&gt;
'title'</p>
<p>[Foo bar]:</p>
<my url>
'title'
<p>[Foo bar]</p>
@ -728,7 +438,10 @@ error:
with two lines.</p>
<pre><code>indented code
</code></pre>
<p>&gt; A block quote.</p></li>
<blockquote>
<pre><code>&gt; A block quote.
</code></pre>
</blockquote></li>
</ol>
@ -764,65 +477,10 @@ error:
<pre><code>bar
</code></pre>
<p>baz</p>
<p>&gt; bam</p></li>
</ol>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 2656
.
1. indented code
paragraph
more code
.
<ol>
<li><pre><code>indented code
</code></pre>
<p>paragraph</p>
<pre><code>more code
</code></pre></li>
</ol>
.
error:
<ol>
<li><p>indented code</p>
<p>paragraph</p>
<pre><code>more code
</code></pre></li>
</ol>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 2675
.
1. indented code
paragraph
more code
.
<ol>
<li><pre><code> indented code
<blockquote>
<pre><code>&gt; bam
</code></pre>
<p>paragraph</p>
<pre><code>more code
</code></pre></li>
</ol>
.
error:
<ol>
<li><p>indented code</p>
<p>paragraph</p>
<pre><code>more code
</code></pre></li>
</blockquote></li>
</ol>
@ -855,7 +513,10 @@ error:
with two lines.</p>
<pre><code>indented code
</code></pre>
<p>&gt; A block quote.</p></li>
<blockquote>
<pre><code> &gt; A block quote.
</code></pre>
</blockquote></li>
</ol>
@ -888,7 +549,10 @@ error:
with two lines.</p>
<pre><code>indented code
</code></pre>
<p>&gt; A block quote.</p></li>
<blockquote>
<pre><code> &gt; A block quote.
</code></pre>
</blockquote></li>
</ol>
@ -921,7 +585,10 @@ error:
with two lines.</p>
<pre><code>indented code
</code></pre>
<p>&gt; A block quote.</p></li>
<blockquote>
<pre><code> &gt; A block quote.
</code></pre>
</blockquote></li>
</ol>
@ -954,7 +621,10 @@ error:
with two lines.</p>
<pre><code>indented code
</code></pre>
<p>&gt; A block quote.</p></li>
<blockquote>
<pre><code> &gt; A block quote.
</code></pre>
</blockquote></li>
</ol>
@ -1089,66 +759,6 @@ error:
</ul>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 3489
.
* a
> b
>
* c
.
<ul>
<li>a
<blockquote>
<p>b</p>
</blockquote></li>
<li>c</li>
</ul>
.
error:
<ul>
<li>a<blockquote>
b</blockquote></li>
<li>c</li>
</ul>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 3507
.
- a
> b
```
c
```
- d
.
<ul>
<li>a
<blockquote>
<p>b</p>
</blockquote>
<pre><code>c
</code></pre></li>
<li>d</li>
</ul>
.
error:
<ul>
<li>a<blockquote>
b</blockquote>
<pre><code>c
</code></pre></li>
<li>d</li>
</ul>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 3550
@ -1304,7 +914,7 @@ src line: 3694
error:
<p>&lt;a href=&quot;/bar/)&quot;&gt;</p>
<a href="/bar\/)">
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@ -1423,7 +1033,7 @@ src line: 3791
error:
<p>&lt;a href=&quot;&amp;ouml;&amp;ouml;.html&quot;&gt;</p>
<a href="&ouml;&ouml;.html">
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@ -1646,7 +1256,7 @@ src line: 3954
error:
<p>&lt;a href=&quot;`&quot;&gt;`</p>
<a href="`">`
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@ -3653,7 +3263,7 @@ src line: 5627
error:
<p>&lt;a&gt;&lt;bab&gt;&lt;c2c&gt;</p>
<a><bab><c2c>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@ -3667,7 +3277,7 @@ src line: 5635
error:
<p>&lt;a/&gt;&lt;b2/&gt;</p>
<a/><b2/>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@ -3683,8 +3293,8 @@ data="foo" ></p>
error:
<p>&lt;a /&gt;&lt;b2
data=&quot;foo&quot; &gt;</p>
<a /><b2
data="foo" >
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@ -3700,8 +3310,8 @@ _boolean zoop:33=zoop:33 /></p>
error:
<p>&lt;a foo=&quot;bar&quot; bam = 'baz &lt;em&gt;&quot;&lt;/em&gt;'
_boolean zoop:33=zoop:33 /&gt;</p>
<a foo="bar" bam = 'baz <em>"</em>'
_boolean zoop:33=zoop:33 />
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@ -3717,8 +3327,8 @@ src line: 5705
error:
<p>&lt;/a&gt;
&lt;/foo &gt;</p>
</a>
</foo >
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@ -3791,7 +3401,7 @@ src line: 5763
error:
<p>&lt;a href=&quot;&amp;ouml;&quot;&gt;</p>
<a href="&ouml;">
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@ -3805,7 +3415,7 @@ src line: 5771
error:
<p>&lt;a href=&quot;*&quot;&gt;</p>
<a href="\*">
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@ -3972,8 +3582,8 @@ bar"></p>
error:
<p>&lt;a href=&quot;foo
bar&quot;&gt;</p>
<a href="foo
bar">
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@ -3989,8 +3599,8 @@ bar"></p>
error:
<p>&lt;a href=&quot;foo\
bar&quot;&gt;</p>
<a href="foo\
bar">
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

270
test/fixtures/stmd/good.txt

@ -1042,6 +1042,198 @@ src line: 1338
</code></pre>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 1373
.
<table>
<tr>
<td>
hi
</td>
</tr>
</table>
okay.
.
<table>
<tr>
<td>
hi
</td>
</tr>
</table>
<p>okay.</p>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 1394
.
<div>
*hello*
<foo><a>
.
<div>
*hello*
<foo><a>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 1422
.
<div></div>
``` c
int x = 33;
```
.
<div></div>
``` c
int x = 33;
```
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 1436
.
<!-- Foo
bar
baz -->
.
<!-- Foo
bar
baz -->
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 1448
.
<?php
echo 'foo'
?>
.
<?php
echo 'foo'
?>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 1460
.
<![CDATA[
function matchwo(a,b)
{
if (a < b && a < 0) then
{
return 1;
}
else
{
return 0;
}
}
]]>
.
<![CDATA[
function matchwo(a,b)
{
if (a < b && a < 0) then
{
return 1;
}
else
{
return 0;
}
}
]]>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 1492
.
<!-- foo -->
<!-- foo -->
.
<!-- foo -->
<pre><code>&lt;!-- foo --&gt;
</code></pre>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 1505
.
Foo
<div>
bar
</div>
.
<p>Foo</p>
<div>
bar
</div>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 1520
.
<div>
bar
</div>
*foo*
.
<div>
bar
</div>
*foo*
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 1584
.
<div>
*Emphasized* text.
</div>
.
<div>
*Emphasized* text.
</div>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 1605
.
<table>
<tr>
<td>
Hi
</td>
</tr>
</table>
.
<table>
<tr>
<td>
Hi
</td>
</tr>
</table>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 1701
@ -1756,6 +1948,44 @@ paragraph
</code></pre>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 2656
.
1. indented code
paragraph
more code
.
<ol>
<li><pre><code>indented code
</code></pre>
<p>paragraph</p>
<pre><code>more code
</code></pre></li>
</ol>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 2675
.
1. indented code
paragraph
more code
.
<ol>
<li><pre><code> indented code
</code></pre>
<p>paragraph</p>
<pre><code>more code
</code></pre></li>
</ol>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 2698
@ -2158,6 +2388,46 @@ src line: 3446
</ul>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 3489
.
* a
> b
>
* c
.
<ul>
<li>a
<blockquote>
<p>b</p>
</blockquote></li>
<li>c</li>
</ul>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 3507
.
- a
> b
```
c
```
- d
.
<ul>
<li>a
<blockquote>
<p>b</p>
</blockquote>
<pre><code>c
</code></pre></li>
<li>d</li>
</ul>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 3528

Loading…
Cancel
Save