prettifying code in readme
This commit is contained in:
parent
40330487b0
commit
1a2e61a1f9
126
ldoc/markup.lua
126
ldoc/markup.lua
|
@ -10,56 +10,8 @@ local prettify = require 'ldoc.prettify'
|
|||
local quit, concat, lstrip = utils.quit, table.concat, stringx.lstrip
|
||||
local markup = {}
|
||||
|
||||
-- workaround Markdown's need for blank lines around indented blocks
|
||||
-- (does mean you have to keep indentation discipline!)
|
||||
function markup.insert_markdown_lines (txt)
|
||||
local res, append = {}, table.insert
|
||||
local last_indent, start_indent, skip, code = -1, -1, false, nil
|
||||
for line in stringx.lines(txt) do
|
||||
line = line:gsub('\t',' ') -- some people like tabs ;)
|
||||
if not line:match '^%s*$' then --ignore blank lines
|
||||
local indent = #line:match '^%s*'
|
||||
if start_indent < 0 then -- initialize indents at start
|
||||
start_indent = indent
|
||||
last_indent = indent
|
||||
end
|
||||
if indent < start_indent then -- end of indented block
|
||||
append(res,'')
|
||||
skip = false
|
||||
if code then
|
||||
code = concat(code,'\n')
|
||||
code, err = prettify.lua(code)
|
||||
if code then
|
||||
append(res,code)
|
||||
append(res,'</pre>')
|
||||
end
|
||||
code = nil
|
||||
end
|
||||
end
|
||||
if not skip and indent > last_indent then -- start of indent
|
||||
append(res,'')
|
||||
skip = true
|
||||
start_indent = indent
|
||||
if indent >= 4 then
|
||||
code = {}
|
||||
end
|
||||
end
|
||||
if code then
|
||||
append(code, line:sub(start_indent))
|
||||
else
|
||||
append(res,line)
|
||||
end
|
||||
last_indent = indent
|
||||
elseif not code then
|
||||
append(res,'')
|
||||
end
|
||||
end
|
||||
res = concat(res,'\n')
|
||||
return res
|
||||
end
|
||||
|
||||
-- inline <references> use same lookup as @see
|
||||
local function resolve_inline_references (ldoc, txt, item)
|
||||
local function resolve_inline_references (ldoc, txt, item, plain)
|
||||
return (txt:gsub('@{([^}]-)}',function (name)
|
||||
local qname,label = utils.splitv(name,'%s*|')
|
||||
if not qname then
|
||||
|
@ -77,7 +29,7 @@ local function resolve_inline_references (ldoc, txt, item)
|
|||
if not label then
|
||||
label = ref.label
|
||||
end
|
||||
if not markup.plain then -- a nastiness with markdown.lua and underscores
|
||||
if not plain then -- a nastiness with markdown.lua and underscores
|
||||
label = label:gsub('_','\\_')
|
||||
end
|
||||
local res = ('<a href="%s">%s</a>'):format(ldoc.href(ref),label)
|
||||
|
@ -100,21 +52,66 @@ function markup.add_sections(F, txt)
|
|||
return txt
|
||||
end
|
||||
|
||||
local function indent_line (line)
|
||||
line = line:gsub('\t',' ') -- support for barbarians ;)
|
||||
local indent = #line:match '^%s*'
|
||||
return indent,line
|
||||
end
|
||||
|
||||
local function non_blank (line)
|
||||
return line:find '%S'
|
||||
end
|
||||
|
||||
-- before we pass Markdown documents to markdown, we need to do three things:
|
||||
-- - resolve any @{refs}
|
||||
-- - insert any section ids which were generated by add_sections above
|
||||
-- - prettify any code blocks
|
||||
|
||||
local function process_multiline_markdown(ldoc, txt, F)
|
||||
local res, L, append = {}, 1, table.insert
|
||||
local res, L, append = {}, 0, table.insert
|
||||
local filename = F.filename
|
||||
local err_item = {
|
||||
warning = function (self,msg)
|
||||
io.stderr:write(F.filename..':'..L..': '..msg,'\n')
|
||||
io.stderr:write(filename..':'..L..': '..msg,'\n')
|
||||
end
|
||||
}
|
||||
for line in stringx.lines(txt) do
|
||||
line = resolve_inline_references(ldoc, line, err_item)
|
||||
local section = F.sections[L]
|
||||
if section then
|
||||
append(res,('<a name="%s"></a>'):format(section))
|
||||
end
|
||||
append(res,line)
|
||||
local get = stringx.lines(txt)
|
||||
local getline = function()
|
||||
L = L + 1
|
||||
return get()
|
||||
end
|
||||
local line = getline()
|
||||
local indent,code,start_indent
|
||||
while line do
|
||||
line = resolve_inline_references(ldoc, line, err_item)
|
||||
indent, line = indent_line(line)
|
||||
if indent >= 4 then -- indented code block
|
||||
code = {}
|
||||
while indent >= 4 or not non_blank(line) do
|
||||
if not start_indent then start_indent = indent end
|
||||
append(code,line:sub(start_indent))
|
||||
line = getline()
|
||||
if line == nil then break end
|
||||
indent, line = indent_line(line)
|
||||
end
|
||||
start_indent = nil
|
||||
if #code > 1 then table.remove(code) end
|
||||
code = concat(code,'\n')
|
||||
if code ~= '' then
|
||||
code, err = prettify.lua(filename,code,L)
|
||||
append(res, code)
|
||||
append(res,'</pre>')
|
||||
else
|
||||
append(res ,code)
|
||||
end
|
||||
else
|
||||
local section = F.sections[L]
|
||||
if section then
|
||||
append(res,('<a name="%s"></a>'):format(section))
|
||||
end
|
||||
append(res,line)
|
||||
line = getline()
|
||||
end
|
||||
end
|
||||
return concat(res,'\n')
|
||||
end
|
||||
|
@ -134,7 +131,7 @@ function markup.create (ldoc, format)
|
|||
if format == 'plain' then
|
||||
processor = function(txt, item)
|
||||
if txt == nil then return '' end
|
||||
return resolve_inline_references(ldoc, txt, item)
|
||||
return resolve_inline_references(ldoc, txt, item, true)
|
||||
end
|
||||
else
|
||||
local ok,formatter = pcall(require,format)
|
||||
|
@ -147,19 +144,18 @@ function markup.create (ldoc, format)
|
|||
else
|
||||
txt = resolve_inline_references(ldoc, txt, item)
|
||||
end
|
||||
if txt:find '\n' and ldoc.extended_markdown then -- multiline text
|
||||
txt = markup.insert_markdown_lines(txt)
|
||||
end
|
||||
txt = formatter(txt)
|
||||
-- We will add our own paragraph tags, if needed.
|
||||
return (txt:gsub('^%s*<p>(.+)</p>%s*$','%1'))
|
||||
end
|
||||
end
|
||||
markup.resolve_inline_references = function(txt, errfn)
|
||||
return resolve_inline_references(ldoc, txt, errfn)
|
||||
return resolve_inline_references(ldoc, txt, errfn, markup.plain)
|
||||
end
|
||||
markup.processor = processor
|
||||
prettify.resolve_inline_references = markup.resolve_inline_references
|
||||
prettify.resolve_inline_references = function(txt, errfn)
|
||||
return resolve_inline_references(ldoc, txt, errfn, true)
|
||||
end
|
||||
return processor
|
||||
end
|
||||
|
||||
|
|
|
@ -26,15 +26,16 @@ end
|
|||
|
||||
local spans = {keyword=true,number=true,string=true,comment=true,global=true}
|
||||
|
||||
function prettify.lua (fname, code)
|
||||
function prettify.lua (fname, code, initial_lineno)
|
||||
local res = List()
|
||||
res:append(header)
|
||||
res:append '<pre>\n'
|
||||
intial_lineno = initial_lineno or 0
|
||||
|
||||
local tok = lexer.lua(code,{},{})
|
||||
local error_reporter = {
|
||||
warning = function (self,msg)
|
||||
io.stderr:write(fname..':'..tok:lineno()..': '..msg,'\n')
|
||||
io.stderr:write(fname..':'..tok:lineno()+initial_lineno..': '..msg,'\n')
|
||||
end
|
||||
}
|
||||
local t,val = tok()
|
||||
|
|
Loading…
Reference in New Issue