libFAUDES

Sections

Index

rti2html.cpp

Go to the documentation of this file.
00001 /** rti2hmtl.cpp  Utility to generate a html documents from rti files */
00002 
00003 /* FAU Discrete Event Systems Library (libfaudes)
00004 
00005 Copyright (C) 2009 Ruediger Berndt
00006 Copyright (C) 2009 Thomas Moor
00007 
00008 This library is free software; you can redistribute it and/or
00009 modify it under the terms of the GNU Lesser General Public
00010 License as published by the Free Software Foundation; either
00011 version 2.1 of the License, or (at your option) any later version.
00012 
00013 This library is distributed in the hope that it will be useful,
00014 but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016 Lesser General Public License for more details.
00017 
00018 You should have received a copy of the GNU Lesser General Public
00019 License along with this library; if not, write to the Free Software
00020 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
00021    
00022 
00023 #include <string>
00024 #include <cctype>
00025 #include <iostream>
00026 #include <fstream>
00027 #include "corefaudes.h"
00028 
00029 
00030 using namespace faudes;
00031 
00032 // ******************************************************************
00033 // error exit
00034 // ******************************************************************
00035 
00036 void Usage(const std::string& rMessage="") {
00037   // ui hints
00038   if(rMessage!="") {
00039     std::cerr << rMessage << std::endl;
00040     std::cout << "" << std::endl;
00041   }
00042   std::cerr << "rti2html: " << FDVersionString()  << std::endl;
00043   std::cout << "" << std::endl;
00044   std::cerr << "utility to generate an html snippets from an libFAUDES" << std::endl;
00045   std::cerr << "run-time interface definition file" << std::endl;
00046   std::cerr << std::endl << "usage: " << std::endl;
00047   std::cerr << " rti2html <rti input file>  -dump <rti output file>" << std::endl;
00048   std::cerr << " rti2html <rti output file> -merge <rti input file(s)>" << std::endl;
00049   std::cerr << " rti2html <rti input file>  -index <html output file>" << std::endl;
00050   std::cerr << " rti2html <rti input file>  -section <plugin name> <html output file>" << std::endl;
00051   std::cerr << " rti2html <rti input file>  -type <type name> <html output file>" << std::endl;
00052   std::cerr << " rti2html <rti input file>  -function <function name> <html output file>" << std::endl;
00053   std::cerr << " rti2html <rti input file>  -alltypes <html output file>" << std::endl;
00054   std::cerr << " rti2html <rti input file>  -allfunctions <html output file>" << std::endl;
00055   std::cerr << " rti2html <rti input file>  -allsections <html output file>" << std::endl;
00056   std::cerr << " rti2html <rti input file>  -signature <function name> <html output file>" << std::endl;
00057   std::cerr << " rti2html <rti input file>  -shortdoc <function/type name> <html output file>" << std::endl;
00058   std::cerr << " rti2html <rti input file>  -contrib <html output file>" << std::endl;
00059   std::cerr << std::endl << "note: use \"-\" as output file for console output" << std::endl;
00060   exit(1);
00061 }
00062 
00063 
00064 // ******************************************************************
00065 // helper: html for linked type name
00066 // ******************************************************************
00067 
00068 void TypeHtml(std::ostream* pStream, const std::string& rTypeName) {
00069 
00070   // just text if unknown
00071   if(!TypeRegistry::G()->Exists(rTypeName)) {
00072     *pStream << rTypeName;
00073     return;
00074   }
00075 
00076   // retrieve definition
00077   const TypeDefinition& tdef=TypeRegistry::G()->Definition(rTypeName);
00078   std::string tyname = tdef.Name();
00079   std::string tyhtml = tdef.HtmlDoc();
00080   if(tyhtml=="" || tyhtml=="none") {
00081     *pStream << tyname;
00082   } else {
00083     *pStream << "<a href=\"" << tyhtml << "\">" << tyname << "</a>";
00084   }
00085 }
00086 
00087 // ******************************************************************
00088 // helper: html for linked function name
00089 // ******************************************************************
00090 
00091 void FunctionHtml(std::ostream* pStream, const std::string& rFunctionName) {
00092 
00093   // just text if unknown
00094   if(!FunctionRegistry::G()->Exists(rFunctionName)) {
00095     *pStream << rFunctionName;
00096     return;
00097   }
00098 
00099   // retrieve definition
00100   const FunctionDefinition& fdef=FunctionRegistry::G()->Definition(rFunctionName);
00101   std::string fname = fdef.Name();
00102   std::string fhtml = fdef.HtmlDoc();
00103   if(fhtml=="" || fhtml=="none") {
00104     *pStream << fname;
00105   } else {
00106     *pStream << "<a href=\"" << fhtml << "\">" << fname << "</a>";
00107   }
00108 }
00109 
00110 // ******************************************************************
00111 // search for types
00112 // ******************************************************************
00113 
00114 void SearchTypesHtml(std::ostream* pIndexFile, const std::string& key="") {
00115 
00116   // traverse type registry
00117   bool found=false;
00118   for(TypeRegistry::Iterator tit=TypeRegistry::G()->Begin(); 
00119     tit!=TypeRegistry::G()->End(); tit++) 
00120   {
00121     // test for exact key
00122     if(key!="") {
00123       std::string section= tit->second->Name();
00124       std::transform(section.begin(), section.end(), section.begin(), tolower);
00125       if(section!=key) continue;
00126     }
00127     // test for user doc
00128     if(tit->second->TextDoc()=="") continue;
00129     // fine tune format
00130     if(found) *pIndexFile << "," << std::endl;
00131     // record success
00132     if(!found) found=true;
00133     // link for this type
00134     TypeHtml(pIndexFile,tit->second->Name());
00135   }
00136   // done
00137   if(!found) *pIndexFile << "<i>no matches found</i>" << std::endl;
00138 }
00139 
00140 // ******************************************************************
00141 // search for functions
00142 // ******************************************************************
00143 
00144 void SearchFunctionsHtml(std::ostream* pIndexFile, const std::string& key="") {
00145 
00146   // traverse function registry
00147   bool found=false;
00148   for(FunctionRegistry::Iterator fit=FunctionRegistry::G()->Begin(); 
00149     fit!=FunctionRegistry::G()->End(); fit++) 
00150   {
00151     // test for exact key
00152     if(key!="") {
00153       std::string section= fit->second->Name();
00154       std::transform(section.begin(), section.end(), section.begin(), tolower);
00155       if(section!=key) continue;
00156     }
00157     // test for user doc
00158     if(fit->second->TextDoc()=="") continue;
00159     // fine tune format
00160     if(found) *pIndexFile << "," << std::endl;
00161     // record success
00162     if(!found) found=true;
00163     // link for this type
00164     FunctionHtml(pIndexFile,fit->second->Name());
00165   }
00166   // done
00167   if(!found) *pIndexFile << "<i>no matches found</i>" << std::endl;
00168 }
00169 
00170 // ******************************************************************
00171 // search for sections
00172 // ******************************************************************
00173 
00174 void SearchSectionsHtml(std::ostream* pIndexFile, const std::string& key="") {
00175 
00176   // prepare list of all sections
00177   std::set< std::string > sections;
00178   for(TypeRegistry::Iterator tit=TypeRegistry::G()->Begin(); 
00179     tit!=TypeRegistry::G()->End(); tit++) 
00180   {
00181     std::string section= tit->second->KeywordAt(0);
00182     // bail out on trivial
00183     if(section=="") continue;
00184     // test for exact key
00185     if(key!="") {
00186       std::transform(section.begin(), section.end(), section.begin(), tolower);
00187       if(section!=key) continue;
00188     }
00189     // record
00190     sections.insert(tit->second->KeywordAt(0));
00191   }
00192   for(FunctionRegistry::Iterator fit=FunctionRegistry::G()->Begin(); 
00193     fit!=FunctionRegistry::G()->End(); fit++) 
00194   {
00195     std::string section=fit->second->KeywordAt(0);
00196     // bail out on trivial
00197     if(section=="") continue;
00198     // test for exact key
00199     if(key!="") {
00200       std::transform(section.begin(), section.end(), section.begin(), tolower);
00201       if(section!=key) continue;
00202     }
00203     // record
00204     sections.insert(fit->second->KeywordAt(0));
00205   }
00206 
00207   // list sections as links
00208   std::set< std::string >::iterator pit;
00209   for(pit=sections.begin(); pit != sections.end(); pit++) {
00210     // get nice name
00211     std::string sname = *pit;
00212     // have link
00213     std::string shtml = sname+"_index.html";
00214     std::transform(shtml.begin(),shtml.end(),shtml.begin(),tolower);
00215     // have entry as link
00216     if(pit!=sections.begin()) *pIndexFile << "," << std::endl;
00217     *pIndexFile << "<a href=\"" << shtml << "\">" << sname << "</a>";
00218   }
00219 }
00220 
00221 
00222 
00223 
00224 // ******************************************************************
00225 // type index by keyword (aka section name)
00226 // ******************************************************************
00227 
00228 void TypeIndexHtml(std::ostream* pIndexFile, const std::string& key="") {
00229 
00230   // traverse type registry
00231   bool head=false;
00232   for(TypeRegistry::Iterator tit=TypeRegistry::G()->Begin(); 
00233     tit!=TypeRegistry::G()->End(); tit++) 
00234   {
00235     // test for exact key
00236     if(key!="") {
00237       std::string section= tit->second->KeywordAt(0);
00238       std::transform(section.begin(), section.end(), section.begin(), tolower);
00239       if(section!=key) continue;
00240     }
00241     // test for user doc
00242     if(tit->second->TextDoc()=="") continue;
00243     // head line
00244     if(!head) {
00245       head=true;
00246       *pIndexFile << "<div class=\"registry_heading\"><strong>Types</strong></div>" << std::endl;
00247     }
00248     // index entry for this type
00249     *pIndexFile << "<div class=\"registry_index\">";
00250     TypeHtml(pIndexFile,tit->second->Name());
00251     *pIndexFile << "</div>" << std::endl;
00252   }
00253   // done
00254   if(head) *pIndexFile << "<br>" << std::endl;
00255 }
00256 
00257 // ******************************************************************
00258 // function index by keyword (aka plugin)
00259 // ******************************************************************
00260 
00261   void FunctionIndexHtml(std::ostream* pIndexFile, const std::string& key="") {
00262 
00263   // traverse function registry
00264   bool head=false;
00265   for(FunctionRegistry::Iterator fit=FunctionRegistry::G()->Begin(); 
00266     fit!=FunctionRegistry::G()->End(); fit++) 
00267   {
00268     // test for key
00269     if(key!="") {
00270       std::string section= fit->second->KeywordAt(0);
00271       std::transform(section.begin(), section.end(), section.begin(), tolower);
00272       if(section!=key) continue;
00273     }
00274     // test for user doc
00275     if(fit->second->TextDoc()=="") continue;
00276     // index entry for this function
00277     std::string fname = fit->second->Name();
00278     std::string fhtml = fit->second->HtmlDoc();
00279     if(fhtml=="") continue;
00280     // head line
00281     if(!head){
00282       head=true;
00283       *pIndexFile << "<div class=\"registry_heading\"><strong>Functions</strong></div>" << std::endl;
00284     }
00285     // list entry
00286     if(fhtml=="none") {
00287       *pIndexFile << "<div class=\"registry_index\"> "<< fhtml << "</div>" << std::endl;
00288       continue;
00289     }
00290     *pIndexFile << "<div class=\"registry_index\"><a href=\"" << fhtml << "\">" 
00291       << fname << "</a></div>" << std::endl;
00292   }
00293 
00294   // done
00295   if(head) *pIndexFile << "<br>" << std::endl;
00296 
00297 }
00298 
00299 // ******************************************************************
00300 // html index by keyword (aka section)
00301 // ******************************************************************
00302 
00303 void IndexHtml(std::ostream* pIndexFile, const std::string& key="") {
00304 
00305   // prepare list of all sections
00306   std::set< std::string > sections;
00307   for(TypeRegistry::Iterator tit=TypeRegistry::G()->Begin(); 
00308     tit!=TypeRegistry::G()->End(); tit++) 
00309   {
00310     std::string sec=tit->second->KeywordAt(0);
00311     if(sec!="") sections.insert(sec);
00312   }
00313   for(FunctionRegistry::Iterator fit=FunctionRegistry::G()->Begin(); 
00314     fit!=FunctionRegistry::G()->End(); fit++) 
00315   {
00316     std::string sec=fit->second->KeywordAt(0);
00317     if(sec!="") sections.insert(sec);
00318   }
00319 
00320   // sections headline
00321   if(sections.size()!=0) 
00322     *pIndexFile << "<div class=\"registry_heading\"><strong>Sections</strong></div>" << std::endl;
00323 
00324   // list sections
00325   std::set< std::string >::iterator pit;
00326   for(pit=sections.begin(); pit != sections.end(); pit++) {
00327     // get nice name
00328     std::string sname = *pit;
00329     // have link
00330     std::string shtml = sname+"_index.html";
00331     std::transform(shtml.begin(),shtml.end(),shtml.begin(),tolower);
00332     // have entry
00333     *pIndexFile << "<div class=\"registry_index\"><a href=\"" << shtml << "\">" 
00334       << sname << "</a></div>" << std::endl;
00335   }
00336 
00337   // sections done
00338   if(sections.size()!=0) 
00339     *pIndexFile << "<br>" << std::endl;
00340 
00341   // list types
00342   TypeIndexHtml(pIndexFile,key);
00343 
00344   // list functions
00345   FunctionIndexHtml(pIndexFile,key);
00346 }
00347 
00348 // ******************************************************************
00349 // signature
00350 // ******************************************************************
00351 
00352 void SignatureHtml(std::ostream* pOutFile, std::string function) {
00353 
00354   // bail out on non existence
00355   if(!FunctionRegistry::G()->Exists(function)) return;
00356 
00357   // function definition
00358   const FunctionDefinition& fdef=FunctionRegistry::G()->Definition(function);
00359   
00360   // bail out if there is no signature
00361   if(fdef.VariantsSize()==0) return;
00362 
00363   // start signature box
00364   *pOutFile << "<div class=\"registry_signature\">" << std::endl;
00365   *pOutFile << "<p><strong>Signature:</strong></p>" << std::endl;
00366   *pOutFile << "<p>" << std::endl;
00367 
00368   // loop signatures
00369   for(int i=0; i< fdef.VariantsSize(); i++) {
00370     const Signature& sigi=fdef.Variant(i);
00371     *pOutFile << fdef.Name() << "(";
00372     // loop params
00373     for(int j=0; j < sigi.Size(); j++) {
00374       if(j!=0) *pOutFile << ", ";
00375       const Parameter& parj=sigi.At(j);
00376       *pOutFile << "+" << Parameter::AStr(parj.Attribute()) << "+ ";
00377       TypeHtml(pOutFile,parj.Type());
00378       *pOutFile << " <i>" << parj.Name() << "</i>";
00379     }
00380     *pOutFile << ")<br>" << std::endl;
00381   }
00382   *pOutFile << "</p>" << std::endl;
00383 
00384 
00385   // close signature box
00386   *pOutFile << std::endl << "</div>" << std::endl;
00387 }
00388 
00389 // ******************************************************************
00390 // short doc
00391 // ******************************************************************
00392 
00393 void ShortdocHtml(std::ostream* pOutFile, std::string fname) {
00394 
00395   // its a function
00396   if(FunctionRegistry::G()->Exists(fname)) {
00397 
00398     // function definition
00399     const FunctionDefinition& fdef=FunctionRegistry::G()->Definition(fname);
00400   
00401     // stream doc
00402     *pOutFile << "<div class=\"registry_signature\">" << std::endl;
00403     *pOutFile << "<p>" << fdef.TextDoc() << "<p>" << std::endl;
00404     *pOutFile << "</div>" << std::endl;
00405 
00406     // stream signature
00407     SignatureHtml(pOutFile,fname);
00408   }
00409 
00410   // its a type
00411   if(TypeRegistry::G()->Exists(fname)) {
00412 
00413     // type definition
00414     const TypeDefinition& tdef=TypeRegistry::G()->Definition(fname);
00415   
00416     // stream doc
00417     *pOutFile << "<div class=\"registry_signature\">" << std::endl;
00418     *pOutFile << "<p>" << tdef.TextDoc() << "<p>" << std::endl;
00419     *pOutFile << "</div>" << std::endl;
00420 
00421   }
00422 
00423 
00424 }
00425 
00426 // ******************************************************************
00427 // command line ui
00428 // ******************************************************************
00429 
00430 
00431 int main(int argc, char *argv[]) {
00432 
00433   // min args
00434   if(argc < 3) Usage();
00435 
00436   // merge registry files
00437   if(std::string(argv[2])=="-merge") {
00438     // bail out
00439     if(argc < 4) Usage();
00440     // load from files
00441     for(int i=3; i< argc; i++) {
00442       TypeRegistry::G()->MergeDocumentation(std::string(argv[i]));
00443       FunctionRegistry::G()->MergeDocumentation(std::string(argv[i]));
00444     }
00445     // dump
00446     if(std::string(argv[1]) != "-") {
00447       SaveRegistry(std::string(argv[1]));
00448     } else {
00449       SaveRegistry();
00450     }
00451     return 0;
00452   } 
00453 
00454   // load registry 
00455   LoadRegistry(argv[1]);
00456 
00457   // dump registry
00458   if(std::string(argv[2])=="-dump") {
00459     // bail out
00460     if(argc != 4) Usage();
00461     // dump
00462     if(std::string(argv[argc-1]) != "-") {
00463       SaveRegistry(std::string(argv[argc-1]));
00464     } else {
00465       SaveRegistry();
00466     }
00467     return 0;
00468   } 
00469 
00470   // output file
00471   std::ostream* hout= &std::cout;
00472   std::ofstream fout;
00473   if(std::string(argv[argc-1]) != "-") {
00474     fout.open(argv[argc-1], std::ios::out);
00475     hout = &fout;
00476   }
00477   
00478   // generate global index
00479   if(std::string(argv[2])=="-index") {
00480     // bail out
00481     if(argc != 4) Usage();
00482     // run and done
00483     IndexHtml(hout);
00484     return 0;
00485   } 
00486 
00487   // generate section index
00488   if(std::string(argv[2])=="-section") {
00489     // bail out
00490     if(argc != 5) Usage();
00491     // have lower case plugin pattern
00492     std::string pattern=std::string(argv[3]);
00493     std::transform(pattern.begin(), pattern.end(), pattern.begin(), tolower);
00494     // run and done
00495     IndexHtml(hout,pattern);
00496     return 0;
00497   } 
00498 
00499   // generate linked function
00500   if(std::string(argv[2])=="-function") {
00501     // bail out
00502     if(argc != 5) Usage();
00503     // run and done
00504     FunctionHtml(hout,argv[3]);
00505     return 0;
00506   } 
00507 
00508   // generate linked function list
00509   if(std::string(argv[2])=="-allfunctions") {
00510     // bail out
00511     if(argc != 4) Usage();
00512     // run and done
00513     SearchFunctionsHtml(hout);
00514     return 0;
00515   } 
00516 
00517   // generate linked type
00518   if(std::string(argv[2])=="-type") {
00519     // bail out
00520     if(argc != 5) Usage();
00521     // run and done
00522     TypeHtml(hout,argv[3]);
00523     return 0;
00524   } 
00525 
00526   // generate all types
00527   if(std::string(argv[2])=="-alltypes") {
00528     // bail out
00529     if(argc != 4) Usage();
00530     // run and done
00531     SearchTypesHtml(hout);
00532     return 0;
00533   } 
00534 
00535   // generate all sections
00536   if(std::string(argv[2])=="-allsections") {
00537     // bail out
00538     if(argc != 4) Usage();
00539     // run and done
00540     SearchSectionsHtml(hout);
00541     return 0;
00542   } 
00543 
00544   // generate signature
00545   if(std::string(argv[2])=="-signature") {
00546     // bail out
00547     if(argc != 5) Usage();
00548     // run and done
00549     SignatureHtml(hout,argv[3]);
00550     return 0;
00551   } 
00552 
00553   // generate short doc
00554   if(std::string(argv[2])=="-shortdoc") {
00555     // bail out
00556     if(argc != 5) Usage();
00557     // run and done
00558     ShortdocHtml(hout,argv[3]);
00559     return 0;
00560   } 
00561 
00562   // plain text contributers
00563   if(std::string(argv[2])=="-contrib") {
00564     // bail out
00565     if(argc != 4) Usage();
00566     // run and done
00567     *hout << FDContributorsString();
00568     return 0;
00569   } 
00570 
00571   // error
00572   Usage();
00573   return(1);
00574 }

libFAUDES 2.18b --- 2010-12-17 --- c++ source docu by doxygen 1.6.3