-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstmd.ado
128 lines (113 loc) · 3 KB
/
stmd.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
*! version 1.7.1
*! Doug Hemken
*! 2 Oct 2019
// ISSUES
// ======
// revise dialogs
// capture program drop stmd
program define stmd, rclass
syntax anything(everything) /// input file name, plus arguments
[, ///
SAVing(string) REPLace /// name of HTML file
noREMove ///
embedimage /// requires Stata 16
hardwrap ///
nomsg ///
nostop ///
pegdown ///
docx /// requires Stata 16
flexdocx /// requires Stata 16
pdf /// requires Stata 16
]
version 15
* version checks
if (c(stata_version) < 16) {
if ("`docx'"=="docx" | "`flexdocx'"=="flexdocx" | "`pdf'"=="pdf") {
display in error "option {cmd:`docx' `flexdocx' `pdf'} requires Stata 16"
display "try {cmd:dynpandoc} if installed"
exit 9
}
else if ("`embedimage'"=="embedimage") {
display in error "option {cmd:embedimage} requires Stata 16"
display "option ignored"
local embedimage ""
}
}
gettoken infile opargs : anything
// display `"source file: `infile'"'
* infile checks
local infile = ustrtrim(usubinstr(`"`infile'"', `"""', "", .))
confirm file `"`infile'"'
* outfile checks
local saving = ustrtrim(`"`saving'"')
if (`'"`saving'"' == "" ) {
if ("`docx'" != "" | "`flexdocx'" != "") {
mata:(void)pathchangesuffix("`infile'", "docx", "saving", 0)
}
else if ("`pdf'" != "") {
mata:(void)pathchangesuffix("`infile'", "pdf", "saving", 0)
}
else {
mata:(void)pathchangesuffix("`infile'", "html", "saving", 0)
}
}
else {
mata:_getfileextension(`"`saving'"', "targetext")
if (`"`targetext'"' == "docx") {
local docx = "docx"
}
else if (`"`targetext'"' == "pdf") {
local pdf = "pdf"
}
}
mata: (void)pathresolve("`c(pwd)'", `"`saving'"', "saving")
local issame = 0
mata: (void)filesarethesame("`infile'", "`saving'", "issame")
if ("`issame'" == "1") {
display in error "target file can not be the same as the source file"
exit 602
}
if ("`replace'"=="") {
confirm new file "`saving'"
}
// display `"target file: `saving' "'
* intermediate dyndoc file
tempfile dyn
* intermediate docx file
if ("`pdf'" != "") {
tempfile docxfile
}
* process
stmd2dyn "`infile'", saving(`dyn') `replace' `msg'
if ("`pdf'" == "") {
dyndoc `"`dyn'"' `opargs', saving(`"`saving'"') `replace' `remove' ///
`embedimage' ///
`hardwrap' `msg' `stop' `pegdown' `docx' `flexdocx'
}
else {
// di `'"intermediate docx: `docxfile'"'
dyndoc `"`dyn'"' `opargs', saving(`"`docxfile'"') `replace' `remove' ///
`embedimage' ///
`hardwrap' nomsg `stop' `pegdown' docx `flexdocx'
if ("`msg'" != "nomsg") {
display " {text:docx file saved as {it:`docxfile'}}"
}
docx2pdf `"`docxfile'"', saving(`"`saving'"') `replace' `msg'
}
end
mata:
void _getfileextension(string scalar path,
string scalar loc)
{
string scalar p
p = strtrim(strlower(pathsuffix(path)))
st_local(loc, p)
}
void _getpathparent( string scalar path,
string scalar loc)
{
string scalar p
p = pathgetparent(path)
st_local(loc, p)
}
end