Skip to content

Commit

Permalink
Make larsers.FencedDiv use the full markdown parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Witiko committed Nov 30, 2022
1 parent d57568b commit d247439
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 34 deletions.
51 changes: 37 additions & 14 deletions lunamark/reader/markdown.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1121,6 +1121,20 @@ function M.new(writer, options)
parsers.tilde_infostring)
end

if options.fenced_divs then
local function check_div_level(s, i, current_level) -- luacheck: ignore s i
current_level = tonumber(current_level)
return current_level > 0
end

local is_inside_div = Cmt(Cb("div_level"), check_div_level)

larsers.fencestart = larsers.fencestart
+ is_inside_div -- break out of a paragraph when we
-- are inside a div and see a closing tag
* larsers.fenced_div_end
end

larsers.Endline = parsers.newline * -( -- newline, but not before...
parsers.blankline -- paragraph break
+ parsers.tightblocksep -- nested list
Expand Down Expand Up @@ -1305,19 +1319,27 @@ function M.new(writer, options)
writer.string(infostring))
end

larsers.FencedDiv = P{ larsers.fenced_div_begin
* C((V(1)
+ parsers.newline
+ ( parsers.any
- ( larsers.fenced_div_begin
+ larsers.fenced_div_end))
* ( parsers.any
- parsers.newline
* ( larsers.fenced_div_begin
+ larsers.fenced_div_end))^0
* parsers.newline)^0)
* larsers.fenced_div_end }
/ function (attr, div) return parse_blocks(div .. "\n\n"), attr end
local function increment_div_level(increment)
local function update_div_level(s, i, current_level) -- luacheck: ignore s i
current_level = tonumber(current_level)
local next_level = tostring(current_level + increment)
return true, next_level
end

return Cg( Cmt(Cb("div_level"), update_div_level)
, "div_level")
end

larsers.FencedDiv = larsers.fenced_div_begin * increment_div_level(1)
* parsers.blanklines
* Ct( (V("Block") - larsers.fenced_div_end)^-1
* (parsers.blanklines / function()
return writer.interblocksep
end
* (V("Block") - larsers.fenced_div_end))^0)
* parsers.blanklines
* larsers.fenced_div_end * increment_div_level(-1)
/ function (attr, div) return div, attr end
/ writer.div

-- strip off leading > and indents, and run through blocks
Expand Down Expand Up @@ -1693,7 +1715,8 @@ function M.new(writer, options)
local syntax =
{ "Blocks",

Blocks = larsers.Blank^0 * parsers.Block^-1
Blocks = Cg(Ct("") / "0", "div_level") -- initialize div_level to 0
* larsers.Blank^0 * parsers.Block^-1
* (larsers.Blank^0 / function()
return writer.interblocksep
end
Expand Down
28 changes: 8 additions & 20 deletions tests/lunamark/ext_fenced_divs.test
Original file line number Diff line number Diff line change
Expand Up @@ -87,42 +87,31 @@ This is not a div
>>>
<p>::: This is not a div :::</p>

<div class="myclass" lang="fr">
<p>Some better div</p>
</div>
<div class="myclass" lang="fr">Some better div</div>

<div class="level1">
<p>Fenced divs can be nested</p>
<div class="level2-more-colons">
<p>This is a nested div</p>
</div>
<div class="level2-more-colons">This is a nested div</div>
<div class="level2-fewer-colons">
<p>This is also a nested div that contains</p>
<p>several paragraphs of text</p>
</div>
several paragraphs of text</div>
<div class="level2-same-number-of-colons">
<p>This is another nested div</p>
<div class="level3">
<p>with another nested div inside</p>
</div>
<div class="level3">with another nested div inside</div>
</div>
</div>

<div class="some-classname">
<div id="some-id">
<div lang="some">
<p>Divs can be nested directly inside one another without surrounding text.</p>
</div>
<div lang="some">Divs can be nested directly inside one another without surrounding text.</div>
</div>
</div>

<div class="level1">
<p>This is the beginning of a div</p>
<blockquote>
<p>This is a blockquote</p>
<div class="level2-inside-blockquote">
<p>This is a div inside a blockquote</p>
</div>
<div class="level2-inside-blockquote">This is a div inside a blockquote</div>
</blockquote>
</div>

Expand All @@ -131,8 +120,7 @@ This is not a div
<blockquote>
<p>This is a blockquote that contains three colons: :::</p>
</blockquote>
<p>This is the end of a div</p>
</div>
This is the end of a div</div>

<p>Here are some extra colons that will be shown as just text:</p>
<p>::::</p>
Expand All @@ -145,7 +133,7 @@ This is not a div
</code></pre>
</div>

<p>::: {.some-classname} This is not a div</p>
::: {.some-classname} This is not a div
<pre><code>:::
</code></pre>

Expand Down

0 comments on commit d247439

Please sign in to comment.