mirror of
https://github.com/xoofx/markdig.git
synced 2026-02-13 13:54:56 +00:00
Compare commits
41 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
191bc940c7 | ||
|
|
05673758e3 | ||
|
|
cff2b9a8ca | ||
|
|
586095a475 | ||
|
|
eae2082a1e | ||
|
|
4576548df3 | ||
|
|
266e0c8bfd | ||
|
|
f5c07dbab5 | ||
|
|
c8a28a1ad7 | ||
|
|
72cc454314 | ||
|
|
7fe2c1f939 | ||
|
|
087e7a68b6 | ||
|
|
abeabf15a1 | ||
|
|
f9bfcaab7b | ||
|
|
afa0182f02 | ||
|
|
b83de5934d | ||
|
|
c294d3bfb4 | ||
|
|
a7cdb2351a | ||
|
|
46ef21a3ed | ||
|
|
18c8d0178c | ||
|
|
ebc79dafbd | ||
|
|
a1d2467643 | ||
|
|
8220f0fa56 | ||
|
|
ec385acc7f | ||
|
|
04c1cc62d4 | ||
|
|
abdbd65f60 | ||
|
|
1f32a060da | ||
|
|
699d80c150 | ||
|
|
56bcac7600 | ||
|
|
cab3365104 | ||
|
|
3821bd00fe | ||
|
|
62701fd0f1 | ||
|
|
1be5e60506 | ||
|
|
c9f1512358 | ||
|
|
8f23aed6af | ||
|
|
6a62ae9c69 | ||
|
|
2c3de5688b | ||
|
|
f3c08b4ec4 | ||
|
|
69e3baafe5 | ||
|
|
5844ccc395 | ||
|
|
be9c6fa54b |
@@ -10,12 +10,13 @@ You can **try Markdig online** and compare it to other implementations on [babel
|
||||
|
||||
## Features
|
||||
|
||||
- **Very fast parser** (no-regexp), very lightweight in terms of GC pressure. See benchmarks
|
||||
- **Abstract Syntax Tree**
|
||||
- **Very fast parser and html renderer** (no-regexp), very lightweight in terms of GC pressure. See benchmarks
|
||||
- **Abstract Syntax Tree** with precise source code location for syntax tree, useful when building a Markdown editor.
|
||||
- Checkout [MarkdownEditor for Visual Studio](https://visualstudiogallery.msdn.microsoft.com/eaab33c3-437b-4918-8354-872dfe5d1bfe) powered by Markdig!
|
||||
- Converter to **HTML**
|
||||
- Passing more than **600+ tests** from the latest [CommonMark specs](http://spec.commonmark.org/)
|
||||
- Includes all the core elements of CommonMark:
|
||||
- including GFM fenced code blocks.
|
||||
- including **GFM fenced code blocks**.
|
||||
- **Extensible** architecture
|
||||
- Even the core Markdown/CommonMark parsing is pluggable, so it allows to disable builtin Markdown/Commonmark parsing (e.g [Disable HTML parsing](https://github.com/lunet-io/markdig/blob/7964bd0160d4c18e4155127a4c863d61ebd8944a/src/Markdig/MarkdownExtensions.cs#L306)) or change behaviour (e.g change matching `#` of a headers with `@`)
|
||||
- Built-in with **20+ extensions**, including:
|
||||
|
||||
@@ -59,9 +59,11 @@
|
||||
<DesignTime>True</DesignTime>
|
||||
<DependentUpon>Specs.tt</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="TestHtmlAttributes.cs" />
|
||||
<Compile Include="TestHtmlHelper.cs" />
|
||||
<Compile Include="TestLineReader.cs" />
|
||||
<Compile Include="TestLinkHelper.cs" />
|
||||
<Compile Include="TestPragmaLines.cs" />
|
||||
<Compile Include="TestSourcePosition.cs" />
|
||||
<Compile Include="TestStringSliceList.cs" />
|
||||
<Compile Include="TestPlayParser.cs" />
|
||||
|
||||
@@ -11,3 +11,11 @@ This is a test with a :) and a :angry: smiley
|
||||
.
|
||||
<p>This is a test with a 😃 and a 😠 smiley</p>
|
||||
````````````````````````````````
|
||||
|
||||
An emoji needs to be preceded by a space and followed by a space:
|
||||
|
||||
```````````````````````````````` example
|
||||
These are not:) an :)emoji with a:) x:angry:x
|
||||
.
|
||||
<p>These are not:) an :)emoji with a:) x:angry:x</p>
|
||||
````````````````````````````````
|
||||
|
||||
@@ -61,3 +61,60 @@ multi-paragraph list items.<a href="#fnref:3" class="footnote-back-ref">↩<
|
||||
</div>
|
||||
````````````````````````````````
|
||||
|
||||
Check with mulitple consecutive footnotes:
|
||||
|
||||
```````````````````````````````` example
|
||||
Here is a footnote[^1]. And another one[^2]. And a third one[^3]. And a fourth[^4].
|
||||
|
||||
[^1]: Footnote 1 text
|
||||
|
||||
[^2]: Footnote 2 text
|
||||
|
||||
a
|
||||
|
||||
[^3]: Footnote 3 text
|
||||
|
||||
[^4]: Footnote 4 text
|
||||
.
|
||||
<p>Here is a footnote<a id="fnref:1" href="#fn:1" class="footnote-ref"><sup>1</sup></a>. And another one<a id="fnref:2" href="#fn:2" class="footnote-ref"><sup>2</sup></a>. And a third one<a id="fnref:3" href="#fn:3" class="footnote-ref"><sup>3</sup></a>. And a fourth<a id="fnref:4" href="#fn:4" class="footnote-ref"><sup>4</sup></a>.</p>
|
||||
<p>a</p>
|
||||
<div class="footnotes">
|
||||
<hr />
|
||||
<ol>
|
||||
<li id="fn:1">
|
||||
<p>Footnote 1 text<a href="#fnref:1" class="footnote-back-ref">↩</a></p></li>
|
||||
<li id="fn:2">
|
||||
<p>Footnote 2 text<a href="#fnref:2" class="footnote-back-ref">↩</a></p></li>
|
||||
<li id="fn:3">
|
||||
<p>Footnote 3 text<a href="#fnref:3" class="footnote-back-ref">↩</a></p></li>
|
||||
<li id="fn:4">
|
||||
<p>Footnote 4 text<a href="#fnref:4" class="footnote-back-ref">↩</a></p></li>
|
||||
</ol>
|
||||
</div>
|
||||
````````````````````````````````
|
||||
|
||||
Another test with consecutive footnotes without a blank line separator:
|
||||
|
||||
```````````````````````````````` example
|
||||
Here is a footnote[^1]. And another one[^2]. And a third one[^3]. And a fourth[^4].
|
||||
|
||||
[^1]: Footnote 1 text
|
||||
[^2]: Footnote 2 text
|
||||
[^3]: Footnote 3 text
|
||||
[^4]: Footnote 4 text
|
||||
.
|
||||
<p>Here is a footnote<a id="fnref:1" href="#fn:1" class="footnote-ref"><sup>1</sup></a>. And another one<a id="fnref:2" href="#fn:2" class="footnote-ref"><sup>2</sup></a>. And a third one<a id="fnref:3" href="#fn:3" class="footnote-ref"><sup>3</sup></a>. And a fourth<a id="fnref:4" href="#fn:4" class="footnote-ref"><sup>4</sup></a>.</p>
|
||||
<div class="footnotes">
|
||||
<hr />
|
||||
<ol>
|
||||
<li id="fn:1">
|
||||
<p>Footnote 1 text<a href="#fnref:1" class="footnote-back-ref">↩</a></p></li>
|
||||
<li id="fn:2">
|
||||
<p>Footnote 2 text<a href="#fnref:2" class="footnote-back-ref">↩</a></p></li>
|
||||
<li id="fn:3">
|
||||
<p>Footnote 3 text<a href="#fnref:3" class="footnote-back-ref">↩</a></p></li>
|
||||
<li id="fn:4">
|
||||
<p>Footnote 4 text<a href="#fnref:4" class="footnote-back-ref">↩</a></p></li>
|
||||
</ol>
|
||||
</div>
|
||||
````````````````````````````````
|
||||
|
||||
@@ -83,29 +83,6 @@ They are' not matching 'quotes
|
||||
.
|
||||
<p>They are' not matching 'quotes</p>
|
||||
````````````````````````````````
|
||||
|
||||
Double quotes using ``` `` ``` are working if they match another `''` pair, and there is no other double quotes on the line (otherwise they would be parsed as a code span):
|
||||
|
||||
```````````````````````````````` example
|
||||
This is ``a double quote''
|
||||
.
|
||||
<p>This is “a double quote”</p>
|
||||
````````````````````````````````
|
||||
|
||||
```````````````````````````````` example
|
||||
This is ``a code span''``
|
||||
.
|
||||
<p>This is <code>a code span''</code></p>
|
||||
````````````````````````````````
|
||||
|
||||
```````````````````````````````` example
|
||||
hello ``there```
|
||||
test
|
||||
.
|
||||
<p>hello “there”`
|
||||
test</p>
|
||||
````````````````````````````````
|
||||
|
||||
An emphasis starting inside left/right quotes will span over the right quote:
|
||||
|
||||
```````````````````````````````` example
|
||||
@@ -133,3 +110,26 @@ This is a en ellipsis...
|
||||
.
|
||||
<p>This is a en ellipsis…</p>
|
||||
````````````````````````````````
|
||||
|
||||
Check that a smartypants are not breaking pipetable parsing:
|
||||
|
||||
```````````````````````````````` example
|
||||
a | b
|
||||
-- | --
|
||||
0 | 1
|
||||
.
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>a</th>
|
||||
<th>b</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td>0</td>
|
||||
<td>1</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
````````````````````````````````
|
||||
|
||||
@@ -3811,6 +3811,7 @@ namespace Markdig.Tests
|
||||
// main :: IO ()
|
||||
// main = print $ parseTags tags
|
||||
// </code></pre>
|
||||
// okay
|
||||
//
|
||||
// Should be rendered as:
|
||||
// <pre language="haskell"><code>
|
||||
@@ -3819,9 +3820,10 @@ namespace Markdig.Tests
|
||||
// main :: IO ()
|
||||
// main = print $ parseTags tags
|
||||
// </code></pre>
|
||||
// <p>okay</p>
|
||||
|
||||
Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 134, "Leaf blocks HTML blocks");
|
||||
TestParser.TestSpec("<pre language=\"haskell\"><code>\nimport Text.HTML.TagSoup\n\nmain :: IO ()\nmain = print $ parseTags tags\n</code></pre>", "<pre language=\"haskell\"><code>\nimport Text.HTML.TagSoup\n\nmain :: IO ()\nmain = print $ parseTags tags\n</code></pre>", "");
|
||||
TestParser.TestSpec("<pre language=\"haskell\"><code>\nimport Text.HTML.TagSoup\n\nmain :: IO ()\nmain = print $ parseTags tags\n</code></pre>\nokay", "<pre language=\"haskell\"><code>\nimport Text.HTML.TagSoup\n\nmain :: IO ()\nmain = print $ parseTags tags\n</code></pre>\n<p>okay</p>", "");
|
||||
}
|
||||
}
|
||||
// A script tag (type 1):
|
||||
@@ -3840,6 +3842,7 @@ namespace Markdig.Tests
|
||||
//
|
||||
// document.getElementById("demo").innerHTML = "Hello JavaScript!";
|
||||
// </script>
|
||||
// okay
|
||||
//
|
||||
// Should be rendered as:
|
||||
// <script type="text/javascript">
|
||||
@@ -3847,9 +3850,10 @@ namespace Markdig.Tests
|
||||
//
|
||||
// document.getElementById("demo").innerHTML = "Hello JavaScript!";
|
||||
// </script>
|
||||
// <p>okay</p>
|
||||
|
||||
Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 135, "Leaf blocks HTML blocks");
|
||||
TestParser.TestSpec("<script type=\"text/javascript\">\n// JavaScript example\n\ndocument.getElementById(\"demo\").innerHTML = \"Hello JavaScript!\";\n</script>", "<script type=\"text/javascript\">\n// JavaScript example\n\ndocument.getElementById(\"demo\").innerHTML = \"Hello JavaScript!\";\n</script>", "");
|
||||
TestParser.TestSpec("<script type=\"text/javascript\">\n// JavaScript example\n\ndocument.getElementById(\"demo\").innerHTML = \"Hello JavaScript!\";\n</script>\nokay", "<script type=\"text/javascript\">\n// JavaScript example\n\ndocument.getElementById(\"demo\").innerHTML = \"Hello JavaScript!\";\n</script>\n<p>okay</p>", "");
|
||||
}
|
||||
}
|
||||
// A style tag (type 1):
|
||||
@@ -3869,6 +3873,7 @@ namespace Markdig.Tests
|
||||
//
|
||||
// p {color:blue;}
|
||||
// </style>
|
||||
// okay
|
||||
//
|
||||
// Should be rendered as:
|
||||
// <style
|
||||
@@ -3877,9 +3882,10 @@ namespace Markdig.Tests
|
||||
//
|
||||
// p {color:blue;}
|
||||
// </style>
|
||||
// <p>okay</p>
|
||||
|
||||
Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 136, "Leaf blocks HTML blocks");
|
||||
TestParser.TestSpec("<style\n type=\"text/css\">\nh1 {color:red;}\n\np {color:blue;}\n</style>", "<style\n type=\"text/css\">\nh1 {color:red;}\n\np {color:blue;}\n</style>", "");
|
||||
TestParser.TestSpec("<style\n type=\"text/css\">\nh1 {color:red;}\n\np {color:blue;}\n</style>\nokay", "<style\n type=\"text/css\">\nh1 {color:red;}\n\np {color:blue;}\n</style>\n<p>okay</p>", "");
|
||||
}
|
||||
}
|
||||
// If there is no matching end tag, the block will end at the
|
||||
@@ -4044,15 +4050,17 @@ namespace Markdig.Tests
|
||||
//
|
||||
// bar
|
||||
// baz -->
|
||||
// okay
|
||||
//
|
||||
// Should be rendered as:
|
||||
// <!-- Foo
|
||||
//
|
||||
// bar
|
||||
// baz -->
|
||||
// <p>okay</p>
|
||||
|
||||
Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 143, "Leaf blocks HTML blocks");
|
||||
TestParser.TestSpec("<!-- Foo\n\nbar\n baz -->", "<!-- Foo\n\nbar\n baz -->", "");
|
||||
TestParser.TestSpec("<!-- Foo\n\nbar\n baz -->\nokay", "<!-- Foo\n\nbar\n baz -->\n<p>okay</p>", "");
|
||||
}
|
||||
}
|
||||
// A processing instruction (type 3):
|
||||
@@ -4071,6 +4079,7 @@ namespace Markdig.Tests
|
||||
// echo '>';
|
||||
//
|
||||
// ?>
|
||||
// okay
|
||||
//
|
||||
// Should be rendered as:
|
||||
// <?php
|
||||
@@ -4078,9 +4087,10 @@ namespace Markdig.Tests
|
||||
// echo '>';
|
||||
//
|
||||
// ?>
|
||||
// <p>okay</p>
|
||||
|
||||
Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 144, "Leaf blocks HTML blocks");
|
||||
TestParser.TestSpec("<?php\n\n echo '>';\n\n?>", "<?php\n\n echo '>';\n\n?>", "");
|
||||
TestParser.TestSpec("<?php\n\n echo '>';\n\n?>\nokay", "<?php\n\n echo '>';\n\n?>\n<p>okay</p>", "");
|
||||
}
|
||||
}
|
||||
// A declaration (type 4):
|
||||
@@ -4126,6 +4136,7 @@ namespace Markdig.Tests
|
||||
// }
|
||||
// }
|
||||
// ]]>
|
||||
// okay
|
||||
//
|
||||
// Should be rendered as:
|
||||
// <![CDATA[
|
||||
@@ -4140,9 +4151,10 @@ namespace Markdig.Tests
|
||||
// }
|
||||
// }
|
||||
// ]]>
|
||||
// <p>okay</p>
|
||||
|
||||
Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 146, "Leaf blocks HTML blocks");
|
||||
TestParser.TestSpec("<![CDATA[\nfunction matchwo(a,b)\n{\n if (a < b && a < 0) then {\n return 1;\n\n } else {\n\n return 0;\n }\n}\n]]>", "<![CDATA[\nfunction matchwo(a,b)\n{\n if (a < b && a < 0) then {\n return 1;\n\n } else {\n\n return 0;\n }\n}\n]]>", "");
|
||||
TestParser.TestSpec("<![CDATA[\nfunction matchwo(a,b)\n{\n if (a < b && a < 0) then {\n return 1;\n\n } else {\n\n return 0;\n }\n}\n]]>\nokay", "<![CDATA[\nfunction matchwo(a,b)\n{\n if (a < b && a < 0) then {\n return 1;\n\n } else {\n\n return 0;\n }\n}\n]]>\n<p>okay</p>", "");
|
||||
}
|
||||
}
|
||||
// The opening tag can be indented 1-3 spaces, but not 4:
|
||||
@@ -5387,8 +5399,8 @@ namespace Markdig.Tests
|
||||
TestParser.TestSpec(" > # Foo\n > bar\n > baz", "<pre><code>> # Foo\n> bar\n> baz\n</code></pre>", "");
|
||||
}
|
||||
}
|
||||
// The Laziness clause allows us to omit the `>` before a
|
||||
// paragraph continuation line:
|
||||
// The Laziness clause allows us to omit the `>` before
|
||||
// [paragraph continuation text]:
|
||||
[TestFixture]
|
||||
public partial class TestContainerblocksBlockquotes
|
||||
{
|
||||
@@ -5561,8 +5573,8 @@ namespace Markdig.Tests
|
||||
TestParser.TestSpec("> ```\nfoo\n```", "<blockquote>\n<pre><code></code></pre>\n</blockquote>\n<p>foo</p>\n<pre><code></code></pre>", "");
|
||||
}
|
||||
}
|
||||
// Note that in the following case, we have a paragraph
|
||||
// continuation line:
|
||||
// Note that in the following case, we have a [lazy
|
||||
// continuation line]:
|
||||
[TestFixture]
|
||||
public partial class TestContainerblocksBlockquotes
|
||||
{
|
||||
@@ -5595,7 +5607,7 @@ namespace Markdig.Tests
|
||||
//
|
||||
// the `- bar` is indented too far to start a list, and can't
|
||||
// be an indented code block because indented code blocks cannot
|
||||
// interrupt paragraphs, so it is a [paragraph continuation line].
|
||||
// interrupt paragraphs, so it is [paragraph continuation text].
|
||||
//
|
||||
// A block quote can be empty:
|
||||
[TestFixture]
|
||||
@@ -7286,6 +7298,7 @@ namespace Markdig.Tests
|
||||
// - foo
|
||||
// - bar
|
||||
// - baz
|
||||
// - boo
|
||||
//
|
||||
// Should be rendered as:
|
||||
// <ul>
|
||||
@@ -7293,7 +7306,11 @@ namespace Markdig.Tests
|
||||
// <ul>
|
||||
// <li>bar
|
||||
// <ul>
|
||||
// <li>baz</li>
|
||||
// <li>baz
|
||||
// <ul>
|
||||
// <li>boo</li>
|
||||
// </ul>
|
||||
// </li>
|
||||
// </ul>
|
||||
// </li>
|
||||
// </ul>
|
||||
@@ -7301,7 +7318,7 @@ namespace Markdig.Tests
|
||||
// </ul>
|
||||
|
||||
Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 254, "Container blocks List items");
|
||||
TestParser.TestSpec("- foo\n - bar\n - baz", "<ul>\n<li>foo\n<ul>\n<li>bar\n<ul>\n<li>baz</li>\n</ul>\n</li>\n</ul>\n</li>\n</ul>", "");
|
||||
TestParser.TestSpec("- foo\n - bar\n - baz\n - boo", "<ul>\n<li>foo\n<ul>\n<li>bar\n<ul>\n<li>baz\n<ul>\n<li>boo</li>\n</ul>\n</li>\n</ul>\n</li>\n</ul>\n</li>\n</ul>", "");
|
||||
}
|
||||
}
|
||||
// One is not enough:
|
||||
@@ -7318,16 +7335,18 @@ namespace Markdig.Tests
|
||||
// - foo
|
||||
// - bar
|
||||
// - baz
|
||||
// - boo
|
||||
//
|
||||
// Should be rendered as:
|
||||
// <ul>
|
||||
// <li>foo</li>
|
||||
// <li>bar</li>
|
||||
// <li>baz</li>
|
||||
// <li>boo</li>
|
||||
// </ul>
|
||||
|
||||
Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 255, "Container blocks List items");
|
||||
TestParser.TestSpec("- foo\n - bar\n - baz", "<ul>\n<li>foo</li>\n<li>bar</li>\n<li>baz</li>\n</ul>", "");
|
||||
TestParser.TestSpec("- foo\n - bar\n - baz\n - boo", "<ul>\n<li>foo</li>\n<li>bar</li>\n<li>baz</li>\n<li>boo</li>\n</ul>", "");
|
||||
}
|
||||
}
|
||||
// Here we need four, because the list marker is wider:
|
||||
@@ -13868,7 +13887,7 @@ namespace Markdig.Tests
|
||||
// [link reference definition] elsewhere in the
|
||||
// document and is not followed by `[]` or a link label.
|
||||
// The contents of the first link label are parsed as inlines,
|
||||
// which are used as the link's text. the link's URI and title
|
||||
// which are used as the link's text. The link's URI and title
|
||||
// are provided by the matching link reference definition.
|
||||
// Thus, `[foo]` is equivalent to `[foo][]`.
|
||||
[TestFixture]
|
||||
@@ -17103,6 +17122,88 @@ namespace Markdig.Tests
|
||||
Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 1, "Extensions Footontes");
|
||||
TestParser.TestSpec("Here is a footnote reference,[^1] and another.[^longnote]\n\nThis is another reference to [^1]\n\n[^1]: Here is the footnote.\n\nAnd another reference to [^longnote]\n\n[^longnote]: Here's one with multiple blocks.\n\n Subsequent paragraphs are indented to show that they\nbelong to the previous footnote.\n\n > This is a block quote\n > Inside a footnote\n\n { some.code }\n\n The whole paragraph can be indented, or just the first\n line. In this way, multi-paragraph footnotes work like\n multi-paragraph list items.\n\nThis paragraph won't be part of the note, because it\nisn't indented.", "<p>Here is a footnote reference,<a id=\"fnref:1\" href=\"#fn:1\" class=\"footnote-ref\"><sup>1</sup></a> and another.<a id=\"fnref:3\" href=\"#fn:2\" class=\"footnote-ref\"><sup>2</sup></a></p>\n<p>This is another reference to <a id=\"fnref:2\" href=\"#fn:1\" class=\"footnote-ref\"><sup>1</sup></a></p>\n<p>And another reference to <a id=\"fnref:4\" href=\"#fn:2\" class=\"footnote-ref\"><sup>2</sup></a></p>\n<p>This paragraph won't be part of the note, because it\nisn't indented.</p>\n<div class=\"footnotes\">\n<hr />\n<ol>\n<li id=\"fn:1\">\n<p>Here is the footnote.<a href=\"#fnref:1\" class=\"footnote-back-ref\">↩</a><a href=\"#fnref:2\" class=\"footnote-back-ref\">↩</a></p>\n</li>\n<li id=\"fn:2\">\n<p>Here's one with multiple blocks.</p>\n<p>Subsequent paragraphs are indented to show that they\nbelong to the previous footnote.</p>\n<blockquote>\n<p>This is a block quote\nInside a footnote</p>\n</blockquote>\n<pre><code>{ some.code }\n</code></pre>\n<p>The whole paragraph can be indented, or just the first\nline. In this way, multi-paragraph footnotes work like\nmulti-paragraph list items.<a href=\"#fnref:3\" class=\"footnote-back-ref\">↩</a><a href=\"#fnref:4\" class=\"footnote-back-ref\">↩</a></p>\n</li>\n</ol>\n</div>", "footnotes");
|
||||
}
|
||||
}
|
||||
// Check with mulitple consecutive footnotes:
|
||||
[TestFixture]
|
||||
public partial class TestExtensionsFootontes
|
||||
{
|
||||
[Test]
|
||||
public void Example002()
|
||||
{
|
||||
// Example 2
|
||||
// Section: Extensions Footontes
|
||||
//
|
||||
// The following CommonMark:
|
||||
// Here is a footnote[^1]. And another one[^2]. And a third one[^3]. And a fourth[^4].
|
||||
//
|
||||
// [^1]: Footnote 1 text
|
||||
//
|
||||
// [^2]: Footnote 2 text
|
||||
//
|
||||
// a
|
||||
//
|
||||
// [^3]: Footnote 3 text
|
||||
//
|
||||
// [^4]: Footnote 4 text
|
||||
//
|
||||
// Should be rendered as:
|
||||
// <p>Here is a footnote<a id="fnref:1" href="#fn:1" class="footnote-ref"><sup>1</sup></a>. And another one<a id="fnref:2" href="#fn:2" class="footnote-ref"><sup>2</sup></a>. And a third one<a id="fnref:3" href="#fn:3" class="footnote-ref"><sup>3</sup></a>. And a fourth<a id="fnref:4" href="#fn:4" class="footnote-ref"><sup>4</sup></a>.</p>
|
||||
// <p>a</p>
|
||||
// <div class="footnotes">
|
||||
// <hr />
|
||||
// <ol>
|
||||
// <li id="fn:1">
|
||||
// <p>Footnote 1 text<a href="#fnref:1" class="footnote-back-ref">↩</a></p></li>
|
||||
// <li id="fn:2">
|
||||
// <p>Footnote 2 text<a href="#fnref:2" class="footnote-back-ref">↩</a></p></li>
|
||||
// <li id="fn:3">
|
||||
// <p>Footnote 3 text<a href="#fnref:3" class="footnote-back-ref">↩</a></p></li>
|
||||
// <li id="fn:4">
|
||||
// <p>Footnote 4 text<a href="#fnref:4" class="footnote-back-ref">↩</a></p></li>
|
||||
// </ol>
|
||||
// </div>
|
||||
|
||||
Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 2, "Extensions Footontes");
|
||||
TestParser.TestSpec("Here is a footnote[^1]. And another one[^2]. And a third one[^3]. And a fourth[^4].\n\n[^1]: Footnote 1 text\n\n[^2]: Footnote 2 text\n\na\n\n[^3]: Footnote 3 text\n\n[^4]: Footnote 4 text", "<p>Here is a footnote<a id=\"fnref:1\" href=\"#fn:1\" class=\"footnote-ref\"><sup>1</sup></a>. And another one<a id=\"fnref:2\" href=\"#fn:2\" class=\"footnote-ref\"><sup>2</sup></a>. And a third one<a id=\"fnref:3\" href=\"#fn:3\" class=\"footnote-ref\"><sup>3</sup></a>. And a fourth<a id=\"fnref:4\" href=\"#fn:4\" class=\"footnote-ref\"><sup>4</sup></a>.</p>\n<p>a</p>\n<div class=\"footnotes\">\n<hr />\n<ol>\n<li id=\"fn:1\">\n<p>Footnote 1 text<a href=\"#fnref:1\" class=\"footnote-back-ref\">↩</a></p></li>\n<li id=\"fn:2\">\n<p>Footnote 2 text<a href=\"#fnref:2\" class=\"footnote-back-ref\">↩</a></p></li>\n<li id=\"fn:3\">\n<p>Footnote 3 text<a href=\"#fnref:3\" class=\"footnote-back-ref\">↩</a></p></li>\n<li id=\"fn:4\">\n<p>Footnote 4 text<a href=\"#fnref:4\" class=\"footnote-back-ref\">↩</a></p></li>\n</ol>\n</div>", "footnotes");
|
||||
}
|
||||
}
|
||||
// Another test with consecutive footnotes without a blank line separator:
|
||||
[TestFixture]
|
||||
public partial class TestExtensionsFootontes
|
||||
{
|
||||
[Test]
|
||||
public void Example003()
|
||||
{
|
||||
// Example 3
|
||||
// Section: Extensions Footontes
|
||||
//
|
||||
// The following CommonMark:
|
||||
// Here is a footnote[^1]. And another one[^2]. And a third one[^3]. And a fourth[^4].
|
||||
//
|
||||
// [^1]: Footnote 1 text
|
||||
// [^2]: Footnote 2 text
|
||||
// [^3]: Footnote 3 text
|
||||
// [^4]: Footnote 4 text
|
||||
//
|
||||
// Should be rendered as:
|
||||
// <p>Here is a footnote<a id="fnref:1" href="#fn:1" class="footnote-ref"><sup>1</sup></a>. And another one<a id="fnref:2" href="#fn:2" class="footnote-ref"><sup>2</sup></a>. And a third one<a id="fnref:3" href="#fn:3" class="footnote-ref"><sup>3</sup></a>. And a fourth<a id="fnref:4" href="#fn:4" class="footnote-ref"><sup>4</sup></a>.</p>
|
||||
// <div class="footnotes">
|
||||
// <hr />
|
||||
// <ol>
|
||||
// <li id="fn:1">
|
||||
// <p>Footnote 1 text<a href="#fnref:1" class="footnote-back-ref">↩</a></p></li>
|
||||
// <li id="fn:2">
|
||||
// <p>Footnote 2 text<a href="#fnref:2" class="footnote-back-ref">↩</a></p></li>
|
||||
// <li id="fn:3">
|
||||
// <p>Footnote 3 text<a href="#fnref:3" class="footnote-back-ref">↩</a></p></li>
|
||||
// <li id="fn:4">
|
||||
// <p>Footnote 4 text<a href="#fnref:4" class="footnote-back-ref">↩</a></p></li>
|
||||
// </ol>
|
||||
// </div>
|
||||
|
||||
Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 3, "Extensions Footontes");
|
||||
TestParser.TestSpec("Here is a footnote[^1]. And another one[^2]. And a third one[^3]. And a fourth[^4].\n\n[^1]: Footnote 1 text\n[^2]: Footnote 2 text\n[^3]: Footnote 3 text\n[^4]: Footnote 4 text", "<p>Here is a footnote<a id=\"fnref:1\" href=\"#fn:1\" class=\"footnote-ref\"><sup>1</sup></a>. And another one<a id=\"fnref:2\" href=\"#fn:2\" class=\"footnote-ref\"><sup>2</sup></a>. And a third one<a id=\"fnref:3\" href=\"#fn:3\" class=\"footnote-ref\"><sup>3</sup></a>. And a fourth<a id=\"fnref:4\" href=\"#fn:4\" class=\"footnote-ref\"><sup>4</sup></a>.</p>\n<div class=\"footnotes\">\n<hr />\n<ol>\n<li id=\"fn:1\">\n<p>Footnote 1 text<a href=\"#fnref:1\" class=\"footnote-back-ref\">↩</a></p></li>\n<li id=\"fn:2\">\n<p>Footnote 2 text<a href=\"#fnref:2\" class=\"footnote-back-ref\">↩</a></p></li>\n<li id=\"fn:3\">\n<p>Footnote 3 text<a href=\"#fnref:3\" class=\"footnote-back-ref\">↩</a></p></li>\n<li id=\"fn:4\">\n<p>Footnote 4 text<a href=\"#fnref:4\" class=\"footnote-back-ref\">↩</a></p></li>\n</ol>\n</div>", "footnotes");
|
||||
}
|
||||
}
|
||||
// # Extensions
|
||||
//
|
||||
@@ -17978,6 +18079,26 @@ namespace Markdig.Tests
|
||||
Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 1, "Extensions Emoji");
|
||||
TestParser.TestSpec("This is a test with a :) and a :angry: smiley", "<p>This is a test with a 😃 and a 😠 smiley</p>", "emojis");
|
||||
}
|
||||
}
|
||||
// An emoji needs to be preceded by a space and followed by a space:
|
||||
[TestFixture]
|
||||
public partial class TestExtensionsEmoji
|
||||
{
|
||||
[Test]
|
||||
public void Example002()
|
||||
{
|
||||
// Example 2
|
||||
// Section: Extensions Emoji
|
||||
//
|
||||
// The following CommonMark:
|
||||
// These are not:) an :)emoji with a:) x:angry:x
|
||||
//
|
||||
// Should be rendered as:
|
||||
// <p>These are not:) an :)emoji with a:) x:angry:x</p>
|
||||
|
||||
Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 2, "Extensions Emoji");
|
||||
TestParser.TestSpec("These are not:) an :)emoji with a:) x:angry:x", "<p>These are not:) an :)emoji with a:) x:angry:x</p>", "emojis");
|
||||
}
|
||||
}
|
||||
// # Extensions
|
||||
//
|
||||
@@ -18722,7 +18843,7 @@ namespace Markdig.Tests
|
||||
// <p>This is a “text”</p>
|
||||
|
||||
Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 1, "Extensions SmartyPants Quotes");
|
||||
TestParser.TestSpec("This is a \"text\"", "<p>This is a “text”</p>", "smartypants");
|
||||
TestParser.TestSpec("This is a \"text\"", "<p>This is a “text”</p>", "pipetables+smartypants");
|
||||
}
|
||||
}
|
||||
[TestFixture]
|
||||
@@ -18741,7 +18862,7 @@ namespace Markdig.Tests
|
||||
// <p>This is a ‘text’</p>
|
||||
|
||||
Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 2, "Extensions SmartyPants Quotes");
|
||||
TestParser.TestSpec("This is a 'text'", "<p>This is a ‘text’</p>", "smartypants");
|
||||
TestParser.TestSpec("This is a 'text'", "<p>This is a ‘text’</p>", "pipetables+smartypants");
|
||||
}
|
||||
}
|
||||
[TestFixture]
|
||||
@@ -18760,7 +18881,7 @@ namespace Markdig.Tests
|
||||
// <p>This is a «text»</p>
|
||||
|
||||
Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 3, "Extensions SmartyPants Quotes");
|
||||
TestParser.TestSpec("This is a <<text>>", "<p>This is a «text»</p>", "smartypants");
|
||||
TestParser.TestSpec("This is a <<text>>", "<p>This is a «text»</p>", "pipetables+smartypants");
|
||||
}
|
||||
}
|
||||
// Unbalanced quotes are not changed:
|
||||
@@ -18780,7 +18901,7 @@ namespace Markdig.Tests
|
||||
// <p>This is a "text</p>
|
||||
|
||||
Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 4, "Extensions SmartyPants Quotes");
|
||||
TestParser.TestSpec("This is a \"text", "<p>This is a "text</p>", "smartypants");
|
||||
TestParser.TestSpec("This is a \"text", "<p>This is a "text</p>", "pipetables+smartypants");
|
||||
}
|
||||
}
|
||||
[TestFixture]
|
||||
@@ -18799,7 +18920,7 @@ namespace Markdig.Tests
|
||||
// <p>This is a 'text</p>
|
||||
|
||||
Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 5, "Extensions SmartyPants Quotes");
|
||||
TestParser.TestSpec("This is a 'text", "<p>This is a 'text</p>", "smartypants");
|
||||
TestParser.TestSpec("This is a 'text", "<p>This is a 'text</p>", "pipetables+smartypants");
|
||||
}
|
||||
}
|
||||
[TestFixture]
|
||||
@@ -18818,7 +18939,7 @@ namespace Markdig.Tests
|
||||
// <p>This is a <<text</p>
|
||||
|
||||
Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 6, "Extensions SmartyPants Quotes");
|
||||
TestParser.TestSpec("This is a <<text", "<p>This is a <<text</p>", "smartypants");
|
||||
TestParser.TestSpec("This is a <<text", "<p>This is a <<text</p>", "pipetables+smartypants");
|
||||
}
|
||||
}
|
||||
// Unbalanced quotes inside other quotes are not changed:
|
||||
@@ -18838,7 +18959,7 @@ namespace Markdig.Tests
|
||||
// <p>This is a “text 'with” a another text'</p>
|
||||
|
||||
Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 7, "Extensions SmartyPants Quotes");
|
||||
TestParser.TestSpec("This is a \"text 'with\" a another text'", "<p>This is a “text 'with” a another text'</p>", "smartypants");
|
||||
TestParser.TestSpec("This is a \"text 'with\" a another text'", "<p>This is a “text 'with” a another text'</p>", "pipetables+smartypants");
|
||||
}
|
||||
}
|
||||
[TestFixture]
|
||||
@@ -18857,7 +18978,7 @@ namespace Markdig.Tests
|
||||
// <p>This is a ‘text <<with’ a another text>></p>
|
||||
|
||||
Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 8, "Extensions SmartyPants Quotes");
|
||||
TestParser.TestSpec("This is a 'text <<with' a another text>>", "<p>This is a ‘text <<with’ a another text>></p>", "smartypants");
|
||||
TestParser.TestSpec("This is a 'text <<with' a another text>>", "<p>This is a ‘text <<with’ a another text>></p>", "pipetables+smartypants");
|
||||
}
|
||||
}
|
||||
[TestFixture]
|
||||
@@ -18876,7 +18997,7 @@ namespace Markdig.Tests
|
||||
// <p>This is a «text 'with» a another text'</p>
|
||||
|
||||
Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 9, "Extensions SmartyPants Quotes");
|
||||
TestParser.TestSpec("This is a <<text 'with>> a another text'", "<p>This is a «text 'with» a another text'</p>", "smartypants");
|
||||
TestParser.TestSpec("This is a <<text 'with>> a another text'", "<p>This is a «text 'with» a another text'</p>", "pipetables+smartypants");
|
||||
}
|
||||
}
|
||||
// Quotes requires to have the same rules than emphasis `_` regarding left/right frankling rules:
|
||||
@@ -18896,7 +19017,7 @@ namespace Markdig.Tests
|
||||
// <p>It's not quotes'</p>
|
||||
|
||||
Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 10, "Extensions SmartyPants Quotes");
|
||||
TestParser.TestSpec("It's not quotes'", "<p>It's not quotes'</p>", "smartypants");
|
||||
TestParser.TestSpec("It's not quotes'", "<p>It's not quotes'</p>", "pipetables+smartypants");
|
||||
}
|
||||
}
|
||||
[TestFixture]
|
||||
@@ -18915,7 +19036,7 @@ namespace Markdig.Tests
|
||||
// <p>They are ' not matching quotes '</p>
|
||||
|
||||
Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 11, "Extensions SmartyPants Quotes");
|
||||
TestParser.TestSpec("They are ' not matching quotes '", "<p>They are ' not matching quotes '</p>", "smartypants");
|
||||
TestParser.TestSpec("They are ' not matching quotes '", "<p>They are ' not matching quotes '</p>", "pipetables+smartypants");
|
||||
}
|
||||
}
|
||||
[TestFixture]
|
||||
@@ -18934,10 +19055,10 @@ namespace Markdig.Tests
|
||||
// <p>They are' not matching 'quotes</p>
|
||||
|
||||
Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 12, "Extensions SmartyPants Quotes");
|
||||
TestParser.TestSpec("They are' not matching 'quotes", "<p>They are' not matching 'quotes</p>", "smartypants");
|
||||
TestParser.TestSpec("They are' not matching 'quotes", "<p>They are' not matching 'quotes</p>", "pipetables+smartypants");
|
||||
}
|
||||
}
|
||||
// Double quotes using ``` `` ``` are working if they match another `''` pair, and there is no other double quotes on the line (otherwise they would be parsed as a code span):
|
||||
// An emphasis starting inside left/right quotes will span over the right quote:
|
||||
[TestFixture]
|
||||
public partial class TestExtensionsSmartyPantsQuotes
|
||||
{
|
||||
@@ -18948,78 +19069,76 @@ namespace Markdig.Tests
|
||||
// Section: Extensions SmartyPants Quotes
|
||||
//
|
||||
// The following CommonMark:
|
||||
// This is ``a double quote''
|
||||
//
|
||||
// Should be rendered as:
|
||||
// <p>This is “a double quote”</p>
|
||||
|
||||
Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 13, "Extensions SmartyPants Quotes");
|
||||
TestParser.TestSpec("This is ``a double quote''", "<p>This is “a double quote”</p>", "smartypants");
|
||||
}
|
||||
}
|
||||
[TestFixture]
|
||||
public partial class TestExtensionsSmartyPantsQuotes
|
||||
{
|
||||
[Test]
|
||||
public void Example014()
|
||||
{
|
||||
// Example 14
|
||||
// Section: Extensions SmartyPants Quotes
|
||||
//
|
||||
// The following CommonMark:
|
||||
// This is ``a code span''``
|
||||
//
|
||||
// Should be rendered as:
|
||||
// <p>This is <code>a code span''</code></p>
|
||||
|
||||
Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 14, "Extensions SmartyPants Quotes");
|
||||
TestParser.TestSpec("This is ``a code span''`` ", "<p>This is <code>a code span''</code></p>", "smartypants");
|
||||
}
|
||||
}
|
||||
[TestFixture]
|
||||
public partial class TestExtensionsSmartyPantsQuotes
|
||||
{
|
||||
[Test]
|
||||
public void Example015()
|
||||
{
|
||||
// Example 15
|
||||
// Section: Extensions SmartyPants Quotes
|
||||
//
|
||||
// The following CommonMark:
|
||||
// hello ``there```
|
||||
// test
|
||||
//
|
||||
// Should be rendered as:
|
||||
// <p>hello “there”`
|
||||
// test</p>
|
||||
|
||||
Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 15, "Extensions SmartyPants Quotes");
|
||||
TestParser.TestSpec("hello ``there```\ntest", "<p>hello “there”`\ntest</p>", "smartypants");
|
||||
}
|
||||
}
|
||||
// An emphasis starting inside left/right quotes will span over the right quote:
|
||||
[TestFixture]
|
||||
public partial class TestExtensionsSmartyPantsQuotes
|
||||
{
|
||||
[Test]
|
||||
public void Example016()
|
||||
{
|
||||
// Example 16
|
||||
// Section: Extensions SmartyPants Quotes
|
||||
//
|
||||
// The following CommonMark:
|
||||
// This is "a *text" with an emphasis*
|
||||
//
|
||||
// Should be rendered as:
|
||||
// <p>This is “a <em>text” with an emphasis</em></p>
|
||||
|
||||
Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 16, "Extensions SmartyPants Quotes");
|
||||
TestParser.TestSpec("This is \"a *text\" with an emphasis*", "<p>This is “a <em>text” with an emphasis</em></p>", "smartypants");
|
||||
Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 13, "Extensions SmartyPants Quotes");
|
||||
TestParser.TestSpec("This is \"a *text\" with an emphasis*", "<p>This is “a <em>text” with an emphasis</em></p>", "pipetables+smartypants");
|
||||
}
|
||||
}
|
||||
// ## SmartyPants Separators
|
||||
[TestFixture]
|
||||
public partial class TestExtensionsSmartyPantsSeparators
|
||||
{
|
||||
[Test]
|
||||
public void Example014()
|
||||
{
|
||||
// Example 14
|
||||
// Section: Extensions SmartyPants Separators
|
||||
//
|
||||
// The following CommonMark:
|
||||
// This is a -- text
|
||||
//
|
||||
// Should be rendered as:
|
||||
// <p>This is a – text</p>
|
||||
|
||||
Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 14, "Extensions SmartyPants Separators");
|
||||
TestParser.TestSpec("This is a -- text", "<p>This is a – text</p>", "pipetables+smartypants");
|
||||
}
|
||||
}
|
||||
[TestFixture]
|
||||
public partial class TestExtensionsSmartyPantsSeparators
|
||||
{
|
||||
[Test]
|
||||
public void Example015()
|
||||
{
|
||||
// Example 15
|
||||
// Section: Extensions SmartyPants Separators
|
||||
//
|
||||
// The following CommonMark:
|
||||
// This is a --- text
|
||||
//
|
||||
// Should be rendered as:
|
||||
// <p>This is a — text</p>
|
||||
|
||||
Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 15, "Extensions SmartyPants Separators");
|
||||
TestParser.TestSpec("This is a --- text", "<p>This is a — text</p>", "pipetables+smartypants");
|
||||
}
|
||||
}
|
||||
[TestFixture]
|
||||
public partial class TestExtensionsSmartyPantsSeparators
|
||||
{
|
||||
[Test]
|
||||
public void Example016()
|
||||
{
|
||||
// Example 16
|
||||
// Section: Extensions SmartyPants Separators
|
||||
//
|
||||
// The following CommonMark:
|
||||
// This is a en ellipsis...
|
||||
//
|
||||
// Should be rendered as:
|
||||
// <p>This is a en ellipsis…</p>
|
||||
|
||||
Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 16, "Extensions SmartyPants Separators");
|
||||
TestParser.TestSpec("This is a en ellipsis...", "<p>This is a en ellipsis…</p>", "pipetables+smartypants");
|
||||
}
|
||||
}
|
||||
// Check that a smartypants are not breaking pipetable parsing:
|
||||
[TestFixture]
|
||||
public partial class TestExtensionsSmartyPantsSeparators
|
||||
{
|
||||
[Test]
|
||||
public void Example017()
|
||||
@@ -19028,51 +19147,28 @@ namespace Markdig.Tests
|
||||
// Section: Extensions SmartyPants Separators
|
||||
//
|
||||
// The following CommonMark:
|
||||
// This is a -- text
|
||||
// a | b
|
||||
// -- | --
|
||||
// 0 | 1
|
||||
//
|
||||
// Should be rendered as:
|
||||
// <p>This is a – text</p>
|
||||
// <table>
|
||||
// <thead>
|
||||
// <tr>
|
||||
// <th>a</th>
|
||||
// <th>b</th>
|
||||
// </tr>
|
||||
// </thead>
|
||||
// <tbody>
|
||||
// <tr>
|
||||
// <td>0</td>
|
||||
// <td>1</td>
|
||||
// </tr>
|
||||
// </tbody>
|
||||
// </table>
|
||||
|
||||
Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 17, "Extensions SmartyPants Separators");
|
||||
TestParser.TestSpec("This is a -- text", "<p>This is a – text</p>", "smartypants");
|
||||
}
|
||||
}
|
||||
[TestFixture]
|
||||
public partial class TestExtensionsSmartyPantsSeparators
|
||||
{
|
||||
[Test]
|
||||
public void Example018()
|
||||
{
|
||||
// Example 18
|
||||
// Section: Extensions SmartyPants Separators
|
||||
//
|
||||
// The following CommonMark:
|
||||
// This is a --- text
|
||||
//
|
||||
// Should be rendered as:
|
||||
// <p>This is a — text</p>
|
||||
|
||||
Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 18, "Extensions SmartyPants Separators");
|
||||
TestParser.TestSpec("This is a --- text", "<p>This is a — text</p>", "smartypants");
|
||||
}
|
||||
}
|
||||
[TestFixture]
|
||||
public partial class TestExtensionsSmartyPantsSeparators
|
||||
{
|
||||
[Test]
|
||||
public void Example019()
|
||||
{
|
||||
// Example 19
|
||||
// Section: Extensions SmartyPants Separators
|
||||
//
|
||||
// The following CommonMark:
|
||||
// This is a en ellipsis...
|
||||
//
|
||||
// Should be rendered as:
|
||||
// <p>This is a en ellipsis…</p>
|
||||
|
||||
Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 19, "Extensions SmartyPants Separators");
|
||||
TestParser.TestSpec("This is a en ellipsis...", "<p>This is a en ellipsis…</p>", "smartypants");
|
||||
TestParser.TestSpec("a | b\n-- | --\n0 | 1", "<table>\n<thead>\n<tr>\n<th>a</th>\n<th>b</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>0</td>\n<td>1</td>\n</tr>\n</tbody>\n</table>", "pipetables+smartypants");
|
||||
}
|
||||
}
|
||||
// # Extensions
|
||||
@@ -19317,15 +19413,15 @@ namespace Markdig.Tests
|
||||
// - Item4
|
||||
//
|
||||
// Should be rendered as:
|
||||
// <ul>
|
||||
// <li><input disabled="disabled" type="checkbox" /> Item1</li>
|
||||
// <li><input disabled="disabled" type="checkbox" checked="checked" /> Item2</li>
|
||||
// <li><input disabled="disabled" type="checkbox" /> Item3</li>
|
||||
// <ul class="contains-task-list">
|
||||
// <li class="task-list-item"><input disabled="disabled" type="checkbox" /> Item1</li>
|
||||
// <li class="task-list-item"><input disabled="disabled" type="checkbox" checked="checked" /> Item2</li>
|
||||
// <li class="task-list-item"><input disabled="disabled" type="checkbox" /> Item3</li>
|
||||
// <li>Item4</li>
|
||||
// </ul>
|
||||
|
||||
Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 1, "Extensions TaskLists");
|
||||
TestParser.TestSpec("- [ ] Item1\n- [x] Item2\n- [ ] Item3\n- Item4", "<ul>\n<li><input disabled=\"disabled\" type=\"checkbox\" /> Item1</li>\n<li><input disabled=\"disabled\" type=\"checkbox\" checked=\"checked\" /> Item2</li>\n<li><input disabled=\"disabled\" type=\"checkbox\" /> Item3</li>\n<li>Item4</li>\n</ul>", "tasklists");
|
||||
TestParser.TestSpec("- [ ] Item1\n- [x] Item2\n- [ ] Item3\n- Item4", "<ul class=\"contains-task-list\">\n<li class=\"task-list-item\"><input disabled=\"disabled\" type=\"checkbox\" /> Item1</li>\n<li class=\"task-list-item\"><input disabled=\"disabled\" type=\"checkbox\" checked=\"checked\" /> Item2</li>\n<li class=\"task-list-item\"><input disabled=\"disabled\" type=\"checkbox\" /> Item3</li>\n<li>Item4</li>\n</ul>", "tasklists");
|
||||
}
|
||||
}
|
||||
// A task is not recognized outside a list item:
|
||||
|
||||
@@ -54,7 +54,7 @@ SOFTWARE.
|
||||
new KeyValuePair<string, string>(Host.ResolvePath("MathSpecs.md"), "mathematics"),
|
||||
new KeyValuePair<string, string>(Host.ResolvePath("BootstrapSpecs.md"), "bootstrap+pipetables+figures+attributes"),
|
||||
new KeyValuePair<string, string>(Host.ResolvePath("MediaSpecs.md"), "medialinks"),
|
||||
new KeyValuePair<string, string>(Host.ResolvePath("SmartyPantsSpecs.md"), "smartypants"),
|
||||
new KeyValuePair<string, string>(Host.ResolvePath("SmartyPantsSpecs.md"), "pipetables+smartypants"), // Check with smartypants to make sure that it doesn't break pipetables
|
||||
new KeyValuePair<string, string>(Host.ResolvePath("AutoIdentifierSpecs.md"), "autoidentifiers"),
|
||||
new KeyValuePair<string, string>(Host.ResolvePath("TaskListSpecs.md"), "tasklists"),
|
||||
};
|
||||
|
||||
@@ -12,10 +12,10 @@ A task list item consist of `[ ]` or `[x]` or `[X]` inside a list item (ordered
|
||||
- [ ] Item3
|
||||
- Item4
|
||||
.
|
||||
<ul>
|
||||
<li><input disabled="disabled" type="checkbox" /> Item1</li>
|
||||
<li><input disabled="disabled" type="checkbox" checked="checked" /> Item2</li>
|
||||
<li><input disabled="disabled" type="checkbox" /> Item3</li>
|
||||
<ul class="contains-task-list">
|
||||
<li class="task-list-item"><input disabled="disabled" type="checkbox" /> Item1</li>
|
||||
<li class="task-list-item"><input disabled="disabled" type="checkbox" checked="checked" /> Item2</li>
|
||||
<li class="task-list-item"><input disabled="disabled" type="checkbox" /> Item3</li>
|
||||
<li>Item4</li>
|
||||
</ul>
|
||||
````````````````````````````````
|
||||
|
||||
96
src/Markdig.Tests/TestHtmlAttributes.cs
Normal file
96
src/Markdig.Tests/TestHtmlAttributes.cs
Normal file
@@ -0,0 +1,96 @@
|
||||
// Copyright (c) Alexandre Mutel. All rights reserved.
|
||||
// This file is licensed under the BSD-Clause 2 license.
|
||||
// See the license.txt file in the project root for more information.
|
||||
|
||||
using Markdig.Renderers.Html;
|
||||
using NUnit.Framework;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Markdig.Tests
|
||||
{
|
||||
[TestFixture()]
|
||||
public class TestHtmlAttributes
|
||||
{
|
||||
[Test]
|
||||
public void TestAddClass()
|
||||
{
|
||||
var attributes = new HtmlAttributes();
|
||||
attributes.AddClass("test");
|
||||
Assert.NotNull(attributes.Classes);
|
||||
Assert.AreEqual(new List<string>() { "test" }, attributes.Classes);
|
||||
|
||||
attributes.AddClass("test");
|
||||
Assert.AreEqual(1, attributes.Classes.Count);
|
||||
|
||||
attributes.AddClass("test1");
|
||||
Assert.AreEqual(new List<string>() { "test", "test1" }, attributes.Classes);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestAddProperty()
|
||||
{
|
||||
var attributes = new HtmlAttributes();
|
||||
attributes.AddProperty("key1", "1");
|
||||
Assert.NotNull(attributes.Properties);
|
||||
Assert.AreEqual(new List<KeyValuePair<string, string>>() { new KeyValuePair<string, string>("key1", "1") }, attributes.Properties);
|
||||
|
||||
attributes.AddPropertyIfNotExist("key1", "1");
|
||||
Assert.NotNull(attributes.Properties);
|
||||
Assert.AreEqual(new List<KeyValuePair<string, string>>() { new KeyValuePair<string, string>("key1", "1") }, attributes.Properties);
|
||||
|
||||
attributes.AddPropertyIfNotExist("key2", "2");
|
||||
Assert.AreEqual(new List<KeyValuePair<string, string>>() { new KeyValuePair<string, string>("key1", "1"), new KeyValuePair<string, string>("key2", "2") }, attributes.Properties);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestCopyTo()
|
||||
{
|
||||
var from = new HtmlAttributes();
|
||||
from.AddClass("test");
|
||||
from.AddProperty("key1", "1");
|
||||
|
||||
var to = new HtmlAttributes();
|
||||
from.CopyTo(to);
|
||||
|
||||
Assert.True(ReferenceEquals(from.Classes, to.Classes));
|
||||
Assert.True(ReferenceEquals(from.Properties, to.Properties));
|
||||
|
||||
// From: Classes From: Properties To: Classes To: Properties
|
||||
// test1: null null null null
|
||||
from = new HtmlAttributes();
|
||||
to = new HtmlAttributes();
|
||||
from.CopyTo(to, false, false);
|
||||
Assert.Null(to.Classes);
|
||||
Assert.Null(to.Properties);
|
||||
|
||||
// test2: ["test"] ["key1", "1"] null null
|
||||
from = new HtmlAttributes();
|
||||
to = new HtmlAttributes();
|
||||
from.AddClass("test");
|
||||
from.AddProperty("key1", "1");
|
||||
from.CopyTo(to, false, false);
|
||||
Assert.AreEqual(new List<string>() { "test" }, to.Classes);
|
||||
Assert.AreEqual(new List<KeyValuePair<string, string>>() { new KeyValuePair<string, string>("key1", "1")}, to.Properties);
|
||||
|
||||
// test3: null null ["test"] ["key1", "1"]
|
||||
from = new HtmlAttributes();
|
||||
to = new HtmlAttributes();
|
||||
to.AddClass("test");
|
||||
to.AddProperty("key1", "1");
|
||||
from.CopyTo(to, false, false);
|
||||
Assert.AreEqual(new List<string>() { "test" }, to.Classes);
|
||||
Assert.AreEqual(new List<KeyValuePair<string, string>>() { new KeyValuePair<string, string>("key1", "1") }, to.Properties);
|
||||
|
||||
// test4: ["test1"] ["key2", "2"] ["test"] ["key1", "1"]
|
||||
from = new HtmlAttributes();
|
||||
to = new HtmlAttributes();
|
||||
from.AddClass("test1");
|
||||
from.AddProperty("key2", "2");
|
||||
to.AddClass("test");
|
||||
to.AddProperty("key1", "1");
|
||||
from.CopyTo(to, false, false);
|
||||
Assert.AreEqual(new List<string>() { "test", "test1" }, to.Classes);
|
||||
Assert.AreEqual(new List<KeyValuePair<string, string>>() { new KeyValuePair<string, string>("key1", "1"), new KeyValuePair<string, string>("key2", "2") }, to.Properties);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -64,7 +64,8 @@ namespace Markdig.Tests
|
||||
var extensionGroups = extensionsGroupText.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
foreach (var extensionsText in extensionGroups)
|
||||
{
|
||||
var pipeline = new MarkdownPipelineBuilder().Configure(extensionsText);
|
||||
var builder = new MarkdownPipelineBuilder();
|
||||
var pipeline = extensionsText == "self" ? builder.UseSelfPipeline() : builder.Configure(extensionsText);
|
||||
yield return new KeyValuePair<string, MarkdownPipeline>(extensionsText, pipeline.Build());
|
||||
}
|
||||
}
|
||||
@@ -78,7 +79,7 @@ namespace Markdig.Tests
|
||||
private static string Compact(string html)
|
||||
{
|
||||
// Normalize the output to make it compatible with CommonMark specs
|
||||
html = html.Replace("\r", "").Trim();
|
||||
html = html.Replace("\r\n", "\n").Replace(@"\r", @"\n").Trim();
|
||||
html = Regex.Replace(html, @"\s+</li>", "</li>");
|
||||
html = Regex.Replace(html, @"<li>\s+", "<li>");
|
||||
html = html.Normalize(NormalizationForm.FormKD);
|
||||
|
||||
@@ -27,6 +27,85 @@ Later in a text we are using HTML and it becomes an abbr tag HTML
|
||||
Console.WriteLine(result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestSelfPipeline1()
|
||||
{
|
||||
var text = @"<!--markdig:pipetables-->
|
||||
|
||||
a | b
|
||||
- | -
|
||||
0 | 1
|
||||
";
|
||||
TestParser.TestSpec(text, @"<!--markdig:pipetables-->
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>a</th>
|
||||
<th>b</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td>0</td>
|
||||
<td>1</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
", "self");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestListBug()
|
||||
{
|
||||
// TODO: Add this test back to the CommonMark specs
|
||||
var text = @"- item1
|
||||
- item2
|
||||
- item3
|
||||
- item4";
|
||||
TestParser.TestSpec(text, @"<ul>
|
||||
<li>item1
|
||||
<ul>
|
||||
<li>item2
|
||||
<ul>
|
||||
<li>item3
|
||||
<ul>
|
||||
<li>item4</li>
|
||||
</ul></li>
|
||||
</ul></li>
|
||||
</ul></li>
|
||||
</ul>
|
||||
");
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public void TestHtmlBug()
|
||||
{
|
||||
TestParser.TestSpec(@" # header1
|
||||
|
||||
<pre class='copy'>
|
||||
blabla
|
||||
</pre>
|
||||
|
||||
# header2
|
||||
", @"<h1>header1</h1>
|
||||
<pre class='copy'>
|
||||
blabla
|
||||
</pre>
|
||||
<h1>header2</h1>");
|
||||
}
|
||||
|
||||
|
||||
|
||||
[Test]
|
||||
public void TestBugAdvancaed()
|
||||
{
|
||||
TestParser.TestSpec(@"`https://{domain}/callbacks`
|
||||
#### HEADING
|
||||
Paragraph
|
||||
", "<p><code>https://{domain}/callbacks</code></p>\n<h4 id=\"heading\">HEADING</h4>\n<p>Paragraph</p>", "advanced");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestSamePipelineAllExtensions()
|
||||
{
|
||||
|
||||
81
src/Markdig.Tests/TestPragmaLines.cs
Normal file
81
src/Markdig.Tests/TestPragmaLines.cs
Normal file
@@ -0,0 +1,81 @@
|
||||
// Copyright (c) Alexandre Mutel. All rights reserved.
|
||||
// This file is licensed under the BSD-Clause 2 license.
|
||||
// See the license.txt file in the project root for more information.
|
||||
|
||||
using Markdig.Renderers;
|
||||
using Markdig.Syntax;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Markdig.Tests
|
||||
{
|
||||
[TestFixture]
|
||||
public class TestPragmaLines
|
||||
{
|
||||
[Test]
|
||||
public void TestFindClosest()
|
||||
{
|
||||
var doc = Markdown.Parse(
|
||||
"test1\n" + // 0
|
||||
"\n" + // 1
|
||||
"test2\n" + // 2
|
||||
"\n" + // 3
|
||||
"test3\n" + // 4
|
||||
"\n" + // 5
|
||||
"test4\n" + // 6
|
||||
"\n" + // 7
|
||||
"# Heading\n" + // 8
|
||||
"\n" + // 9
|
||||
"Long para\n" + // 10
|
||||
"on multiple\n" + // 11
|
||||
"lines\n" + // 12
|
||||
"to check that\n" + // 13
|
||||
"lines are\n" + // 14
|
||||
"correctly \n" + // 15
|
||||
"found\n" + // 16
|
||||
"\n" + // 17
|
||||
"- item1\n" + // 18
|
||||
"- item2\n" + // 19
|
||||
"- item3\n" + // 20
|
||||
"\n" + // 21
|
||||
"This is a last paragraph\n" // 22
|
||||
, new MarkdownPipelineBuilder().UsePragmaLines().Build());
|
||||
|
||||
foreach (var exact in new int[] {0, 2, 4, 6, 8, 10, 18, 19, 20, 22})
|
||||
{
|
||||
Assert.AreEqual(exact, doc.FindClosestLine(exact));
|
||||
}
|
||||
|
||||
Assert.AreEqual(22, doc.FindClosestLine(23));
|
||||
|
||||
Assert.AreEqual(10, doc.FindClosestLine(11));
|
||||
Assert.AreEqual(10, doc.FindClosestLine(12));
|
||||
Assert.AreEqual(10, doc.FindClosestLine(13));
|
||||
Assert.AreEqual(18, doc.FindClosestLine(14)); // > 50% of the paragraph, we switch to next
|
||||
Assert.AreEqual(18, doc.FindClosestLine(15));
|
||||
Assert.AreEqual(18, doc.FindClosestLine(16));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestFindClosest1()
|
||||
{
|
||||
var text =
|
||||
"- item1\n" + // 0
|
||||
" - item11\n" + // 1
|
||||
" - item12\n" + // 2
|
||||
" - item121\n" + // 3
|
||||
" - item13\n" + // 4
|
||||
" - item131\n" + // 5
|
||||
" - item1311\n"; // 6
|
||||
|
||||
var pipeline = new MarkdownPipelineBuilder().UsePragmaLines().Build();
|
||||
var doc = Markdown.Parse(text, pipeline);
|
||||
|
||||
for (int exact = 0; exact < 7; exact++)
|
||||
{
|
||||
Assert.AreEqual(exact, doc.FindClosestLine(exact));
|
||||
}
|
||||
|
||||
Assert.AreEqual(6, doc.FindClosestLine(50));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -206,19 +206,6 @@ literal ( 0, 4) 4-5
|
||||
Assert.AreEqual(SourceSpan.Empty, link.TitleSpan);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestHtmlInline()
|
||||
{
|
||||
// 0123456789
|
||||
Check("01<b>4</b>", @"
|
||||
paragraph ( 0, 0) 0-9
|
||||
literal ( 0, 0) 0-1
|
||||
html ( 0, 2) 2-4
|
||||
literal ( 0, 5) 5-5
|
||||
html ( 0, 6) 6-9
|
||||
");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestAutolinkInline()
|
||||
{
|
||||
@@ -252,6 +239,64 @@ literal ( 4, 0) 16-16
|
||||
");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestHtmlBlock1()
|
||||
{
|
||||
// 0 1
|
||||
// 01 2 345678901 23
|
||||
Check("0\n\n<!--A-->\n1\n", @"
|
||||
paragraph ( 0, 0) 0-0
|
||||
literal ( 0, 0) 0-0
|
||||
html ( 2, 0) 3-10
|
||||
paragraph ( 3, 0) 12-12
|
||||
literal ( 3, 0) 12-12
|
||||
");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestHtmlComment()
|
||||
{
|
||||
// 0 1 2
|
||||
// 012345678901 234567890 1234
|
||||
Check("# 012345678\n<!--0-->\n123\n", @"
|
||||
heading ( 0, 0) 0-10
|
||||
literal ( 0, 2) 2-10
|
||||
html ( 1, 0) 12-19
|
||||
paragraph ( 2, 0) 21-23
|
||||
literal ( 2, 0) 21-23
|
||||
");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestHtmlInline()
|
||||
{
|
||||
// 0123456789
|
||||
Check("01<b>4</b>", @"
|
||||
paragraph ( 0, 0) 0-9
|
||||
literal ( 0, 0) 0-1
|
||||
html ( 0, 2) 2-4
|
||||
literal ( 0, 5) 5-5
|
||||
html ( 0, 6) 6-9
|
||||
");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestHtmlInline1()
|
||||
{
|
||||
// 0
|
||||
// 0123456789
|
||||
Check("0<!--A-->1", @"
|
||||
paragraph ( 0, 0) 0-9
|
||||
literal ( 0, 0) 0-0
|
||||
html ( 0, 1) 1-8
|
||||
literal ( 0, 9) 9-9
|
||||
");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
[Test]
|
||||
public void TestThematicBreak()
|
||||
{
|
||||
|
||||
@@ -98,7 +98,7 @@ namespace Markdig.Extensions.DefinitionLists
|
||||
processor.Open(definitionItem);
|
||||
|
||||
// Update the end position
|
||||
currentDefinitionList.Span.End = processor.Line.End;
|
||||
currentDefinitionList.UpdateSpanEnd(processor.Line.End);
|
||||
|
||||
return BlockState.Continue;
|
||||
}
|
||||
|
||||
@@ -67,18 +67,34 @@ namespace Markdig.Extensions.Emoji
|
||||
public override bool Match(InlineProcessor processor, ref StringSlice slice)
|
||||
{
|
||||
string match;
|
||||
|
||||
// Previous char must be a space
|
||||
if (!slice.PeekCharExtra(-1).IsWhiteSpaceOrZero())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Try to match an existing emoji
|
||||
var startPosition = slice.Start;
|
||||
if (!textMatchHelper.TryMatch(slice.Text, slice.Start, slice.Length, out match))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Following char must be a space
|
||||
if (!slice.PeekCharExtra(match.Length).IsWhiteSpaceOrZero())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// If we have a smiley, we decode it to emoji
|
||||
string emoji;
|
||||
if (!SmileyToEmoji.TryGetValue(match, out emoji))
|
||||
{
|
||||
emoji = match;
|
||||
}
|
||||
|
||||
// Decode the eomji to unicode
|
||||
string unicode;
|
||||
if (!EmojiToUnicode.TryGetValue(emoji, out unicode))
|
||||
{
|
||||
|
||||
@@ -114,7 +114,7 @@ namespace Markdig.Extensions.Figures
|
||||
figure.Add(caption);
|
||||
}
|
||||
|
||||
figure.Span.End = line.End;
|
||||
figure.UpdateSpanEnd(line.End);
|
||||
|
||||
// Don't keep the last line
|
||||
return BlockState.BreakDiscard;
|
||||
@@ -123,7 +123,7 @@ namespace Markdig.Extensions.Figures
|
||||
// Reset the indentation to the column before the indent
|
||||
processor.GoToColumn(processor.ColumnBeforeIndent);
|
||||
|
||||
figure.Span.End = line.End;
|
||||
figure.UpdateSpanEnd(line.End);
|
||||
|
||||
return BlockState.Continue;
|
||||
}
|
||||
|
||||
@@ -80,7 +80,7 @@ namespace Markdig.Extensions.Footers
|
||||
{
|
||||
processor.NextChar(); // Skip following space
|
||||
}
|
||||
block.Span.End = processor.Line.End;
|
||||
block.UpdateSpanEnd(processor.Line.End);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -26,9 +26,14 @@ namespace Markdig.Extensions.Footnotes
|
||||
}
|
||||
|
||||
public override BlockState TryOpen(BlockProcessor processor)
|
||||
{
|
||||
return TryOpen(processor, false);
|
||||
}
|
||||
|
||||
private BlockState TryOpen(BlockProcessor processor, bool isContinue)
|
||||
{
|
||||
// We expect footnote to appear only at document level and not indented more than a code indent block
|
||||
if (processor.IsCodeIndent || processor.CurrentContainer.GetType() != typeof(MarkdownDocument) )
|
||||
if (processor.IsCodeIndent || (!isContinue && processor.CurrentContainer.GetType() != typeof(MarkdownDocument)) || (isContinue && !(processor.CurrentContainer is Footnote)))
|
||||
{
|
||||
return BlockState.None;
|
||||
}
|
||||
@@ -42,7 +47,7 @@ namespace Markdig.Extensions.Footnotes
|
||||
processor.GoToColumn(saved);
|
||||
return BlockState.None;
|
||||
}
|
||||
|
||||
|
||||
// Advance the column
|
||||
int deltaColumn = processor.Start - start;
|
||||
processor.Column = processor.Column + deltaColumn;
|
||||
@@ -88,9 +93,23 @@ namespace Markdig.Extensions.Footnotes
|
||||
return BlockState.ContinueDiscard;
|
||||
}
|
||||
|
||||
if (footnote.IsLastLineEmpty && processor.Column == 0)
|
||||
if (processor.Column == 0)
|
||||
{
|
||||
return BlockState.Break;
|
||||
if (footnote.IsLastLineEmpty)
|
||||
{
|
||||
// Close the current footnote
|
||||
processor.Close(footnote);
|
||||
|
||||
// Parse any opening footnote
|
||||
return TryOpen(processor);
|
||||
}
|
||||
|
||||
// Make sure that consecutive footnotes without a blanklines are parsed correctly
|
||||
if (TryOpen(processor, true) == BlockState.Continue)
|
||||
{
|
||||
processor.Close(footnote);
|
||||
return BlockState.Continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
footnote.IsLastLineEmpty = false;
|
||||
|
||||
@@ -46,7 +46,7 @@ namespace Markdig.Extensions.GenericAttributes
|
||||
// Try to find if there is any attributes { in the info string on the first line of a FencedCodeBlock
|
||||
if (line.Start < line.End)
|
||||
{
|
||||
var indexOfAttributes = line.Text.LastIndexOf('{', line.End);
|
||||
int indexOfAttributes = line.IndexOf('{');
|
||||
if (indexOfAttributes >= 0)
|
||||
{
|
||||
// Work on a copy
|
||||
|
||||
@@ -13,7 +13,7 @@ namespace Markdig.Extensions.Mathematics
|
||||
/// An inline parser for <see cref="MathInline"/>.
|
||||
/// </summary>
|
||||
/// <seealso cref="Markdig.Parsers.InlineParser" />
|
||||
/// <seealso cref="Markdig.Parsers.IDelimiterProcessor" />
|
||||
/// <seealso cref="IPostInlineProcessor" />
|
||||
public class MathInlineParser : InlineParser
|
||||
{
|
||||
/// <summary>
|
||||
|
||||
79
src/Markdig/Extensions/PragmaLines/PragmaLineExtension.cs
Normal file
79
src/Markdig/Extensions/PragmaLines/PragmaLineExtension.cs
Normal file
@@ -0,0 +1,79 @@
|
||||
// Copyright (c) Alexandre Mutel. All rights reserved.
|
||||
// This file is licensed under the BSD-Clause 2 license.
|
||||
// See the license.txt file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using Markdig.Helpers;
|
||||
using Markdig.Renderers;
|
||||
using Markdig.Renderers.Html;
|
||||
using Markdig.Syntax;
|
||||
using Markdig.Syntax.Inlines;
|
||||
|
||||
namespace Markdig.Extensions.PragmaLines
|
||||
{
|
||||
/// <summary>
|
||||
/// Extension to a span for each line containing the original line id (using id = pragma-line#line_number_zero_based)
|
||||
/// </summary>
|
||||
/// <seealso cref="Markdig.IMarkdownExtension" />
|
||||
public class PragmaLineExtension : IMarkdownExtension
|
||||
{
|
||||
public void Setup(MarkdownPipelineBuilder pipeline)
|
||||
{
|
||||
pipeline.DocumentProcessed -= PipelineOnDocumentProcessed;
|
||||
pipeline.DocumentProcessed += PipelineOnDocumentProcessed;
|
||||
}
|
||||
|
||||
public void Setup(IMarkdownRenderer renderer)
|
||||
{
|
||||
}
|
||||
|
||||
private static void PipelineOnDocumentProcessed(MarkdownDocument document)
|
||||
{
|
||||
int index = 0;
|
||||
AddPragmas(document, ref index);
|
||||
}
|
||||
|
||||
private static void AddPragmas(Block block, ref int index)
|
||||
{
|
||||
var attribute = block.GetAttributes();
|
||||
var pragmaId = GetPragmaId(block);
|
||||
if ( attribute.Id == null)
|
||||
{
|
||||
attribute.Id = pragmaId;
|
||||
}
|
||||
else if (block.Parent != null)
|
||||
{
|
||||
var heading = block as HeadingBlock;
|
||||
|
||||
// If we have a heading, we will try to add the tag inside it
|
||||
// otherwise we will add it just before
|
||||
var tag = $"<a id=\"{pragmaId}\"></a>";
|
||||
if (heading?.Inline?.FirstChild != null)
|
||||
{
|
||||
heading.Inline.FirstChild.InsertBefore(new HtmlInline() { Tag = tag });
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
block.Parent.Insert(index, new HtmlBlock(null) { Lines = new StringLineGroup(tag) });
|
||||
index++;
|
||||
}
|
||||
}
|
||||
|
||||
var container = block as ContainerBlock;
|
||||
if (container != null)
|
||||
{
|
||||
for (int i = 0; i < container.Count; i++)
|
||||
{
|
||||
var subBlock = container[i];
|
||||
AddPragmas(subBlock, ref i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static string GetPragmaId(Block block)
|
||||
{
|
||||
return $"pragma-line-{block.Line}";
|
||||
}
|
||||
}
|
||||
}
|
||||
99
src/Markdig/Extensions/SelfPipeline/SelfPipelineExtension.cs
Normal file
99
src/Markdig/Extensions/SelfPipeline/SelfPipelineExtension.cs
Normal file
@@ -0,0 +1,99 @@
|
||||
// Copyright (c) Alexandre Mutel. All rights reserved.
|
||||
// This file is licensed under the BSD-Clause 2 license.
|
||||
// See the license.txt file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using Markdig.Renderers;
|
||||
|
||||
namespace Markdig.Extensions.SelfPipeline
|
||||
{
|
||||
/// <summary>
|
||||
/// Extension to enable SelfPipeline, to configure a Markdown parsing/convertion to HTML automatically
|
||||
/// from an embedded special tag in the input text <code><!--markdig:extensions--></code> where extensions is a string
|
||||
/// that specifies the extensions to use for the pipeline as exposed by <see cref="MarkdownExtensions.Configure"/> extension method
|
||||
/// on the <see cref="MarkdownPipelineBuilder"/>. This extension will invalidate all other extensions and will override them.
|
||||
/// </summary>
|
||||
public sealed class SelfPipelineExtension : IMarkdownExtension
|
||||
{
|
||||
public const string DefaultTag = "markdig";
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="SelfPipelineExtension"/> class.
|
||||
/// </summary>
|
||||
/// <param name="tag">The matching start tag.</param>
|
||||
/// <param name="defaultExtensions">The default extensions.</param>
|
||||
/// <exception cref="System.ArgumentException">Tag cannot contain `<` or `>` characters</exception>
|
||||
public SelfPipelineExtension(string tag = null, string defaultExtensions = null)
|
||||
{
|
||||
tag = tag?.Trim();
|
||||
tag = string.IsNullOrEmpty(tag) ? DefaultTag : tag;
|
||||
if (tag.IndexOfAny(new []{'<', '>'}) >= 0)
|
||||
{
|
||||
throw new ArgumentException("Tag cannot contain `<` or `>` characters", nameof(tag));
|
||||
}
|
||||
|
||||
if (defaultExtensions != null)
|
||||
{
|
||||
// Check that this default pipeline is supported
|
||||
// Will throw an ArgumentInvalidException if not
|
||||
new MarkdownPipelineBuilder().Configure(defaultExtensions);
|
||||
}
|
||||
DefaultExtensions = defaultExtensions;
|
||||
SelfPipelineHintTagStart = "<!--" + tag + ":";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the default pipeline to configure if no tag was found in the input text. Default is <c>null</c> (core pipeline).
|
||||
/// </summary>
|
||||
public string DefaultExtensions { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the self pipeline hint tag start that will be matched.
|
||||
/// </summary>
|
||||
public string SelfPipelineHintTagStart { get; }
|
||||
|
||||
public void Setup(MarkdownPipelineBuilder pipeline)
|
||||
{
|
||||
// Make sure that this pipeline has only one extension (itself)
|
||||
if (pipeline.Extensions.Count > 1)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
"The SelfPipeline extension cannot be configured with other extensions");
|
||||
}
|
||||
}
|
||||
|
||||
public void Setup(IMarkdownRenderer renderer)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a pipeline automatically configured from an input markdown based on the presence of the configuration tag.
|
||||
/// </summary>
|
||||
/// <param name="inputText">The input text.</param>
|
||||
/// <returns>The pipeline configured from the input</returns>
|
||||
/// <exception cref="System.ArgumentNullException"></exception>
|
||||
public MarkdownPipeline CreatePipelineFromInput(string inputText)
|
||||
{
|
||||
if (inputText == null) throw new ArgumentNullException(nameof(inputText));
|
||||
|
||||
var builder = new MarkdownPipelineBuilder();
|
||||
string defaultConfig = DefaultExtensions;
|
||||
var indexOfSelfPipeline = inputText.IndexOf(SelfPipelineHintTagStart, StringComparison.OrdinalIgnoreCase);
|
||||
if (indexOfSelfPipeline >= 0)
|
||||
{
|
||||
var optionStart = indexOfSelfPipeline + SelfPipelineHintTagStart.Length;
|
||||
var endOfTag = inputText.IndexOf("-->", optionStart, StringComparison.OrdinalIgnoreCase);
|
||||
if (endOfTag >= 0)
|
||||
{
|
||||
defaultConfig = inputText.Substring(optionStart, endOfTag - optionStart).Trim();
|
||||
}
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(defaultConfig))
|
||||
{
|
||||
builder.Configure(defaultConfig);
|
||||
}
|
||||
return builder.Build();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -28,10 +28,10 @@ namespace Markdig.Extensions.SmartyPants
|
||||
|
||||
public void Setup(MarkdownPipelineBuilder pipeline)
|
||||
{
|
||||
if (!pipeline.InlineParsers.Contains<SmaryPantsInlineParser>())
|
||||
if (!pipeline.InlineParsers.Contains<SmartyPantsInlineParser>())
|
||||
{
|
||||
// Insert the parser after the code span parser
|
||||
pipeline.InlineParsers.InsertAfter<CodeInlineParser>(new SmaryPantsInlineParser());
|
||||
pipeline.InlineParsers.InsertAfter<CodeInlineParser>(new SmartyPantsInlineParser());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
using System.Collections.Generic;
|
||||
using Markdig.Helpers;
|
||||
using Markdig.Parsers;
|
||||
using Markdig.Syntax;
|
||||
using Markdig.Syntax.Inlines;
|
||||
|
||||
namespace Markdig.Extensions.SmartyPants
|
||||
@@ -11,14 +12,14 @@ namespace Markdig.Extensions.SmartyPants
|
||||
/// <summary>
|
||||
/// The inline parser for SmartyPants.
|
||||
/// </summary>
|
||||
public class SmaryPantsInlineParser : InlineParser
|
||||
public class SmartyPantsInlineParser : InlineParser, IPostInlineProcessor
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="SmaryPantsInlineParser"/> class.
|
||||
/// Initializes a new instance of the <see cref="SmartyPantsInlineParser"/> class.
|
||||
/// </summary>
|
||||
public SmaryPantsInlineParser()
|
||||
public SmartyPantsInlineParser()
|
||||
{
|
||||
OpeningCharacters = new[] {'`', '\'', '"', '<', '>', '.', '-'};
|
||||
OpeningCharacters = new[] {'\'', '"', '<', '>', '.', '-'};
|
||||
}
|
||||
|
||||
public override bool Match(InlineProcessor processor, ref StringSlice slice)
|
||||
@@ -30,6 +31,8 @@ namespace Markdig.Extensions.SmartyPants
|
||||
// " “ ” “ ” 'left-double-quote', 'right-double-quote'
|
||||
// << >> « » « » 'left-angle-quote', 'right-angle-quote'
|
||||
// ... … … 'ellipsis'
|
||||
|
||||
// Special case: – and — are handle as a PostProcess step to avoid conflicts with pipetables header separator row
|
||||
// -- – – 'ndash'
|
||||
// --- — — 'mdash'
|
||||
|
||||
@@ -44,13 +47,6 @@ namespace Markdig.Extensions.SmartyPants
|
||||
|
||||
switch (c)
|
||||
{
|
||||
case '`':
|
||||
if (slice.PeekChar(1) == '`')
|
||||
{
|
||||
slice.NextChar();
|
||||
type = SmartyPantType.DoubleQuote; // We will resolve them at the end of parsing all inlines
|
||||
}
|
||||
break;
|
||||
case '\'':
|
||||
type = SmartyPantType.Quote; // We will resolve them at the end of parsing all inlines
|
||||
if (slice.PeekChar(1) == '\'')
|
||||
@@ -83,12 +79,8 @@ namespace Markdig.Extensions.SmartyPants
|
||||
case '-':
|
||||
if (slice.NextChar() == '-')
|
||||
{
|
||||
type = SmartyPantType.Dash2;
|
||||
if (slice.PeekChar(1) == '-')
|
||||
{
|
||||
slice.NextChar();
|
||||
type = SmartyPantType.Dash3;
|
||||
}
|
||||
processor.ParserStates[Index] = string.Empty;
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -289,5 +281,85 @@ namespace Markdig.Extensions.SmartyPants
|
||||
|
||||
pants.Clear();
|
||||
}
|
||||
|
||||
bool IPostInlineProcessor.PostProcess(InlineProcessor state, Inline root, Inline lastChild, int postInlineProcessorIndex,
|
||||
bool isFinalProcessing)
|
||||
{
|
||||
// Don't try to process anything if there are no dash
|
||||
if (state.ParserStates[Index] == null)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
var child = root;
|
||||
var pendingContainers = new Stack<Inline>();
|
||||
|
||||
while (true)
|
||||
{
|
||||
while (child != null)
|
||||
{
|
||||
var next = child.NextSibling;
|
||||
|
||||
if (child is LiteralInline)
|
||||
{
|
||||
var literal = (LiteralInline) child;
|
||||
|
||||
var startIndex = 0;
|
||||
|
||||
var indexOfDash = literal.Content.IndexOf("--", startIndex);
|
||||
if (indexOfDash >= 0)
|
||||
{
|
||||
var type = SmartyPantType.Dash2;
|
||||
if (literal.Content.PeekCharAbsolute(indexOfDash + 2) == '-')
|
||||
{
|
||||
type = SmartyPantType.Dash3;
|
||||
}
|
||||
var nextContent = literal.Content;
|
||||
var originalSpan = literal.Span;
|
||||
literal.Span.End -= literal.Content.End - indexOfDash + 1;
|
||||
literal.Content.End = indexOfDash - 1;
|
||||
nextContent.Start = indexOfDash + (type == SmartyPantType.Dash2 ? 2 : 3);
|
||||
|
||||
var pant = new SmartyPant()
|
||||
{
|
||||
Span = new SourceSpan(literal.Content.End + 1, nextContent.Start - 1),
|
||||
Line = literal.Line,
|
||||
Column = literal.Column,
|
||||
OpeningCharacter = '-',
|
||||
Type = type
|
||||
};
|
||||
literal.InsertAfter(pant);
|
||||
|
||||
var postLiteral = new LiteralInline()
|
||||
{
|
||||
Span = new SourceSpan(pant.Span.End + 1, originalSpan.End),
|
||||
Line = literal.Line,
|
||||
Column = literal.Column,
|
||||
Content = nextContent
|
||||
};
|
||||
pant.InsertAfter(postLiteral);
|
||||
|
||||
// Use the pending literal to proceed further
|
||||
next = postLiteral;
|
||||
}
|
||||
}
|
||||
else if (child is ContainerInline)
|
||||
{
|
||||
pendingContainers.Push(((ContainerInline)child).FirstChild);
|
||||
}
|
||||
|
||||
child = next;
|
||||
}
|
||||
if (pendingContainers.Count > 0)
|
||||
{
|
||||
child = pendingContainers.Pop();
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -15,8 +15,8 @@ namespace Markdig.Extensions.Tables
|
||||
/// The inline parser used to transform a <see cref="ParagraphBlock"/> into a <see cref="Table"/> at inline parsing time.
|
||||
/// </summary>
|
||||
/// <seealso cref="Markdig.Parsers.InlineParser" />
|
||||
/// <seealso cref="Markdig.Parsers.IDelimiterProcessor" />
|
||||
public class PipeTableParser : InlineParser, IDelimiterProcessor
|
||||
/// <seealso cref="IPostInlineProcessor" />
|
||||
public class PipeTableParser : InlineParser, IPostInlineProcessor
|
||||
{
|
||||
private LineBreakInlineParser lineBreakParser;
|
||||
|
||||
@@ -123,7 +123,7 @@ namespace Markdig.Extensions.Tables
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool ProcessDelimiters(InlineProcessor state, Inline root, Inline lastChild, int delimiterProcessorIndex, bool isFinalProcessing)
|
||||
public bool PostProcess(InlineProcessor state, Inline root, Inline lastChild, int postInlineProcessorIndex, bool isFinalProcessing)
|
||||
{
|
||||
var container = root as ContainerInline;
|
||||
var tableState = state.ParserStates[Index] as TableState;
|
||||
@@ -379,20 +379,12 @@ namespace Markdig.Extensions.Tables
|
||||
}
|
||||
|
||||
// Perform delimiter processor that are coming after this processor
|
||||
var delimiterProcessors = state.Parsers.DelimiterProcessors;
|
||||
for (int i = 0; i < delimiterProcessors.Length; i++)
|
||||
foreach (var cell in cells)
|
||||
{
|
||||
if (delimiterProcessors[i] == this)
|
||||
{
|
||||
foreach (var cell in cells)
|
||||
{
|
||||
var paragraph = (ParagraphBlock) cell[0];
|
||||
|
||||
state.ProcessDelimiters(i + 1, paragraph.Inline, null, true);
|
||||
}
|
||||
break;
|
||||
}
|
||||
var paragraph = (ParagraphBlock) cell[0];
|
||||
state.PostProcessInlines(postInlineProcessorIndex + 1, paragraph.Inline, null, true);
|
||||
}
|
||||
|
||||
// Clear cells when we are done
|
||||
cells.Clear();
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
// See the license.txt file in the project root for more information.
|
||||
using Markdig.Helpers;
|
||||
using Markdig.Parsers;
|
||||
using Markdig.Renderers.Html;
|
||||
using Markdig.Syntax;
|
||||
|
||||
namespace Markdig.Extensions.TaskLists
|
||||
@@ -18,15 +19,29 @@ namespace Markdig.Extensions.TaskLists
|
||||
public TaskListInlineParser()
|
||||
{
|
||||
OpeningCharacters = new[] {'['};
|
||||
ListClass = "contains-task-list";
|
||||
ListItemClass = "task-list-item";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the list class used for a task list.
|
||||
/// </summary>
|
||||
public string ListClass { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the list item class used for a task list.
|
||||
/// </summary>
|
||||
public string ListItemClass { get; set; }
|
||||
|
||||
public override bool Match(InlineProcessor processor, ref StringSlice slice)
|
||||
{
|
||||
// A tasklist is either
|
||||
// [ ]
|
||||
// or [x] or [X]
|
||||
|
||||
if (!(processor.Block.Parent is ListItemBlock))
|
||||
var listItemBlock = processor.Block.Parent as ListItemBlock;
|
||||
|
||||
if (listItemBlock == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@@ -56,6 +71,19 @@ namespace Markdig.Extensions.TaskLists
|
||||
};
|
||||
taskItem.Span.End = taskItem.Span.Start + 2;
|
||||
processor.Inline = taskItem;
|
||||
|
||||
// Add proper class for task list
|
||||
if (!string.IsNullOrEmpty(ListItemClass))
|
||||
{
|
||||
listItemBlock.GetAttributes().AddClass(ListItemClass);
|
||||
}
|
||||
|
||||
var listBlock = (ListBlock) listItemBlock.Parent;
|
||||
if (!string.IsNullOrEmpty(ListClass))
|
||||
{
|
||||
listBlock.GetAttributes().AddClass(ListClass);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -101,19 +101,5 @@ namespace Markdig.Helpers
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool ReplacyBy<TElement>(T element) where TElement : T
|
||||
{
|
||||
if (element == null) throw new ArgumentNullException(nameof(element));
|
||||
for (int i = 0; i < Count; i++)
|
||||
{
|
||||
if (this[i] is TElement)
|
||||
{
|
||||
this[i] = element;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -194,52 +194,49 @@ namespace Markdig.Helpers
|
||||
/// Searches the specified text within this slice.
|
||||
/// </summary>
|
||||
/// <param name="text">The text.</param>
|
||||
/// <param name="offset">The offset.</param>
|
||||
/// <param name="ignoreCase">true if ignore case</param>
|
||||
/// <returns><c>true</c> if the text was found; <c>false</c> otherwise</returns>
|
||||
public bool Search(string text, out int index)
|
||||
public int IndexOf(string text, int offset = 0, bool ignoreCase = false)
|
||||
{
|
||||
return Search(text, 0, out index);
|
||||
var end = End - text.Length + 1;
|
||||
if (ignoreCase)
|
||||
{
|
||||
for (int i = Start + offset; i <= end; i++)
|
||||
{
|
||||
if (MatchLowercase(text, End, i - Start))
|
||||
{
|
||||
return i; ;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = Start + offset; i <= end; i++)
|
||||
{
|
||||
if (Match(text, End, i - Start))
|
||||
{
|
||||
return i; ;
|
||||
}
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Searches the specified text within this slice.
|
||||
/// Searches for the specified character within this slice.
|
||||
/// </summary>
|
||||
/// <param name="text">The text.</param>
|
||||
/// <param name="offset">The offset.</param>
|
||||
/// <returns><c>true</c> if the text was found; <c>false</c> otherwise</returns>
|
||||
public bool Search(string text, int offset, out int index)
|
||||
/// <returns>A value >= 0 if the character was found, otherwise < 0</returns>
|
||||
public int IndexOf(char c)
|
||||
{
|
||||
var end = End - text.Length + 1;
|
||||
index = Start + offset;
|
||||
for (int i = index; i <= end; i ++)
|
||||
for (int i = Start; i <= End; i++)
|
||||
{
|
||||
if (Match(text, End, i))
|
||||
if (Text[i] == c)
|
||||
{
|
||||
index = i + text.Length;
|
||||
return true;
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Searches the specified text within this slice (matching lowercase).
|
||||
/// </summary>
|
||||
/// <param name="text">The text.</param>
|
||||
/// <param name="offset">The offset.</param>
|
||||
/// <returns><c>true</c> if the text was found; <c>false</c> otherwise</returns>
|
||||
public bool SearchLowercase(string text, out int endOfIndex)
|
||||
{
|
||||
var end = End - text.Length + 1;
|
||||
endOfIndex = 0;
|
||||
for (int i = Start; i <= end; i++)
|
||||
{
|
||||
if (MatchLowercase(text, End, i))
|
||||
{
|
||||
endOfIndex = i + text.Length;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return -1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
// See the license.txt file in the project root for more information.
|
||||
using System;
|
||||
using System.IO;
|
||||
using Markdig.Extensions.SelfPipeline;
|
||||
using Markdig.Parsers;
|
||||
using Markdig.Renderers;
|
||||
using Markdig.Syntax;
|
||||
@@ -30,17 +31,19 @@ namespace Markdig
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a Markdown string to HTML.
|
||||
/// Converts a Markdown string to HTML and output to the specified writer.
|
||||
/// </summary>
|
||||
/// <param name="markdown">A Markdown text.</param>
|
||||
/// <param name="writer">The destination <see cref="TextWriter"/> that will receive the result of the conversion.</param>
|
||||
/// <param name="pipeline">The pipeline used for the conversion.</param>
|
||||
/// <returns>The Markdown document that has been parsed</returns>
|
||||
/// <exception cref="System.ArgumentNullException">if reader or writer variable are null</exception>
|
||||
public static void ToHtml(string markdown, TextWriter writer, MarkdownPipeline pipeline = null)
|
||||
public static MarkdownDocument ToHtml(string markdown, TextWriter writer, MarkdownPipeline pipeline = null)
|
||||
{
|
||||
if (markdown == null) throw new ArgumentNullException(nameof(markdown));
|
||||
if (writer == null) throw new ArgumentNullException(nameof(writer));
|
||||
pipeline = pipeline ?? new MarkdownPipelineBuilder().Build();
|
||||
pipeline = CheckForSelfPipeline(pipeline, markdown);
|
||||
|
||||
// We override the renderer with our own writer
|
||||
var renderer = new HtmlRenderer(writer);
|
||||
@@ -49,6 +52,8 @@ namespace Markdig
|
||||
var document = Parse(markdown, pipeline);
|
||||
renderer.Render(document);
|
||||
writer.Flush();
|
||||
|
||||
return document;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -64,6 +69,7 @@ namespace Markdig
|
||||
if (renderer == null) throw new ArgumentNullException(nameof(renderer));
|
||||
pipeline = pipeline ?? new MarkdownPipelineBuilder().Build();
|
||||
|
||||
pipeline = CheckForSelfPipeline(pipeline, markdown);
|
||||
var document = Parse(markdown, pipeline);
|
||||
pipeline.Setup(renderer);
|
||||
return renderer.Render(document);
|
||||
@@ -93,7 +99,18 @@ namespace Markdig
|
||||
if (markdown == null) throw new ArgumentNullException(nameof(markdown));
|
||||
pipeline = pipeline ?? new MarkdownPipelineBuilder().Build();
|
||||
|
||||
pipeline = CheckForSelfPipeline(pipeline, markdown);
|
||||
return MarkdownParser.Parse(markdown, pipeline);
|
||||
}
|
||||
|
||||
private static MarkdownPipeline CheckForSelfPipeline(MarkdownPipeline pipeline, string markdown)
|
||||
{
|
||||
var selfPipeline = pipeline.Extensions.Find<SelfPipelineExtension>();
|
||||
if (selfPipeline != null)
|
||||
{
|
||||
return selfPipeline.CreatePipelineFromInput(markdown);
|
||||
}
|
||||
return pipeline;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,8 @@ using Markdig.Extensions.Hardlines;
|
||||
using Markdig.Extensions.ListExtras;
|
||||
using Markdig.Extensions.Mathematics;
|
||||
using Markdig.Extensions.MediaLinks;
|
||||
using Markdig.Extensions.PragmaLines;
|
||||
using Markdig.Extensions.SelfPipeline;
|
||||
using Markdig.Extensions.SmartyPants;
|
||||
using Markdig.Extensions.Tables;
|
||||
using Markdig.Extensions.TaskLists;
|
||||
@@ -58,6 +60,35 @@ namespace Markdig
|
||||
.UseGenericAttributes(); // Must be last as it is one parser that is modifying other parsers
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Uses the self pipeline extension that will detect the pipeline to use from the markdown input that contains a special tag. See <see cref="SelfPipelineExtension"/>
|
||||
/// </summary>
|
||||
/// <param name="pipeline">The pipeline.</param>
|
||||
/// <param name="defaultTag">The default tag to use to match the self pipeline configuration. By default, <see cref="SelfPipelineExtension.DefaultTag"/>, meaning that the HTML tag will be <--markdig:extensions--></param>
|
||||
/// <param name="defaultExtensions">The default extensions to configure if no pipeline setup was found from the Markdown document</param>
|
||||
/// <returns>The modified pipeline</returns>
|
||||
public static MarkdownPipelineBuilder UseSelfPipeline(this MarkdownPipelineBuilder pipeline, string defaultTag = SelfPipelineExtension.DefaultTag, string defaultExtensions = null)
|
||||
{
|
||||
if (pipeline.Extensions.Count != 0)
|
||||
{
|
||||
throw new InvalidOperationException("The SelfPipeline extension cannot be used with other extensions");
|
||||
}
|
||||
|
||||
pipeline.Extensions.Add(new SelfPipelineExtension(defaultTag, defaultExtensions));
|
||||
return pipeline;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Uses pragma lines to output span with an id containing the line number (pragma-line#line_number_zero_based`)
|
||||
/// </summary>
|
||||
/// <param name="pipeline">The pipeline.</param>
|
||||
/// <returns>The modified pipeline</returns>
|
||||
public static MarkdownPipelineBuilder UsePragmaLines(this MarkdownPipelineBuilder pipeline)
|
||||
{
|
||||
pipeline.Extensions.AddIfNotAlready<PragmaLineExtension>();
|
||||
return pipeline;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Uses precise source code location (useful for syntax highlighting).
|
||||
/// </summary>
|
||||
@@ -143,7 +174,7 @@ namespace Markdig
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Uses the boostrap extension.
|
||||
/// Uses the bootstrap extension.
|
||||
/// </summary>
|
||||
/// <param name="pipeline">The pipeline.</param>
|
||||
/// <returns>The modified pipeline</returns>
|
||||
@@ -360,6 +391,8 @@ namespace Markdig
|
||||
{
|
||||
switch (extension.ToLowerInvariant())
|
||||
{
|
||||
case "common":
|
||||
break;
|
||||
case "advanced":
|
||||
pipeline.UseAdvancedExtensions();
|
||||
break;
|
||||
@@ -424,10 +457,10 @@ namespace Markdig
|
||||
pipeline.UseTaskLists();
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentException($"unknown extension {extension}");
|
||||
throw new ArgumentException($"Invalid extension `{extension}` from `{extensions}`", nameof(extensions));
|
||||
}
|
||||
}
|
||||
return pipeline;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,7 +47,11 @@ namespace Markdig
|
||||
|
||||
internal ProcessDocumentDelegate DocumentProcessed;
|
||||
|
||||
internal void Setup(IMarkdownRenderer renderer)
|
||||
/// <summary>
|
||||
/// Allows to setup a <see cref="IMarkdownRenderer"/>.
|
||||
/// </summary>
|
||||
/// <param name="renderer">The markdown renderer to setup</param>
|
||||
public void Setup(IMarkdownRenderer renderer)
|
||||
{
|
||||
if (renderer == null) throw new ArgumentNullException(nameof(renderer));
|
||||
foreach (var extension in Extensions)
|
||||
|
||||
@@ -216,7 +216,7 @@ namespace Markdig.Parsers
|
||||
// The line must contain only fence opening character followed only by whitespaces.
|
||||
if (count <=0 && !processor.IsCodeIndent && (c == '\0' || c.IsWhitespace()) && line.TrimEnd())
|
||||
{
|
||||
block.Span.End = line.Start - 1;
|
||||
block.UpdateSpanEnd(line.Start - 1);
|
||||
|
||||
// Don't keep the last line
|
||||
return BlockState.BreakDiscard;
|
||||
|
||||
@@ -171,51 +171,78 @@ namespace Markdig.Parsers
|
||||
return CreateHtmlBlock(state, HtmlBlockType.InterruptingBlock, startColumn, startPosition);
|
||||
}
|
||||
|
||||
private const string EndOfComment = "-->";
|
||||
private const string EndOfCDATA = "]]>";
|
||||
private const string EndOfProcessingInstruction = "?>";
|
||||
|
||||
|
||||
private BlockState MatchEnd(BlockProcessor state, HtmlBlock htmlBlock)
|
||||
{
|
||||
state.GoToColumn(state.ColumnBeforeIndent);
|
||||
|
||||
// Early exit if it is not starting by an HTML tag
|
||||
var line = state.Line;
|
||||
var c = line.CurrentChar;
|
||||
var result = BlockState.Continue;
|
||||
int endof;
|
||||
int index;
|
||||
switch (htmlBlock.Type)
|
||||
{
|
||||
case HtmlBlockType.Comment:
|
||||
if (line.Search("-->", out endof))
|
||||
index = line.IndexOf(EndOfComment);
|
||||
if (index >= 0)
|
||||
{
|
||||
htmlBlock.Span.End = endof - 1;
|
||||
htmlBlock.UpdateSpanEnd(index + EndOfComment.Length);
|
||||
result = BlockState.Break;
|
||||
}
|
||||
break;
|
||||
case HtmlBlockType.CData:
|
||||
if (line.Search("]]>", out endof))
|
||||
index = line.IndexOf(EndOfCDATA);
|
||||
if (index >= 0)
|
||||
{
|
||||
htmlBlock.Span.End = endof - 1;
|
||||
htmlBlock.UpdateSpanEnd(index + EndOfCDATA.Length);
|
||||
result = BlockState.Break;
|
||||
}
|
||||
break;
|
||||
case HtmlBlockType.ProcessingInstruction:
|
||||
if (line.Search("?>", out endof))
|
||||
index = line.IndexOf(EndOfProcessingInstruction);
|
||||
if (index >= 0)
|
||||
{
|
||||
htmlBlock.Span.End = endof - 1;
|
||||
htmlBlock.UpdateSpanEnd(index + EndOfProcessingInstruction.Length);
|
||||
result = BlockState.Break;
|
||||
}
|
||||
break;
|
||||
case HtmlBlockType.DocumentType:
|
||||
if (line.Search(">", out endof))
|
||||
index = line.IndexOf('>');
|
||||
if (index >= 0)
|
||||
{
|
||||
htmlBlock.Span.End = endof - 1;
|
||||
htmlBlock.UpdateSpanEnd(index + 1);
|
||||
result = BlockState.Break;
|
||||
}
|
||||
break;
|
||||
case HtmlBlockType.ScriptPreOrStyle:
|
||||
if (line.SearchLowercase("</script>", out endof) || line.SearchLowercase("</pre>", out endof) || line.SearchLowercase("</style>", out endof))
|
||||
index = line.IndexOf("</script>", 0, true);
|
||||
if (index >= 0)
|
||||
{
|
||||
htmlBlock.Span.End = endof - 1;
|
||||
htmlBlock.UpdateSpanEnd(index + "</script>".Length);
|
||||
result = BlockState.Break;
|
||||
}
|
||||
else
|
||||
{
|
||||
index = line.IndexOf("</pre>", 0, true);
|
||||
if (index >= 0)
|
||||
{
|
||||
htmlBlock.UpdateSpanEnd(index + "</pre>".Length);
|
||||
result = BlockState.Break;
|
||||
}
|
||||
else
|
||||
{
|
||||
index = line.IndexOf("</style>", 0, true);
|
||||
if (index >= 0)
|
||||
{
|
||||
htmlBlock.UpdateSpanEnd(index + "</style>".Length);
|
||||
result = BlockState.Break;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case HtmlBlockType.InterruptingBlock:
|
||||
if (state.IsBlankLine)
|
||||
|
||||
@@ -6,9 +6,9 @@ using Markdig.Syntax.Inlines;
|
||||
namespace Markdig.Parsers
|
||||
{
|
||||
/// <summary>
|
||||
/// A procesor used for <see cref="DelimiterInline"/>.
|
||||
/// A procesor called at the end of processing all inlines.
|
||||
/// </summary>
|
||||
public interface IDelimiterProcessor
|
||||
public interface IPostInlineProcessor
|
||||
{
|
||||
/// <summary>
|
||||
/// Processes the delimiters.
|
||||
@@ -16,10 +16,10 @@ namespace Markdig.Parsers
|
||||
/// <param name="state">The parser state.</param>
|
||||
/// <param name="root">The root inline.</param>
|
||||
/// <param name="lastChild">The last child.</param>
|
||||
/// <param name="delimiterProcessorIndex">Index of this delimiter processor.</param>
|
||||
/// <param name="postInlineProcessorIndex">Index of this delimiter processor.</param>
|
||||
/// <param name="isFinalProcessing"></param>
|
||||
/// <returns><c>true</c> to continue to the next delimiter processor;
|
||||
/// <c>false</c> to stop the process (in case a processor is perfoming sub-sequent processor itself)</returns>
|
||||
bool ProcessDelimiters(InlineProcessor state, Inline root, Inline lastChild, int delimiterProcessorIndex, bool isFinalProcessing);
|
||||
bool PostProcess(InlineProcessor state, Inline root, Inline lastChild, int postInlineProcessorIndex, bool isFinalProcessing);
|
||||
}
|
||||
}
|
||||
@@ -48,7 +48,7 @@ namespace Markdig.Parsers
|
||||
}
|
||||
if (block != null)
|
||||
{
|
||||
block.Span.End = processor.Line.End;
|
||||
block.UpdateSpanEnd(processor.Line.End);
|
||||
}
|
||||
return BlockState.Continue;
|
||||
}
|
||||
|
||||
@@ -20,23 +20,23 @@ namespace Markdig.Parsers
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the registered delimiter processors.
|
||||
/// Gets the registered post inline processors.
|
||||
/// </summary>
|
||||
public IDelimiterProcessor[] DelimiterProcessors { get; private set; }
|
||||
public IPostInlineProcessor[] PostInlineProcessors { get; private set; }
|
||||
|
||||
public override void Initialize(InlineProcessor initState)
|
||||
{
|
||||
// Prepare the list of delimiter processors
|
||||
var delimiterProcessors = new List<IDelimiterProcessor>();
|
||||
// Prepare the list of post inline processors
|
||||
var postInlineProcessors = new List<IPostInlineProcessor>();
|
||||
foreach (var parser in this)
|
||||
{
|
||||
var delimProcessor = parser as IDelimiterProcessor;
|
||||
var delimProcessor = parser as IPostInlineProcessor;
|
||||
if (delimProcessor != null)
|
||||
{
|
||||
delimiterProcessors.Add(delimProcessor);
|
||||
postInlineProcessors.Add(delimProcessor);
|
||||
}
|
||||
}
|
||||
DelimiterProcessors = delimiterProcessors.ToArray();
|
||||
PostInlineProcessors = postInlineProcessors.ToArray();
|
||||
|
||||
base.Initialize(initState);
|
||||
}
|
||||
|
||||
@@ -268,8 +268,8 @@ namespace Markdig.Parsers
|
||||
leafBlock.Inline.DumpTo(DebugLog);
|
||||
}
|
||||
|
||||
// Process all delimiters
|
||||
ProcessDelimiters(0, Root, null, true);
|
||||
// PostProcess all inlines
|
||||
PostProcessInlines(0, Root, null, true);
|
||||
|
||||
//TransformDelimitersToLiterals();
|
||||
|
||||
@@ -281,12 +281,12 @@ namespace Markdig.Parsers
|
||||
}
|
||||
}
|
||||
|
||||
public void ProcessDelimiters(int startingIndex, Inline root, Inline lastChild, bool isFinalProcessing)
|
||||
public void PostProcessInlines(int startingIndex, Inline root, Inline lastChild, bool isFinalProcessing)
|
||||
{
|
||||
for (int i = startingIndex; i < Parsers.DelimiterProcessors.Length; i++)
|
||||
for (int i = startingIndex; i < Parsers.PostInlineProcessors.Length; i++)
|
||||
{
|
||||
var delimiterProcessor = Parsers.DelimiterProcessors[i];
|
||||
if (!delimiterProcessor.ProcessDelimiters(this, root, lastChild, i, isFinalProcessing))
|
||||
var postInlineProcessor = Parsers.PostInlineProcessors[i];
|
||||
if (!postInlineProcessor.PostProcess(this, root, lastChild, i, isFinalProcessing))
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -15,8 +15,8 @@ namespace Markdig.Parsers.Inlines
|
||||
/// An inline parser for <see cref="EmphasisInline"/>.
|
||||
/// </summary>
|
||||
/// <seealso cref="Markdig.Parsers.InlineParser" />
|
||||
/// <seealso cref="Markdig.Parsers.IDelimiterProcessor" />
|
||||
public class EmphasisInlineParser : InlineParser, IDelimiterProcessor
|
||||
/// <seealso cref="IPostInlineProcessor" />
|
||||
public class EmphasisInlineParser : InlineParser, IPostInlineProcessor
|
||||
{
|
||||
private CharacterMap<EmphasisDescriptor> emphasisMap;
|
||||
private readonly DelimitersObjectCache inlinesCache;
|
||||
@@ -85,7 +85,7 @@ namespace Markdig.Parsers.Inlines
|
||||
emphasisMap = new CharacterMap<EmphasisDescriptor>(tempMap);
|
||||
}
|
||||
|
||||
public bool ProcessDelimiters(InlineProcessor state, Inline root, Inline lastChild, int delimiterProcessorIndex, bool isFinalProcessing)
|
||||
public bool PostProcess(InlineProcessor state, Inline root, Inline lastChild, int postInlineProcessorIndex, bool isFinalProcessing)
|
||||
{
|
||||
var container = root as ContainerInline;
|
||||
if (container == null)
|
||||
|
||||
@@ -159,7 +159,7 @@ namespace Markdig.Parsers.Inlines
|
||||
link.IsClosed = true;
|
||||
|
||||
// Process emphasis delimiters
|
||||
state.ProcessDelimiters(0, link, null, false);
|
||||
state.PostProcessInlines(0, link, null, false);
|
||||
|
||||
state.Inline = link;
|
||||
isValidLink = true;
|
||||
@@ -238,7 +238,7 @@ namespace Markdig.Parsers.Inlines
|
||||
inlineState.Inline = link;
|
||||
|
||||
// Process emphasis delimiters
|
||||
inlineState.ProcessDelimiters(0, link, null, false);
|
||||
inlineState.PostProcessInlines(0, link, null, false);
|
||||
|
||||
// If we have a link (and not an image),
|
||||
// we also set all [ delimiters before the opening delimiter to inactive.
|
||||
|
||||
@@ -65,13 +65,24 @@ namespace Markdig.Parsers.Inlines
|
||||
|
||||
// The LiteralInlineParser is always matching (at least an empty string)
|
||||
var endPosition = slice.Start + length - 1;
|
||||
processor.Inline = new LiteralInline()
|
||||
|
||||
var previousInline = processor.Inline as LiteralInline;
|
||||
if (previousInline != null && ReferenceEquals(previousInline.Content.Text, slice.Text) &&
|
||||
previousInline.Content.End + 1 == slice.Start)
|
||||
{
|
||||
Content = length > 0 ? new StringSlice(slice.Text, slice.Start, endPosition) : StringSlice.Empty,
|
||||
Span = new SourceSpan(startPosition, processor.GetSourcePosition(endPosition)),
|
||||
Line = line,
|
||||
Column = column,
|
||||
};
|
||||
previousInline.Content.End = endPosition;
|
||||
previousInline.Span.End = processor.GetSourcePosition(endPosition);
|
||||
}
|
||||
else
|
||||
{
|
||||
processor.Inline = new LiteralInline()
|
||||
{
|
||||
Content = length > 0 ? new StringSlice(slice.Text, slice.Start, endPosition) : StringSlice.Empty,
|
||||
Span = new SourceSpan(startPosition, processor.GetSourcePosition(endPosition)),
|
||||
Line = line,
|
||||
Column = column,
|
||||
};
|
||||
}
|
||||
|
||||
slice.Start = nextStart;
|
||||
|
||||
|
||||
@@ -153,7 +153,7 @@ namespace Markdig.Parsers
|
||||
}
|
||||
|
||||
// Update list-item source end position
|
||||
listItem.Span.End = state.Line.End;
|
||||
listItem.UpdateSpanEnd(state.Line.End);
|
||||
|
||||
return BlockState.Continue;
|
||||
}
|
||||
@@ -170,11 +170,11 @@ namespace Markdig.Parsers
|
||||
{
|
||||
if (state.Indent > columWidth && state.IsCodeIndent)
|
||||
{
|
||||
state.GoToColumn(columWidth);
|
||||
state.GoToColumn(state.ColumnBeforeIndent + columWidth);
|
||||
}
|
||||
|
||||
// Update list-item source end position
|
||||
listItem.Span.End = state.Line.End;
|
||||
listItem.UpdateSpanEnd(state.Line.End);
|
||||
|
||||
return BlockState.Continue;
|
||||
}
|
||||
@@ -352,11 +352,11 @@ namespace Markdig.Parsers
|
||||
isLastListItem = false;
|
||||
}
|
||||
|
||||
// Update end-position for the list
|
||||
if (listBlock.Count > 0)
|
||||
{
|
||||
listBlock.Span.End = listBlock[listBlock.Count - 1].Span.End;
|
||||
}
|
||||
//// Update end-position for the list
|
||||
//if (listBlock.Count > 0)
|
||||
//{
|
||||
// listBlock.Span.End = listBlock[listBlock.Count - 1].Span.End;
|
||||
//}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ namespace Markdig.Parsers
|
||||
return TryParseSetexHeading(processor, block);
|
||||
}
|
||||
|
||||
block.Span.End = processor.Line.End;
|
||||
block.UpdateSpanEnd(processor.Line.End);
|
||||
return BlockState.Continue;
|
||||
}
|
||||
|
||||
@@ -139,7 +139,7 @@ namespace Markdig.Parsers
|
||||
return BlockState.BreakDiscard;
|
||||
}
|
||||
|
||||
block.Span.End = state.Line.End;
|
||||
block.UpdateSpanEnd(state.Line.End);
|
||||
|
||||
return BlockState.Continue;
|
||||
}
|
||||
|
||||
@@ -61,7 +61,6 @@ namespace Markdig.Parsers
|
||||
var c = processor.CurrentChar;
|
||||
if (c != quote.QuoteChar)
|
||||
{
|
||||
block.Span.End = processor.Start - 1;
|
||||
return processor.IsBlankLine ? BlockState.BreakDiscard : BlockState.None;
|
||||
}
|
||||
|
||||
@@ -71,18 +70,8 @@ namespace Markdig.Parsers
|
||||
processor.NextChar(); // Skip following space
|
||||
}
|
||||
|
||||
block.Span.End = processor.Line.End;
|
||||
block.UpdateSpanEnd(processor.Line.End);
|
||||
return BlockState.Continue;
|
||||
}
|
||||
|
||||
public override bool Close(BlockProcessor processor, Block block)
|
||||
{
|
||||
var quoteBlock = block as QuoteBlock;
|
||||
if (quoteBlock?.LastChild != null)
|
||||
{
|
||||
quoteBlock.Span.End = quoteBlock.LastChild.Span.End;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,6 @@ namespace Markdig
|
||||
{
|
||||
public static partial class Markdown
|
||||
{
|
||||
public const string Version = "0.5.3";
|
||||
public const string Version = "0.6.0";
|
||||
}
|
||||
}
|
||||
@@ -44,9 +44,14 @@ namespace Markdig.Renderers.Html
|
||||
if (name == null) throw new ArgumentNullException(nameof(name));
|
||||
if (Classes == null)
|
||||
{
|
||||
Classes = new List<string>(2); // Use half list compare to default capacity (4), as we don't expect lots of classes
|
||||
Classes = new List<string>(2);
|
||||
// Use half list compare to default capacity (4), as we don't expect lots of classes
|
||||
}
|
||||
|
||||
if (!Classes.Contains(name))
|
||||
{
|
||||
Classes.Add(name);
|
||||
}
|
||||
Classes.Add(name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -113,7 +118,7 @@ namespace Markdig.Renderers.Html
|
||||
}
|
||||
if (htmlAttributes.Classes == null)
|
||||
{
|
||||
htmlAttributes.Classes = shared ? Classes : new List<string>(Classes);
|
||||
htmlAttributes.Classes = shared ? Classes : Classes != null ? new List<string>(Classes) : null;
|
||||
}
|
||||
else if (Classes != null)
|
||||
{
|
||||
@@ -122,7 +127,7 @@ namespace Markdig.Renderers.Html
|
||||
|
||||
if (htmlAttributes.Properties == null)
|
||||
{
|
||||
htmlAttributes.Properties = shared ? Properties : new List<KeyValuePair<string, string>>(Properties);
|
||||
htmlAttributes.Properties = shared ? Properties : Properties != null ? new List<KeyValuePair<string, string>>(Properties) : null;
|
||||
}
|
||||
else if (Properties != null)
|
||||
{
|
||||
|
||||
@@ -32,7 +32,9 @@ namespace Markdig.Renderers.Html
|
||||
}
|
||||
else
|
||||
{
|
||||
renderer.WriteLine("<ul>");
|
||||
renderer.Write("<ul");
|
||||
renderer.WriteAttributes(listBlock);
|
||||
renderer.WriteLine(">");
|
||||
}
|
||||
foreach (var item in listBlock)
|
||||
{
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
// Copyright (c) Alexandre Mutel. All rights reserved.
|
||||
// This file is licensed under the BSD-Clause 2 license.
|
||||
// See the license.txt file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using Markdig.Syntax;
|
||||
using Markdig.Syntax.Inlines;
|
||||
|
||||
@@ -11,6 +13,16 @@ namespace Markdig.Renderers
|
||||
/// </summary>
|
||||
public interface IMarkdownRenderer
|
||||
{
|
||||
/// <summary>
|
||||
/// Occurs when before writing an object.
|
||||
/// </summary>
|
||||
event Action<IMarkdownRenderer, MarkdownObject> ObjectWriteBefore;
|
||||
|
||||
/// <summary>
|
||||
/// Occurs when after writing an object.
|
||||
/// </summary>
|
||||
event Action<IMarkdownRenderer, MarkdownObject> ObjectWriteAfter;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the object renderers that will render <see cref="Block"/> and <see cref="Inline"/> elements.
|
||||
/// </summary>
|
||||
|
||||
@@ -35,6 +35,16 @@ namespace Markdig.Renderers
|
||||
|
||||
public bool IsLastInContainer { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Occurs when before writing an object.
|
||||
/// </summary>
|
||||
public event Action<IMarkdownRenderer, MarkdownObject> ObjectWriteBefore;
|
||||
|
||||
/// <summary>
|
||||
/// Occurs when after writing an object.
|
||||
/// </summary>
|
||||
public event Action<IMarkdownRenderer, MarkdownObject> ObjectWriteAfter;
|
||||
|
||||
/// <summary>
|
||||
/// Writes the children of the specified <see cref="ContainerBlock"/>.
|
||||
/// </summary>
|
||||
@@ -105,6 +115,10 @@ namespace Markdig.Renderers
|
||||
|
||||
var objectType = obj.GetType();
|
||||
|
||||
// Calls before writing an object
|
||||
var writeBefore = ObjectWriteBefore;
|
||||
writeBefore?.Invoke(this, obj);
|
||||
|
||||
// Handle regular renderers
|
||||
IMarkdownObjectRenderer renderer = previousObjectType == objectType ? previousRenderer : null;
|
||||
if (renderer == null && !renderersPerType.TryGetValue(objectType, out renderer))
|
||||
@@ -142,6 +156,10 @@ namespace Markdig.Renderers
|
||||
|
||||
previousObjectType = objectType;
|
||||
previousRenderer = renderer;
|
||||
|
||||
// Calls after writing an object
|
||||
var writeAfter = ObjectWriteAfter;
|
||||
writeAfter?.Invoke(this, obj);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -75,5 +75,19 @@ namespace Markdig.Syntax
|
||||
{
|
||||
ProcessInlinesEnd?.Invoke(state, null);
|
||||
}
|
||||
|
||||
public void UpdateSpanEnd(int spanEnd)
|
||||
{
|
||||
// Update parent spans
|
||||
var parent = this;
|
||||
while (parent != null)
|
||||
{
|
||||
if (spanEnd > parent.Span.End)
|
||||
{
|
||||
parent.Span.End = spanEnd;
|
||||
}
|
||||
parent = parent.Parent;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
138
src/Markdig/Syntax/BlockExtensions.cs
Normal file
138
src/Markdig/Syntax/BlockExtensions.cs
Normal file
@@ -0,0 +1,138 @@
|
||||
// Copyright (c) Alexandre Mutel. All rights reserved.
|
||||
// This file is licensed under the BSD-Clause 2 license.
|
||||
// See the license.txt file in the project root for more information.
|
||||
namespace Markdig.Syntax
|
||||
{
|
||||
/// <summary>
|
||||
/// Extensions for <see cref="Block"/>
|
||||
/// </summary>
|
||||
public static class BlockExtensions
|
||||
{
|
||||
// TODO: Add test for this code
|
||||
|
||||
public static Block FindBlockAtPosition(this Block rootBlock, int position)
|
||||
{
|
||||
var contains = rootBlock.CompareToPosition(position) == 0;
|
||||
var blocks = rootBlock as ContainerBlock;
|
||||
if (blocks == null || blocks.Count == 0 || !contains)
|
||||
{
|
||||
return contains ? rootBlock : null;
|
||||
}
|
||||
|
||||
var lowerIndex = 0;
|
||||
var upperIndex = blocks.Count - 1;
|
||||
|
||||
// binary search on lines
|
||||
Block block = null;
|
||||
while (lowerIndex <= upperIndex)
|
||||
{
|
||||
int midIndex = (upperIndex - lowerIndex) / 2 + lowerIndex;
|
||||
block = blocks[midIndex];
|
||||
int comparison = block.CompareToPosition(position);
|
||||
if (comparison == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
block = null;
|
||||
if (comparison < 0)
|
||||
lowerIndex = midIndex + 1;
|
||||
else
|
||||
upperIndex = midIndex - 1;
|
||||
}
|
||||
|
||||
if (block == null)
|
||||
{
|
||||
return rootBlock;
|
||||
}
|
||||
|
||||
// Recursively go deep into the block
|
||||
return FindBlockAtPosition(block, position);
|
||||
}
|
||||
|
||||
|
||||
public static int FindClosestLine(this MarkdownDocument root, int line)
|
||||
{
|
||||
var closestBlock = root.FindClosestBlock(line);
|
||||
return closestBlock?.Line ?? 0;
|
||||
}
|
||||
|
||||
public static Block FindClosestBlock(this Block rootBlock, int line)
|
||||
{
|
||||
var blocks = rootBlock as ContainerBlock;
|
||||
if (blocks == null || blocks.Count == 0)
|
||||
{
|
||||
return rootBlock.Line == line ? rootBlock : null;
|
||||
}
|
||||
|
||||
var lowerIndex = 0;
|
||||
var upperIndex = blocks.Count - 1;
|
||||
|
||||
// binary search on lines
|
||||
while (lowerIndex <= upperIndex)
|
||||
{
|
||||
int midIndex = (upperIndex - lowerIndex) / 2 + lowerIndex;
|
||||
var block = blocks[midIndex];
|
||||
int comparison = block.Line.CompareTo(line);
|
||||
if (comparison == 0)
|
||||
{
|
||||
return block;
|
||||
}
|
||||
if (comparison < 0)
|
||||
lowerIndex = midIndex + 1;
|
||||
else
|
||||
upperIndex = midIndex - 1;
|
||||
}
|
||||
|
||||
// If we are between two lines, try to find the best spot
|
||||
if (lowerIndex > 0 && lowerIndex < blocks.Count)
|
||||
{
|
||||
var prevBlock = blocks[lowerIndex - 1].FindClosestBlock(line) ?? blocks[lowerIndex - 1];
|
||||
var nextBlock = blocks[lowerIndex].FindClosestBlock(line) ?? blocks[lowerIndex];
|
||||
|
||||
if (prevBlock.Line == line)
|
||||
{
|
||||
return prevBlock;
|
||||
}
|
||||
|
||||
if (nextBlock.Line == line)
|
||||
{
|
||||
return nextBlock;
|
||||
}
|
||||
|
||||
// we calculate the position of the current line relative to the line found and previous line
|
||||
var prevLine = prevBlock.Line;
|
||||
var nextLine = nextBlock.Line;
|
||||
|
||||
var middle = (line - prevLine) * 1.0 / (nextLine - prevLine);
|
||||
// If relative position < 0.5, we select the previous line, otherwise we select the line found
|
||||
return middle < 0.5 ? prevBlock : nextBlock;
|
||||
}
|
||||
|
||||
if (lowerIndex == 0)
|
||||
{
|
||||
var prevBlock = blocks[lowerIndex].FindClosestBlock(line) ?? blocks[lowerIndex];
|
||||
return prevBlock;
|
||||
}
|
||||
|
||||
if (lowerIndex == blocks.Count)
|
||||
{
|
||||
var prevBlock = blocks[lowerIndex - 1].FindClosestBlock(line) ?? blocks[lowerIndex - 1];
|
||||
return prevBlock;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public static bool ContainsPosition(this Block block, int position)
|
||||
{
|
||||
return CompareToPosition(block, position) == 0;
|
||||
}
|
||||
|
||||
public static int CompareToPosition(this Block block, int position)
|
||||
{
|
||||
return position < block.Span.Start ? 1 : position > block.Span.End + 1 ? -1 : 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -69,10 +69,7 @@ namespace Markdig.Syntax
|
||||
children[Count++] = item;
|
||||
item.Parent = this;
|
||||
|
||||
if (item.Span.End > Span.End)
|
||||
{
|
||||
Span.End = item.Span.End;
|
||||
}
|
||||
UpdateSpanEnd(item.Span.End);
|
||||
}
|
||||
|
||||
private void EnsureCapacity(int min)
|
||||
|
||||
@@ -19,6 +19,8 @@ namespace Markdig.Syntax
|
||||
/// <returns>An iteration over the descendant elements</returns>
|
||||
public static IEnumerable<MarkdownObject> Descendants(this MarkdownObject markdownObject)
|
||||
{
|
||||
// TODO: implement a recursiveless method
|
||||
|
||||
var block = markdownObject as ContainerBlock;
|
||||
if (block != null)
|
||||
{
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
{
|
||||
"title": "Markdig",
|
||||
"version": "0.5.3",
|
||||
"version": "0.6.0",
|
||||
"authors": [ "Alexandre Mutel" ],
|
||||
"description": "A fast, powerfull, CommonMark compliant, extensible Markdown processor for .NET",
|
||||
"description": "A fast, powerfull, CommonMark compliant, extensible Markdown processor for .NET with 20+ builtin extensions (pipetables, footnotes, definition lists... etc.)",
|
||||
"copyright": "Alexandre Mutel",
|
||||
"language": "en-US",
|
||||
"packOptions": {
|
||||
@@ -11,7 +11,7 @@
|
||||
"projectUrl": "https://github.com/lunet-io/markdig",
|
||||
"iconUrl": "https://raw.githubusercontent.com/lunet-io/markdig/master/img/markdig.png",
|
||||
"requireLicenseAcceptance": false,
|
||||
"releaseNotes": "> 0.5.3:\nFix bug in pipetables when a trailing `|` was put on the header row separator.\n> 0.5.2:\nAdd better support for precise source location for link parts (label, url...)\n> 0.5.1:\nAdd support for task lists.\nAdd support for precise source location pipelineBuilder.UsePreciseSourceLocation.\nBreaking change: Markdown.ToHtml() accept now only a string instead of a TextReader as this improve the overall performance.",
|
||||
"releaseNotes": "- Fix conflicts between PipeTables and SmartyPants extensions\n- Add SelfPipeline extension\n",
|
||||
"tags": [ "Markdown CommonMark md html md2html" ]
|
||||
},
|
||||
"configurations": {
|
||||
|
||||
Reference in New Issue
Block a user