/***************************************************************************
LatexCode.cpp - description
-------------------
begin : Mit Jul 24 2002
copyright : (C) 2002 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 "latexgenerator.h"
namespace highlight
{
LatexGenerator::LatexGenerator()
: CodeGenerator ( LATEX ),
replaceQuotes ( false ),
disableBabelShortHand ( false ),
prettySymbols ( false )
{
// avoid "Underfull \hbox (badness 10000)" warnings
newLineTag = "\\\\\n";
longLineTag = "\\hspace*{\\fill}" + newLineTag;
spacer = "\\ ";
maskWs=true;
maskWsBegin = "\\hlstd{";
maskWsEnd = "}";
excludeWs=true;
styleCommentOpen="%";
}
LatexGenerator::~LatexGenerator()
{}
void LatexGenerator::printBody()
{
*out << "\\noindent\n" ;
if ( ! this->getBaseFont().empty() )
*out << "\\" << this->getBaseFont() << "\n" ;
else
*out << "\\ttfamily\n";
if ( ! this->getBaseFontSize().empty() )
*out << "\\" << this->getBaseFontSize() << "\n" ;
if ( disableBabelShortHand )
*out << "\\shorthandoff{\"}\n";
processRootState();
*out << "\\mbox{}\n"
<< "\\normalfont\n";
if ( ! this->getBaseFontSize().empty() )
*out << "\\normalsize\n" ;
if ( disableBabelShortHand )
*out << "\\shorthandon{\"}\n";
}
string LatexGenerator::getHeader()
{
ostringstream os;
os << "\\documentclass{article}\n"
<< "\\usepackage{color}\n"
<< "\\usepackage{alltt}\n";
if ( StringTools::change_case ( encoding ) =="utf-8" )
{
os << "\\usepackage{ucs}\n\\usepackage[utf8x]{inputenc}\n";
}
else if ( encodingDefined() )
{
os << "\\usepackage[latin1]{inputenc}\n";
}
//needed for Righttorque symbol
if ( preFormatter.isEnabled() )
{
os << "\\usepackage{marvosym}\n";
}
if ( langInfo.highlightingEnabled() )
{
if ( includeStyleDef )
{
os << "\n"<.}\n"
<<"\\setbox\\hlboxdollar=\\hbox{\\verb.$.}\n"
<<"\\setbox\\hlboxunderscore=\\hbox{\\verb._.}\n"
<<"\\setbox\\hlboxand=\\hbox{\\verb.&.}\n"
<<"\\setbox\\hlboxhash=\\hbox{\\verb.#.}\n"
<<"\\setbox\\hlboxat=\\hbox{\\verb.@.}\n"
<<"\\setbox\\hlboxbackslash=\\hbox{\\verb.\\.}\n"
<<"\\setbox\\hlboxpercent=\\hbox{\\verb.\\%.}\n"
<<"\\setbox\\hlboxhat=\\hbox{\\verb.^.}\n"
<<"\\def\\urltilda{\\kern -.15em\\lower .7ex\\hbox{\\~{}}\\kern .04em}\n";
}
return os.str();
}
string LatexGenerator::getFooter()
{
ostringstream os;
os << "\\end {document}\n"
<< "(* LaTeX generated by highlight "
<< HIGHLIGHT_VERSION
<< ", "
<< HIGHLIGHT_URL
<< " *)\n";
return os.str();
}
void LatexGenerator::initOutputTags(){
openTags.push_back ( "\\hlstd{" );
openTags.push_back ( "\\hlstr{" );
openTags.push_back ( "\\hlnum{" );
openTags.push_back ( "\\hlslc{" );
openTags.push_back ( "\\hlcom{" );
openTags.push_back ( "\\hlesc{" );
openTags.push_back ( "\\hldir{" );
openTags.push_back ( "\\hldstr{" );
openTags.push_back ( "\\hlline{" );
openTags.push_back ( "\\hlsym{" );
for ( int i=0;i' :
return prettySymbols ? "\\usebox{\\hlboxgreaterthan}" : "$>$";
break;
case '{':
return prettySymbols ? "\\usebox{\\hlboxopenbrace}" : "\\{";
break;
case '}':
return prettySymbols ? "\\usebox{\\hlboxclosebrace}" : "\\}";
break;
case '&':
case '$':
case '#':
case '%':
{
string m ( "\\" );
m += c;
return m;
}
break;
case '\"':
return ( replaceQuotes ) ?"\\dq{}":"\"";
break;
case '_':
return "\\textunderscore ";
break;
case '^':
return "\\textasciicircum ";
break;
case '\\':
return "$\\backslash$";
break;
case '~':
return prettySymbols ? "\\urltilda " : "$\\sim$";
break;
case '|':
return "\\textbar ";
break;
// avoid latex compilation failure if [ or * follows a line break (\\)
case '*':
case '[':
case ']':
// avoid "merging" of consecutive '-' chars when included in bold font ( \bf )
case '-':
{
string m ( 1, '{' );
m += c;
m += '}';
return m;
}
break;
default :
return string ( 1, c );
}
}
string LatexGenerator::getKeywordOpenTag ( unsigned int styleID )
{
return "\\hl"+langInfo.getKeywordClasses() [styleID]+"{";
}
string LatexGenerator::getKeywordCloseTag ( unsigned int styleID )
{
return "}";
}
string LatexGenerator::getStyleDefinition()
{
if ( styleDefinitionCache.empty() )
{
ostringstream os;
os << getAttributes ( STY_NAME_STD, docStyle.getDefaultStyle() );
os << getAttributes ( STY_NAME_NUM, docStyle.getNumberStyle() );
os << getAttributes ( STY_NAME_ESC, docStyle.getEscapeCharStyle() );
os << getAttributes ( STY_NAME_STR, docStyle.getStringStyle() );
os << getAttributes ( STY_NAME_DST, docStyle.getDirectiveStringStyle() );
os << getAttributes ( STY_NAME_SLC, docStyle.getSingleLineCommentStyle() );
os << getAttributes ( STY_NAME_COM, docStyle.getCommentStyle() );
os << getAttributes ( STY_NAME_DIR, docStyle.getDirectiveStyle() );
os << getAttributes ( STY_NAME_SYM, docStyle.getSymbolStyle() );
os << getAttributes ( STY_NAME_LIN, docStyle.getLineStyle() );
KeywordStyles styles = docStyle.getKeywordStyles();
for ( KSIterator it=styles.begin(); it!=styles.end(); it++ )
{
os << getAttributes ( it->first, it->second );
}
os << "\\definecolor{bgcolor}{rgb}{"
<< docStyle.getBgColour().getRed ( LATEX ) << ","
<< docStyle.getBgColour().getGreen ( LATEX ) << ","
<< docStyle.getBgColour().getBlue ( LATEX )
<< "}\n";
styleDefinitionCache=os.str();
}
return styleDefinitionCache;
}
}