/******************************************************************************
*
*
*
*
* Copyright (C) 1997-2003 by Dimitri van Heesch.
*
* Permission to use, copy, modify, and distribute this software and its
* documentation under the terms of the GNU General Public License is hereby
* granted. No representations are made about the suitability of this software
* for any purpose. It is provided "as is" without express or implied warranty.
* See the GNU General Public License for more details.
*
* Documents produced by Doxygen are derivative works derived from the
* input used in their production; they are not affected by this license.
*
*/
#include "htmldocvisitor.h"
#include "docparser.h"
#include "language.h"
#include "doxygen.h"
#include "outputgen.h"
#include "code.h"
#include "dot.h"
#include "message.h"
#include "config.h"
static QString htmlAttribsToString(const HtmlAttribList &attribs)
{
QString result;
HtmlAttribListIterator li(attribs);
HtmlAttrib *att;
for (li.toFirst();(att=li.current());++li)
{
result+=" ";
result+=att->name;
if (!att->value.isEmpty()) result+="=\""+att->value+"\"";
}
return result;
}
//-------------------------------------------------------------------------
HtmlDocVisitor::HtmlDocVisitor(QTextStream &t,BaseCodeDocInterface &ci)
: m_t(t), m_ci(ci), m_insidePre(FALSE), m_hide(FALSE)
{
}
//--------------------------------------
// visitor functions for leaf nodes
//--------------------------------------
void HtmlDocVisitor::visit(DocWord *w)
{
if (m_hide) return;
filter(w->word());
}
void HtmlDocVisitor::visit(DocLinkedWord *w)
{
if (m_hide) return;
startLink(w->ref(),w->file(),w->anchor());
filter(w->word());
endLink();
}
void HtmlDocVisitor::visit(DocWhiteSpace *w)
{
if (m_hide) return;
if (m_insidePre)
{
m_t << w->chars();
}
else
{
m_t << " ";
}
}
void HtmlDocVisitor::visit(DocSymbol *s)
{
if (m_hide) return;
switch(s->symbol())
{
case DocSymbol::BSlash: m_t << "\\"; break;
case DocSymbol::At: m_t << "@"; break;
case DocSymbol::Less: m_t << "<"; break;
case DocSymbol::Greater: m_t << ">"; break;
case DocSymbol::Amp: m_t << "&"; break;
case DocSymbol::Dollar: m_t << "$"; break;
case DocSymbol::Hash: m_t << "#"; break;
case DocSymbol::Percent: m_t << "%"; break;
case DocSymbol::Copy: m_t << "©"; break;
case DocSymbol::Tm: m_t << "&tm;"; break;
case DocSymbol::Reg: m_t << "®"; break;
case DocSymbol::Apos: m_t << "'"; break;
case DocSymbol::Quot: m_t << "\""; break;
case DocSymbol::Uml: m_t << "&" << s->letter() << "uml;"; break;
case DocSymbol::Acute: m_t << "&" << s->letter() << "acute;"; break;
case DocSymbol::Grave: m_t << "&" << s->letter() << "grave;"; break;
case DocSymbol::Circ: m_t << "&" << s->letter() << "circ;"; break;
case DocSymbol::Tilde: m_t << "&" << s->letter() << "tilde;"; break;
case DocSymbol::Szlig: m_t << "ß"; break;
case DocSymbol::Cedil: m_t << "&" << s->letter() << "cedil;"; break;
case DocSymbol::Ring: m_t << "&" << s->letter() << "ring;"; break;
case DocSymbol::Nbsp: m_t << " "; break;
default:
err("Error: unknown symbol found\n");
}
}
void HtmlDocVisitor::visit(DocURL *u)
{
if (m_hide) return;
m_t << "isEmail()) m_t << "mailto:";
m_t << u->url() << "\">";
filter(u->url());
m_t << "";
}
void HtmlDocVisitor::visit(DocLineBreak *)
{
if (m_hide) return;
m_t << "
\n";
}
void HtmlDocVisitor::visit(DocHorRuler *)
{
if (m_hide) return;
m_t << "
\n";
}
void HtmlDocVisitor::visit(DocStyleChange *s)
{
if (m_hide) return;
switch (s->style())
{
case DocStyleChange::Bold:
if (s->enable()) m_t << "attribs()) << ">"; else m_t << "";
break;
case DocStyleChange::Italic:
if (s->enable()) m_t << "attribs()) << ">"; else m_t << "";
break;
case DocStyleChange::Code:
if (s->enable()) m_t << "attribs()) << ">"; else m_t << "
";
break;
case DocStyleChange::Subscript:
if (s->enable()) m_t << "attribs()) << ">"; else m_t << "";
break;
case DocStyleChange::Superscript:
if (s->enable()) m_t << "attribs()) << ">"; else m_t << "";
break;
case DocStyleChange::Center:
if (s->enable()) m_t << "attribs()) << ">"; else m_t << "";
break;
case DocStyleChange::Small:
if (s->enable()) m_t << "attribs()) << ">"; else m_t << "";
break;
case DocStyleChange::Preformatted:
if (s->enable())
{
m_t << "attribs()) << ">";
m_insidePre=TRUE;
}
else
{
m_insidePre=FALSE;
m_t << "
";
}
case DocStyleChange::Div:
if (s->enable()) m_t << "attribs()) << ">"; else m_t << "
";
break;
case DocStyleChange::Span:
if (s->enable()) m_t << "attribs()) << ">"; else m_t << "";
break;
}
}
void HtmlDocVisitor::visit(DocVerbatim *s)
{
if (m_hide) return;
switch(s->type())
{
case DocVerbatim::Code: // fall though
m_t << "";
parseCode(m_ci,s->context(),s->text().latin1(),s->isExample(),s->exampleFile());
m_t << "
";
break;
case DocVerbatim::Verbatim:
m_t << "";
filter(s->text());
m_t << "
";
break;
case DocVerbatim::HtmlOnly:
m_t << s->text();
break;
case DocVerbatim::LatexOnly:
case DocVerbatim::XmlOnly:
/* nothing */
break;
}
}
void HtmlDocVisitor::visit(DocAnchor *anc)
{
if (m_hide) return;
m_t << "file() << "#"*/ << anc->anchor() << "\"/>";
}
void HtmlDocVisitor::visit(DocInclude *inc)
{
if (m_hide) return;
switch(inc->type())
{
case DocInclude::Include:
m_t << "";
parseCode(m_ci,inc->context(),inc->text().latin1(),inc->isExample(),inc->exampleFile());
m_t << "
";
break;
case DocInclude::DontInclude:
break;
case DocInclude::HtmlInclude:
m_t << inc->text();
break;
case DocInclude::VerbInclude:
m_t << "";
filter(inc->text());
m_t << "
";
break;
}
}
void HtmlDocVisitor::visit(DocIncOperator *op)
{
//printf("DocIncOperator: type=%d first=%d, last=%d text=`%s'\n",
// op->type(),op->isFirst(),op->isLast(),op->text().data());
if (op->isFirst())
{
if (!m_hide) m_t << "";
pushEnabled();
m_hide=TRUE;
}
if (op->type()!=DocIncOperator::Skip)
{
popEnabled();
if (!m_hide) parseCode(m_ci,op->context(),op->text().latin1(),op->isExample(),op->exampleFile());
pushEnabled();
m_hide=TRUE;
}
if (op->isLast())
{
popEnabled();
if (!m_hide) m_t << "
";
}
else
{
if (!m_hide) m_t << endl;
}
}
void HtmlDocVisitor::visit(DocFormula *f)
{
if (m_hide) return;
bool bDisplay = f->text().at(0)=='\\';
if (bDisplay) m_t << "" << endl;
m_t << "text());
m_t << "\"";
/// @todo cache image dimensions on formula generation and give height/width
/// for faster preloading and better rendering of the page
m_t << " src=\"" << f->name() << ".png\">";
if (bDisplay)
m_t << endl << "
" << endl;
}
void HtmlDocVisitor::visit(DocIndexEntry *)
{
}
//--------------------------------------
// visitor functions for compound nodes
//--------------------------------------
void HtmlDocVisitor::visitPre(DocAutoList *l)
{
if (m_hide) return;
if (l->isEnumList())
{
m_t << "
";
}
else
{
m_t << "";
}
if (!l->isPreformatted()) m_t << "\n";
}
void HtmlDocVisitor::visitPost(DocAutoList *l)
{
if (m_hide) return;
if (l->isEnumList())
{
m_t << "
";
}
else
{
m_t << "";
}
if (!l->isPreformatted()) m_t << "\n";
}
void HtmlDocVisitor::visitPre(DocAutoListItem *)
{
if (m_hide) return;
m_t << "";
}
void HtmlDocVisitor::visitPost(DocAutoListItem *)
{
if (m_hide) return;
m_t << "";
}
void HtmlDocVisitor::visitPre(DocPara *)
{
}
void HtmlDocVisitor::visitPost(DocPara *p)
{
if (m_hide) return;
if (!p->isLast() && // omit for last paragraph
!(p->parent() && // and for parameter sections
p->parent()->kind()==DocNode::Kind_ParamSect
)
)
{
m_t << "
\n";
}
}
void HtmlDocVisitor::visitPre(DocRoot *)
{
//m_t << "
New parser:
\n";
}
void HtmlDocVisitor::visitPost(DocRoot *)
{
//m_t << "
Old parser:
\n";
}
void HtmlDocVisitor::visitPre(DocSimpleSect *s)
{
if (m_hide) return;
m_t << "- ";
switch(s->type())
{
case DocSimpleSect::See:
m_t << theTranslator->trSeeAlso(); break;
case DocSimpleSect::Return:
m_t << theTranslator->trReturns(); break;
case DocSimpleSect::Author:
m_t << theTranslator->trAuthor(TRUE,TRUE); break;
case DocSimpleSect::Authors:
m_t << theTranslator->trAuthor(TRUE,FALSE); break;
case DocSimpleSect::Version:
m_t << theTranslator->trVersion(); break;
case DocSimpleSect::Since:
m_t << theTranslator->trSince(); break;
case DocSimpleSect::Date:
m_t << theTranslator->trDate(); break;
case DocSimpleSect::Note:
m_t << theTranslator->trNote(); break;
case DocSimpleSect::Warning:
m_t << theTranslator->trWarning(); break;
case DocSimpleSect::Pre:
m_t << theTranslator->trPrecondition(); break;
case DocSimpleSect::Post:
m_t << theTranslator->trPostcondition(); break;
case DocSimpleSect::Invar:
m_t << theTranslator->trInvariant(); break;
case DocSimpleSect::Remark:
m_t << theTranslator->trRemarks(); break;
case DocSimpleSect::Attention:
m_t << theTranslator->trAttention(); break;
case DocSimpleSect::User: break;
case DocSimpleSect::Rcs: break;
case DocSimpleSect::Unknown: break;
}
// special case 1: user defined title
if (s->type()!=DocSimpleSect::User && s->type()!=DocSimpleSect::Rcs)
{
m_t << ":
- ";
}
}
void HtmlDocVisitor::visitPost(DocSimpleSect *)
{
if (m_hide) return;
m_t << "
\n";
}
void HtmlDocVisitor::visitPre(DocTitle *)
{
}
void HtmlDocVisitor::visitPost(DocTitle *)
{
if (m_hide) return;
m_t << "";
}
void HtmlDocVisitor::visitPre(DocSimpleList *sl)
{
if (m_hide) return;
m_t << "";
if (!sl->isPreformatted()) m_t << "\n";
}
void HtmlDocVisitor::visitPost(DocSimpleList *sl)
{
if (m_hide) return;
m_t << "
";
if (!sl->isPreformatted()) m_t << "\n";
}
void HtmlDocVisitor::visitPre(DocSimpleListItem *)
{
if (m_hide) return;
m_t << "";
}
void HtmlDocVisitor::visitPost(DocSimpleListItem *li)
{
if (m_hide) return;
m_t << "";
if (!li->isPreformatted()) m_t << "\n";
}
void HtmlDocVisitor::visitPre(DocSection *s)
{
if (m_hide) return;
m_t << "level()+1 << ">";
m_t << "anchor();
m_t << "\">" << endl;
filter(s->title());
m_t << "level()+1 << ">\n";
}
void HtmlDocVisitor::visitPost(DocSection *)
{
}
void HtmlDocVisitor::visitPre(DocHtmlList *s)
{
if (m_hide) return;
if (s->type()==DocHtmlList::Ordered)
m_t << "attribs()) << ">\n";
else
m_t << "attribs()) << ">\n";
}
void HtmlDocVisitor::visitPost(DocHtmlList *s)
{
if (m_hide) return;
if (s->type()==DocHtmlList::Ordered)
m_t << "
";
else
m_t << "";
if (!s->isPreformatted()) m_t << "\n";
}
void HtmlDocVisitor::visitPre(DocHtmlListItem *i)
{
if (m_hide) return;
m_t << "attribs()) << ">";
if (!i->isPreformatted()) m_t << "\n";
}
void HtmlDocVisitor::visitPost(DocHtmlListItem *)
{
if (m_hide) return;
m_t << "\n";
}
//void HtmlDocVisitor::visitPre(DocHtmlPre *p)
//{
// m_t << "attribs()) << ">\n";
// m_insidePre=TRUE;
//}
//void HtmlDocVisitor::visitPost(DocHtmlPre *)
//{
// m_insidePre=FALSE;
// m_t << "
\n";
//}
void HtmlDocVisitor::visitPre(DocHtmlDescList *dl)
{
if (m_hide) return;
m_t << "attribs()) << ">\n";
}
void HtmlDocVisitor::visitPost(DocHtmlDescList *)
{
if (m_hide) return;
m_t << "
\n";
}
void HtmlDocVisitor::visitPre(DocHtmlDescTitle *dt)
{
if (m_hide) return;
m_t << "attribs()) << ">";
}
void HtmlDocVisitor::visitPost(DocHtmlDescTitle *)
{
if (m_hide) return;
m_t << "\n";
}
void HtmlDocVisitor::visitPre(DocHtmlDescData *dd)
{
if (m_hide) return;
m_t << "attribs()) << ">";
}
void HtmlDocVisitor::visitPost(DocHtmlDescData *)
{
if (m_hide) return;
m_t << "\n";
}
void HtmlDocVisitor::visitPre(DocHtmlTable *t)
{
if (m_hide) return;
bool hasBorder = FALSE;
bool hasCellSpacing = FALSE;
bool hasCellPadding = FALSE;
HtmlAttribListIterator li(t->attribs());
HtmlAttrib *att;
for (li.toFirst();(att=li.current());++li)
{
if (att->name=="border") hasBorder=TRUE;
else if (att->name=="cellspacing") hasCellSpacing=TRUE;
else if (att->name=="cellpadding") hasCellPadding=TRUE;
}
m_t << "attribs());
if (!hasBorder) m_t << " border=\"1\"";
if (!hasCellSpacing) m_t << " cellspacing=\"3\"";
if (!hasCellPadding) m_t << " cellpadding=\"3\"";
m_t << ">\n";
}
void HtmlDocVisitor::visitPost(DocHtmlTable *)
{
if (m_hide) return;
m_t << "
\n";
}
void HtmlDocVisitor::visitPre(DocHtmlRow *tr)
{
if (m_hide) return;
m_t << "attribs()) << ">\n";
}
void HtmlDocVisitor::visitPost(DocHtmlRow *)
{
if (m_hide) return;
m_t << "
\n";
}
void HtmlDocVisitor::visitPre(DocHtmlCell *c)
{
if (m_hide) return;
if (c->isHeading())
{
m_t << "attribs()) << ">";
}
else
{
m_t << " | attribs()) << ">";
}
}
void HtmlDocVisitor::visitPost(DocHtmlCell *c)
{
if (m_hide) return;
if (c->isHeading()) m_t << ""; else m_t << " | ";
}
void HtmlDocVisitor::visitPre(DocHtmlCaption *c)
{
if (m_hide) return;
bool hasAlign = FALSE;
HtmlAttribListIterator li(c->attribs());
HtmlAttrib *att;
for (li.toFirst();(att=li.current());++li)
{
if (att->name=="align") hasAlign=TRUE;
}
m_t << "attribs());
if (!hasAlign) m_t << " align=\"bottom\"";
m_t << ">";
}
void HtmlDocVisitor::visitPost(DocHtmlCaption *)
{
if (m_hide) return;
m_t << "\n";
}
void HtmlDocVisitor::visitPre(DocInternal *)
{
if (m_hide) return;
m_t << "" << theTranslator->trForInternalUseOnly() << "
" << endl;
m_t << "" << endl;
}
void HtmlDocVisitor::visitPost(DocInternal *)
{
if (m_hide) return;
m_t << "
" << endl;
}
void HtmlDocVisitor::visitPre(DocHRef *href)
{
if (m_hide) return;
m_t << "url() << "\">";
}
void HtmlDocVisitor::visitPost(DocHRef *)
{
if (m_hide) return;
m_t << "";
}
void HtmlDocVisitor::visitPre(DocHtmlHeader *header)
{
if (m_hide) return;
m_t << "level()
<< htmlAttribsToString(header->attribs()) << ">";
}
void HtmlDocVisitor::visitPost(DocHtmlHeader *header)
{
if (m_hide) return;
m_t << "level() << ">\n";
}
void HtmlDocVisitor::visitPre(DocImage *img)
{
if (img->type()==DocImage::Html)
{
if (m_hide) return;
QString baseName=img->name();
int i;
if ((i=baseName.findRev('/'))!=-1 || (i=baseName.findRev('\\'))!=-1)
{
baseName=baseName.right(baseName.length()-i-1);
}
m_t << "" << endl;
m_t << "
name() << "\" alt=\""
<< baseName << "\">" << endl;
if (img->hasCaption())
{
m_t << "
";
}
}
else // other format -> skip
{
pushEnabled();
m_hide=TRUE;
}
}
void HtmlDocVisitor::visitPost(DocImage *img)
{
if (img->type()==DocImage::Html)
{
if (m_hide) return;
if (img->hasCaption())
{
m_t << "
";
}
m_t << "
" << endl;
}
else // other format
{
popEnabled();
}
}
void HtmlDocVisitor::visitPre(DocDotFile *df)
{
if (m_hide) return;
QString baseName=df->file();
int i;
if ((i=baseName.findRev('/'))!=-1)
{
baseName=baseName.right(baseName.length()-i-1);
}
QString outDir = Config_getString("HTML_OUTPUT");
writeDotGraphFromFile(df->file(),outDir,baseName,BITMAP);
m_t << "" << endl;
QString mapName = baseName+".map";
QString mapFile = df->file()+".map";
m_t << "
" << endl;
QString imap = getDotImageMapFromFile(df->file(),outDir);
m_t << "
" << endl;
if (df->hasCaption())
{
m_t << "
";
}
}
void HtmlDocVisitor::visitPost(DocDotFile *df)
{
if (m_hide) return;
if (df->hasCaption())
{
m_t << "
" << endl;
}
m_t << "
" << endl;
}
void HtmlDocVisitor::visitPre(DocLink *lnk)
{
if (m_hide) return;
startLink(lnk->ref(),lnk->file(),lnk->anchor());
}
void HtmlDocVisitor::visitPost(DocLink *)
{
if (m_hide) return;
endLink();
}
void HtmlDocVisitor::visitPre(DocRef *ref)
{
if (m_hide) return;
startLink(ref->ref(),ref->file(),ref->anchor());
if (!ref->hasLinkText()) filter(ref->targetTitle());
}
void HtmlDocVisitor::visitPost(DocRef *)
{
if (m_hide) return;
endLink();
//m_t << " ";
}
void HtmlDocVisitor::visitPre(DocSecRefItem *ref)
{
if (m_hide) return;
QString refName=ref->file();
if (refName.right(Doxygen::htmlFileExtension.length())!=
QString(Doxygen::htmlFileExtension))
{
refName+=Doxygen::htmlFileExtension;
}
m_t << "anchor() << "\">";
}
void HtmlDocVisitor::visitPost(DocSecRefItem *)
{
if (m_hide) return;
m_t << " ";
}
void HtmlDocVisitor::visitPre(DocSecRefList *)
{
if (m_hide) return;
m_t << "" << endl;
m_t << "" << endl;
}
void HtmlDocVisitor::visitPost(DocSecRefList *)
{
if (m_hide) return;
m_t << "
" << endl;
m_t << "" << endl;
}
void HtmlDocVisitor::visitPre(DocLanguage *l)
{
QString langId = Config_getEnum("OUTPUT_LANGUAGE");
if (l->id().lower()!=langId.lower())
{
pushEnabled();
m_hide = TRUE;
}
}
void HtmlDocVisitor::visitPost(DocLanguage *l)
{
QString langId = Config_getEnum("OUTPUT_LANGUAGE");
if (l->id().lower()!=langId.lower())
{
popEnabled();
}
}
void HtmlDocVisitor::visitPre(DocParamSect *s)
{
if (m_hide) return;
m_t << "- ";
switch(s->type())
{
case DocParamSect::Param:
m_t << theTranslator->trParameters(); break;
case DocParamSect::RetVal:
m_t << theTranslator->trReturnValues(); break;
case DocParamSect::Exception:
m_t << theTranslator->trExceptions(); break;
default:
ASSERT(0);
}
m_t << ":";
m_t << "
- " << endl;
m_t << "
" << endl;
}
void HtmlDocVisitor::visitPost(DocParamSect *)
{
if (m_hide) return;
m_t << "
" << endl;
m_t << "
" << endl;
}
void HtmlDocVisitor::visitPre(DocParamList *pl)
{
if (m_hide) return;
m_t << " ";
QStrListIterator li(pl->parameters());
const char *s;
bool first=TRUE;
for (li.toFirst();(s=li.current());++li)
{
if (!first) m_t << ","; else first=FALSE;
filter(s);
}
m_t << " | ";
}
void HtmlDocVisitor::visitPost(DocParamList *)
{
if (m_hide) return;
m_t << " |
" << endl;
}
void HtmlDocVisitor::visitPre(DocXRefItem *x)
{
if (m_hide) return;
m_t << "- file() << Doxygen::htmlFileExtension << "#" << x->anchor() << "\">";
filter(x->title());
m_t << ":
- ";
}
void HtmlDocVisitor::visitPost(DocXRefItem *)
{
if (m_hide) return;
m_t << "
" << endl;
}
void HtmlDocVisitor::visitPre(DocInternalRef *ref)
{
if (m_hide) return;
startLink(0,ref->file(),ref->anchor());
}
void HtmlDocVisitor::visitPost(DocInternalRef *)
{
if (m_hide) return;
endLink();
m_t << " ";
}
void HtmlDocVisitor::visitPre(DocCopy *)
{
}
void HtmlDocVisitor::visitPost(DocCopy *)
{
}
void HtmlDocVisitor::visitPre(DocText *)
{
}
void HtmlDocVisitor::visitPost(DocText *)
{
}
void HtmlDocVisitor::filter(const char *str)
{
if (str==0) return;
const char *p=str;
char c;
while (*p)
{
c=*p++;
switch(c)
{
case '<': m_t << "<"; break;
case '>': m_t << ">"; break;
case '&': m_t << "&"; break;
default: m_t << c;
}
}
}
/// Escape basic entities to produce a valid CDATA attribute value,
/// assume that the outer quoting will be using the double quote "
void HtmlDocVisitor::filterQuotedCdataAttr(const char* str)
{
if (str==0) return;
const char *p=str;
char c;
while (*p)
{
c=*p++;
switch(c)
{
case '&': m_t << "&"; break;
case '"': m_t << """; break;
// For SGML compliance, and given the SGML declaration for HTML syntax,
// it's enough to replace these two, provided that the declaration
// for the HTML version we generate (and as supported by the browser)
// specifies that all the other symbols used in rawVal are
// within the right charachter class (i.e., they're not
// some multinational weird charachters not in the BASESET).
// We assume that 1) the browser will support whatever is remaining
// in the formula and 2) the TeX formulae are generally governed
// by even stricter charachter restrictions so it should be enough.
//
// On some incompliant browsers, additional translation of
// '>' and '<' into ">" and "<", respectively, might be needed;
// but I'm unaware of particular modern (last 4 years) versions
// with such problems, so let's not do it for performance.
// Also, some brousers will (wrongly) not process the entity references
// inside the attribute value and show the &...; form instead,
// so we won't create entites unless necessary to minimize clutter there.
// --vassilii
default: m_t << c;
}
}
}
void HtmlDocVisitor::startLink(const QString &ref,const QString &file,const QString &anchor)
{
QCString *dest;
if (!ref.isEmpty()) // link to entity imported via tag file
{
m_t << "";
}
void HtmlDocVisitor::endLink()
{
m_t << "";
}
void HtmlDocVisitor::pushEnabled()
{
m_enabled.push(new bool(m_hide));
}
void HtmlDocVisitor::popEnabled()
{
bool *v=m_enabled.pop();
ASSERT(v!=0);
m_hide = *v;
delete v;
}