Skip to content

Commit

Permalink
Finish Mata conversion
Browse files Browse the repository at this point in the history
rename files
  • Loading branch information
Hemken committed Mar 27, 2018
1 parent 75c8351 commit 1ebb7ab
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 152 deletions.
12 changes: 6 additions & 6 deletions README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ SUPPORT: [email protected]

HELP: After installation, type

. help md2dyn
. help md2html
. help stmd2dyn
. help stmd2html

FILES:

md2dyn.ado
md2dyn.sthlp
md2html.ado
md2html.sthlp
stmd2dyn.ado
stmd2dyn.sthlp
stmd2html.ado
stmd2html.sthlp
155 changes: 31 additions & 124 deletions md2dyn.ado → stmd2dyn.ado
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
*! version 1.5.4
*! Doug Hemken
*! 26 March 2018
*! 27 March 2018

// ISSUES
// ======
// better, more extensive preamble ?
// NOGRaph option
// wrapper with dyndoc, pandoc
// rewrite in Mata
// better, more extensive preamble, e.g. linesize, other options?
// NOGRaph option
// ignore executable code within non-executable fence

capture program drop md2dyn
capture program drop stmd2dyn
capture mata: mata clear
program define md2dyn, rclass
syntax anything(name=infile), [LINElength(integer 256)] ///
[SAVing(string) replace]
program define stmd2dyn, rclass
syntax anything(name=infile), [SAVing(string) replace]

local infile = ustrtrim(usubinstr(`"`infile'"', `"""', "", .))
*display `"infile is `infile'"'
Expand All @@ -37,131 +36,26 @@ display in error "target file can not be the same as the source file"
}

* Read in file

/* preserve
clear
quietly {
infix str doc_line 1-`linelength' using `"`infile'"'
compress doc_line
}
*/
mata: X=docread("`infile'")
// mata: st_sstore(., 1, X)

* Then identify code blocks
/*
quietly {
generate linenum = _n
* find codeblocks and dynamic codeblocks
local infofence = "^( ? ? ?)(```+|~~~+)\{?s(tata)?\/?(,.*)?\}?$"
generate codebegin = ustrregexm(doc_line, "`infofence'")
generate prespace = length(ustrregexs(1)) if ustrregexm(doc_line, "`infofence'")
generate fencel = length(ustrregexs(2)) if ustrregexm(doc_line, "`infofence'")
generate infoopts = ustrregexs(4) if ustrregexm(doc_line, "`infofence'")
replace codebegin = 0 if ustrregexm(infoopts, "eval=FALSE") & codebegin
generate codeopts = ustrregexm(infoopts, ",") if codebegin
generate noecho = ustrregexm(infoopts, "echo=FALSE") if codebegin & codeopts
replace noecho = 1 if ustrregexm(doc_line, "\/") & codebegin & codeopts
generate noresults = ustrregexm(infoopts, "results=FALSE") if codebegin & codeopts
generate noprompt = ustrregexm(infoopts, "noprompt=TRUE") if codebegin & codeopts
local codefence = "^( ? ? ?)(```+|~~~+)([ ]*)$"
generate codefence = ustrregexm(doc_line, "`codefence'") if ~codebegin
replace fencel = length(ustrregexs(2)) if ustrregexm(doc_line, "`codefence'")
* find the end of each codeblock
levelsof linenum if codebegin, local(cb)
foreach block of local cb {
di "begin at `block'"
summarize fencel in `block'
local fencel = r(mean)
levelsof linenum if codefence & (fencel >= `fencel') & (linenum > `block'), local(cbe)
local cbe : word 1 of `cbe'
di "end at `cbe'"
replace codebegin = -1 in `cbe'
}
*/
mata: fenceinfo = _fence_info(X)
mata: infotags = _info_tags(X)
mata: tagmatchs = _tag_match(infotags)
/*
* Add dd_do
expand 2 if codebegin ~= 0, generate(dup)
sort linenum dup
replace doc_line = "`"*fencel if codebegin==1 & dup==0
replace doc_line = "<<dd_do>>" if codebegin==1 & dup==1 & codeopts==0
replace doc_line = "<<dd_do: nocommands>>" if codebegin==1 & dup==1 & noecho==1
replace doc_line = "<<dd_do: nooutput>>" if codebegin==1 & dup==1 & noresults==1
replace doc_line = "<<dd_do: noprompt>>" if codebegin==1 & dup==1 & noprompt==1
replace doc_line = "<<dd_do: quietly>>" if codebegin==1 & dup==1 & noecho==1 & noresults==1
replace doc_line = "<</dd_do>>" if codebegin==-1 & dup==0
drop dup
replace linenum = _n
}
*/
mata: dotags = _dd_do(fenceinfo, tagmatchs)
/*
* Add graphics preamble
quietly {
expand 6 if linenum == 1, generate(duppre)
sort linenum duppre
by linenum duppre: generate preamble = _n
replace doc_line = "<<dd_do: quietly>>" if linenum==1 & preamble==1
replace doc_line = "capture graph describe Graph" if duppre==1 & preamble==1
replace doc_line = "tempname gdate" if duppre==1 & preamble==2
replace doc_line = `"local \`gdate' = "\`r(command_date)' \`r(command_time)'" "' if duppre==1 & preamble==3
replace doc_line = "<</dd_do>>" if duppre==1 & preamble==4
drop duppre
replace linenum = _n
}
* Add new graphics links
quietly {
expand 11 if codebegin==-1 & ustrregexm(doc_line, "`codefence'"), generate(dupgr)
sort linenum dupgr
by linenum dupgr: generate graphcheck = _n
replace doc_line = `"<<dd_do: quietly>>"' if codebegin==-1 & dupgr==1 & graphcheck==1
replace doc_line = `"capture graph describe Graph"' if graphcheck==2
replace doc_line = `"local checkdate = "\`r(command_date)' \`r(command_time)'" "' if graphcheck==3
replace doc_line = `"<</dd_do>>"' if graphcheck==4
replace doc_line = `"<<dd_skip_if: ="\`\`gdate''"~="" & "\`\`gdate''"=="\`checkdate'">>"' if graphcheck==5
replace doc_line = `"<<dd_graph>>"' if graphcheck==6
replace doc_line = `"<<dd_skip_end>>"' if graphcheck==7
replace doc_line = `"<<dd_do: quietly>>"' if graphcheck==8
replace doc_line = `"local \`gdate' = "\`r(command_date)' \`r(command_time)'""' if graphcheck==9
replace doc_line = `"<</dd_do>>"' if graphcheck==10

drop dupgr
replace linenum= _n
}
*/
* Then identify code blocks and tags
mata: fenceinfo = _fence_info(X) // fences
mata: infotags = _info_tags(X) // retrieve infotags
mata: tagmatchs = _tag_match(infotags) // parse infotags
mata: dotags = _dd_do(fenceinfo, tagmatchs) // generate <<dd_do>>

* Identify display directives
/*
quietly {
generate dispbegin = ustrregexm(doc_line, "`s") if ~codebegin
replace doc_line = ustrregexra(doc_line, "`s", "<<dd_display: ") if dispbegin==1
replace doc_line = ustrregexra(doc_line, "`", ">>") if dispbegin==1
}
*/
mata: document = _stitch(X, fenceinfo, dotags)
mata: X = _inline_code(X)

* assemble pieces of a dyndoc
mata: document = _stitch(X, fenceinfo, dotags)

* Write out the result
/* quietly keep doc_line
quietly compress doc_line
mata: document = st_sdata(.,1)
mata: saving = st_local("saving")
mata: unlink(saving)
mata: docwrite(saving, document)
display " {text:Output saved as {it:`saving'}}"
*/
mata: saving = st_local("saving")
mata: docwrite(saving, document)
display " {text:Output saved as {it:`saving'}}"

* Finish up
// restore
return local outfile "`saving'"
end

Expand All @@ -179,6 +73,7 @@ string colvector docread(string scalar filename) {
void function docwrite(string scalar filename, ///
string colvector document) {
unlink(filename)
fh = fopen(filename, "w")
for (i=1; i<=length(document); i++) {
fput(fh, document[i])
Expand Down Expand Up @@ -287,7 +182,7 @@ string colvector function _stitch(string colvector X,
lce = 0
Y = _gr_preamble()
for (i=1; i<=rows(X); i++) {
X[i,.]
//X[i,.]
if (fenceinfo[i,2]==1) {
Y = Y \ X[(lce+1)..i,.]\dotags[i,.]
lce = i
Expand All @@ -299,5 +194,17 @@ string colvector function _stitch(string colvector X,
}
return(Y)
}
string colvector function _inline_code(string colvector X) {
for (i=1; i<=rows(X); i++) {
dispdir = ustrregexm(X[i,1], "(`|~)\{?s(tata)?(.*)(`)")
while (dispdir) {
X[i,1] = ustrregexra(X[i,1], "(`|~)\{?s(tata)?", "<<dd_display: ")
X[i,1] = ustrregexra(X[i,1], "`", ">>")
dispdir = ustrregexm(X[i,1], "(`|~)\{?s(tata)?(.*)(`)")
}
}
return(X)
}
end
24 changes: 12 additions & 12 deletions md2dyn.sthlp → stmd2dyn.sthlp
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@
{vieweralsosee "dyndoc" "help dyndoc"}{...}
{vieweralsosee "dyntext" "help dyntext"}{...}
{vieweralsosee "dynamic tags" "help dynamic tags"}{...}
{viewerjumpto "Syntax" "md2dyn##syntax"}{...}
{viewerjumpto "Description" "md2dyn##description"}{...}
{viewerjumpto "Options" "md2dyn##options"}{...}
{viewerjumpto "Remarks" "md2dyn##remarks"}{...}
{viewerjumpto "Examples" "md2dyn##examples"}{...}
{viewerjumpto "Syntax" "stmd2dyn##syntax"}{...}
{viewerjumpto "Description" "stmd2dyn##description"}{...}
{viewerjumpto "Options" "stmd2dyn##options"}{...}
{viewerjumpto "Remarks" "stmd2dyn##remarks"}{...}
{viewerjumpto "Examples" "stmd2dyn##examples"}{...}
{title:Title}

{phang}
{bf:md2dyn} Convert common Markdown to Stata {cmd: dyndoc}
{bf:stmd2dyn} Convert common Markdown to Stata {cmd: dyndoc}
dynamic document format{p_end}


{marker syntax}{...}
{title:Syntax}

{p 8 17 2}
{cmd:md2dyn}
using filename
{cmd:stmd2dyn}
filename
[{cmd:,} {it:options}]

{synoptset 20 tabbed}{...}
Expand All @@ -40,7 +40,7 @@ using filename
{title:Description}

{pstd}
{cmd:md2dyn} Takes a Markdown document using conventional markdown
{cmd:stmd2dyn} Takes a Markdown document using conventional markdown
specification and converts it to Stata's dialect using {cmd: dyndoc}
dynamic tags.

Expand All @@ -63,14 +63,14 @@ using filename

{pstd}
If {it: filename2} is not specified, then {it: filename} with an
.smd file extension is tried.
.dyn file extension is tried.



{marker examples}{...}
{title:Examples}

{phang}{cmd:. md2dyn using example.stmd}{p_end}
{phang}{cmd:. stmd2dyn using example.stmd}{p_end}


{title:Author}
Expand All @@ -80,4 +80,4 @@ If {it: filename2} is not specified, then {it: filename} with an
{p 4} Univ of Wisc-Madison{p_end}
{p 4} {browse "mailto:[email protected]":[email protected]}{p_end}
{p 4} https://www.ssc.wisc.edu/~hemken/Stataworkshops{p_end}
{p 4} https://github.com/Hemken/md2dyn{p_end}
{p 4} https://github.com/Hemken/stmd2dyn{p_end}
File renamed without changes.
10 changes: 6 additions & 4 deletions md2html.ado → stmd2html.ado
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
*! version 1.0
*! version 1.1
*! Doug Hemken
*! 16 March 2018

capture program drop md2html
program define md2html, rclass
// pass options and arguments to dyndoc

capture program drop stmd2html
program define stmd2html, rclass
syntax anything(name=infile) /// input file name
[, SAVing(string) replace] // name of HTML file
display `"`infile'"'
Expand All @@ -29,7 +31,7 @@ display in error "target file can not be the same as the source file"
* intermediate dyndoc file
tempfile dyn
* process
md2dyn `infile', saving(`dyn') `replace'
stmd2dyn `infile', saving(`dyn') `replace'
dyndoc `dyn', saving(`saving') `replace'

end
12 changes: 6 additions & 6 deletions md2html.sthlp → stmd2html.sthlp
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
{title:Title}

{phang}
{bf:md2dyn} Convert common Markdown to HTML format using Stata {cmd: dyndoc}{p_end}
{bf:stmd2html} Convert dynamic Markdown to HTML format using Stata {cmd: dyndoc}{p_end}


{marker syntax}{...}
{title:Syntax}

{p 8 17 2}
{cmd:md2html}
using filename
{cmd:stmd2html}
filename
[{cmd:,} {it:options}]

{synoptset 20 tabbed}{...}
Expand All @@ -42,9 +42,9 @@ using filename
{title:Description}

{pstd}
{cmd:md2dyn} Takes a Markdown document using conventional markdown
{cmd:md2dyn} Takes a dynamic Markdown document using conventional markdown
specification and converts it to HTML via Stata's {cmd: dyndoc}
command. This is just a wrapper for {cmd: md2dyn} followed by
command. This is just a wrapper for {cmd: stmd2dyn} followed by
{cmd: dyndoc}


Expand All @@ -71,7 +71,7 @@ If {it: filename2} is not specified, then {it: filename} with an
{marker examples}{...}
{title:Examples}

{phang}{cmd:. md2html using example.stmd}{p_end}
{phang}{cmd:. stmd2html using example.stmd}{p_end}


{title:Author}
Expand Down
4 changes: 4 additions & 0 deletions update stmd2dyn.do
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ado uninstall stmd2dyn
net from https://www.ssc.wisc.edu/~hemken/Stataworkshops/
net install stmd2dyn, replace
which stmd2dyn

0 comments on commit 1ebb7ab

Please sign in to comment.