pike.git / bin / mkxml.pike

version» Context lines:

pike.git/bin/mkxml.pike:1: - /* $Id: mkxml.pike,v 1.23 2001/05/09 14:02:05 grubba Exp $ */ + /* $Id: mkxml.pike,v 1.24 2001/07/17 03:50:43 nilsson Exp $ */      import Stdio;   import Array;    - mapping parse=([]); + mapping parse=([ " appendix":([]) ]);   int illustration_counter;    - mapping manpage_suffix= - ([ -  "Image":"i", -  "Image.image":"i", - ]); -  -  +    function verbose=werror;      #define error(X) throw( ({ (X), backtrace()[0..sizeof(backtrace())-2] }) )      /*      module : mapping <- moduleM    "desc" : text    "see also" : array of references    "note" : mapping of "desc": text
pike.git/bin/mkxml.pike:38:    "known bugs" : mapping of "desc": text    "args" : array of mappings <- argM    "args" : array of args names and types    "desc" : description    "names" : multiset of method name(s)      Quoting: Only '<' must be quoted as '&lt;'.      */    - mapping moduleM, classM, methodM, argM, nowM, descM; + mapping moduleM, classM, methodM, argM, nowM, descM, appendixM;      mapping focM(mapping dest,string name,string line)   {    if (!dest->_order) dest->_order=({});    if (-1==search(dest->_order,name)) dest->_order+=({name});    return dest[name] || (dest[name]=(["_line":line]));   }      string stripws(string s)   { -  return desc_stripws(s); - } -  - string desc_stripws(string s) - { +     if (s=="") return s; -  +     array lines = s / "\n"; -  +  +  lines = map(lines, lambda(string s) { +  s = reverse(s); +  sscanf(s, "%*[ \t\r]%s", s); +  s = reverse(s); +  return s; }); +     int m=10000;    foreach (lines,string s)    if (s!="")    {    sscanf(s,"%[ ]%s",string a,string b);    if (b!="") m=min(strlen(a),m);    } -  +     return map(lines,lambda(string s) { return s[m..]; })*"\n";   }      mapping lower_nowM()   {    if (nowM &&    (nowM==parse    || nowM==classM    || nowM==methodM    || nowM==moduleM)) return nowM;
pike.git/bin/mkxml.pike:91:   #define complain(X) (X)      string file_version = "";      mapping keywords=   (["$Id":lambda(string arg, string line)    {    file_version = " version='Id: "+arg[..search(arg, "$")-1]+"'";    werror("mkxml: Version: "+file_version+"\n");    }, +  "appendix":lambda(string arg,string line) { +  descM=nowM=appendixM=focM(parse[" appendix"],stripws(arg),line); +  report("appendix "+arg);},    "module":lambda(string arg,string line)    { classM=descM=nowM=moduleM=focM(parse,stripws(arg),line);    methodM=0;    if (!nowM->classes) nowM->classes=(["_order":({})]);    if (!nowM->modules) nowM->modules=(["_order":({})]);    report("module "+arg); },    "class":lambda(string arg,string line)    { if (!moduleM) return complain("class w/o module");    descM=nowM=classM=focM(moduleM->classes,stripws(arg),line);    methodM=0; report("class "+arg); },
pike.git/bin/mkxml.pike:191:   {    string res="";    while (sscanf(s,"%s\t%s",string a,s)==2)    {    res+=a;    res+=" "[(strlen(res)%8)..7];    }    return res+s;   }    -  - object(File) make_file(string filename) - { -  stderr->write("creating "+filename+"...\n"); -  if (file_size(filename)>0) -  { -  rm(filename+"~"); -  mv(filename,filename+"~"); -  } -  object f=File(); -  if (!f->open(filename,"wtc")) -  { -  stderr->write("failed."); -  exit(1); -  } -  return f; - } -  - string synopsis_to_html(string s,mapping huh) - { -  string type,name,arg; -  s=replace(s,({"<",">"}),({"&lt;","&gt;"})); -  if (sscanf(s,"%s%*[ \t]%s(%s",type,name,arg)!=4) -  { -  sscanf(s,"%s(%s",name,arg),type=""; -  werror(sprintf(huh->_line+": suspicios method %O\n",(s/"(")[0])); -  } -  if (arg[..1]==")(") name+="()",arg=arg[2..]; -  -  if (!arg) arg=""; -  -  return -  type+" <b>"+name+"</b>("+ -  replace(arg,({","," "}),({", ","\240"})); - } -  +    string htmlify(string s)   {   #define HTMLIFY(S) \    (replace((S),({"&lt;","&gt;",">","&","\240"}),({"&lt;","&gt;","&gt;","&amp;","&nbsp;"})))       string t="",u,v;    while (sscanf(s,"%s<%s>%s",u,v,s)==3)    t+=HTMLIFY(u)+"<"+v+">";    return t+HTMLIFY(s);   }
pike.git/bin/mkxml.pike:266:    q=prefix+what;    }    else    q=what;       return "<ref to="+linkify(q)+">"+htmlify(stuff)+"</ref>";   }      string fixdesc(string s,string prefix,string where)   { -  s=desc_stripws(s); +  s=htmlify(stripws(replace(s, "<p>", "\n")));    -  string t,u,v,q; +  Parser.HTML p = Parser.HTML(); +  p->add_container("illustration", +  lambda(Parser.HTML p, mapping args, string c) +  { +  return ({ sprintf("<illustration %s%{ %s='%s'%}>%s</illustration>", +  where, (array)args, replace(c, "lena()", "src")) }); +  });    -  t=s; s=""; -  - // we don't need to parse 'ref' at all - // while (sscanf(t,"%s<ref%s>%s</ref>%s",t,q,u,v)==4) - // { - // if (search(u,"<ref")!=-1) - // { - // werror("warning: unclosed <ref>\n%O\n",s); - // u=replace(u,"<ref","&lt;ref"); - // } -  - // if (sscanf(q," to=%s",q)) - // s+=htmlify(t)+make_nice_reference(q,prefix,u); - // else - // s+=htmlify(t)+make_nice_reference(u,prefix,u); - // t=v; - // } - // if (search(s,"<ref")!=-1) - // { - // werror("%O\n",s); - // error("buu\n"); - // } -  -  s+=htmlify(t); -  -  t=s; s=""; -  for (;;) +  foreach( ({ "pre", "table", "execute", "ul" }), string tag) +  p->add_container(tag, +  lambda(Parser.HTML p, mapping args, string c)    { -  string a,b,c; -  if (sscanf(t,"%s<%s>%s",a,b,c)<3) break; +  return ({ sprintf("<%s%{ %s='%s'%}>%s</%s>", tag, +  (array)args, safe_newlines(c), tag) }); +  });    -  if (b[..11]=="illustration" && -  sscanf(t,"%s<illustration%s>%s</illustration>%s",t,q,u,v)==4) +  p->add_container("data_description", +  lambda(Parser.HTML p, mapping args, string c)    { -  s+=replace(t,"\n\n","\n\n<p>")+ -  "<illustration "+where+" src='image_ill.pnm"+q+"'>\n" -  +replace(u,"lena()","src")+"</illustration>"; -  t=v; -  } -  else if (b[..2]=="pre" && -  sscanf(t,"%s<pre%s>%s</pre>%s",t,q,u,v)==4) +  if(args->type=="mapping") { +  Parser.HTML i = Parser.HTML()-> +  add_container("elem", +  lambda(Parser.HTML p, mapping args, string c)    { -  s+=replace(t,"\n\n","\n\n<p>")+ -  "<pre"+q+">\n"+u+"</pre>"; -  t=v; +  if(!args->type) +  throw("mkxml: Type attribute missing on elem tag."); +  if(args->type!="int" && args->type!="float" && +  args->type!="string") +  throw("mkxml: Unknown type "+args->type+" in elem type attribute.\n"); +  if(!args->name) +  throw("mkxml: Name attribute missing on elem tag."); +  return "<group>\n<member><type><" + args->type + +  "/></type><index>\"" + args->name + "\"</index></member>\n" +  "<text><p>" + c + "</p></text>\n</group>\n"; +  }); +  return ({ "<mapping>\n " + +  safe_newlines(i->finish(c)->read()) + +  "</mapping>\n " });    } -  else -  { -  s+=replace(a,"\n\n","\n\n<p>")+"<"+b+">"; -  t=c; -  } -  } -  s+=replace(t,"\n\n","\n\n<p>"); +  throw("mkxml: Unknown data_description type "+args->type+".\n"); +  });    -  if (where) { +  +  +  +  s = p->finish(s)->read(); +  s = "<p>" + (s/"\n\n")*"</p>\n\n<p>" + "</p>"; +  +  if (where)    return "<source-position " + where + file_version + "/>\n"+s; -  } +     return s;   }         multiset(string) get_method_names(string *decls)   {    string decl,name;    multiset(string) names=(<>);    foreach (decls,decl)    {    sscanf(decl,"%*s%*[\t ]%s%*[\t (]%*s",name);    names[name]=1;    }    return names;   }    - string *nice_order(string *arr) + array(string) nice_order(string *arr)   {    sort(map(arr,replace,({"_","`"}),({"ÿ","þ"})),    arr);    return arr;   }      string addprefix(string suffix,string prefix)   {    return prefix+suffix;   }    - array fix_dotstuff(array(string) in) - { -  if (!sizeof(in)) return ({}); -  array(string) last; -  in=Array.map(in,replace,({"->",">","<"}),({".","&lt;","&gt;"})); -  last=in[0]/"."; -  last=last[..sizeof(last)-2]; -  int i; -  array res=in[..0]; -  for (i=1; i<sizeof(in); i++) -  { -  array(string) z=in[i]/"."; -  if (equal(z[..sizeof(z)-2],last)) -  res+=({"."+z[-1]}); -  else -  { -  last=z[..sizeof(z)-2]; -  res+=in[i..i]; -  } -  } -  return res; - } -  +    #define S(X) ("'"+(X)+"'") /* XML arg quote */      string doctype(string type,void|string indent)   {    array(string) endparan(string in)    {    int i;    int q=1;    for (i=0; i<strlen(in); i++)    switch (in[i])
pike.git/bin/mkxml.pike:577:       f->write("</"+enttype+">");   }      Parser.HTML html2xml;      void document(string enttype,    mapping huh,string name,string prefix,    object f)   { -  string *names; +  array(string) names;       if (huh->names)    names=map(indices(huh->names),addprefix,name);    else    names=({name});       verbose("mkxml: "+name+" : "+names*","+"\n");       array v=name/".";    string canname=v[-1];    sscanf(canname,"%s->",canname);    sscanf(canname,"%s()",canname);       if (convname[canname]) canname=convname[canname];       switch (enttype)    { -  +  case "appendix": +  f->write("<"+enttype+" name="+S(name)+">\n"); +  break;    case "class":    case "module":    f->write("<"+enttype+" name="+S(canname)+">\n");    break;    default:    f->write("<docgroup homogen-type="+S(enttype)+    " homogen-name="+S(canname)+">\n");       if (huh->decl)    {
pike.git/bin/mkxml.pike:807:    foreach(huh->modules->_order,string n)    {    document("module",huh->modules[n],    prefix+n,prefix+n+".",f);    }    }   // end ANCHOR       switch (enttype)    { +  case "appendix":    case "class":    case "module":    f->write("</"+enttype+">\n\n");    break;    default:    f->write("</docgroup>\n\n");    break;    }   }    -  + array(string) tag_quote_args(Parser.HTML p, mapping args) { +  return ({ sprintf("<%s%{ %s='%s'%}>", p->tag_name(), (array)args) }); + } +    void make_doc_files()   {    html2xml=Parser.HTML(); -  html2xml->add_tag("p",lambda(mixed...) { return ({"</p><p>"}); }); +     html2xml->add_tag("br",lambda(mixed...) { return ({"<br/>"}); });    html2xml->add_tag("wbr",lambda(mixed...) { return ({"<wbr/>"}); }); -  +  +  html2xml->add_tags( ([ "dl":tag_quote_args, +  "dt":tag_quote_args, +  "dd":tag_quote_args, +  "table":tag_quote_args, +  "tr":tag_quote_args, +  "th":tag_quote_args, +  "td":tag_quote_args, +  "a":tag_quote_args, +  "ref":tag_quote_args ]) ); +  +  /*    html2xml->add_tag("dl",    lambda(Parser.HTML p, mapping args)    {    return ({ sprintf("<dl%{ %s='%s'%}>", (array)args) });    });    html2xml->add_tag("dt",    lambda(Parser.HTML p, mapping args)    {    return ({ sprintf("<dt%{ %s='%s'%}>", (array)args) });    }); -  +  html2xml->add_tag("table", +  lambda(Parser.HTML p, mapping args) +  { +  return ({ sprintf("<table%{ %s='%s'%}>", (array)args) }); +  });    html2xml->add_tag("tr",    lambda(Parser.HTML p, mapping args)    {    return ({ sprintf("<tr%{ %s='%s'%}>", (array)args) });    }); -  +  html2xml->add_tag("td", +  lambda(Parser.HTML p, mapping args) +  { +  return ({ sprintf("<td%{ %s='%s'%}>", (array)args) }); +  }); +  html2xml->add_tag("th", +  lambda(Parser.HTML p, mapping args) +  { +  return ({ sprintf("<th%{ %s='%s'%}>", (array)args) }); +  }); +  html2xml->add_tag("ref", +  lambda(Parser.HTML p, mapping args) +  { +  return ({ sprintf("<ref%{ %s='%s'%}>", (array)args) }); +  }); +  */ +     html2xml->add_container(    "text",    lambda(Parser.HTML p,mapping args,string cont)    {   // werror("%O\n",cont);    cont=p->clone()->finish(cont)->read();   // werror("---\n");   // werror("%O\n",cont);    string res="<text><p>"+cont+"</p></text>";    string t;    do    {    t=res;    res=replace(res,"<p></p>","");    res=replace(res,"<br/></p>","</p>");    }    while (t!=res);    return ({res});    }); -  +  html2xml->add_container("link", +  lambda(Parser.HTML p, mapping args, string c) +  { +  return ({ sprintf("<ref%{ %s=\"%s\"%}>%s</ref>", (array)args, c) }); +  }); +  html2xml->add_container("execute", +  lambda(Parser.HTML p, mapping args, string c) +  { +  return ({ c }); +  });    -  stderr->write("modules: "+sort(indices(parse))*", "+"\n"); +  stderr->write("modules: "+sort(indices(parse)-({" appendix"}))*", "+"\n");       stdout->write("<module name=''>\n");    -  foreach (sort(indices(parse)-({"_order"})),string module) +  foreach (sort(indices(parse)-({"_order", " appendix"})),string module)    document("module",parse[module],module,module+".",stdout);    -  +  if(appendixM) +  foreach(parse[" appendix"]->_order, string title) +  document("appendix",parse[" appendix"][title],title,"",stdout); +     stdout->write("</module>\n");   }    - int inpre=0; -  +    void process_line(string s,string currentfile,int line)   {    s=getridoftabs(s);       int i;    if ((i=search(s,"**!"))!=-1 || (i=search(s,"//!"))!=-1)    {    string kw,arg;       sscanf(s[i+3..],"%*[ \t]%[^: \t\n\r]%*[: \t]%s",kw,arg);    if (keywords[kw])    {    string err;    if ( (err=keywords[kw](arg,"file='"+currentfile+"' line='"+line+"'")) )    {    stderr->write("mkxml: "+    currentfile+"file='"+currentfile+"' line="+line);    exit(1);    } -  inpre=0; +     }    else if (s[i+3..]!="")    {    string d=s[i+3..];    // sscanf(d,"%*[ \t]!%s",d);    // if (search(s,"$Id")!=-1) report("Id: "+d);    if (!descM) descM=methodM;    if (!descM)    {    stderr->write("mkxml: "+
pike.git/bin/mkxml.pike:917:    descM->desc+=d;    }    else if (descM)    {    if (!descM->desc) descM->desc="";    else descM->desc+="\n";    }    }   }    + string safe_newlines(string in) { +  string old; +  do { +  old = in; +  in = replace(in, "\n\n", "\n \n"); +  } while(old!=in); +  return in; + } +    int main(int ac,string *files)   {    string s,t;    int line; -  string *ss=({""}); +  array(string) ss=({""});    object f;       string currentfile;       nowM=parse;       stderr->write("reading and parsing data...\n");       files=files[1..];       if (sizeof(files) && files[0]=="--nonverbose")    files=files[1..],verbose=lambda(){};       stderr->write("mkxml: reading files...\n");       for (;;)    {    int i; -  inpre=0; +        if (!f)    {    if (!sizeof(files)) break;    verbose("mkxml: reading "+files[0]+"...\n");    file_version = "";    f=File();    currentfile=files[0];    files=files[1..];    if (!f->open(currentfile,"r")) { f=0; continue; }