Skip to content

Commit

Permalink
Merge pull request #67 from Omikhleia/feat-mark-extension
Browse files Browse the repository at this point in the history
feat: Support `mark` (highlight) extension
  • Loading branch information
Witiko authored Sep 27, 2023
2 parents 8e8c309 + 619e8f3 commit 55bfb1f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
6 changes: 6 additions & 0 deletions bin/lunamark
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,10 @@ environment variable `LUNAMARK_EXTENSIONS` (see ENVIRONMENT below).
: Enable strike-through support for a text enclosed within double
tildes, as in `~~deleted~~`.
`(-) mark`
: Enable highlighting support for a text enclosed within double
equals, as in `==marked==`.
`(-) subscript`
: Enable superscript support. Superscripts may be written by surrounding
the subscripted text by `~` characters, as in `2~10~`.
Expand Down Expand Up @@ -519,6 +523,7 @@ given in parentheses:
(-) fancy_lists Pandoc style fancy lists
(-) task_list GitHub-Flavored Markdown task list
(-) strikeout Strike-through with double tildes
(-) mark Highlight with double equals
(-) subscript Subscripted text between tildes
(-) superscript Superscripted text between circumflexes
(-) bracketed_spans Spans with attributes
Expand Down Expand Up @@ -590,6 +595,7 @@ local extensions = { -- defaults
fancy_lists = false,
task_list = false,
strikeout = false,
mark = false,
subscript = false,
superscript = false,
bracketed_spans = false,
Expand Down
26 changes: 23 additions & 3 deletions lunamark/reader/markdown.lua
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ parsers.citation_chars = parsers.alphanumeric
parsers.internal_punctuation = S(":;,.?")

parsers.doubleasterisks = P("**")
parsers.doubleequals = P("==")
parsers.doubleunderscores = P("__")
parsers.doubletildes = P("~~")
parsers.fourspaces = P(" ")
Expand Down Expand Up @@ -752,6 +753,10 @@ end
-- : Enable strike-through support for a text enclosed within double
-- tildes, as in `~~deleted~~`.
--
-- `mark`
-- : Enable highlighting support for a text enclosed within double
-- equals, as in `==marked==`.
--
-- `superscript`
-- : Enable superscript support. Superscripts may be written by surrounding
-- the superscripted text by `^` characters, as in `2^10^.
Expand Down Expand Up @@ -922,11 +927,14 @@ function M.new(writer, options)
-- Basic parsers (local)
------------------------------------------------------------------------------

local specials = "*_~`&[]<!\\-@^"
if options.smart then
larsers.specialchar = S("*_~`&[]<!\\'\"-.@^")
else
larsers.specialchar = S("*_~`&[]<!\\-@^")
specials = specials .. "'.\""
end
if options.mark then
specials = specials .. "="
end
larsers.specialchar = S(specials)

larsers.normalchar = parsers.any - (larsers.specialchar
+ parsers.spacing
Expand Down Expand Up @@ -1209,6 +1217,12 @@ function M.new(writer, options)
parsers.doubletildes)
) / writer.strikeout

larsers.Mark = parsers.between(parsers.Inline, parsers.doubleequals,
parsers.doubleequals)
/ function (inlines)
return writer.span(inlines, { class="mark" })
end

larsers.Span = ( parsers.between(parsers.Inline, parsers.lbracket,
parsers.rbracket) ) * ( parsers.attributes )
/ writer.span
Expand Down Expand Up @@ -1799,6 +1813,7 @@ function M.new(writer, options)
+ V("Emph")
+ V("Span")
+ V("Strikeout")
+ V("Mark")
+ V("Subscript")
+ V("Superscript")
+ V("InlineNote")
Expand All @@ -1824,6 +1839,7 @@ function M.new(writer, options)
Emph = larsers.Emph,
Span = larsers.Span,
Strikeout = larsers.Strikeout,
Mark = larsers.Mark,
Subscript = larsers.Subscript,
Superscript = larsers.Superscript,
InlineNote = larsers.InlineNote,
Expand Down Expand Up @@ -1878,6 +1894,10 @@ function M.new(writer, options)
syntax.Strikeout = parsers.fail
end

if not options.mark then
syntax.Mark = parsers.fail
end

if not options.raw_attribute then
syntax.RawInLine = parsers.fail
end
Expand Down
10 changes: 10 additions & 0 deletions lunamark/writer/html5.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ function M.new(options)
options = options or {}
local Html5 = html.new(options)

local htmlSpan = Html5.span -- inherited, for overriding

Html5.container = "section"

Html5.template = [[
Expand All @@ -36,6 +38,14 @@ $body
return {"<del>", s, "</del>"}
end

function Html5.span(s, attr)
-- HTML5 has semantic <mark>
if attr.class == "mark" then
return {"<mark>", s, "</mark>"}
end
return htmlSpan(s, attr)
end

return Html5
end

Expand Down
5 changes: 5 additions & 0 deletions tests/lunamark/ext_mark.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
lunamark -Xmark
<<<
Some ==highlighted text==.
>>>
<p>Some <span class="mark">highlighted text</span>.</p>

0 comments on commit 55bfb1f

Please sign in to comment.