@graphs.dpuser
@makehtmlsum.dpuser

text = importdoc("functions_doc.h")
replace text, "result = ", "<i>result</i> = "
replace text, "Syntax", "<br><br><b><font size=+2>Syntax</font></b><br>"
replace text, "Arguments", "<br><br><b><font size=+2>Arguments</font></b><br>"
replace text, "Switches", "<br><br><b><font size=+2>Switches</font></b><br>"
replace text, "Examples", "<br><br><b><font size=+2>Examples</font></b><br>"
replace text, "See also", "<br><br><b><font size=+2>See also</font></b><br>"
replace text, " > ", " &gt; "
replace text, " < ", " &lt; "
for i=1, nelements(text) {
  if (text[i] == "Returns") text[i] = "<br><br><b><font size=+2>Returns</font></b><br>"
  if (text[i] == "Notes") text[i] = "<br><br><b><font size=+2>Notes</font></b><br>"
  if (strlen(text[i]) > 6) {
    if (text[i][1:6] == "<code>") text[i] = "<br><tt>&nbsp;&nbsp;&nbsp;" + text[i][7:strlen(text[i])] + "</tt>"
  }
}

function makehtmlfunctionheader, topic {
header = stringarray(0)
header += "<html>"
header += "<head>"
header += "   <meta name=\"Author\" content=\"Thomas Ott\">"
//header += "   <title>DPUSER - The Next Generation</title>"
header += "   <title>DPUSER - The Next Generation: Function " + topic + "</title>"
header += ""
header += "</head>"
header += ""
header += "<body>"
header += ""
makehtmlfunctionheader = header
}

footer = stringarray(0)
footer += ""
footer += "</body>"
footer += "</html>"

while (nelements(text) > 0) {
  i = 1
  while(text[i] != "" && i < nelements(text)) i++
  function = text[1:i]
  print function[1]
  if (i < nelements(text)) {
    text = text[i+1:nelements(text)]
  } else {
    text = stringarray(0)
  }
  mtitle = stringarray(0)
//  mtitle += "<table CELLSPACING=0 CELLPADDING=20 WIDTH=\"100%\" BGCOLOR=\"#FFFF99\">"
//  mtitle += "<tr><td><b><font size=+3>" + "function " + function[1] + "</font></b></td></tr></table>"
  mtitle += "<h1 class=\"declaration\">function " + function[1] + "</h1>"
  mtitle += "<p>"
  if (fileexists(function[1] + ".png") == 1) mtitle += "<img src=\"" + function[1] + ".png\" align=\"right\">"
  table = 0
  for i = 2, nelements(function) {
    if (function[i] == "<br><br><b><font size=+2>Syntax</font></b><br>") if (table == 1) table = -1
    if (function[i] == "<br><br><b><font size=+2>Examples</font></b><br>") if (table == 1) table = -1
    if (function[i] == "<br><br><b><font size=+2>See also</font></b><br>") if (table == 1) table = -1
    if (function[i] == "<br><br><b><font size=+2>Returns</font></b><br>") if (table == 1) table = -1
    if (function[i] == "<br><br><b><font size=+2>Notes</font></b><br>") if (table == 1) table = -1
    if (function[i] == "<br><br><b><font size=+2>Arguments</font></b><br>") {
      if (table == 1) {
        table = 3
      } else {
        table = 2
      }
    }
    if (function[i] == "<br><br><b><font size=+2>Switches</font></b><br>") {
      if (table == 1) {
        table = 3
      } else {
        table = 2
      }
    }
    if (strlen(function[i]) > 10) {
      if (function[i][1:9] == "function_") {
        function[i] = "<a href = \"" + function[i] + ".html\">function " + function[i][10:strlen(function[i])] + "</a><br>"
      }
      if (function[i][1:10] == "procedure_") {
        function[i] = "<a href = \"" + function[i] + ".html\">procedure " + function[i][11:strlen(function[i])] + "</a><br>"
      }
      if (function[i][1:9] == "category_") {
        function[i] = "<a href = \"category.html#" + function[i][10:strlen(function[i])] + "\">category index: " + function[i][10:strlen(function[i])] + "</a><br>
"
      }
    }
    if (table == 3) {
      mtitle += "</table>"
      table = 2
    }
    if (function[i] == " ") function[i] = "<br><p>"
    if (table == 2) {
      mtitle += function[i]
      mtitle += "<table CELLSPACING=0 CELLPADDING=0>"      
    }
    if (table == -1) {
      mtitle += "</table>"
      table = 0
    }
    if (table == 1) {
      pos = strpos(function[i], ":")
      if (pos == -1) pos = strpos(function[i], " ")
      if (pos > 0) {
        mtitle += "<tr VALIGN=TOP><td nowrap>" + function[i][1:pos] + "&nbsp;&nbsp;</td>\n<td>" + function[i][pos + 1:strlen(function[i])] + "</td></tr>"
      } else {
        mtitle += function[i]
      }
      table = 1
    } else {
      if (table != 2) mtitle += function[i]
    }
    if (table == 2) table = 1
  }
  if (table == 1) mtitle += "</table>"

  // test if function name is obsolete ("function_name (obsolete)"). When true the " (obsolete)"-string will be removed for the file-name
  filename = function[1]
  if (strpos(filename, "obsolete") != -1) {
    filename_len = nelements(filename)
    filename = filename[1:filename_len-11] 
  }

  export "function_" + filename + ".html", makehtmlfunctionheader(function[1]) + mtitle + footer
}


text = importdoc("procedures_doc.h")
replace text, "Syntax", "<br><br><b><font size=+2>Syntax</font></b><br>"
replace text, "Arguments", "<br><br><b><font size=+2>Arguments</font></b><br>"
replace text, "Switches", "<br><br><b><font size=+2>Switches</font></b><br>"
replace text, "Examples", "<br><br><b><font size=+2>Examples</font></b><br>"
replace text, "Notes", "<br><br><b><font size=+2>Notes</font></b><br>"
replace text, "See also", "<br><br><b><font size=+2>See also</font></b><br>"
replace text, " > ", " &gt; "
replace text, " < ", " &lt; "
for i=1, nelements(text) {
  if (strlen(text[i]) > 6) {
    if (text[i][1:6] == "<code>") text[i] = "<br><tt>&nbsp;&nbsp;&nbsp;" + text[i][7:strlen(text[i])] + "</tt>"
  }
}

function makehtmlprocedureheader, topic {
header = stringarray(0)
header += "<html>"
header += "<head>"
header += "   <meta name=\"Author\" content=\"Thomas Ott\">"
//header += "   <title>DPUSER - The Next Generation</title>"
header += "   <title>DPUSER - The Next Generation: Procedure " + topic + "</title>"
header += ""
header += "</head>"
header += ""
header += "<body>"
header += ""
makehtmlprocedureheader = header
}

footer = stringarray(0)
footer += ""
footer += "</body>"
footer += "</html>"

while (nelements(text) > 0) {
  i = 1
  while(strlen(text[i]) > 0 && i < nelements(text)) i++
  function = text[1:i]
  print function[1]
  if (i < nelements(text)) {
    text = text[i+1:nelements(text)]
  } else {
    text = stringarray(0)
  }
  mtitle = stringarray(0)
//  mtitle += "<table CELLSPACING=0 CELLPADDING=20 WIDTH=\"100%\" BGCOLOR=\"#FFFF99\">"
//  mtitle += "<tr><td><b><font size=+3>" + "procedure " + function[1] + "</font></b></td></tr></table><p>"
  mtitle += "<h1 class=\"declaration\">procedure " + function[1] + "</h1>"
  table = 0
  mtitle += "<p>"
  for i = 2, nelements(function) {
    if (function[i] == "<br><br><b><font size=+2>Syntax</font></b><br>") if (table == 1) table = -1
    if (function[i] == "<br><br><b><font size=+2>Examples</font></b><br>") if (table == 1) table = -1
    if (function[i] == "<br><br><b><font size=+2>Notes</font></b><br>") if (table == 1) table = -1
    if (function[i] == "<br><br><b><font size=+2>See also</font></b><br>") if (table == 1) table = -1
    if (function[i] == "<br><br><b><font size=+2>Arguments</font></b><br>") {
      if (table == 1) {
        table = 3
      } else {
        table = 2
      }
    }
    if (function[i] == "<br><br><b><font size=+2>Switches</font></b><br>") {
      if (table == 1) {
        table = 3
      } else {
        table = 2
      }
    }
    if (strlen(function[i]) > 10) {
      if (function[i][1:9] == "function_") {
        function[i] = "<a href = \"" + function[i] + ".html\">function " + function[i][10:strlen(function[i])] + "</a><br>"
      }
      if (function[i][1:10] == "procedure_") {
        function[i] = "<a href = \"" + function[i] + ".html\">procedure " + function[i][11:strlen(function[i])] + "</a><br>"
      }
    }
    if (table == 3) {
      mtitle += "</table>"
      table = 2
    }
    if (table == 2) {
      mtitle += function[i]
      mtitle += "<table CELLSPACING=0 CELLPADDING=0 WIDTH=\"100%\">"      
    }
    if (table == -1) {
      mtitle += "</table>"
      table = 0
    }
    if (function[i] == " ") function[i] = "<br><p>"
    if (table == 1) {
      pos = strpos(function[i], ":")
      if (pos == -1) pos = strpos(function[i], " ")
      if (pos > 0) {
        mtitle += "<tr VALIGN=TOP><td nowrap>" + function[i][1:pos] + "&nbsp;&nbsp;</td>\n<td>" + function[i][pos + 1:strlen(function[i])] + "</td></tr>"
      } else {
        mtitle += function[i]
      }
      table = 1
    } else {
      if (table != 2) mtitle += function[i]
    }
    if (table == 2) table = 1
  }
  if (table == 1) mtitle += "</table>"
        
  // test if procedure name is obsolete ("procedure_name (obsolete)"). When true the " (obsolete)"-string will be removed for the file-name
  filename = function[1]
  if (strpos(filename, "obsolete") != -1) {
    filename_len = nelements(filename)
    filename = filename[1:filename_len-11] 
  }
        
  export "procedure_" + filename + ".html", makehtmlprocedureheader(function[1]) + mtitle + footer
}

