forked from lunarmodules/ldoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchange.txt
175 lines (173 loc) · 5.69 KB
/
change.txt
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
diff --git a/ldoc/parse.lua b/ldoc/parse.lua
index 5bebc3a..28102f1 100644
--- a/ldoc/parse.lua
+++ b/ldoc/parse.lua
@@ -6,6 +6,21 @@ local tools = require 'ldoc.tools'
local doc = require 'ldoc.doc'
local Item,File = doc.Item,doc.File
+-- This functionality is only needed for UML support.
+-- If this does not load it will only trigger a failure
+-- if the UML syntax was detected.
+local bOk, http = pcall( require, "socket.http")
+local mime = nil
+if bOk == false then
+ http = nil
+else
+ bOk, mime = pcall( require, "mime")
+ if bOk == false then
+ mime = nil
+ end
+end
+
+
------ Parsing the Source --------------
-- This uses the lexer from PL, but it should be possible to use Peter Odding's
-- excellent Lpeg based lexer instead.
@@ -46,6 +61,138 @@ function parse_tags(text)
return preamble,tag_items
end
+-- Used to preprocess the tag text prior to extracting
+-- tags. This allows us to replace tags with other text.
+-- For example, we embed images into the document.
+local function preprocess_tag_strings( s )
+
+ local function create_embedded_image( filename, fileType )
+
+ local html = ""
+
+ if mime == nil then
+ local errStr = "LDoc error, Lua socket/mime module needed for UML"
+ -- Just log the error in the doc
+ html = "<b><u>"..errStr.."</u></b>"
+ print(errStr)
+ return html
+ end
+
+ if fileType == nil then
+ fileType = "png"
+ end
+
+ -- Now open the new image file and embed it
+ -- into the text as an HTML image
+ local fp = io.open( filename, "r" )
+ if fp then
+ -- This could be more efficient instead of
+ -- reading all since the definitions are
+ -- typically small this will work for now
+ local img = fp:read("*all")
+ fp:close()
+
+ html = string.format( '<img src="data:image/%s;base64,%s" />', fileType, mime.b64( img ) )
+ else
+ local errStr = string.format("LDoc error opening %s image file: %q", fileType, filename)
+ -- Just log the error in the doc
+ html = "<br><br><b><u>"..errStr.."</u></b><br><br>"
+ print(errStr)
+ end
+
+ return html
+ end
+
+ ----------------------------------------------------------
+ -- Embedded UML
+ ------------------
+ local epos
+ local execPath = "plantuml %s"
+ local spos = string.find(s, "@startuml")
+ if spos then
+ _, epos = string.find(s, "@enduml")
+ end
+
+ if spos and epos then
+
+ local filename = os.tmpname()
+ local sUml = string.sub(s,spos,epos) -- UML definition text
+
+ -- Grab the text before and after the UML definition
+ local preStr = string.match(s, "(.*)@startuml")
+ local postStr = string.match(s, "@enduml(.*)")
+ local fileType = "png"
+ local fp = io.open( filename, "w" )
+ local html = ""
+
+ --Add support for optional formatting in a json format
+ if string.sub( sUml, 10,10 ) == "{" then
+ local sFmt = string.match( sUml, ".*{(.*)}" )
+
+ -- Remove the formatter
+ sUml = string.gsub( sUml, ".*}", "@startuml" )
+
+ -- To avoid adding the dependency of JSON we will
+ -- parse what we need.
+
+ -- "exec":"path"
+ -- This allows you to alter the UML generation engine and path for execution
+ execPath = string.match(sFmt, '.-"exec"%s-:%s-"(.*)".-') or execPath
+
+ -- "removeTags":true
+ -- if true, the @startuml and @enduml are removed, this
+ -- makes it possible to support other UML parsers.
+ sRemoveTags = string.match(sFmt, '.-"removeTags"%s-:%s-(%a*).-')
+ if sRemoveTags == "true" then
+ sUml = string.gsub( sUml, "^%s*@startuml", "" )
+ sUml = string.gsub( sUml, "@enduml%s*$", "" )
+ end
+
+ -- "fileType":"gif"
+ -- This defines a different file type that is generated by
+ -- the UML parsers.
+ fileType = string.match(sFmt, '.-"fileType"%s-:%s-"(.*)".-') or fileType
+ end
+
+ if fp then
+ -- write the UML text to a file
+ fp:write( sUml )
+ fp:close()
+
+ -- create the diagram, overwrites the existing file
+ os.execute( string.format(execPath, filename ) )
+
+ -- create the embedded text for the image
+ html = create_embedded_image( filename, fileType )
+
+ os.remove( filename ) -- this is the PNG from plantUml
+ else
+ local errStr = "LDoc error creating UML temp file"
+ -- Just log the error in the doc
+ html = "<br><br><b><u>"..errStr.."</u></b><br><br>"
+ print(errStr)
+ end
+ s = preStr..html..postStr
+
+ end -- embed UML
+
+ ----------------------------------------------------------
+ -- Embedded Image
+ ------------------
+ local fileType, filename = string.match(s, '@embed_(.*){"(.*)"}')
+ if fileType and filename then
+
+ -- create the embedded text for the image
+ html = create_embedded_image( filename, fileType )
+
+ s = string.gsub(s, "@embed_.*{.*}", html)
+
+ end -- embedded image
+
+ return s
+
+end -- preprocess_tag_strings
+
-- This takes the collected comment block, and uses the docstyle to
-- extract tags and values. Assume that the summary ends in a period or a question
-- mark, and everything else in the preamble is the description.
@@ -53,6 +200,9 @@ end
-- Alias substitution and @TYPE NAME shortcutting is handled by Item.check_tag
local function extract_tags (s)
if s:match '^%s*$' then return {} end
+
+ s = preprocess_tag_strings( s )
+
local preamble,tag_items = parse_tags(s)
local strip = tools.strip
local summary, description = preamble:match('^(.-[%.?])(%s.+)')