
/* code to generate html */


/* Global defines */
anchor:={};
anchor["0"]:="a";
anchor["name"]:="";

link:={};
link["0"]:="a";
link["href"]:="";

frameset:={};
frameset["0"]:="frameset";
frameset["border"]:="0";

frame:={};
frame["0"]:="frame";

caption:={};
caption["0"]:="caption";

table:={};
table["0"]:="table";

form:={};
form["0"]:="form";

textarea:={};
textarea["0"]:="textarea";

textfield:={};
textfield["0"]:="input";
textfield["TYPE"]:="text";

button:={};
button["0"]:="input";
button["TYPE"]:="submit";

bullets:={};
bullets["0"]:="ul";

bullet:={};
bullet["0"]:="li";

newline:="
";
Gt():="&gt;";
Lt():="&lt;";




HtmlNewParagraph():= (newline : "<p>" : newline);

HtmlTitle(title):=
[
"<head>
  <title>" : title : "</title>
  <link rel=\"stylesheet\" href=\"yacas.css\" TYPE=\"text/css\" MEDIA=\"screen\">
</head>";
];

HtmlAnchor(name):=
[
  anchor["name"]:=name;
  HtmlTag(anchor,"");
];
Bodied("HtmlAnchor",60000);

HtmlTable(cellpadding,width,body):=
[
  table["cellpadding"]:=String(cellpadding);
  table["width"]:=width;
  HtmlTag(table,body);
];

Bullets(list):=HtmlTag(bullets,list);
Bullet (list):=HtmlTag(bullet ,list);


HtmlCaption(title):=
[
 HtmlTag(caption,title);
];

HtmlForm(action,body):=
[
  form["method"]:="get";
  form["action"]:=action;
  HtmlTag(form,body);
];


HtmlTextArea(name,width,height,body) :=
[
  textarea["name"]:=name;
  textarea["cols"]:=String(width);
  textarea["rows"]:=String(height);
  HtmlTag(textarea,body);
];

HtmlTextField(name,size,value):=
[
  textfield["name"]:=name;
  textfield["size"]:=String(size);
  textfield["value"]:=value;
  HtmlTag(textfield,"");
];

HtmlSubmitButton(name,value):=
[
  button["name"]:=name;
  button["value"]:=value;
  HtmlTag(button,"");
];


HtmlLink(description,file,tag,target):=
[
  If(tag != "",
    link["href"]:= file : "#" : tag,
    link["href"]:= file);
    
  If(target != "",link["target"] :=target);
  HtmlTag(link,description);
];

HtmlFrameSetRows(columns,body):=
[
  frameset["cols"]:="";
  frameset["rows"]:=columns;
  HtmlTag(frameset,body);
];

HtmlFrameSetCols(columns,body):=
[
  frameset["cols"]:=columns;
  frameset["rows"]:="";
  HtmlTag(frameset,body);
];

HtmlFrame(source,name):=
[
  frame["src"]:=source;
  frame["name"]:=name;
  HtmlTag(frame,"");
];


/* export a html tag type, using the specifications in the
   tags assoc list.
   */
HtmlTag(tags,content):=
[
  Local(result,tag,analytics);
  result:="<" : tags["0"];
  ForEach(tag,AssocIndices(tags))
  [
    If (tag != "0" And tags[tag] != "",
       result:= result : " " : tag : "=" : "\"" : tags[tag] : "\""
       ); 
  ];

  analytics:="";
  If(tags["0"] = "body",
    analytics:="<script src=\"http://www.google-analytics.com/urchin.js\" type=\"text/javascript\">
</script>
<script type=\"text/javascript\">
_uacct = \"UA-2425144-1\";
urchinTracker();
</script>
");

  
  result:= result : ">" : newline :
           content : newline :
           analytics : "</" : tags["0"] : ">" : newline;
  
  result;
];

/* output directory management */
htmldir:="";
SetHtmlDirectory(dir):= [htmldir:=dir;];
HtmlFile(file) := [htmldir : file;];


/* loading and saving site info */
site:={};
ClearSite() := [site:={};];
LoadSite():=
[
  FromFile("siteall")
  [
    site:=Read();
  ];
];

SaveSite():=
[
  ToFile("siteall")
  [
    Write(site);
    WriteString(";");
  ];
];

MySQLQuery(pidstr,string):=
[
  Local(result);
  ToFile("sqlin":pidstr) WriteString(string);
  SystemCall("mysql mysql < ":"sqlin":pidstr:" > sqlout":pidstr);
  SystemCall(FindFile("tools/mysqlstubs"):" sqlout":pidstr:" sqlout_":pidstr);
  result:= FromFile("sqlout_":pidstr)Read();
  SystemCall("rm -rf sqlin":pidstr);
  SystemCall("rm -rf sqlout":pidstr);
  SystemCall("rm -rf sqlout_":pidstr);
  result;
];

