/* Read pgplot.doc and create c++ file for inclusion in helpmap.cpp */

separator = "------------------------------------------------------------------------"
file = import("pgplot.doc", /text)
file += separator
declaration = 0

functions = procedures = stringarray(0)
description = ""
name = ""

for i=1, nelements(file) {
  print "start " + i
  if (file[i] == separator) {
// next line has short description
    i++
    line = file[i]
    description = line[strpos(line, "--") + 3:strlen(line)]
    name = strtrim(lower(line[9:strpos(line, " --")-1]))
    print name + " " + description
    i += 3
    line = file[i]
    print line
    if (line[7] == "S") {
      procedures += "{"
      procedures += "{ \"" + name + "\" }, {"
      procedures += "{ \"" + description + "\" },"
      procedures += "{ \"Syntax\" },"
      while (line != separator) {
        if (strpos(line, "\\") != -1) {
          lline = ""
          for j=1, nelements(line) {
            if (line[j]=="\\") {
              lline += "\\\\"
            } else {
              lline += line[j]
            }
          }
          line = lline
        }
        replace line, "\"", "\\\""
        newline = "{ \"" + line + "\" }"
        if (file[i+1] != separator) newline += ","
        procedures += newline
        i++
        line = file[i]
      }
      procedures += "}"
      procedures += "},"
    } else {
      functions += "{"
      functions += "{ \"" + name + "\" }, {"
      functions += "{ \"" + description + "\" },"
      functions += "{ \"Syntax\" },"
      while (line != separator) {
        replace line, "\"", "\\\""
        newline = "{ \"" + line + "\" }"
        if (file[i+1] != separator) newline += ","
        functions += newline
        i++
        line = file[i]
      }
      functions += "}"
      functions += "},"
    }
    if (i < nelements(file)) i--
  }
}

replace procedures, "<", "&lt;"
replace procedures, ">", "&gt;"
replace procedures, " & ", " &amp; "
replace functions, "<", "&lt;"
replace functions, ">", "&gt;"
replace functions, " & ", " &amp; "

export "pgplot_procedures.cpp", procedures
export "pgplot_functions.cpp", functions
