This repository was archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathuser_guide_builder.livecodescript
More file actions
318 lines (251 loc) · 8.92 KB
/
user_guide_builder.livecodescript
File metadata and controls
318 lines (251 loc) · 8.92 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
script "UserGuideBuilder"
local sMarkdownText
local sVersion
local sOutputPath
local sEdition
private command Initialize pEdition, pVersion, pOutputPath
if pOutputPath is empty then
throw "Guide output path must be specified"
exit Initialize
end if
set the defaultfolder to builderRepoFolder()
set the hideconsolewindows to true
put empty into sMarkdownText
put pVersion into sVersion
put pOutputPath into sOutputPath
put pEdition into sEdition
end Initialize
private command Finalize
end Finalize
command guideBuilderRun pEdition, pVersion, pReleaseType, pOutputDir
local tError
builderLog "report", "Building user guide for version" && pVersion
try
Initialize pEdition, pVersion, pOutputDir
BaseCreate
OutputGuide
Finalize
catch tError
builderLog "error", tError
end try
end guideBuilderRun
----------------------------------------------------------------
-- Output
----------------------------------------------------------------
private function OutputGetGuideFilename tSuffix, pVersion
if pVersion is empty then
put sVersion into pVersion
end if
local tBasename
put merge("LiveCodeUserGuide-") & replaceText(pVersion, "[-,\.]", "_") into tBasename
return sOutputPath & slash & tBasename & "." & tSuffix
end OutputGetGuideFilename
private function OutputGetGuideTitle
return merge("LiveCode [[sVersion]] User Guide")
end OutputGetGuideTitle
private command OutputGuide
OutputGuideJson
OutputGuideHtml
OutputGuidePdf
end OutputGuide
private function OutputEscape pMarkdown, pEscapeHtml
-- Try to do the minimum possible escaping to be able to insert the
-- markdown into a JavaScript string
replace "\" with "\\" in pMarkdown
replace "'" with "\'" in pMarkdown
replace return with "\n" in pMarkdown
if pEscapeHtml then
replace "<" with "<" in pMarkdown
replace ">" with ">" in pMarkdown
end if
return pMarkdown
end OutputEscape
private command OutputGuideJson
local tGuideJsonPath
put builderGuideFolder(sEdition) & slash & "distributed_guide.js" into tGuideJsonPath
local tGuideJson
put FileGetUTF8Contents(tGuideJsonPath) into tGuideJson
local tJS
put "var tUserGuideData =" & return & "{" & return & tab & quote & "guides" & quote & ":[" into tJS
put tGuideJson after tJS
put return & tab & "]" & return & "}" after tJS
local tGuideJSPath
put OutputGetGuideFilename("js") into tGuideJSPath
FileSetUTF8Contents tGuideJSPath, tJS
end OutputGuideJson
private command OutputGuideHtml
local tHtmlTemplatePath
put builderSystemFolder() & "/user-guide-template.html" into tHtmlTemplatePath
local tHtml
put FileGetUTF8Contents(tHtmlTemplatePath) into tHtml
replace "@PREFIX@" with OutputEscape(sMarkdownText["guide"], false) in tHtml
replace "@GUIDE_JSON@" with OutputGetGuideFilename("js") in tHtml
local tOutpath
put OutputGetGuideFilename("html") into tOutpath
builderLog "report", merge("Writing [[tOutpath]]")
FileSetUTF8Contents tOutpath, tHtml
end OutputGuideHtml
private command OutputGuidePdf
local tCommand
-- Use wkhtmltopdf to convert the HTML representation
if $WKHTMLTOPDF is not empty then
put $WKHTMLTOPDF into tCommand
else
put builderRepoFolder() & "/builder/wkhtmltopdf" into tCommand
end if
local tTitle, tDate, tHtmlPath, tPdfPath
put OutputGetGuideTitle() into tTitle
put the date into tDate
put OutputGetGuideFilename("html") into tHtmlPath
put OutputGetGuideFilename("pdf") into tPdfPath
local tArgs
put empty into tArgs
put merge("--header-right '[[tTitle]] [[tDate]]' ") after tArgs
put "--header-font-size 8 --header-spacing 5 " after tArgs
put "--footer-center [page] --footer-font-size 8 --footer-spacing 5 " after tArgs
put "--margin-top 30 --margin-bottom 20 --margin-left 20 --margin-right 20 " after tArgs
put "--enable-internal-links --encoding UTF-8 " after tArgs
local tExitCode
builderLog "report", merge("Generating [[tPdfPath]] with [[tCommand]]")
get shell(tCommand && tArgs && tHtmlPath && tPdfPath)
put the result into tExitCode
if tExitCode is not 0 then
throw merge("Failed to run [[tCommand]]: exit code [[tExitCode]]")
end if
end OutputGuidePdf
----------------------------------------------------------------
-- Base file sections
----------------------------------------------------------------
private command BaseCreateTitle
MarkdownAppend "guide", "<h1 class=" & quote & "title" & quote & ">" & OutputGetGuideTitle() & "</h1>" & return
end BaseCreateTitle
private command BaseCreateContents
MarkdownAppend "guide", "[TOC]"
end BaseCreateContents
private command BaseCreate
builderLog "report", "Creating base user guide"
BaseCreateTitle
BaseCreateContents
end BaseCreate
----------------------------------------------------------------
-- File helpers
----------------------------------------------------------------
private function FolderListFiles pFolder, pExtension
local tDefaultFolder
put the defaultFolder into tDefaultFolder
local tFiles
set the defaultFolder to pFolder
put the files into tFiles
if pExtension is not empty then
filter tFiles with "*." & pExtension
end if
set the defaultFolder to tDefaultFolder
return tFiles
end FolderListFiles
private function FileGetBasename pPath
set the itemdelimiter to slash
put item -1 of pPath into pPath
set the itemdelimiter to "."
if 1 < the number of items in pPath then
put item 1 to -2 of pPath into pPath
end if
return pPath
end FileGetBasename
private function FileGetPath pType
switch pType
case "ide-guides"
return builderIDEFolder() & "/Documentation/guides"
case "repo-guides"
return builderRepoFolder() & "/docs/guides"
default
return pType
end switch
end FileGetPath
private function FileGetUTF8Contents pPath
return FileGetContents(pPath, "UTF-8")
end FileGetUTF8Contents
private function FileGetContents pPath, pEncoding
local tContents, tResult
if pEncoding is empty then
open file pPath for binary read
else
open file pPath for pEncoding text read
end if
put the result into tResult
if tResult is not empty then
throw merge("Failed to open [[pPath]]: [[tResult]]")
end if
read from file pPath until end
put the result into tResult
if tResult is not empty and tResult is not "eof" then
throw merge("Failed to read [[pPath]]: [[tResult]]")
end if
put it into tContents
close file pPath -- unchecked
return tContents
end FileGetContents
private command FileSetUTF8Contents pPath, pContents
local tResult
open file pPath for "UTF-8" text write
put the result into tResult
if tResult is not empty then
throw merge("Failed to open [[pPath]]: [[tResult]]")
end if
write pContents to file pPath
put the result into tResult
if tResult is not empty then
throw merge("Failed to write [[pPath]]: [[tResult]]")
end if
close file pPath -- unchecked
end FileSetUTF8Contents
----------------------------------------------------------------
-- Markdown helpers
----------------------------------------------------------------
private command MarkdownAppend pKey, pText
put pText & return after sMarkdownText[pKey]
end MarkdownAppend
/*
Markdown files can have a YAML-like metadata block at the start.
The format is very inflexible. The block is delimited by lines
of exactly three hyphens "---", and every line in between *must*
be blank or contain a "key: value" pair. For example:
---
# Comment
version: 8.0.0-dp-9
---
*/
private command MarkdownSplitMetadata pText, @rMarkdown, @rMetadata
-- Check for a metadata block
if the first line of pText is not "---" then
put empty into rMetadata
put pText into rMarkdown
exit MarkdownSplitMetadata
end if
-- Search for a terminal "---" line
local tMetadataEnd
put lineOffset("---", pText, 1) into tMetadataEnd
if tMetadataEnd is 1 then
throw "Unterminated metadata header"
end if
local tLine
set the itemdelimiter to colon
repeat for each line tLine in (line 2 to tMetadataEnd of pText)
put word 1 to -1 of tLine into tLine
if item 1 of tLine is empty or char 1 of tLine is "#" then
next repeat
else
put word 1 to -1 of (item 2 to -1 of tLine) into rMetadata[item 1 of tLine]
end if
end repeat
put line (tMetadataEnd + 2) to -1 of pText into rMarkdown
end MarkdownSplitMetadata
----------------------------------------------------------------
-- HTML helper functions
----------------------------------------------------------------
private function HtmlEscape pText
replace "&" with "&" in pText
replace "<" with "<" in pText
replace ">" with ">" in pText
replace quote with """ in pText
return ptext
end HtmlEscape