*! 28jan2011
program html
version 10.1
mata: DoIt()
end
version 10.1
mata:
void DoIt() {
cmd=st_local("0")
subcmd=firstword(cmd)
if (subcmd=="page") Page(cmd)
else if (any(subcmd:==("add","image","buttons","boxes"))) Matter(subcmd,cmd)
else if (subcmd=="printer") 1 //not done?
else if (subcmd=="write") Write(cmd)
else errel(subcmd+" is not a valid html sub-command")
}
void Page(cmd) {
syntaxl(cmd,&(action="[anything]"),(&(page="[p:age()]"),&(replace="[r:eplace]")))
if (!any(strtrim(action):==("create","clear",""))) errel(`"html page sub-command can be "create" or "clear""')
if (action=="create" | action=="") (void) htmlpageInit(page,replace)
else rmexternal("html_page_"+(page==""?"pg1":page))
}
void Matter(type,cmd) { //>>def func<<
pointer (class htmlpage) scalar page
syntaxl(cmd,&(main="[anything]"), (&(scheme="[s:cheme()]"),&(blclass="[class()]"),&(top="[top]"),&(left="[left]"),&(div="[div()]"),&(pageid="[p:age()]"),&(showif="[showif()]")), remainder="")
if (top+left+(strlen(div)>0)>1) errel("Only one of -top-, -left-, and -div- can be specified")
divid=(div,"top","left")[1+top+2*left]
page=htmlpageGet(pageid)
block=""
if (type=="add") {
if (strlen(strtrim(main))) errel("-html add- doesn't take a direct paramater (probably should...)")
optionel(remainder,&(block="[t:ext()]"))
block
}
else if (type=="image") {
optionel(remainder,&(width="[w:idth()]"))
width=strlen(width)?strtoreal(width):800
stata("qui graph dir")
memgraphs=tokens(st_global("r(list)"))
if (any(strtrim(main):==memgraphs)) {
tf=st_tempfilename()
stata(sprintf("qui graph export %s, as(png) name(%s) width(%f)",tf,main,width))
(*page).images=(*page).images\"png",ftostr(tf)
}
else (*page).images=(*page).images\substr(pp3(main),2),ftostr(main)
block=sprintf("
",rows((*page).images))
}
else if (type=="buttons") {
optionel(remainder,(&(name="n:ame()"),&(init="[i:nit()]")))
vals=Gettokens(main)
for (v=1;v<=length(vals);v++) {
hi=vals[v]==init?"_btn_hi":""
block=block+sprintf(`"%s"',hi,name,vals[v],vals[v])
}
sput((*page).controls,sprintf(`"controls.%s="%s";"',name,init))
sput((*page).controls,sprintf(`"controls.%s_sch="%s";"',name,scheme))
}
else if (type=="boxes") {
optionel(remainder,(&(names="n:ames()"),&(init="[i:nit()]")))
labels=Gettokens(main)
names=Gettokens(names)
inits=strlower(substr(Gettokens(init),1,1))
inits="yes":*(inits:=="1":|inits:=="y":|inits:=="t")
_editvalue(inits,"","no")
for (b=1;b<=length(labels);b++) {
block=block+sprintf(`"%s "',labels[b],"checked"*(inits[b]=="yes"),names[b])
sput((*page).controls,sprintf(`"controls.%s="%s";"',names[b],inits[b]))
sput((*page).controls,sprintf(`"controls.%s_sch="%s";"',names[b],scheme))
}
}
(*page).addblock(block,scheme,showif,divid,blclass)
}
void Write(cmd) {
pointer (class htmlpage) scalar page
syntaxl(cmd,&(path="[using]"), (&(scheme="[s:cheme()]"),&(pageid="[p:age()]"),&(ms="[ms]")))
page=htmlpageGet(pageid)
ext=ms&length((*page).images)?"mht":"html"
if (path=="") path=mksubdirs("html_aux."+ext) //removed ext from here(?)
else path=pcanon(path,"file",ext)
(*page).write(path,scheme,ext)
if (ms) html_wordify(path)
html_browse(path)
}
string vector Gettokens(string scalar stuff) {
t=tokeninit()
tokenset(t,stuff)
ts=tokengetall(t)
for (i=1; i<=cols(ts); i++) {
if (substr(ts[i],1,1)==`"""') ts[i] = substr(ts[i],2,strlen(ts[i])-2)
else if (substr(ts[i],1,2)=="`"+`"""') ts[i] = substr(ts[i],3,strlen(ts[i])-4)
}
return(ts)
}
end