diff options
| author | Jonas Smedegaard <dr@jones.dk> | 2025-05-09 13:44:47 +0200 |
|---|---|---|
| committer | Jonas Smedegaard <dr@jones.dk> | 2025-05-10 12:58:56 +0200 |
| commit | b3ad61eec252fd287785b5ae4f9f230118276c8c (patch) | |
| tree | 487aa10292cde40aed736b0a21e8892dacf9d65e /_extensions | |
| parent | 29d0965e1711d3f5adebcc72d8e3e5265da73531 (diff) | |
use pseudo-enum table to track parser enclosure state
Diffstat (limited to '_extensions')
| -rw-r--r-- | _extensions/ruc-play/semantic-markdown/semantic-markdown.lua | 30 |
1 files changed, 19 insertions, 11 deletions
diff --git a/_extensions/ruc-play/semantic-markdown/semantic-markdown.lua b/_extensions/ruc-play/semantic-markdown/semantic-markdown.lua index 4df6d38..f6f0579 100644 --- a/_extensions/ruc-play/semantic-markdown/semantic-markdown.lua +++ b/_extensions/ruc-play/semantic-markdown/semantic-markdown.lua @@ -57,6 +57,14 @@ -- @see <https://pandoc.org/lua-filters.html#common-pitfalls> os.setlocale 'C' +--- pseudo-enum table to track parser enclosure state +--- @see <https://stackoverflow.com/a/70529481/18619283> +local Enclosure = { + NONE = "0", + BRACKETED = "1", + BRACED = "2", +} + -- TODO: cover non-ASCII Unicode characters -- @see <https://www.lua.org/manual/5.4/manual.html#6.5> --- curie_prefix - CURIE prefix component as set of chars @@ -201,7 +209,7 @@ function Statements (block) -- flags for enclosing stages -- TODO: support nested bracket enclosure - local bracketed, braced + local enclosure = Enclosure.NONE -- amount of detected statements in this block local statement_count = 0 @@ -216,7 +224,7 @@ function Statements (block) if el.t ~= 'Str' then -- TODO: support mixed-use braced enclosure - if not braced then + if enclosure ~= Enclosure.BRACED then table.insert(stack, el) end goto continue @@ -224,7 +232,7 @@ function Statements (block) -- unenclosed -- TODO: support backslash except immediately before bracket - if not (bracketed or braced) then + if enclosure == Enclosure.NONE then _, x, s = el.text:find("^([^%[\\]*)") if x then a = x + 1 @@ -234,9 +242,9 @@ function Statements (block) if el.text:sub(a, a) == "[" then -- entering bracketed enclosure - bracketed = true pos = a + 1 stack_next = stack_next..s + enclosure = Enclosure.BRACKETED -- staying unenclosed else @@ -248,7 +256,7 @@ function Statements (block) -- in bracketed enclosure -- TODO: support backslash except immediately before bracket/brace -- TODO: support nested bracket enclosure - if bracketed and not braced then + if enclosure == Enclosure.BRACKETED then _, x, s = el.text:find("^([^%[%]}\\]*)", pos) if x then b = x + 1 @@ -260,14 +268,13 @@ function Statements (block) -- entering braced enclosure if el.text:sub(c, c) == "{" then - braced = true pos = c + 1 stack_next = stack_next..s + enclosure = Enclosure.BRACED -- leaving non-annotation enclosure else - bracketed = false - braced = false + enclosure = Enclosure.NONE -- TODO: clear only back to entering this bracketed enclosure stack = {} @@ -284,7 +291,7 @@ function Statements (block) -- in braced enclosure, leaving it -- TODO: support mixed-use enclosure - if braced then + if enclosure == Enclosure.BRACED then _, d1 = el.text:find("^"..curie_long.."}", pos) _, d2 = el.text:find("^"..curie_no_ref.."}", pos) _, d3 = el.text:find("^"..curie_local.."}", pos) @@ -296,11 +303,12 @@ function Statements (block) end if d then statement_count = statement_count + 1 - bracketed = false - braced = false pos = d + 1 + -- TODO: instead recursively call Statements() on remains of Str stack_next = stack_next..el.text:sub(pos) + + enclosure = Enclosure.NONE end end |
