"Fossies" - the Fresh Open Source Software Archive

Member "ansifilter-2.18/src/htmlgenerator.cpp" (30 Jan 2021, 17466 Bytes) of package /linux/privat/ansifilter-2.18.tar.bz2:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "htmlgenerator.cpp" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.17_vs_2.18.

    1 /***************************************************************************
    2                      htmlgenerator.cpp  -  description
    3                              -------------------
    4 
    5     copyright            : (C) 2007-2018 by Andre Simon
    6     email                : a.simon@mailbox.org
    7  ***************************************************************************/
    8 
    9 /*
   10 This file is part of ANSIFilter.
   11 
   12 ANSIFilter is free software: you can redistribute it and/or modify
   13 it under the terms of the GNU General Public License as published by
   14 the Free Software Foundation, either version 3 of the License, or
   15 (at your option) any later version.
   16 
   17 ANSIFilter is distributed in the hope that it will be useful,
   18 but WITHOUT ANY WARRANTY; without even the implied warranty of
   19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   20 GNU General Public License for more details.
   21 
   22 You should have received a copy of the GNU General Public License
   23 along with ANSIFilter.  If not, see <http://www.gnu.org/licenses/>.
   24 */
   25 
   26 #include <fstream>
   27 #include <iostream>
   28 #include <sstream>
   29 #include <algorithm>
   30 
   31 #include "htmlgenerator.h"
   32 #include "version.h"
   33 
   34 namespace ansifilter
   35 {
   36 
   37 HtmlGenerator::HtmlGenerator ():
   38     CodeGenerator(HTML),
   39     fileSuffix(".html")
   40 {
   41     newLineTag="\n";
   42     styleCommentOpen="/*";
   43     styleCommentClose="*/";
   44     spacer=" ";
   45 }
   46 
   47 string HtmlGenerator::getOpenTag()
   48 {
   49     ostringstream fmtStream;
   50     string attrName("style");
   51 
   52     if (applyDynStyles){
   53         attrName = "class";
   54 
   55         ostringstream fgColStream;
   56          if (elementStyle.isFgColorSet()) {
   57             fgColStream << elementStyle.getFgColour().getRed(HTML)
   58                         << elementStyle.getFgColour().getGreen(HTML)
   59                         << elementStyle.getFgColour().getBlue(HTML);
   60          }
   61 
   62         ostringstream bgColStream;
   63         if (elementStyle.isBgColorSet()) {
   64 
   65             bgColStream << elementStyle.getBgColour().getRed(HTML)
   66                         << elementStyle.getBgColour().getGreen(HTML)
   67                         << elementStyle.getBgColour().getBlue(HTML);
   68         }
   69 
   70         StyleInfo sInfo( fgColStream.str(), bgColStream.str(),
   71                          elementStyle.isBold(), elementStyle.isItalic(), elementStyle.isConceal(),
   72                          elementStyle.isBlink(), elementStyle.isUnderline() );
   73 
   74         std::vector<StyleInfo>::iterator fit = std::find(documentStyles.begin(), documentStyles.end(), sInfo );
   75         if (fit == documentStyles.end()){
   76             documentStyles.push_back(sInfo);
   77             fmtStream << "af_"<< documentStyles.size();
   78         } else {
   79             fmtStream << "af_"<< 1+(int)std::distance(documentStyles.begin(), fit);
   80         }
   81 
   82     } else {
   83 
   84         if (elementStyle.isBold()) {
   85             fmtStream<< "font-weight:bold;";
   86         }
   87         if (elementStyle.isItalic()) {
   88             fmtStream<< "font-style:italic;";
   89         }
   90         if (elementStyle.isBlink()) {
   91             fmtStream<< "text-decoration:blink;";
   92         }
   93         if (elementStyle.isUnderline()) {
   94             fmtStream<< "text-decoration:underline;";
   95         }
   96         if (elementStyle.isConceal()) {
   97             fmtStream<< "display:none;";
   98         }
   99 
  100         if (elementStyle.isFgColorSet()) {
  101             fmtStream << "color:#"
  102                     << elementStyle.getFgColour().getRed(HTML)
  103                     << elementStyle.getFgColour().getGreen(HTML)
  104                     << elementStyle.getFgColour().getBlue(HTML)
  105                     << ";";
  106         }
  107 
  108         if (elementStyle.isBgColorSet()) {
  109             fmtStream <<"background-color:#"
  110                     << elementStyle.getBgColour().getRed(HTML)
  111                     << elementStyle.getBgColour().getGreen(HTML)
  112                     << elementStyle.getBgColour().getBlue(HTML)
  113                     <<";";
  114         }
  115     }
  116 
  117 
  118     string fmt  = fmtStream.str();
  119     tagIsOpen = fmt.size()>0;
  120     if (tagIsOpen) {
  121         ostringstream spanTag;
  122         spanTag<< "<span "<<attrName<<"=\""<<fmt<<"\">";
  123         return spanTag.str();
  124     }
  125     return "";
  126 }
  127 
  128 string HtmlGenerator::getCloseTag()
  129 {
  130     string retVal = tagIsOpen ? "</span>"  : "";
  131     tagIsOpen = false;
  132     return retVal;
  133 }
  134 
  135 string HtmlGenerator::getGeneratorComment()
  136 {
  137     ostringstream s;
  138     s << "<!--HTML generated by ansifilter "
  139       << ANSIFILTER_VERSION << ", " <<  ANSIFILTER_URL <<"-->\n";
  140 
  141     return s.str();
  142 }
  143 
  144 string HtmlGenerator::getHeader()
  145 {
  146     ostringstream os;
  147     os << "<!DOCTYPE html>"
  148        << "\n<html>\n<head>\n";
  149     if (encodingDefined()) {
  150         os << "<meta charset=\""
  151            << encoding
  152            << "\">\n";
  153     }
  154 
  155     os << "<style type=\"text/css\">\n";
  156     os << "pre {\n";
  157     os << "  font-family:"<< font << ";\n";
  158     os << "  font-size:"<< fontSize << ";\n";
  159 
  160     if (parseCP437) {
  161         os << "  color: #e5e5e5;\n";
  162     }
  163     os << "}\n\n";
  164     os << ".af_line {\n";
  165     os << "  color: gray;\n";
  166     os << "  text-decoration: none;\n";
  167     os << "}\n\n";
  168 
  169     if (parseCP437||parseAsciiBin || parseAsciiTundra) {
  170      os << "body {  background-color: black; } \n";
  171     }
  172     os << "</style>\n";
  173 
  174     if (!styleSheetPath.empty()) {
  175         os << "<link rel=\"stylesheet\" type=\"text/css\" href=\""
  176            << styleSheetPath << "\">\n";
  177     }
  178     os << "<title>" << docTitle << "</title>\n";
  179     os << "</head>\n<body>\n<pre>";
  180 
  181     return os.str();
  182 }
  183 
  184 string HtmlGenerator::getFooter()
  185 {
  186     string footer;
  187     footer += getCloseTag();
  188     footer += "</pre>\n</body>\n</html>\n";
  189 
  190      if (!omitVersionInfo)
  191         footer += getGeneratorComment();
  192 
  193     return footer;
  194 }
  195 
  196 void HtmlGenerator::printBody()
  197 {
  198     processInput();
  199 }
  200 
  201 void HtmlGenerator::insertLineNumber ()
  202 {
  203   if ( showLineNumbers && !parseCP437) {
  204 
  205         ostringstream lnum;
  206         lnum << setw ( 5 ) << right;
  207         if ( numberCurrentLine ) {
  208 
  209             lnum << lineNumber;
  210             if (addFunnyAnchors)
  211                 *out << "<a href=\"#l_" << lineNumber<< "\"";
  212             else
  213                 *out << "<span";
  214 
  215             if (addAnchors) {
  216                 *out << " id=\"l_" << lineNumber<< "\" ";
  217             }
  218             *out << " class=\"af_line\">";
  219 
  220             *out <<lnum.str() << ( addFunnyAnchors  ? "</a> " : "</span> ");
  221         } else {
  222             *out << lnum.str(); //for indentation
  223         }
  224     }
  225 }
  226 
  227 string HtmlGenerator::getHyperlink(string uri, string txt){
  228     ostringstream os;
  229     os <<"<a href='"<<uri<<"'>"<<txt<<"</a>";
  230     return os.str();
  231 }
  232 
  233 bool HtmlGenerator::printDynamicStyleFile ( const string &outPath ) {
  234 
  235     //do not overwrite
  236     ifstream infile(outPath.c_str());
  237     if (infile.good()) return true;
  238 
  239     ofstream indexfile ( outPath.c_str() );
  240 
  241     if ( !indexfile.fail() ) {
  242         indexfile << "/* CSS generated by ansifilter - styles derived from document formatting\n   Ansifilter will not overwrite this file\n*/\n";
  243 
  244         for (unsigned int i=0; i<documentStyles.size();i++){
  245             StyleInfo sInfo = documentStyles[i];
  246             indexfile << "span.af_" << (i+1) <<" {";
  247 
  248             if (sInfo.isBold) {
  249                 indexfile<< "font-weight:bold;";
  250             }
  251             if (sInfo.isItalic) {
  252                 indexfile<< "font-style:italic;";
  253             }
  254             if (sInfo.isBlink) {
  255                 indexfile<< "text-decoration:blink;";
  256             }
  257             if (sInfo.isUnderLine) {
  258                 indexfile<< "text-decoration:underline;";
  259             }
  260             if (sInfo.isConcealed) {
  261                 indexfile<< "display:none;";
  262             }
  263 
  264             if (sInfo.fgColor!="") {
  265                 indexfile << "color:#"
  266                           << sInfo.fgColor
  267                           << ";";
  268             }
  269 
  270             if (sInfo.bgColor!="") {
  271                 indexfile << "background-color:#"
  272                           << sInfo.bgColor
  273                           << ";";
  274             }
  275 
  276             indexfile << "}\n";
  277         }
  278 
  279 
  280     } else {
  281         return false;
  282     }
  283     return true;
  284 }
  285 
  286 string HtmlGenerator::maskCharacter(unsigned char c)
  287 {
  288     switch (c) {
  289     case '<' :
  290         return "&lt;";
  291         break;
  292     case '>' :
  293         return "&gt;";
  294         break;
  295     case '&' :
  296         return "&amp;";
  297         break;
  298     case '\"' :
  299         return "&quot;";
  300         break;
  301     case '\'' :
  302       return "&apos;";
  303       break;
  304     case '\t' : // see deletion of nonprintable chars below
  305         return "\t";
  306         break;
  307 
  308     case '@' :
  309         return "&#64;";
  310         break;
  311 
  312     default :
  313         if (c>0x1f ) { // printable?
  314             return string( 1, c );
  315         } else {
  316             return "";
  317         }
  318     }
  319 }
  320 
  321 string HtmlGenerator::maskCP437Character(unsigned char c)
  322 {
  323   switch (c) {
  324     case 0 :
  325       return " ";
  326       break;
  327 
  328     case '<' :
  329       return "&lt;";
  330       break;
  331     case '>' :
  332       return "&gt;";
  333       break;
  334     case '&' :
  335       return "&amp;";
  336       break;
  337     case '\"' :
  338       return "&quot;";
  339       break;
  340     case '\'' :
  341       return "&apos;";
  342       break;
  343     case '\t' : // see deletion of nonprintable chars below
  344       return "\t";
  345       break;
  346 
  347     case 0x01:
  348       return "&#x263a;";
  349       break;
  350     case 0x02:
  351       return "&#x263b;";
  352       break;
  353     case 0x03:
  354       return "&#x2665;";
  355       break;
  356     case 0x04:
  357       return "&#x2666;";
  358       break;
  359     case 0x05:
  360       return "&#x2663;";
  361       break;
  362     case 0x06:
  363       return "&#x2660;";
  364       break;
  365     case 0x08:
  366       return "&#x25d8;";
  367       break;
  368 
  369     case 0x0a:
  370       return "&#x25d9;";
  371       break;
  372     case 0x0b:
  373       return "&#x2642;";
  374       break;
  375     case 0x0c:
  376       return "&#x2640;";
  377       break;
  378 
  379     case 0x10:
  380       return "&#x25BA;";
  381       break;
  382     case 0x11:
  383       return "&#x25C4;";
  384       break;
  385     case 0x12:
  386       return "&#x2195;";
  387       break;
  388     case 0x13:
  389       return "&#x203C;";
  390       break;
  391     case 0x14:
  392       return "&#x00b6;";
  393       break;
  394     case 0x15:
  395       return "&#x00a7;";
  396       break;
  397     case 0x16:
  398       return "&#x25ac;";
  399       break;
  400     case 0x17:
  401       return "&#x21A8;";
  402       break;
  403     case 0x18:
  404       return "&#x2191;";
  405       break;
  406     case 0x19:
  407       return "&#x2193;";
  408       break;
  409     case 0x1a:
  410       return "&#x2192;";
  411       break;
  412     case 0x1b:
  413       return "&#x2190;";
  414       break;
  415     case 0x1c:
  416       return "&#x221F;";
  417       break;
  418     case 0x1d:
  419       return "&#x2194;";
  420       break;
  421     case 0x1e:
  422       return "&#x25B2;";
  423       break;
  424     case 0x1f:
  425       return "&#x25BC;";
  426       break;
  427 
  428     case 0x80:
  429       return "&#x00c7;";
  430       break;
  431     case 0x81:
  432       return "&#x00fc;";
  433       break;
  434     case 0x82:
  435       return "&#x00e9;";
  436       break;
  437     case 0x83:
  438       return "&#x00e2;";
  439       break;
  440     case 0x84:
  441       return "&#x00e4;";
  442       break;
  443     case 0x85:
  444       return "&#x00e0;";
  445       break;
  446     case 0x86:
  447       return "&#x00e5;";
  448       break;
  449     case 0x87:
  450       return "&#x00e7;";
  451       break;
  452     case 0x88:
  453       return "&#x00ea;";
  454       break;
  455     case 0x89:
  456       return "&#x00eb;";
  457       break;
  458     case 0x8a:
  459       return "&#x00e8;";
  460       break;
  461     case 0x8b:
  462       return "&#x00ef;";
  463       break;
  464     case 0x8c:
  465       return "&#x00ee;";
  466       break;
  467     case 0x8d:
  468       return "&#x00ec;";
  469       break;
  470     case 0x8e:
  471       return "&#x00c4;";
  472       break;
  473     case 0x8f:
  474       return "&#x00c5;";
  475       break;
  476 
  477     case 0x90:
  478       return "&#x00c9;";
  479       break;
  480     case 0x91:
  481       return "&#x00e6;";
  482       break;
  483     case 0x92:
  484       return "&#x00c6;";
  485       break;
  486     case 0x93:
  487       return "&#x00f4;";
  488       break;
  489     case 0x94:
  490       return "&#x00f6;";
  491       break;
  492     case 0x95:
  493       return "&#x00f2;";
  494       break;
  495     case 0x96:
  496       return "&#x00fb;";
  497       break;
  498     case 0x97:
  499       return "&#x00f9;";
  500       break;
  501     case 0x98:
  502       return "&#x00ff;";
  503       break;
  504     case 0x99:
  505       return "&#x00d6;";
  506       break;
  507     case 0x9a:
  508       return "&#x00dc;";
  509       break;
  510     case 0x9b:
  511       return "&#x00a2;";
  512       break;
  513     case 0x9c:
  514       return "&#x00a3;";
  515       break;
  516     case 0x9d:
  517       return "&#x00a5;";
  518       break;
  519     case 0x9e:
  520       return "&#x20a7;";
  521       break;
  522     case 0x9f:
  523       return "&#x0192;";
  524       break;
  525 
  526 
  527     case 0xa0:
  528       return "&#x00e1;";
  529       break;
  530     case 0xa1:
  531       return "&#x00ed;";
  532       break;
  533     case 0xa2:
  534       return "&#x00f3;";
  535       break;
  536     case 0xa3:
  537       return "&#x00fa;";
  538       break;
  539     case 0xa4:
  540       return "&#x00f1;";
  541       break;
  542     case 0xa5:
  543       return "&#x00d1;";
  544       break;
  545     case 0xa6:
  546       return "&#x00aa;";
  547       break;
  548     case 0xa7:
  549       return "&#x00ba;";
  550       break;
  551     case 0xa8:
  552       return "&#x00bf;";
  553       break;
  554     case 0xa9:
  555       return "&#x2310;";
  556       break;
  557     case 0xaa:
  558       return "&#x00ac;";
  559       break;
  560     case 0xab:
  561       return "&#x00bd;";
  562       break;
  563     case 0xac:
  564       return "&#x00bc;";
  565       break;
  566     case 0xad:
  567       return "&#x00a1;";
  568       break;
  569     case 0xae:
  570       return "&#x00ab;";
  571       break;
  572     case 0xaf:
  573       return "&#x00bb;";
  574       break;
  575 
  576       //shades
  577     case 0xb0:
  578       return "&#9617;";
  579       break;
  580     case 0xb1:
  581       return "&#9618;";
  582       break;
  583     case 0xb2:
  584       return "&#9619;";
  585       break;
  586 
  587       //box drawings
  588     case 0xb3:
  589       return "&#9474;";
  590       break;
  591     case 0xb4:
  592       return "&#9508;";
  593       break;
  594     case 0xb5:
  595       return "&#9569;";
  596       break;
  597     case 0xb6:
  598       return "&#9570;";
  599       break;
  600     case 0xb7:
  601       return "&#9558;";
  602       break;
  603     case 0xb8:
  604       return "&#9557;";
  605       break;
  606     case 0xb9:
  607       return "&#9571;";
  608       break;
  609     case 0xba:
  610       return "&#9553;";
  611       break;
  612     case 0xbb:
  613       return "&#9559;";
  614       break;
  615     case 0xbc:
  616       return "&#9565;";
  617       break;
  618     case 0xbd:
  619       return "&#9564;";
  620       break;
  621     case 0xbe:
  622       return "&#9563;";
  623       break;
  624     case 0xbf:
  625       return "&#9488;";
  626       break;
  627 
  628     case 0xc0:
  629       return "&#9492;";
  630       break;
  631     case 0xc1:
  632       return "&#9524;";
  633       break;
  634     case 0xc2:
  635       return "&#9516;";
  636       break;
  637     case 0xc3:
  638       return "&#9500;";
  639       break;
  640     case 0xc4:
  641       return "&#9472;";
  642       break;
  643     case 0xc5:
  644       return "&#9532;";
  645       break;
  646     case 0xc6:
  647       return "&#9566;";
  648       break;
  649     case 0xc7:
  650       return "&#9567;";
  651       break;
  652     case 0xc8:
  653       return "&#9562;";
  654       break;
  655     case 0xc9:
  656       return "&#9556;";
  657       break;
  658     case 0xca:
  659       return "&#9577;";
  660       break;
  661     case 0xcb:
  662       return "&#9574;";
  663       break;
  664     case 0xcc:
  665       return "&#9568;";
  666       break;
  667     case 0xcd:
  668       return "&#9552;";
  669       break;
  670     case 0xce:
  671       return "&#9580;";
  672       break;
  673     case 0xcf:
  674       return "&#9575;";
  675       break;
  676 
  677     case 0xd0:
  678       return "&#9576;";
  679       break;
  680     case 0xd1:
  681       return "&#9572;";
  682       break;
  683     case 0xd2:
  684       return "&#9573;";
  685       break;
  686     case 0xd3:
  687       return "&#9561;";
  688       break;
  689     case 0xd4:
  690       return "&#9560;";
  691       break;
  692     case 0xd5:
  693       return "&#9554;";
  694       break;
  695     case 0xd6:
  696       return "&#9555;";
  697       break;
  698     case 0xd7:
  699       return "&#9579;";
  700       break;
  701     case 0xd8:
  702       return "&#9578;";
  703       break;
  704     case 0xd9:
  705       return "&#9496;";
  706       break;
  707     case 0xda:
  708       return "&#9484;";
  709       break;
  710 
  711       //https://de.wikipedia.org/wiki/Unicodeblock_Blockelemente
  712     case 0xdb:
  713       return "&#9608;";
  714       break;
  715     case 0xdc:
  716       return "&#9604;";
  717       break;
  718     case 0xdd:
  719       return "&#9612;";
  720       break;
  721     case 0xde:
  722       return "&#9616;";
  723       break;
  724     case 0xdf:
  725       return "&#9600;";
  726       break;
  727 
  728     case 0xe0:
  729       return "&#x03b1;";
  730       break;
  731     case 0xe1:
  732       return "&#x00df;";
  733       break;
  734     case 0xe2:
  735       return "&#x0393;";
  736       break;
  737     case 0xe3:
  738       return "&#x03c0;";
  739       break;
  740     case 0xe4:
  741       return "&#x03a3;";
  742       break;
  743     case 0xe5:
  744       return "&#x03c3;";
  745       break;
  746     case 0xe6:
  747       return "&#x00b5;";
  748       break;
  749     case 0xe7:
  750       return "&#x03c4;";
  751       break;
  752     case 0xe8:
  753       return "&#x03a6;";
  754       break;
  755     case 0xe9:
  756       return "&#x0398;";
  757       break;
  758     case 0xea:
  759       return "&#x03a9;";
  760       break;
  761     case 0xeb:
  762       return "&#x03b4;";
  763       break;
  764 
  765     case 0xec:
  766       return "&#x221e;";
  767       break;
  768     case 0xed:
  769       return "&#x03c6;";
  770       break;
  771     case 0xee:
  772       return "&#x03b5;";
  773       break;
  774     case 0xef:
  775       return "&#x2229;";
  776       break;
  777 
  778     case 0xf0:
  779       return "&#x2261;";
  780       break;
  781 
  782     case 0xf1:
  783       return "&#x00b1;";
  784       break;
  785     case 0xf2:
  786       return "&#x2265;";
  787       break;
  788     case 0xf3:
  789       return "&#x2264;";
  790       break;
  791     case 0xf4:
  792       return "&#x2320;";
  793       break;
  794     case 0xf5:
  795       return "&#x2321;";
  796       break;
  797     case 0xf6:
  798       return "&#x00f7;";
  799       break;
  800     case 0xf7:
  801       return "&#x2248;";
  802       break;
  803     case 0xf8:
  804       return "&#x00b0;";
  805       break;
  806 
  807     case 0xf9:
  808       return "&#x2219;";
  809       break;
  810     case 0xfa:
  811       return "&#x00b7;";
  812       break;
  813     case 0xfb:
  814       return "&#x221a;";
  815       break;
  816     case 0xfc:
  817       return "&#x207F;";
  818       break;
  819     case 0xfd:
  820       return "&#x20b2;";
  821       break;
  822     case 0xfe:
  823       return "&#x25a0;";
  824       break;
  825     case 0xff:
  826       return "&nbsp;";
  827       break;
  828 
  829     default :
  830         if (c>0x1f && c<0x7f) { // printable?
  831             return string( 1, c );
  832         } else {
  833           return "";
  834         }
  835   }
  836 };
  837 
  838 }