nitc :: NitdocDecorator :: defaultinit
# The specific markdown decorator used internally to process MDoc object.
#
# You should use the various methods of `MDoc` like `MDoc::html_documentation`
#
# The class is public so specific behavior can be plugged on it.
class NitdocDecorator
super HTMLDecorator
private var toolcontext = new ToolContext
# The currently processed mdoc.
#
# Unfortunately, this seems to be the simpler way to get the currently processed `MDoc` object.
var current_mdoc: nullable MDoc = null
redef fun add_code(v, block) do
var meta = block.meta or else "nit"
# Do not try to highlight non-nit code.
if meta != "nit" and meta != "nitish" then
v.add "<pre class=\"{meta}\"><code>"
v.emit_in block
v.add "</code></pre>\n"
return
end
# Try to parse code
var code = block.raw_content
var ast = toolcontext.parse_something(code)
if ast isa AError then
v.add "<pre class=\"{meta}\"><code>"
v.emit_in block
v.add "</code></pre>\n"
return
end
v.add "<pre class=\"nitcode\"><code>"
var hl = new HtmlightVisitor
hl.line_id_prefix = ""
hl.highlight_node(ast)
v.add(hl.html)
v.add "</code></pre>\n"
end
redef fun add_span_code(v, text, from, to) do
# Try to parse it
var code = code_from_text(text, from, to)
var ast = toolcontext.parse_something(code)
if ast isa AError then
v.add "<code class=\"rawcode\">"
append_code(v, text, from, to)
else
v.add "<code class=\"nitcode\">"
var hl = new HtmlightVisitor
hl.line_id_prefix = ""
hl.highlight_node(ast)
v.add(hl.html)
end
v.add "</code>"
end
private fun code_from_text(buffer: Text, from, to: Int): String do
var out = new FlatBuffer
for i in [from..to[ do out.add buffer[i]
return out.write_to_string
end
end
src/doc/templates/html_model.nit:447,1--512,3