forked from LuaDevelopmentTools/luadocumentor
-
Notifications
You must be signed in to change notification settings - Fork 2
/
templateengine.lua
132 lines (119 loc) · 3.92 KB
/
templateengine.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
--------------------------------------------------------------------------------
-- Copyright (c) 2011-2012 Sierra Wireless.
-- All rights reserved. This program and the accompanying materials
-- are made available under the terms of the Eclipse Public License v1.0
-- which accompanies this distribution, and is available at
-- http://www.eclipse.org/legal/epl-v10.html
--
-- Contributors:
-- Kevin KIN-FOO <[email protected]>
-- - initial API and implementation and initial documentation
--------------------------------------------------------------------------------
---
-- This library provide html description of elements from the externalapi
local M = {}
-- Load template engine
local pltemplate = require 'pl.template'
-- Markdown handling
local markdown = require 'markdown'
-- apply template to the given element
function M.applytemplate(elem, ident, templatetype,...)
-- define environment
local env = M.getenv(elem, ident,...)
-- load template
local template = M.gettemplate(elem,templatetype)
if not template then
templatetype = templatetype and string.format(' "%s"', templatetype) or ''
local elementname = string.format(' for %s', elem.tag or 'untagged element')
error(string.format('Unable to load %s template %s', templatetype, elementname))
end
-- apply template
local str, err = pltemplate.substitute(template, env)
--manage errors
if not str then
local templateerror = templatetype and string.format(' parsing "%s" template ', templatetype) or ''
error(string.format('An error occured%s for "%s"\n%s',templateerror, elem.tag, err))
end
return str
end
-- get the a new environment for this element
function M.getenv(elem, ident,...)
local currentenv ={}
for k,v in pairs(M.env) do currentenv[k] = v end
if elem and elem.tag then
currentenv['_'..elem.tag]= elem
end
currentenv['i']= ident or 1
currentenv['templateparams']= {...}
return currentenv
end
-- get the template for this element
function M.gettemplate(elem,templatetype)
local tag = elem and elem.tag
if tag then
if templatetype then
return require ("template." .. templatetype.. "." .. tag)
else
return require ("template." .. tag)
end
end
end
---
-- Allow user to format text in descriptions.
-- Default implementation replaces @{---} tags with links and apply markdown.
-- @return #string
local function format(string)
-- Allow to replace encountered tags with valid links
local replace = function(found)
local apiobj = M.env.getelement(found)
if apiobj then
return M.env.fulllinkto(apiobj)
end
return found
end
string = string:gsub('@{%s*(.-)%s*}', replace)
return M.env.markdown( string )
end
---
-- Provide a full link to an element using `purename` and `linkto`.
-- The purename is not escaped.
-- Default implementation is for HTML.
local function purelinkto(o,...)
local ref = M.env.linkto(o,...)
local name = M.env.purename(o,...)
if not ref then
return name
end
return string.format('<a href="%s">%s</a>', ref, name)
end
---
-- Provide a full link to an element using `prettyname` and `linkto`.
-- Default implementation is for HTML.
local function fulllinkto(o,...)
local ref = M.env.linkto(o,...)
local name = M.env.prettyname(o,...)
if not ref then
return name
end
return string.format('<a href="%s">%s</a>', ref, name)
end
--
-- Define default template environnement
--
local defaultenv = {
table = table,
ipairs = ipairs,
pairs = pairs,
markdown = markdown,
applytemplate = M.applytemplate,
format = format,
linkto = function(str) return str end,
purelinkto = purelinkto,
fulllinkto = fulllinkto,
prettyname = function(s) return s end,
getelement = function(s) return nil end
}
-- this is the global env accessible in the templates
-- env should be redefine by docgenerator user to add functions or redefine it.
M.env = defaultenv
return M