Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
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
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