-
Notifications
You must be signed in to change notification settings - Fork 30
/
rundoc.ado
executable file
·329 lines (273 loc) · 10.9 KB
/
rundoc.ado
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
319
320
321
322
323
324
325
326
327
328
329
// CREATES A LOG AND CALLS MARKDOC!
*capture prog drop rundoc
program define rundoc
syntax [anything(name=dofile id="The do file name is")] ///
[, ///
replace /// replaces the exported file
mini /// run in mini mode, independent of Pandoc and wkhtmltopdf
MARKup(name) /// specifies the markup language used in the document
Export(name) /// specifies the exported format
saving(str) /// UNDOCUMENTED
INSTALl /// Installs the required software automatically
Test /// tests the required software to make sure they're running correctly
PANdoc(str) /// specifies the path to Pandoc software on the machine
PRINTer(str) /// the path to the PDF printer on the machine
TEXmaster /// creates a "Main" LaTeX file which is executable
master /// creates a "Main" LaTeX & HTML layout
statax /// Activate the syntax highlighter
TEMPlate(str) /// template docx, CSS, ODT, or LaTeX heading
TITle(str) /// specifies the title of the document (for styling)
AUthor(str) /// specifies the author of mthe document (for styling)
AFFiliation(str) /// specifies author affiliation (for styling)
ADDress(str) /// specifies author contact information (for styling)
Date /// Add the current date to the document
SUMmary(str) /// writing the summary or abstract of the report
VERsion(str) /// add version to dynamic help file
linesize(numlist max=1 int >=80 <=255) /// line size of the document and translator
toc /// Creates table of content
NOIsily /// Debugging Pandoc, pdfLaTeX, and wkhtmltopdf
debug /// Debugging Pandoc, pdfLaTeX, and wkhtmltopdf
ASCIItable /// convert ASCII tables to SMCL in dynamic help files
NUMbered /// number Stata commands
MATHjax /// Interprets mathematics using MathJax
STYle(name) /// specifies the style of the document
suppress /// UNDOCUMENTER, avoids unnecessary warnings
/// Slide options
/// ========================================================================
btheme(str) ///
bcolor(str) ///
bfont(str) ///
bfontsize(str) ///
bcodesize(str) ///
bwidth(str) ///
bheight(str) ///
///SETpath(str) /// the path to the PDF printer on the machine
///Printer(name) /// the printer name (for PDF only)
///TABle /// changes the formats of the table and creates publication ready tables (UNDER DEVELOPMENT AND UNDOCUMENTED)
///RUNhead(str) /// running head for the document (for styling)
///PDFlatex(str) /// this command is discontinued in version 3.0 and replaced by setpath()
///Font(name) /// specifies the document font (ONLY HTML)
///
]
// -------------------------------------------------------------------------
// NOTE:
// Stata 14 introduces ustrltrim() function for removing Unicode whitespace
// characters and blanks. The previous trim() function cannot remove unicode
// whitespace. The program is updated to function for all versions of Stata,
// but yet, there is a slight chance of "unreliable" behavior from MarkDoc
// in older versions of Stata, if the string has a unicode whitespace. This
// can be fixed by finding a solution to avoid "ustrltrim()". Yet, most of
// the torture tests have been positive.
// =========================================================================
local version = int(`c(stata_version)')
if `version' <= 13 {
local trim trim
local version 11
}
if `version' > 13 {
local trim ustrltrim
local version 14
}
version `version'
capture quietly display `dofile'
if _rc == 0 {
local input `dofile'
}
else {
local input "`dofile'"
}
// Avoid Markdoc to re-execute itself in a do-file
if !missing(`"$currentmarkdocdofile"') {
if `"$currentmarkdocdofile"' == `"`dofile'"' {
exit
}
}
else {
global currentmarkdocdofile "`dofile'"
}
// -------------------------------------------------------------------------
// 1-
// 2- create a log file with a name
// 3- execute the do-file
// 4- close the log, restore the snapshot, erase the snapshot
// 5- convert the log file and erase it
// =========================================================================
quietly snapshot save
local number `r(snapshot)'
quietly clear
capture log close rundoc
quietly log using "`input'.smcl", replace smcl name(rundoc)
tempfile tmp
tempname hitch knot
qui file open `knot' using "`tmp'", write replace
qui file open `hitch' using "`input'.do", read
qui file read `hitch' line
if !missing("`debug'") di "{title:Rundoc 1}"
while r(eof) == 0 {
while `trim'(`"`macval(line)'"') != "/***" & r(eof) == 0 {
//fix the problem of graveaccent in the end of the line
capture if substr("`macval(line)'",-1,.) == "`" local graveaccent 1
else local graveaccent ""
if "`graveaccent'" == "1" {
local line `"`macval(line)' "'
local graveaccent ""
}
file write `knot' `"`macval(line)'"' _n
file read `hitch' line
}
// -------------------------------------------------------------------
// If there is no documentation just append the documentation
// ===================================================================
if `trim'(`"`macval(line)'"') == "/***" {
tempfile documentation
tempname doc
qui file open `doc' using "`documentation'", write replace
file write `doc' "/***" _n
tempfile display
tempname disp
qui file open `disp' using "`display'", write replace
file write `disp' `"/**/ di as txt "> " _n ///"' _n
local activate //RESET
local found //RESET
file read `hitch' line
//fix the problem of graveaccent in the end of the line
capture if substr("`macval(line)'",-1,.) == "`" local graveaccent 1
else local graveaccent ""
if "`graveaccent'" == "1" {
local line `"`macval(line)' "'
local graveaccent ""
}
while `"`macval(line)'"' != "***/" & r(eof) == 0 {
if !missing("`found'") file write `disp' " _n ///" _n
local found //RESET
while strpos(`"`macval(line)'"', "!>") > 0 {
local activate 1
*file write `disp' " _n ///" _n
local start = strpos(`"`macval(line)'"', "<!")
local end = strpos(`"`macval(line)'"', "!>")
local l = `end' - `start'
local part = substr(`"`macval(line)'"', 1, `start'-1)
// SECURE PART
// --------------------------------------------------------
local part : subinstr local part "`" "{c 96}", all
*local part : subinstr local part "` " "\\{c 96} ", all
*local part : subinstr local part "`" "{c 96}", all
*local part : subinstr local part " $" " \\$", all
*local part : subinstr local part " \\$$" " $$", all
//write text parts
if missing("`found'") file write `disp' "`" `""> `macval(part)'""' "'"
else file write `disp' "`" `""`macval(part)'""' "'"
local val = substr(`"`macval(line)'"', `start'+2, `l'-2)
*file write `disp' `" %10.2f `macval(val)'"'
file write `disp' `" `macval(val)' "'
local line = substr(`"`macval(line)'"', `end'+2, .)
// avoid "> " for next elements
local found 1
}
if !missing("`found'") & `trim'(`"`macval(line)'"') != "" {
// SECURE PART
// --------------------------------------------------------
local line : subinstr local line "`" "{c 96}", all
*local line : subinstr local line " $" " \\$", all
*local line : subinstr local line " \\$$" " $$", all
file write `disp' `" "`macval(line)'""'
}
if missing("`found'") {
// SECURE LINE
// --------------------------------------------------------
local line : subinstr local line "`" "{c 96}", all
*local line : subinstr local line "$" " \\$", all //{c 36}
*local line : subinstr local line " \\$$" " $$", all
file write `disp' "`" `""> `macval(line)'""' "'" " _n ///" _n
}
if missing("`activate'") {
file write `doc' `"`macval(line)'"' _n
}
file read `hitch' line
//fix the problem of graveaccent in the end of the line
capture if substr("`macval(line)'",-1,.) == "`" local graveaccent 1
else local graveaccent ""
if "`graveaccent'" == "1" {
local line `"`macval(line)' "'
local graveaccent ""
}
}
// Add an empty line in the `disp' file
file write `disp' _n
if `"`macval(line)'"' == "***/" file write `doc' "***/" _n
file close `doc'
file close `disp'
tempname add
if missing("`activate'") {
file open `add' using "`documentation'", read
file read `add' docline
while r(eof) == 0 {
file write `knot' `"`macval(docline)'"' _n
file read `add' docline
}
file close `add'
cap erase "`documentation'"
}
else {
file open `add' using "`display'", read
file read `add' docline
while r(eof) == 0 {
file write `knot' `"`macval(docline)'"' _n
file read `add' docline
}
file close `add'
cap erase "`display'"
}
file read `hitch' line
}
}
file close `knot'
if !missing("`debug'") {
copy "`tmp'" "___code.txt", replace
}
noisily do "`tmp'"
cap file close `hitch'
*capture noisily do "`input'"
qui log off rundoc
snapshot restore `number'
capture snapshot erase `number'
markdoc "`input'.smcl", ///
`mini' ///
`replace' ///
markup(`markup') ///
export(`export') ///
saving(`saving') ///
`install' ///
`test' ///
pandoc("`pandoc'") ///
printer("`printer'") ///
`master' ///
`statax' ///
template(`template') ///
title("`title'") ///
author("`author'") ///
affiliation("`affiliation'") ///
address("`address'") ///
`date' ///
summary("`summary'") ///
version("`version'") ///
style("`style'") ///
linesize(`linesize') ///
`toc' ///
`noisily' ///
`debug' ///
`suppress' ///
`asciitable' ///
`numbered' ///
`mathjax' ///
btheme(`btheme') ///
bcolor(`bcolor') ///
bfont(`bfont') ///
bfontsize(`bfontsize') ///
bcodesize(`bcodesize') ///
bwidth(`bwidth') ///
bheight(`bheight')
capture qui log close rundoc
capture quietly erase "`input'.smcl"
macro drop currentmarkdocdofile
end