Can prettify C files as well as Lua with built-in prettifier

This commit is contained in:
steve donovan 2013-08-25 14:29:30 +02:00
parent 89cb20d752
commit ad909d683b
2 changed files with 16 additions and 8 deletions

View File

@ -7,7 +7,7 @@
--
-- C/C++ support for Lua extensions is provided.
--
-- Available from LuaRocks as 'ldoc' and as a [Zip file](http://stevedonovan.github.com/files/ldoc-1.3.9.zip)
-- Available from LuaRocks as 'ldoc' and as a [Zip file](http://stevedonovan.github.com/files/ldoc-1.4.0.zip)
--
-- [Github Page](https://github.com/stevedonovan/ldoc)
--
@ -475,7 +475,8 @@ if type(ldoc.examples) == 'table' then
})
-- wrap prettify for this example so it knows which file to blame
-- if there's a problem
item.postprocess = function(code) return prettify.lua(f,code,0,true) end
local ext = path.extension(f):sub(2)
item.postprocess = function(code) return prettify.lua(ext,f,code,0,true) end
end)
end

View File

@ -4,9 +4,7 @@
-- A module reference to an example `test-fun.lua` would look like
-- `@{example:test-fun}`.
local List = require 'pl.List'
local lexer = require 'ldoc.lexer'
local globals = require 'ldoc.builtin.globals'
local tnext = lexer.skipws
local prettify = {}
local escaped_chars = {
@ -26,14 +24,23 @@ end
local spans = {keyword=true,number=true,string=true,comment=true,global=true,backtick=true}
function prettify.lua (fname, code, initial_lineno, pre)
local res = List()
local cpp_lang = {c = true, cpp = true, cxx = true, h = true}
function prettify.lua (lang, fname, code, initial_lineno, pre)
local res, lexer, tokenizer = List(), require 'ldoc.lexer'
local tnext = lexer.skipws
if not cpp_lang[lang] then
tokenizer = lexer.lua
else
tokenizer = lexer.cpp
end
if pre then
res:append '<pre>\n'
end
initial_lineno = initial_lineno or 0
local tok = lexer.lua(code,{},{})
local tok = tokenizer(code,{},{})
local error_reporter = {
warning = function (self,msg)
io.stderr:write(fname..':'..tok:lineno()+initial_lineno..': '..msg,'\n')
@ -72,7 +79,7 @@ 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)
return prettify.lua (lang,fname, code, initial_lineno, pre)
else
if not lxsh_highlighers[lang] then
lang = 'lua'