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'
|
2014-12-15 09:02:33 +01:00
|
|
|
local tablex = require 'pl.tablex'
|
2011-09-20 12:59:34 +02:00
|
|
|
local globals = require 'ldoc.builtin.globals'
|
2011-07-10 19:10:53 +02:00
|
|
|
local prettify = {}
|
|
|
|
|
2014-12-15 09:02:33 +01:00
|
|
|
local user_keywords = {}
|
|
|
|
|
2011-07-10 19:10:53 +02:00
|
|
|
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
|
|
|
|
|
2013-05-09 12:52:15 +02:00
|
|
|
local spans = {keyword=true,number=true,string=true,comment=true,global=true,backtick=true}
|
2011-07-10 19:10:53 +02:00
|
|
|
|
2014-11-01 15:45:08 +01:00
|
|
|
local cpp_lang = {C = true, c = true, cpp = true, cxx = true, h = true}
|
2013-08-25 14:29:30 +02:00
|
|
|
|
2014-10-12 16:09:09 +02:00
|
|
|
function prettify.lua (lang, fname, code, initial_lineno, pre, linenos)
|
2020-09-30 20:45:22 +02:00
|
|
|
local res, lexer = List(), require 'ldoc.lexer'
|
|
|
|
local tokenizer
|
2014-10-13 19:38:11 +02:00
|
|
|
local ik = 1
|
2013-08-25 14:29:30 +02:00
|
|
|
if not cpp_lang[lang] then
|
|
|
|
tokenizer = lexer.lua
|
|
|
|
else
|
|
|
|
tokenizer = lexer.cpp
|
|
|
|
end
|
|
|
|
|
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
|
|
|
|
2013-08-25 14:29:30 +02:00
|
|
|
local tok = tokenizer(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
|
|
|
|
}
|
2016-06-27 15:27:15 +02:00
|
|
|
local last_t, last_val
|
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)
|
2014-10-12 16:09:09 +02:00
|
|
|
if linenos and tok:lineno() == linenos[ik] then
|
2014-10-13 19:38:11 +02:00
|
|
|
res:append('<a id="'..linenos[ik]..'"></a>')
|
2014-10-12 16:09:09 +02:00
|
|
|
ik = ik + 1
|
|
|
|
end
|
2011-09-19 13:32:30 +02:00
|
|
|
if globals.functions[val] or globals.tables[val] then
|
|
|
|
t = 'global'
|
|
|
|
end
|
2014-12-15 09:02:33 +01:00
|
|
|
if user_keywords[val] then
|
|
|
|
res:append(span('user-keyword keyword-' .. val,val))
|
|
|
|
elseif spans[t] then
|
2013-05-09 12:52:15 +02:00
|
|
|
if t == 'comment' or t == 'backtick' then -- may contain @{ref} or `..`
|
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
|
2016-06-27 15:27:15 +02:00
|
|
|
last_t, last_val = t,val
|
2011-07-10 19:10:53 +02:00
|
|
|
t,val = tok()
|
|
|
|
end
|
2016-06-27 15:27:15 +02:00
|
|
|
if last_t == 'comment' then
|
|
|
|
res[#res] = span('comment',last_val:gsub('\r*\n$',''))
|
|
|
|
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
|
2013-03-07 12:09:48 +01:00
|
|
|
res:append '</pre>\n'
|
2013-03-06 16:07:28 +01:00
|
|
|
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
|
2013-08-25 14:29:30 +02:00
|
|
|
return prettify.lua (lang,fname, code, initial_lineno, pre)
|
2013-03-06 16:07:28 +01:00
|
|
|
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
|
2013-12-12 16:26:38 +01:00
|
|
|
code = code:gsub("^<pre.->(.-)%s*</pre>$", '%1')
|
2013-03-06 16:07:28 +01:00
|
|
|
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
|
|
|
|
|
2014-12-15 09:02:33 +01:00
|
|
|
function prettify.set_user_keywords(keywords)
|
|
|
|
if keywords then
|
|
|
|
user_keywords = tablex.makeset(keywords)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-07-10 19:10:53 +02:00
|
|
|
return prettify
|
2011-07-11 15:40:44 +02:00
|
|
|
|