/*************************************************************************** htmlgenerator.cpp - description ------------------- begin : Wed Nov 28 2001 copyright : (C) 2001-2007 by Andre Simon email : andre.simon1@gmx.de ***************************************************************************/ /* This file is part of Highlight. Highlight is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Highlight is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Highlight. If not, see . */ #include #include #include #include "htmlgenerator.h" #include "version.h" using namespace std; namespace highlight { HtmlGenerator::HtmlGenerator () : CodeGenerator ( HTML ), brTag ( "
" ), hrTag ( "
" ), idAttr ( "name" ), fileSuffix ( ".html" ), cssClassName ( "hl" ), orderedList ( false ), useInlineCSS ( false ), enclosePreTag ( false ), attachAnchors ( false ), anchorPrefix ( "l" ) { spacer = " "; styleCommentOpen="/*"; styleCommentClose="*/"; } string HtmlGenerator::getHeader() { ostringstream os; os << getHeaderStart ( docTitle ); if ( !useInlineCSS ) { if ( includeStyleDef ) { os << "\n"; } else { os << "\n"; } os << "\n\n"; } else { os << "\n\n"; } return os.str(); } string HtmlGenerator::getFooter() { return getGeneratorComment(); } void HtmlGenerator::printBody() { if ( !fragmentOutput || enclosePreTag ) { if ( !useInlineCSS ) { *out << "
";
			}
			else
			{
				*out << "
getBaseFontSize()
				<< "pt; font-family:'" << this->getBaseFont() <<"';\">";
			}
		}
		if ( showLineNumbers && orderedList ) *out << "
    "; processRootState(); if ( showLineNumbers && orderedList ) *out << "\n
"; if ( !fragmentOutput || enclosePreTag ) *out << "
"; } void HtmlGenerator::initOutputTags () { openTags.push_back ( "" ); if ( useInlineCSS ) { //embedBlockOpen = "
"; openTags.push_back ( getOpenTag ( docStyle.getStringStyle() ) ); openTags.push_back ( getOpenTag ( docStyle.getNumberStyle() ) ); openTags.push_back ( getOpenTag ( docStyle.getSingleLineCommentStyle() ) ); openTags.push_back ( getOpenTag ( docStyle.getCommentStyle() ) ); openTags.push_back ( getOpenTag ( docStyle.getEscapeCharStyle() ) ); openTags.push_back ( getOpenTag ( docStyle.getDirectiveStyle() ) ); openTags.push_back ( getOpenTag ( docStyle.getDirectiveStringStyle() ) ); openTags.push_back ( getOpenTag ( docStyle.getLineStyle() ) ); openTags.push_back ( getOpenTag ( docStyle.getSymbolStyle() ) ); } else { //embedBlockOpen = "
"; openTags.push_back ( getOpenTag ( STY_NAME_STR ) ); openTags.push_back ( getOpenTag ( STY_NAME_NUM ) ); openTags.push_back ( getOpenTag ( STY_NAME_SLC ) ); openTags.push_back ( getOpenTag ( STY_NAME_COM ) ); openTags.push_back ( getOpenTag ( STY_NAME_ESC ) ); openTags.push_back ( getOpenTag ( STY_NAME_DIR ) ); openTags.push_back ( getOpenTag ( STY_NAME_DST ) ); openTags.push_back ( getOpenTag ( STY_NAME_LIN ) ); openTags.push_back ( getOpenTag ( STY_NAME_SYM ) ); } closeTags.push_back ( "" ); for ( int i=1;i" ); } //embedBlockClose = "
"; } string HtmlGenerator::getAttributes ( const string & elemName, const ElementStyle & elem ) { ostringstream s; if ( !elemName.empty() ) { s << "."<"; } string HtmlGenerator::getOpenTag ( const ElementStyle & elem ) { return ""; } string HtmlGenerator::getGeneratorComment() { ostringstream s; s <<"\n\n\n\n"; return s.str(); } string HtmlGenerator::getStyleDefinition() { if ( styleDefinitionCache.empty() ) { ostringstream os; os << "body."<getBaseFontSize(); os << "pt; font-family:'" << this->getBaseFont() << "';}\n"; if ( orderedList ) { os << "li."<getBaseFontSize() <<"pt; }\n"; } os << getAttributes ( STY_NAME_NUM, docStyle.getNumberStyle() ) << getAttributes ( STY_NAME_ESC, docStyle.getEscapeCharStyle() ) << getAttributes ( STY_NAME_STR, docStyle.getStringStyle() ) << getAttributes ( STY_NAME_DST, docStyle.getDirectiveStringStyle() ) << getAttributes ( STY_NAME_SLC, docStyle.getSingleLineCommentStyle() ) << getAttributes ( STY_NAME_COM, docStyle.getCommentStyle() ) << getAttributes ( STY_NAME_DIR, docStyle.getDirectiveStyle() ) << getAttributes ( STY_NAME_SYM, docStyle.getSymbolStyle() ) << getAttributes ( STY_NAME_LIN, docStyle.getLineStyle() ); os << "."<first, it->second ); } styleDefinitionCache=os.str(); } return styleDefinitionCache; } string HtmlGenerator::maskCharacter ( unsigned char c ) { switch ( c ) { case '<' : return "<"; break; case '>' : return ">"; break; case '&' : return "&"; break; case '\"' : return """; break; case '@' : return "@"; break; default : return string ( 1, c ); } } string HtmlGenerator::getNewLine() { string nlStr; if ( markLines.count ( lineNumber-1 ) ) nlStr +=""; if ( showLineNumbers && orderedList ) nlStr +=""; /// set wrapping arrow if previous line was wrapped //else if (preFormatter.isWrappedLine(lineNumber-1)) nlStr += "↵"; if (printNewLines) nlStr+="\n"; return nlStr; } void HtmlGenerator::insertLineNumber ( bool insertNewLine ) { if ( insertNewLine ) { wsBuffer += getNewLine(); } if ( showLineNumbers ) { ostringstream numberPrefix; int lineNo = lineNumber+lineNumberOffset; if ( orderedList ) { if ( useInlineCSS ) { numberPrefix<<"
  • "; } else { numberPrefix<<"
  • "; } // Opera 8 ignores empty list items -> add   if ( line.empty() ) numberPrefix<<" "; } if ( attachAnchors ) numberPrefix << ""; if ( !orderedList ) { ostringstream os; if ( lineNumberFillZeroes ) os.fill ( '0' ); os <" << "\n\n\n"; if ( encodingDefined() ) { header << "\n"; } header << "" << title << "\n"; return header.str(); } bool HtmlGenerator::printIndexFile ( const vector &fileList, const string &outPath ) { string suffix = fileSuffix; string outFilePath = outPath + "index" + suffix; ofstream indexfile ( outFilePath.c_str() ); if ( !indexfile.fail() ) { string inFileName; string inFilePath, newInFilePath; indexfile << getHeaderStart ( "Source Index" ); indexfile << "\n\n

    Source Index

    \n" << hrTag << "\n
      \n"; string::size_type pos; for ( unsigned int i=0; i < fileList.size(); i++ ) { pos= ( fileList[i] ).find_last_of ( Platform::pathSeparator ); if ( pos!=string::npos ) { newInFilePath = ( fileList[i] ).substr ( 0, pos+1 ); } else { newInFilePath=Platform::pathSeparator; } if ( newInFilePath!=inFilePath ) { indexfile << "
    \n

    "; indexfile << newInFilePath; indexfile << "

    \n\n" << hrTag << brTag << "Generated by highlight " << HIGHLIGHT_VERSION << ", " << HIGHLIGHT_URL << ""; indexfile << getGeneratorComment(); } else { return false; } return true; } string HtmlGenerator::getKeywordOpenTag ( unsigned int styleID ) { if ( useInlineCSS ) { return getOpenTag ( docStyle.getKeywordStyle ( langInfo.getKeywordClasses() [styleID] ) ); } return getOpenTag ( langInfo.getKeywordClasses() [styleID] ); } string HtmlGenerator::getKeywordCloseTag ( unsigned int styleID ) { return ""; } string HtmlGenerator::getMetaInfoOpenTag ( const TagInfo& info ) { ostringstream tagStream; tagStream<<""; return tagStream.str(); } string HtmlGenerator::getMetaInfoCloseTag() { return ""; } }