pike.git / lib / modules / Tools.pmod / AutoDoc.pmod / BMMLParser.pike

version» Context lines:

pike.git/lib/modules/Tools.pmod/AutoDoc.pmod/BMMLParser.pike:1:   #!/usr/local/bin/pike - #pike 0.6 + #pike 7.8      // Parse BMML (Black Magic Markup Language) to AutoDoc XML.   // Written by Fredrik Hubinette, dark sourceror and inventor of BMML.    - #include <simulate.h> - #include <stdio.h> - #include <string.h> + import Stdio; + import String;    - multiset efuns = mklist(indices(all_efuns())) | (<"sscanf","gauge","catch">); + multiset efuns = mkmultiset(indices(all_constants())) | +  (<"sscanf","gauge","catch">);   mapping short_descs = ([]);   mapping keywords = ([]);      string new_path;   string docdir;   string prefix="";      // Constants that are known to have been documented in BMML.   protected constant known_constants = (< "PI" >);      /*    * convert original path to internal format    */   string fippel_path(string path)   {    sscanf(path,"./%s",path); -  path=replace(path,"/","_"); +  path=predef::replace(path,"/","_");    if(path[strlen(path)-5..]==".bmml") path=path[..strlen(path)-6];    if(path[strlen(path)-5..]!=".html") path+=".html";       return docdir+path;   }      string implode3(string pre, array(string) stuff, string post)   {    return pre+ stuff * (post+pre) + post;   }      /*    * Three step conversion process...    */   string even_more_magic(string block, int indent)   {    if(-1==search(block,"\t"))    { -  return replace(block,"\n","<br />\n"); +  return predef::replace(block,"\n","<br />\n");    }else{    int e,d;    mixed tmp,tmp2;       tmp=block/"\n";    for(e=0;e<sizeof(tmp);e++)    {    tmp[e]=tmp[e]/"\t";    if(sscanf(tmp[e][0],"%*[ ]%s",tmp2) && tmp2=="")    {
pike.git/lib/modules/Tools.pmod/AutoDoc.pmod/BMMLParser.pike:130:      //! Convert a block of tab-indented text to a set of paragraphs.   string magic(string s, int quote)   {    array(string) ret;    ret=({});       foreach(s/"\n\n",s)    {    sscanf(s,"\t%s",s); -  s=replace(s,"\n\t","\n"); +  s=predef::replace(s,"\n\t","\n");    ret += ({ more_magic(s, quote) });    }       return "<p>" + ret*"\n</p>\n<p>" + "</p>\n";   }      /*    * Magic to convert SYNTAX sections    */ - static inherit Regexp : lastident; - static inherit Regexp : megamagic; + protected inherit Regexp.SimpleRegexp : lastident; + protected inherit Regexp.SimpleRegexp : megamagic;      string syntax_magic(string s)   {    array(string) tmp;    int e;       while(tmp=megamagic::split(s))    {    s=tmp[0]+"<i>"+tmp[1]+"</i>"+tmp[2];    }
pike.git/lib/modules/Tools.pmod/AutoDoc.pmod/BMMLParser.pike:184:    return "<tt>"+magic(s,1)+"</tt>";   }         /* HTML quoting / unquoting */   array(string) from=({"&","<",">"});   array(string) to=({"&amp;","&lt;","&gt;"});      string html_quote(string s)   { -  return replace(s,from,to); +  return predef::replace(s,from,to);   }      string html_unquote(string s)   { -  return replace(s,to, from); +  return predef::replace(s,to, from);   }      string url_quote(string s)   { -  return replace(s, +  return predef::replace(s,    ({" ","`","\"","%"}),    ({"%20","%60","%22","%37"}));      }      string mkdocument(string s, string title, array(string)|void root)   {    string namespace = "predef";    if (root && sizeof(root) && has_suffix(root[0], "::")) {    namespace = root[0][..<2];
pike.git/lib/modules/Tools.pmod/AutoDoc.pmod/BMMLParser.pike:234:    return s;   }      string short(string s)   {    s=strip_prefix(s);    return short_descs[s] ? " - "+short_descs[s] : "";   }       - inherit Regexp:is_example; + inherit Regexp.SimpleRegexp:is_example;    - //! @ignore - list(string) indexes_done=(<>); - list(string) pages_done=(<>); - //! @endignore + multiset(string) indexes_done=(<>); + multiset(string) pages_done=(<>);      void done(string a)   {    pages_done[a]=1;   }      #if 0   string mkindex(string topic, int usehead)   {    string head;
pike.git/lib/modules/Tools.pmod/AutoDoc.pmod/BMMLParser.pike:301: Inside #if 0
   ret+="<li><a href='"+pages[a]+"'>"+strip_prefix(a)+"</a>"+short(a)+    "</li>\n";    }       ret+="</ul>\n";    break;       case "other":    head="<b>Other pages</b>\n";    ret="<ul>\n"; - // perror(sprintf("all pages: %O\n",sort(m_indices(pages)))); - // perror(sprintf("pages done: %O\n",sort(m_indices(pages_done)))); + // werror("all pages: %O\n",sort(m_indices(pages))); + // werror("pages done: %O\n",sort(m_indices(pages_done)));    foreach(my_sort(m_indices(pages) - indices(pages_done) ),a)    {    if(a[0..4]=="index") continue;    ret+="<li><a href='"+pages[a]+"'>"+strip_prefix(a)+"</a>"+short(a)+    "</li>\n";    }    ret+="</ul>\n";    break;       case "efuns":
pike.git/lib/modules/Tools.pmod/AutoDoc.pmod/BMMLParser.pike:325: Inside #if 0
   efuns-=(<"lambda","switch">);    foreach(my_sort(m_indices(efuns)),a)    {    a=html_quote(a);    done(a);    if(pages[a])    {    ret+="<li><a href='"+pages[a]+"'>"+strip_prefix(a)+"</a>"+short(a)+    "</li>\n";    }else{ -  perror("Warning: no page for function: "+a+".\n"); +  werror("Warning: no page for function: "+a+".\n");    }    }    ret+="</ul>\n";    break;       default:    if(!keywords[prefix+topic])    { -  perror("Unknown keyword "+topic+".\n"); +  werror("Unknown keyword "+topic+".\n");    return "";    }       head="<b><a name='"+topic+"'>";    head+=capitalize(topic)+"</a>";    head+=short(topic);    head+="</b>\n";    ret="<ul>\n";    foreach(my_sort(keywords[prefix+topic]),a)    {
pike.git/lib/modules/Tools.pmod/AutoDoc.pmod/BMMLParser.pike:372:    array(string)|void root)   {    string output, short = "";    int headno;    string name;       output="";       cont = cont||read_bytes(path);    - // perror("foo: "+path[strlen(path)-5..]+".\n"); + // werror("foo: "+path[strlen(path)-5..]+".\n");    if(sscanf(cont,"NAME\n\t%s - %s\n",name,short) ||    // Both of the following are broken syntax.    sscanf(cont,"NAME\t\n\t%s - %s\n",name,short) ||    sscanf(cont,"NAME\n %*[\t ]%s - %s\n",name,short))    {    cont=html_quote(cont);       short_descs[html_quote(name)]=short;       string header = "";
pike.git/lib/modules/Tools.pmod/AutoDoc.pmod/BMMLParser.pike:439:       sections=part/"\n\n";       /* Merge sections that do not have a header together */    for(section=0;section<sizeof(sections);section++)    {    string section_header = "";    if (has_prefix(sections[section], "\n"))    sections[section] = sections[section][1..];    sscanf(sections[section], "%s\n", section_header); -  if(!strlen(String.trim_all_whites(section_header)) || +  if(!strlen(String.trim(section_header)) ||    upper_case(section_header) != section_header ||    lower_case(section_header) == section_header)    {    sections[section-1]+="\n\n"+sections[section];    sections=sections[0..section-1]+sections[section+1..0x7fffffff];    section--;    }    }       string term_prev = "";    for(headno=0;headno<sizeof(sections);headno++)    {    string type, rest;    mixed a, b;    sscanf(sections[headno],"%s\n%s",type,rest);    -  type = type && String.trim_all_whites(type); +  type = type && String.trim(type);       switch(type)    {    case "NAME\t":    case "NAME":    if((sscanf(rest, "\t%s - %s", part_name, b)!=2) &&    (sscanf(rest," %*[\t ]%s - %s", part_name, b) != 3)) -  perror("Warning NAME section broken!\n"); +  werror("Warning NAME section broken!\n");       section_path = module_path + part_name/".";    if (!partno) {    module_path = section_path;    }       rest="\t<tt>"+part_name+"</tt> - "+b;       part_names = ({ part_name });   
pike.git/lib/modules/Tools.pmod/AutoDoc.pmod/BMMLParser.pike:490:    case "COPYRIGHT":    case "NOTA BENE":    case "WARNING":    case "THANKS":    case "BUGS":    rest=magic(rest, 0);    break;    case "DIRECTIVE":    if((sscanf(rest, "\t%s", part_name) != 1) &&    (sscanf(rest," %*[\t ]%s", part_name) != 2)) -  perror("Warning DIRECTIVE section broken!\n"); +  werror("Warning DIRECTIVE section broken!\n");       symbol_type = "directive"; -  part_names = map(part_name/"\n", String.trim_all_whites); +  part_names = map(part_name/"\n", String.trim);       rest = "";    break;    case "NOTES":    case "NOTES, TODOs AND OTHER THINGS":    type = "NOTES";    rest=magic(rest, 0);    break;    case "AUTHOR":    rest=magic("Author\n\n" + rest, 0);    break;       default:    werror("Warning: Unknown section header on page %O: %O\n",    path, type);    rest=magic(type + "\n\n" + rest,0);    break;       case "KEYWORD":    case "KEYWORDS": -  a=replace(rest,({"\n"," ","\t"}),({"","",""}))/","; +  a=predef::replace(rest,({"\n"," ","\t"}),({"","",""}))/",";    b=({});    foreach(a,a)    {    a=prefix+a;    keywords[a] = ( keywords[a] || ({}) ) | ({ prefix+name });    b+=({ strip_prefix(a) });    }    rest=implode_nicely(b);    break;       case "SEE ALSO": -  rest=replace(rest,({"\n"," ","\t"}),({"","",""})); +  rest=predef::replace(rest,({"\n"," ","\t"}),({"","",""}));    a=rest/",";    b = map(a, lambda(string tmp) {    string to = tmp;    foreach(({ "builtin/", "efun/", "files/", "math/",    "modules/math/", "modules/math", }),    string prefix) {    if (has_prefix(to, prefix)) {    to = "predef::" + to[sizeof(prefix)..];    break;    }
pike.git/lib/modules/Tools.pmod/AutoDoc.pmod/BMMLParser.pike:634:    "Pike: " + name, root);    }    else if(path[strlen(path)-5..]==".bmml")    {    // Ignore these for now.   #if 0    array(string) sections;    string title;    int section;    -  cont=replace(cont,"$version",version()); +  cont=predef::replace(cont,"$version",version());    cont=html_quote(cont);    sections=cont/"\n\n";       for(section=0;section<sizeof(sections);section++)    {    string tmp,pre,a,b;    tmp=sections[section];    sscanf(tmp,"%[\t ]",pre);       switch(pre)
pike.git/lib/modules/Tools.pmod/AutoDoc.pmod/BMMLParser.pike:658: Inside #if 0
   tmp="<h1><center>"+tmp+"</center></h1>";    break;       case " ":    sscanf(tmp," %s",tmp);    tmp="<h2>"+tmp+"</h2>";    break;       case " ":    sscanf(tmp," %s",tmp); -  tmp=replace(tmp,"\n ","\n"); +  tmp=predef::replace(tmp,"\n ","\n");    tmp=more_magic(tmp,0);    break;       case "\t":    sscanf(tmp,"\t%s %s",pre, a);    switch(pre)    {    case "KEYWORD_INDEX":    sscanf(a,"%s\n",a);    tmp=mkindex(a, 1);
pike.git/lib/modules/Tools.pmod/AutoDoc.pmod/BMMLParser.pike:689: Inside #if 0
   tmp="<a href='"+fippel_path(a)+"'>"+b+"</a>";    break;       case "TAG":    pages[prefix+a]=fippel_path(path)+"#"+a;    done(a);    tmp="<a name='"+a+"'>";    break;       default: -  perror("Unknown directive: "+pre+".\n"); +  werror("Unknown directive: "+pre+".\n");    }       }    sections[section]=tmp;    }    cont="<doc placeholder='true'><text><p>" + sections*"\n</p>\n<p>\n" +    "</p></text></doc>\n";       return mkdocument(cont, title || "Pike manual", root);   #else
pike.git/lib/modules/Tools.pmod/AutoDoc.pmod/BMMLParser.pike:752: Inside #if 0
   {    switch(pre)    {    case 2: output+="<h2>"+tmp+"</h2>"; break;    case 1:    if(tmp[-1]=='\n' && tmp[-2]=='\n')    tmp=tmp[..strlen(tmp)-2];    output+="<pre>\n"+tmp+"</pre>\n";    break;    case 0: -  output+="<p>" + replace(tmp,"\n\n","\n</p><p>\n") + "</p>\n"; +  output+="<p>" + predef::replace(tmp,"\n\n","\n</p><p>\n") + "</p>\n";    break;    }    pre=p;    tmp="";    }    }    tmp+=line+"\n";    }    output=mkdocument(output,"Pike: "+ -  replace((fname/"/")[-1],"_"," "), root); +  predef::replace((fname/"/")[-1],"_"," "), root);   #else    return "";   #endif    }    else    {    if ((flags & .FLAG_VERB_MASK) >= .FLAG_VERBOSE) -  perror("Warning: not converting "+path+".\n"); +  werror("Warning: not converting "+path+".\n");    output="";    }    return output;   }         void scanfiles(string path, string fname)   {    string nf,np;    nf=convert_page(path, fname);
pike.git/lib/modules/Tools.pmod/AutoDoc.pmod/BMMLParser.pike:800:   }      /** Traverse directory **/   void traversedir(string path)   {    object tmp;    string file,tmp2;    string _prefix=prefix;       -  tmp=clone(FILE); +  tmp=FILE();    if(tmp->open(path+"/.bmmlrc","r"))    {    while(tmp2=tmp->gets())    {    string bar="";    sscanf(tmp2,"%*[ \t]%s",tmp2);    if(!strlen(tmp2) || tmp2[0]=='#') continue;    sscanf(tmp2,"%s %s",tmp2,bar);    switch(tmp2)    {    case "prefix":    prefix+=bar+"__";    break;    }    }    }       destruct(tmp);    -  foreach(get_dir(path) - ({"CVS","RCS",".cvsignore",".bmmlrc"}),file) +  foreach(get_dir(path) - ({".gitignore",".bmmlrc"}),file)    {    string tmp;    if(file[-1]=='~') continue;    if(file[0]=='#' && file[-1]=='#') continue;    if(file[0]=='.' && file[1]=='#') continue;       tmp=path+"/"+file;       if(file_size(tmp)==-2)    {
pike.git/lib/modules/Tools.pmod/AutoDoc.pmod/BMMLParser.pike:843:    scanfiles(tmp,file);    }    }       prefix=_prefix;   }      void dodocs(string path, int module)   {    cd(path); -  perror("Doing "+path+"\n"); +  werror("Doing "+path+"\n");    if(!module)    {    docdir="";    }else{    docdir="module"+module;    if(sscanf(reverse(path),"cod/%s",docdir))    {    sscanf(docdir,"%s/",docdir);    docdir=reverse(docdir);    }
pike.git/lib/modules/Tools.pmod/AutoDoc.pmod/BMMLParser.pike:882:    is_example::create("^(" LINE ")+$");   }      int main(int argc, array(string) argv)   {    string np;    int e;       if(argc < 3)    { -  perror("Usage: html_docs.pike to_path from_path [module_doc_path ...]\n"); +  werror("Usage: html_docs.pike to_path from_path [module_doc_path ...]\n");    exit(0);    }    - // perror(sprintf("argv = %O\n",argv)); + // werror("argv = %O\n",argv);       for(e=1;e<sizeof(argv);e++)    argv[e]=combine_path(getcwd(),argv[e]);       new_path=argv[1];       write("Scanning pages for links and keywords.\n");    //writepages=0;    for(e=2;e<sizeof(argv);e++) dodocs(argv[e],e-2);    //writepages=0;
pike.git/lib/modules/Tools.pmod/AutoDoc.pmod/BMMLParser.pike:910:    foreach(keywords[np] || ({}), np)    done(np);          write("Writing html files.\n");    //writepages=1;    for(e=2;e<sizeof(argv);e++) dodocs(argv[e],e-2);       foreach(indices(keywords) - indices(indexes_done),np)    { -  perror("Keywords never indexed: "+np+"\n"); +  werror("Keywords never indexed: "+np+"\n");    }   }