2011-07-05 16:30:49 +02:00
|
|
|
--------------
|
|
|
|
-- Handling markup transformation.
|
|
|
|
-- Currently just does Markdown, but this is intended to
|
|
|
|
-- be the general module for managing other formats as well.
|
|
|
|
|
2011-07-11 15:40:44 +02:00
|
|
|
local doc = require 'ldoc.doc'
|
2011-07-05 16:30:49 +02:00
|
|
|
local utils = require 'pl.utils'
|
2012-10-28 18:51:00 +01:00
|
|
|
local stringx = require 'pl.stringx'
|
2011-07-12 14:14:55 +02:00
|
|
|
local prettify = require 'ldoc.prettify'
|
|
|
|
local quit, concat, lstrip = utils.quit, table.concat, stringx.lstrip
|
2011-07-05 16:30:49 +02:00
|
|
|
local markup = {}
|
|
|
|
|
2012-03-04 17:46:02 +01:00
|
|
|
local backtick_references
|
|
|
|
|
2011-09-17 17:57:22 +02:00
|
|
|
-- inline <references> use same lookup as @see
|
2011-09-19 14:22:18 +02:00
|
|
|
local function resolve_inline_references (ldoc, txt, item, plain)
|
2011-09-20 14:11:31 +02:00
|
|
|
local res = (txt:gsub('@{([^}]-)}',function (name)
|
2013-08-23 13:50:34 +02:00
|
|
|
if name:match '^\\' then return '@{'..name:sub(2)..'}' end
|
2011-09-17 17:57:22 +02:00
|
|
|
local qname,label = utils.splitv(name,'%s*|')
|
|
|
|
if not qname then
|
|
|
|
qname = name
|
|
|
|
end
|
|
|
|
local ref,err = markup.process_reference(qname)
|
|
|
|
if not ref then
|
|
|
|
err = err .. ' ' .. qname
|
2013-12-21 03:50:07 +01:00
|
|
|
if item and item.warning then item:warning(err)
|
2011-09-17 17:57:22 +02:00
|
|
|
else
|
|
|
|
io.stderr:write('nofile error: ',err,'\n')
|
|
|
|
end
|
|
|
|
return '???'
|
|
|
|
end
|
|
|
|
if not label then
|
|
|
|
label = ref.label
|
|
|
|
end
|
2012-03-14 10:38:54 +01:00
|
|
|
if not plain and label then -- a nastiness with markdown.lua and underscores
|
2011-09-17 17:57:22 +02:00
|
|
|
label = label:gsub('_','\\_')
|
|
|
|
end
|
2011-12-06 18:20:17 +01:00
|
|
|
local html = ldoc.href(ref) or '#'
|
2013-03-20 13:41:54 +01:00
|
|
|
label = label or qname
|
2011-12-06 18:20:17 +01:00
|
|
|
local res = ('<a href="%s">%s</a>'):format(html,label)
|
2011-09-17 17:57:22 +02:00
|
|
|
return res
|
|
|
|
end))
|
2012-03-04 17:46:02 +01:00
|
|
|
if backtick_references then
|
|
|
|
res = res:gsub('`([^`]+)`',function(name)
|
|
|
|
local ref,err = markup.process_reference(name)
|
|
|
|
if ref then
|
2012-03-05 14:34:16 +01:00
|
|
|
return ('<a href="%s">%s</a> '):format(ldoc.href(ref),name)
|
2012-03-04 17:46:02 +01:00
|
|
|
else
|
2013-08-22 09:43:26 +02:00
|
|
|
return '<code>'..name..'</code>'
|
2012-03-04 17:46:02 +01:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
2011-09-20 14:11:31 +02:00
|
|
|
return res
|
2011-09-17 17:57:22 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- for readme text, the idea here is to create module sections at ## so that
|
|
|
|
-- they can appear in the contents list as a ToC.
|
2011-07-11 15:40:44 +02:00
|
|
|
function markup.add_sections(F, txt)
|
2012-03-14 10:38:54 +01:00
|
|
|
local sections, L, first = {}, 1, true
|
2013-11-17 09:38:24 +01:00
|
|
|
local title_pat
|
|
|
|
local lstrip = stringx.lstrip
|
2011-07-11 15:40:44 +02:00
|
|
|
for line in stringx.lines(txt) do
|
2012-03-14 10:38:54 +01:00
|
|
|
if first then
|
|
|
|
local level,header = line:match '^(#+)%s*(.+)'
|
|
|
|
if level then
|
|
|
|
level = level .. '#'
|
|
|
|
else
|
|
|
|
level = '##'
|
|
|
|
end
|
2013-09-27 15:37:46 +02:00
|
|
|
title_pat = '^'..level..'([^#]%s*.+)'
|
2013-11-17 09:38:24 +01:00
|
|
|
title_pat = lstrip(title_pat)
|
2012-03-14 10:38:54 +01:00
|
|
|
first = false
|
2013-11-18 09:26:40 +01:00
|
|
|
F.display_name = header
|
2012-03-14 10:38:54 +01:00
|
|
|
end
|
|
|
|
local title = line:match (title_pat)
|
2011-07-11 15:40:44 +02:00
|
|
|
if title then
|
2013-11-18 09:26:40 +01:00
|
|
|
-- Markdown allows trailing '#'...
|
2012-03-14 10:38:54 +01:00
|
|
|
title = title:gsub('%s*#+$','')
|
2013-11-17 09:38:24 +01:00
|
|
|
sections[L] = F:add_document_section(lstrip(title))
|
2011-07-06 15:24:05 +02:00
|
|
|
end
|
2011-09-17 17:57:22 +02:00
|
|
|
L = L + 1
|
2011-07-11 15:40:44 +02:00
|
|
|
end
|
2011-09-17 17:57:22 +02:00
|
|
|
F.sections = sections
|
|
|
|
return txt
|
2011-07-11 15:40:44 +02:00
|
|
|
end
|
|
|
|
|
2011-09-19 14:22:18 +02:00
|
|
|
local function indent_line (line)
|
|
|
|
line = line:gsub('\t',' ') -- support for barbarians ;)
|
|
|
|
local indent = #line:match '^%s*'
|
|
|
|
return indent,line
|
|
|
|
end
|
|
|
|
|
2013-12-12 16:26:38 +01:00
|
|
|
local function blank (line)
|
|
|
|
return not line:find '%S'
|
2011-09-19 14:22:18 +02:00
|
|
|
end
|
|
|
|
|
2012-03-14 10:38:54 +01:00
|
|
|
local global_context, local_context
|
|
|
|
|
|
|
|
-- before we pass Markdown documents to markdown/discount, we need to do three things:
|
|
|
|
-- - resolve any @{refs} and (optionally) `refs`
|
|
|
|
-- - any @lookup directives that set local context for ref lookup
|
2011-09-19 14:22:18 +02:00
|
|
|
-- - insert any section ids which were generated by add_sections above
|
|
|
|
-- - prettify any code blocks
|
|
|
|
|
2011-09-17 17:57:22 +02:00
|
|
|
local function process_multiline_markdown(ldoc, txt, F)
|
2011-09-19 14:22:18 +02:00
|
|
|
local res, L, append = {}, 0, table.insert
|
|
|
|
local filename = F.filename
|
2011-09-17 17:57:22 +02:00
|
|
|
local err_item = {
|
|
|
|
warning = function (self,msg)
|
2011-09-19 14:22:18 +02:00
|
|
|
io.stderr:write(filename..':'..L..': '..msg,'\n')
|
2011-07-11 15:40:44 +02:00
|
|
|
end
|
2011-09-17 17:57:22 +02:00
|
|
|
}
|
2011-09-19 14:22:18 +02:00
|
|
|
local get = stringx.lines(txt)
|
|
|
|
local getline = function()
|
|
|
|
L = L + 1
|
|
|
|
return get()
|
|
|
|
end
|
2013-03-06 16:07:28 +01:00
|
|
|
local function pretty_code (code, lang)
|
|
|
|
code = concat(code,'\n')
|
|
|
|
if code ~= '' then
|
|
|
|
local err
|
2013-12-12 16:26:38 +01:00
|
|
|
-- If we omit the following '\n', a '--' (or '//') comment on the
|
|
|
|
-- last line won't be recognized.
|
2013-03-06 16:07:28 +01:00
|
|
|
code, err = prettify.code(lang,filename,code..'\n',L,false)
|
|
|
|
append(res,'<pre>')
|
|
|
|
append(res, code)
|
|
|
|
append(res,'</pre>')
|
|
|
|
else
|
|
|
|
append(res,code)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
local indent,start_indent
|
2012-03-14 10:38:54 +01:00
|
|
|
local_context = nil
|
2013-03-06 16:07:28 +01:00
|
|
|
local line = getline()
|
2011-09-19 14:22:18 +02:00
|
|
|
while line do
|
2012-03-16 13:42:58 +01:00
|
|
|
local name = line:match '^@lookup%s+(%S+)'
|
2012-03-14 10:38:54 +01:00
|
|
|
if name then
|
|
|
|
local_context = name .. '.'
|
|
|
|
line = getline()
|
|
|
|
end
|
2013-03-06 16:07:28 +01:00
|
|
|
local fence = line:match '^```(.*)'
|
|
|
|
if fence then
|
|
|
|
local plain = fence==''
|
|
|
|
line = getline()
|
|
|
|
local code = {}
|
|
|
|
while not line:match '^```' do
|
|
|
|
if not plain then
|
|
|
|
append(code, line)
|
|
|
|
else
|
|
|
|
append(res, ' '..line)
|
|
|
|
end
|
|
|
|
line = getline()
|
|
|
|
end
|
|
|
|
pretty_code (code,fence)
|
|
|
|
line = getline() -- skip fence
|
|
|
|
end
|
2011-09-19 14:22:18 +02:00
|
|
|
indent, line = indent_line(line)
|
|
|
|
if indent >= 4 then -- indented code block
|
2013-03-06 16:07:28 +01:00
|
|
|
local code = {}
|
2011-09-20 15:59:34 +02:00
|
|
|
local plain
|
2013-12-12 16:26:38 +01:00
|
|
|
while indent >= 4 or blank(line) do
|
2011-09-20 15:59:34 +02:00
|
|
|
if not start_indent then
|
|
|
|
start_indent = indent
|
|
|
|
if line:match '^%s*@plain%s*$' then
|
|
|
|
plain = true
|
|
|
|
line = getline()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if not plain then
|
2013-12-12 16:26:38 +01:00
|
|
|
append(code,line:sub(start_indent + 1))
|
2011-09-20 15:59:34 +02:00
|
|
|
else
|
2013-03-06 16:07:28 +01:00
|
|
|
append(res,line)
|
2011-09-20 15:59:34 +02:00
|
|
|
end
|
2011-09-19 14:22:18 +02:00
|
|
|
line = getline()
|
|
|
|
if line == nil then break end
|
|
|
|
indent, line = indent_line(line)
|
|
|
|
end
|
|
|
|
start_indent = nil
|
2013-12-12 16:26:38 +01:00
|
|
|
while #code > 1 and blank(code[#code]) do -- trim blank lines.
|
|
|
|
table.remove(code)
|
|
|
|
end
|
2013-03-06 16:07:28 +01:00
|
|
|
pretty_code (code,'lua')
|
2011-09-19 14:22:18 +02:00
|
|
|
else
|
|
|
|
local section = F.sections[L]
|
|
|
|
if section then
|
|
|
|
append(res,('<a name="%s"></a>'):format(section))
|
|
|
|
end
|
2012-03-16 13:41:26 +01:00
|
|
|
line = resolve_inline_references(ldoc, line, err_item)
|
2011-09-19 14:22:18 +02:00
|
|
|
append(res,line)
|
|
|
|
line = getline()
|
2011-09-17 17:57:22 +02:00
|
|
|
end
|
2011-07-11 15:40:44 +02:00
|
|
|
end
|
2012-03-05 14:34:16 +01:00
|
|
|
res = concat(res,'\n')
|
|
|
|
return res
|
2011-07-11 15:40:44 +02:00
|
|
|
end
|
|
|
|
|
2011-07-06 15:24:05 +02:00
|
|
|
|
2012-12-28 10:00:10 +01:00
|
|
|
-- Handle markdown formatters
|
|
|
|
-- Try to get the one the user has asked for, but if it's not available,
|
|
|
|
-- try all the others we know about. If they don't work, fall back to text.
|
|
|
|
|
|
|
|
local function generic_formatter(format)
|
|
|
|
local ok, f = pcall(require, format)
|
|
|
|
return ok and f
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2012-12-28 09:56:43 +01:00
|
|
|
local formatters =
|
|
|
|
{
|
2013-03-08 12:40:59 +01:00
|
|
|
markdown = function(format)
|
|
|
|
local ok, markdown = pcall(require, 'markdown')
|
|
|
|
if not ok then
|
|
|
|
print('format: using built-in markdown')
|
|
|
|
ok, markdown = pcall(require, 'ldoc.markdown')
|
|
|
|
end
|
|
|
|
return ok and markdown
|
|
|
|
end,
|
2012-12-28 10:00:10 +01:00
|
|
|
discount = generic_formatter,
|
|
|
|
lunamark = function(format)
|
2013-03-08 12:40:59 +01:00
|
|
|
local ok, lunamark = pcall(require, format)
|
|
|
|
if ok then
|
|
|
|
local writer = lunamark.writer.html.new()
|
|
|
|
local parse = lunamark.reader.markdown.new(writer,
|
|
|
|
{ smart = true })
|
|
|
|
return function(text) return parse(text) end
|
2012-12-28 09:56:43 +01:00
|
|
|
end
|
2013-03-08 12:40:59 +01:00
|
|
|
end
|
2012-12-28 09:56:43 +01:00
|
|
|
}
|
|
|
|
|
2012-12-28 10:00:10 +01:00
|
|
|
|
|
|
|
local function get_formatter(format)
|
|
|
|
local formatter = (formatters[format] or generic_formatter)(format)
|
|
|
|
if formatter then return formatter end
|
|
|
|
|
|
|
|
for name, f in pairs(formatters) do
|
|
|
|
formatter = f(name)
|
|
|
|
if formatter then
|
|
|
|
print('format: '..format..' not found, using '..name)
|
|
|
|
return formatter
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function text_processor(ldoc)
|
|
|
|
return function(txt,item)
|
|
|
|
if txt == nil then return '' end
|
2013-03-08 12:40:59 +01:00
|
|
|
-- hack to separate paragraphs with blank lines
|
|
|
|
txt = txt:gsub('\n\n','\n<p>')
|
2012-12-28 10:00:10 +01:00
|
|
|
return resolve_inline_references(ldoc, txt, item, true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-09 12:52:15 +02:00
|
|
|
local plain_processor
|
2012-12-28 10:00:10 +01:00
|
|
|
|
|
|
|
local function markdown_processor(ldoc, formatter)
|
2013-05-09 12:52:15 +02:00
|
|
|
return function (txt,item,plain)
|
2012-12-28 10:00:10 +01:00
|
|
|
if txt == nil then return '' end
|
2013-05-09 12:52:15 +02:00
|
|
|
if plain then
|
|
|
|
if not plain_processor then
|
|
|
|
plain_processor = text_processor(ldoc)
|
|
|
|
end
|
|
|
|
return plain_processor(txt,item)
|
|
|
|
end
|
2012-12-28 10:00:10 +01:00
|
|
|
if utils.is_type(item,doc.File) then
|
|
|
|
txt = process_multiline_markdown(ldoc, txt, item)
|
|
|
|
else
|
|
|
|
txt = resolve_inline_references(ldoc, txt, item)
|
2012-12-28 09:56:43 +01:00
|
|
|
end
|
2012-12-28 10:00:10 +01:00
|
|
|
txt = formatter(txt)
|
|
|
|
-- We will add our own paragraph tags, if needed.
|
|
|
|
return (txt:gsub('^%s*<p>(.+)</p>%s*$','%1'))
|
2012-12-28 09:56:43 +01:00
|
|
|
end
|
2012-12-28 10:00:10 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
local function get_processor(ldoc, format)
|
|
|
|
if format == 'plain' then return text_processor(ldoc) end
|
|
|
|
|
|
|
|
local formatter = get_formatter(format)
|
|
|
|
if formatter then
|
|
|
|
markup.plain = false
|
|
|
|
return markdown_processor(ldoc, formatter)
|
|
|
|
end
|
|
|
|
|
|
|
|
print('format: '..format..' not found, falling back to text')
|
|
|
|
return text_processor(ldoc)
|
|
|
|
end
|
2012-12-28 09:56:43 +01:00
|
|
|
|
|
|
|
|
2013-03-06 16:07:28 +01:00
|
|
|
function markup.create (ldoc, format, pretty)
|
2011-07-11 15:40:44 +02:00
|
|
|
local processor
|
|
|
|
markup.plain = true
|
2013-08-22 09:43:26 +02:00
|
|
|
if format == 'backtick' then
|
|
|
|
ldoc.backtick_references = true
|
|
|
|
format = 'plain'
|
|
|
|
end
|
2012-03-04 17:46:02 +01:00
|
|
|
backtick_references = ldoc.backtick_references
|
2012-03-14 10:38:54 +01:00
|
|
|
global_context = ldoc.package and ldoc.package .. '.'
|
2013-03-06 16:07:28 +01:00
|
|
|
prettify.set_prettifier(pretty)
|
2012-03-14 10:38:54 +01:00
|
|
|
|
2013-08-21 14:52:09 +02:00
|
|
|
markup.process_reference = function(name,istype)
|
2012-12-31 16:02:40 +01:00
|
|
|
if local_context == 'none.' and not name:match '%.' then
|
|
|
|
return nil,'not found'
|
|
|
|
end
|
2012-03-19 18:07:11 +01:00
|
|
|
local mod = ldoc.single or ldoc.module or ldoc.modules[1]
|
2013-08-21 14:52:09 +02:00
|
|
|
local ref,err = mod:process_see_reference(name, ldoc.modules, istype)
|
2012-03-14 10:38:54 +01:00
|
|
|
if ref then return ref end
|
|
|
|
if global_context then
|
|
|
|
local qname = global_context .. name
|
2013-08-21 14:52:09 +02:00
|
|
|
ref = mod:process_see_reference(qname, ldoc.modules, istype)
|
2012-03-14 10:38:54 +01:00
|
|
|
if ref then return ref end
|
|
|
|
end
|
|
|
|
if local_context then
|
|
|
|
local qname = local_context .. name
|
2013-08-21 14:52:09 +02:00
|
|
|
ref = mod:process_see_reference(qname, ldoc.modules, istype)
|
2012-03-14 10:38:54 +01:00
|
|
|
if ref then return ref end
|
|
|
|
end
|
|
|
|
-- note that we'll return the original error!
|
|
|
|
return ref,err
|
2011-07-11 15:40:44 +02:00
|
|
|
end
|
2012-03-14 10:38:54 +01:00
|
|
|
|
2011-07-11 15:40:44 +02:00
|
|
|
markup.href = function(ref)
|
|
|
|
return ldoc.href(ref)
|
|
|
|
end
|
2011-07-12 14:14:55 +02:00
|
|
|
|
2012-12-28 10:00:10 +01:00
|
|
|
processor = get_processor(ldoc, format)
|
|
|
|
if not markup.plain and backtick_references == nil then
|
|
|
|
backtick_references = true
|
2011-07-05 16:30:49 +02:00
|
|
|
end
|
2012-12-28 10:00:10 +01:00
|
|
|
|
2011-09-17 17:57:22 +02:00
|
|
|
markup.resolve_inline_references = function(txt, errfn)
|
2011-09-19 14:22:18 +02:00
|
|
|
return resolve_inline_references(ldoc, txt, errfn, markup.plain)
|
2011-07-11 15:40:44 +02:00
|
|
|
end
|
|
|
|
markup.processor = processor
|
2011-09-19 14:22:18 +02:00
|
|
|
prettify.resolve_inline_references = function(txt, errfn)
|
|
|
|
return resolve_inline_references(ldoc, txt, errfn, true)
|
|
|
|
end
|
2011-07-11 15:40:44 +02:00
|
|
|
return processor
|
2011-07-05 16:30:49 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
return markup
|