fix for last Geoff patch (was asleep)

This commit is contained in:
steve donovan 2012-12-28 11:00:10 +02:00
parent 0f68f97513
commit b039ac1574
1 changed files with 66 additions and 40 deletions

View File

@ -168,34 +168,81 @@ local function process_multiline_markdown(ldoc, txt, F)
end end
-- 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
local formatters = local formatters =
{ {
markdown = function() markdown = generic_formatter,
local ok, f = pcall(require, "markdown") discount = generic_formatter,
return ok and f lunamark = function(format)
end, local ok, lunamark = pcall(require, format)
lunamark = function()
local ok, lunamark = pcall(require, "lunamark")
if ok then if ok then
local writer = lunamark.writer.html.new() local writer = lunamark.writer.html.new()
local parse = lunamark.reader.markdown.new(writer, local parse = lunamark.reader.markdown.new(writer,
{ smart = true }) { smart = true })
return function(text) return parse(text) end return function(text) return parse(text) end
end end
print('format: lunamark not found, using markdown')
ok, f = pcall(require,'markdown')
return ok and f
end end
} }
local generic_formatter = function(format)
local ok, f = pcall(require, format) local function get_formatter(format)
if not ok then local formatter = (formatters[format] or generic_formatter)(format)
print('format: discount not found, using markdown') if formatter then return formatter end
ok, f = pcall(require,'markdown')
for name, f in pairs(formatters) do
formatter = f(name)
if formatter then
print('format: '..format..' not found, using '..name)
return formatter
end end
return ok and f
end end
end
local function text_processor(ldoc)
return function(txt,item)
if txt == nil then return '' end
return resolve_inline_references(ldoc, txt, item, true)
end
end
local function markdown_processor(ldoc, formatter)
return function (txt,item)
if txt == nil then return '' end
if utils.is_type(item,doc.File) then
txt = process_multiline_markdown(ldoc, txt, item)
else
txt = resolve_inline_references(ldoc, txt, item)
end
txt = formatter(txt)
-- We will add our own paragraph tags, if needed.
return (txt:gsub('^%s*<p>(.+)</p>%s*$','%1'))
end
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
function markup.create (ldoc, format) function markup.create (ldoc, format)
@ -226,32 +273,11 @@ function markup.create (ldoc, format)
return ldoc.href(ref) return ldoc.href(ref)
end end
if format == 'plain' then processor = get_processor(ldoc, format)
processor = function(txt, item) if not markup.plain and backtick_references == nil then
if txt == nil then return '' end backtick_references = true
return resolve_inline_references(ldoc, txt, item, true)
end
else
local formatter = (formatters[format] or generic_formatter)()
if not formatter then
quit("can't load formatter: "..format)
end
if backtick_references == nil then
backtick_references = true
end
markup.plain = false
processor = function (txt,item)
if txt == nil then return '' end
if utils.is_type(item,doc.File) then
txt = process_multiline_markdown(ldoc, txt, item)
else
txt = resolve_inline_references(ldoc, txt, item)
end
txt = formatter(txt)
-- We will add our own paragraph tags, if needed.
return (txt:gsub('^%s*<p>(.+)</p>%s*$','%1'))
end
end end
markup.resolve_inline_references = function(txt, errfn) markup.resolve_inline_references = function(txt, errfn)
return resolve_inline_references(ldoc, txt, errfn, markup.plain) return resolve_inline_references(ldoc, txt, errfn, markup.plain)
end end