2011-07-10 19:10:53 +02:00
|
|
|
-- Making Lua source code look pretty.
|
|
|
|
-- A simple scanner based prettifier, which scans comments for @{ref} and code
|
|
|
|
-- for known modules and functions.
|
|
|
|
-- A module reference to an example `test-fun.lua` would look like
|
|
|
|
-- `@{example:test-fun}`.
|
2012-10-28 18:51:00 +01:00
|
|
|
local List = require 'pl.List'
|
2011-07-10 19:10:53 +02:00
|
|
|
local lexer = require 'ldoc.lexer'
|
2011-09-20 12:59:34 +02:00
|
|
|
local globals = require 'ldoc.builtin.globals'
|
2011-07-11 15:40:44 +02:00
|
|
|
local tnext = lexer.skipws
|
2011-07-10 19:10:53 +02:00
|
|
|
local prettify = {}
|
|
|
|
|
|
|
|
local escaped_chars = {
|
|
|
|
['&'] = '&',
|
|
|
|
['<'] = '<',
|
|
|
|
['>'] = '>',
|
|
|
|
}
|
|
|
|
local escape_pat = '[&<>]'
|
|
|
|
|
|
|
|
local function escape(str)
|
|
|
|
return (str:gsub(escape_pat,escaped_chars))
|
|
|
|
end
|
|
|
|
|
|
|
|
local function span(t,val)
|
|
|
|
return ('<span class="%s">%s</span>'):format(t,val)
|
|
|
|
end
|
|
|
|
|
2011-09-19 13:32:30 +02:00
|
|
|
local spans = {keyword=true,number=true,string=true,comment=true,global=true}
|
2011-07-10 19:10:53 +02:00
|
|
|
|
2013-03-06 16:07:28 +01:00
|
|
|
function prettify.lua (fname, code, initial_lineno, pre)
|
2011-07-10 19:10:53 +02:00
|
|
|
local res = List()
|
2013-03-06 16:07:28 +01:00
|
|
|
if pre then
|
2013-03-05 15:21:54 +01:00
|
|
|
res:append '<pre>\n'
|
|
|
|
end
|
2012-03-23 13:21:43 +01:00
|
|
|
initial_lineno = initial_lineno or 0
|
2011-07-10 19:10:53 +02:00
|
|
|
|
|
|
|
local tok = lexer.lua(code,{},{})
|
2011-09-17 17:57:22 +02:00
|
|
|
local error_reporter = {
|
|
|
|
warning = function (self,msg)
|
2011-09-19 14:22:18 +02:00
|
|
|
io.stderr:write(fname..':'..tok:lineno()+initial_lineno..': '..msg,'\n')
|
2011-09-17 17:57:22 +02:00
|
|
|
end
|
|
|
|
}
|
2011-07-10 19:10:53 +02:00
|
|
|
local t,val = tok()
|
2011-07-11 09:34:18 +02:00
|
|
|
if not t then return nil,"empty file" end
|
2011-07-10 19:10:53 +02:00
|
|
|
while t do
|
|
|
|
val = escape(val)
|
2011-09-19 13:32:30 +02:00
|
|
|
if globals.functions[val] or globals.tables[val] then
|
|
|
|
t = 'global'
|
|
|
|
end
|
2011-07-10 19:10:53 +02:00
|
|
|
if spans[t] then
|
2011-07-11 15:40:44 +02:00
|
|
|
if t == 'comment' then -- may contain @{ref}
|
2011-09-17 17:57:22 +02:00
|
|
|
val = prettify.resolve_inline_references(val,error_reporter)
|
2011-07-11 15:40:44 +02:00
|
|
|
end
|
2011-07-10 19:10:53 +02:00
|
|
|
res:append(span(t,val))
|
|
|
|
else
|
|
|
|
res:append(val)
|
|
|
|
end
|
|
|
|
t,val = tok()
|
|
|
|
end
|
2012-03-19 12:09:01 +01:00
|
|
|
local last = res[#res]
|
|
|
|
if last:match '\n$' then
|
|
|
|
res[#res] = last:gsub('\n+','')
|
|
|
|
end
|
2013-03-06 16:07:28 +01:00
|
|
|
if pre then
|
|
|
|
res:append '<pre/>\n'
|
|
|
|
end
|
2011-07-10 19:10:53 +02:00
|
|
|
return res:join ()
|
|
|
|
end
|
|
|
|
|
2013-03-06 16:07:28 +01:00
|
|
|
local lxsh
|
|
|
|
|
|
|
|
local lxsh_highlighers = {bib=true,c=true,lua=true,sh=true}
|
|
|
|
|
|
|
|
function prettify.code (lang,fname,code,initial_lineno,pre)
|
|
|
|
if not lxsh then
|
|
|
|
return prettify.lua (fname, code, initial_lineno, pre)
|
|
|
|
else
|
|
|
|
if not lxsh_highlighers[lang] then
|
|
|
|
lang = 'lua'
|
|
|
|
end
|
|
|
|
code = lxsh.highlighters[lang](code, {
|
|
|
|
formatter = lxsh.formatters.html,
|
|
|
|
external = true
|
|
|
|
})
|
|
|
|
if not pre then
|
|
|
|
code = code:gsub("^<pre*.->(.*)</pre>$", '%1')
|
|
|
|
end
|
|
|
|
return code
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function prettify.set_prettifier (pretty)
|
|
|
|
local ok
|
|
|
|
if pretty == 'lxsh' then
|
|
|
|
ok,lxsh = pcall(require,'lxsh')
|
|
|
|
if not ok then
|
|
|
|
print('pretty: '..pretty..' not found, using built-in Lua')
|
|
|
|
lxsh = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-07-10 19:10:53 +02:00
|
|
|
return prettify
|
2011-07-11 15:40:44 +02:00
|
|
|
|