-
-
Save GoldsteinE/ef20aa4fc07a6211d410c0dacf53f768 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local langs = { | |
cpp = 'C++' | |
} | |
local function extract_lang(tag) | |
local info_string = tag:as_info_string() | |
if info_string == nil or info_string == "" then | |
return nil | |
end | |
local lang = langs[info_string] | |
if lang ~= nil then | |
return lang | |
end | |
local first = info_string:sub(1, 1) | |
local rest = info_string:sub(2) | |
return first:upper() .. rest:lower() | |
end | |
return function(event) | |
local tag = event:as_start_tag() | |
if tag ~= nil then | |
local lang = extract_lang(tag) | |
if lang ~= nil then | |
return { | |
Event:html( | |
'<figure class="code-wrapper"><figcaption class="code-lang">' | |
.. lang | |
.. '</figcaption>' | |
), | |
event | |
} | |
end | |
end | |
local tag = event:as_end_tag() | |
if tag ~= nil then | |
if extract_lang(tag) ~= nil then | |
return { | |
event, | |
Event:html('</figure>') | |
} | |
end | |
end | |
return { event } | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local target = "" | |
local last_events = {} | |
return function(event) | |
-- On heading start, initialize last_events and consume input event | |
local tag = event:as_start_tag() | |
if tag ~= nil and tag:is_heading() then | |
table.insert(last_events, event) | |
return { } | |
end | |
-- On heading end, produce linkified header and return all consumed events | |
tag = event:as_end_tag() | |
if tag ~= nil and tag:is_heading() then | |
local link_start = Event:html('<a href="' .. target .. '" class="header-link">') | |
local link_end = Event:html('</a>') | |
local events = last_events | |
table.insert(events, 1, link_start) | |
table.insert(events, event) | |
table.insert(events, link_end) | |
target = "" | |
last_events = {} | |
return events | |
end | |
-- If there is `{#target}` in text, extract `#target | |
local text = event:as_text() | |
if text ~= nil then | |
local new_target = text:match('{(#[^"]-)}') | |
if new_target ~= nil then | |
target = new_target | |
end | |
end | |
-- If we're consuming events, keep doing it | |
if #last_events ~= 0 then | |
table.insert(last_events, event) | |
return { } | |
end | |
return { event } | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
return function(event) | |
local html = event:as_html() | |
if html ~= nil | |
and html:find("^%s*<!%-%-[^-].-%-%->%s*$") == 1 | |
and html:find("<!%-%- more %-%->") == nil | |
then | |
return { } | |
end | |
return { event } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment