Doxygen
Loading...
Searching...
No Matches
HtmlCodeGenerator Class Referencefinal

Generator for HTML code fragments. More...

#include <src/htmlgen.h>

Inheritance diagram for HtmlCodeGenerator:
Collaboration diagram for HtmlCodeGenerator:

Classes

struct  LineInfo

Public Member Functions

 HtmlCodeGenerator (TextStream *t, const DString &relPath)
 HtmlCodeGenerator (TextStream *t)
void setTextStream (TextStream *t)
void setFileName (const DString fileName)
DString fileName ()
OutputType type () const override
std::unique_ptr< OutputCodeIntfclone () override
void codify (const DString &text) override
void stripCodeComments (bool b) override
void startSpecialComment () override
void endSpecialComment () override
void setStripIndentAmount (size_t amount) override
void writeCodeLink (CodeSymbolType type, const DString &ref, const DString &file, const DString &anchor, const DString &name, const DString &tooltip) override
void writeTooltip (const DString &id, const DocLinkInfo &docInfo, const DString &decl, const DString &desc, const SourceLinkInfo &defInfo, const SourceLinkInfo &declInfo) override
void writeLineNumber (const DString &, const DString &, const DString &, int, bool) override
void startCodeLine (int) override
void endCodeLine () override
void startFontClass (const DString &s) override
void endFontClass () override
void writeCodeAnchor (const DString &anchor) override
void startCodeFragment (const DString &style) override
void endCodeFragment (const DString &) override
void startFold (int, const DString &, const DString &) override
void endFold () override
void setRelativePath (const DString &path)
Public Member Functions inherited from OutputCodeIntf
 OutputCodeIntf ()=default

Private Member Functions

void _writeCodeLink (const DString &className, const DString &ref, const DString &file, const DString &anchor, const DString &name, const DString &tooltip)
void _startOpenLine ()

Private Attributes

TextStreamm_t
size_t m_col = 0
DString m_relPath
DString m_fileName
bool m_lineOpen = false
bool m_stripCodeComments = false
bool m_hide = false
LineInfo m_lastLineInfo
size_t m_stripIndentAmount = 0

Detailed Description

Generator for HTML code fragments.

Definition at line 22 of file htmlgen.h.

Constructor & Destructor Documentation

◆ HtmlCodeGenerator() [1/2]

HtmlCodeGenerator::HtmlCodeGenerator ( TextStream * t,
const DString & relPath )

Definition at line 838 of file htmlgen.cpp.

839 : m_t(t), m_relPath(relPath)
840{
841 //printf("%p:HtmlCodeGenerator()\n",(void*)this);
842}
DString m_relPath
Definition htmlgen.h:71
TextStream * m_t
Definition htmlgen.h:69

References m_relPath, and m_t.

◆ HtmlCodeGenerator() [2/2]

HtmlCodeGenerator::HtmlCodeGenerator ( TextStream * t)

Definition at line 833 of file htmlgen.cpp.

833 : m_t(t)
834{
835 //printf("%p:HtmlCodeGenerator()\n",(void*)this);
836}

References m_t.

Member Function Documentation

◆ _startOpenLine()

void HtmlCodeGenerator::_startOpenLine ( )
private

Definition at line 1196 of file htmlgen.cpp.

1197{
1198 //printf("_startOpenLine open=%d\n",m_lineOpen);
1199 *m_t << "<div class=\"line\">";
1200 bool wasHidden=m_hide;
1201 m_hide = false;
1202 m_lineOpen = true;
1208 m_hide = wasHidden;
1209}
void writeLineNumber(const DString &, const DString &, const DString &, int, bool) override
Definition htmlgen.cpp:956
LineInfo m_lastLineInfo
Definition htmlgen.h:87

References HtmlCodeGenerator::LineInfo::anchor, HtmlCodeGenerator::LineInfo::fileName, HtmlCodeGenerator::LineInfo::line, m_hide, m_lastLineInfo, m_lineOpen, m_t, HtmlCodeGenerator::LineInfo::ref, HtmlCodeGenerator::LineInfo::writeAnchor, and writeLineNumber().

Referenced by endFold().

◆ _writeCodeLink()

void HtmlCodeGenerator::_writeCodeLink ( const DString & className,
const DString & ref,
const DString & file,
const DString & anchor,
const DString & name,
const DString & tooltip )
private

Definition at line 1004 of file htmlgen.cpp.

1008{
1009 m_col+=name.length();
1010 if (m_hide) return;
1011 if (!ref.empty())
1012 {
1013 *m_t << "<a class=\"" << className << "Ref\" ";
1014 *m_t << externalLinkTarget();
1015 }
1016 else
1017 {
1018 *m_t << "<a class=\"" << className << "\" ";
1019 }
1020 *m_t << "href=\"";
1021 DString fn = f;
1023 *m_t << createHtmlUrl(m_relPath,ref,
1024 fileName()==fn,fn,anchor);
1025 *m_t << "\"";
1026 if (!tooltip.empty()) *m_t << " title=\"" << convertToHtml(tooltip) << "\"";
1027 *m_t << ">";
1028 codify(name);
1029 *m_t << "</a>";
1030}
bool empty() const
Returns true iff the string is empty (std::string compatible alias for isEmpty()).
Definition dstring.h:152
size_t length() const
Returns the length of the string, not counting the 0-terminator.
Definition dstring.h:155
void codify(const DString &text) override
Definition htmlgen.cpp:849
DString fileName()
Definition htmlgen.h:30
DString externalLinkTarget(const bool parent)
Definition util.cpp:4493
DString convertToHtml(const DString &s, bool keepEntities)
Definition util.cpp:3293
void addHtmlExtensionIfMissing(DString &fName)
Definition util.cpp:3933
DString createHtmlUrl(const DString &relPath, const DString &ref, bool isLocalFile, const DString &targetFileName, const DString &anchor)
Definition util.cpp:4504

References addHtmlExtensionIfMissing(), codify(), convertToHtml(), createHtmlUrl(), DString::empty(), externalLinkTarget(), fileName(), DString::length(), m_col, m_hide, m_relPath, and m_t.

Referenced by writeCodeLink(), and writeLineNumber().

◆ clone()

std::unique_ptr< OutputCodeIntf > HtmlCodeGenerator::clone ( )
inlineoverridevirtual

Implements OutputCodeIntf.

Definition at line 33 of file htmlgen.h.

33{ return std::make_unique<HtmlCodeGenerator>(*this); }

◆ codify()

void HtmlCodeGenerator::codify ( const DString & text)
overridevirtual

Implements OutputCodeIntf.

Definition at line 849 of file htmlgen.cpp.

850{
851 if (!str.empty())
852 {
853 int tabSize = Config_getInt(TAB_SIZE);
854 const char *p=str.data();
855 if (m_hide) // only update column count
856 {
858 }
859 else // actually output content and keep track of m_col
860 {
861 while (*p)
862 {
863 char c=*p++;
864 switch(c)
865 {
866 case '\t': {
867 int spacesToNextTabStop = tabSize - (m_col%tabSize);
868 while (spacesToNextTabStop--)
869 {
870 if (m_col>=m_stripIndentAmount) *m_t << " ";
871 m_col++;
872 }
873 }
874 break;
875 case ' ': if (m_col>=m_stripIndentAmount) *m_t << " ";
876 m_col++;
877 break;
878 case '\n': *m_t << "\n"; m_col=0;
879 break;
880 case '\r': break;
881 case '<': *m_t << "&lt;"; m_col++;
882 break;
883 case '>': *m_t << "&gt;"; m_col++;
884 break;
885 case '&': *m_t << "&amp;"; m_col++;
886 break;
887 case '\'': *m_t << "&#39;"; m_col++; // &apos; is not valid XHTML
888 break;
889 case '"': *m_t << "&quot;"; m_col++;
890 break;
891 case '\\':
892 if (*p=='<')
893 { *m_t << "&lt;"; p++; }
894 else if (*p=='>')
895 { *m_t << "&gt;"; p++; }
896 else if (*p=='[')
897 { *m_t << "\\&zwj;["; m_col++;p++; }
898 else if (*p==']')
899 { *m_t << "\\&zwj;]"; m_col++;p++; }
900 else if (*p=='(')
901 { *m_t << "\\&zwj;("; m_col++;p++; }
902 else if (*p==')')
903 { *m_t << "\\&zwj;)"; m_col++;p++; }
904 else
905 *m_t << "\\";
906 m_col++;
907 break;
908 default:
909 {
910 uint8_t uc = static_cast<uint8_t>(c);
911 if (uc<32)
912 {
913 *m_t << "&#x24" << hex[uc>>4] << hex[uc&0xF] << ";";
914 m_col++;
915 }
916 else if (uc<0x80) // printable ASCII char
917 {
918 *m_t << c;
919 m_col++;
920 }
921 else // multibyte UTF-8 char
922 {
923 p=writeUTF8Char(*m_t,p-1);
924 m_col++;
925 }
926 }
927 break;
928 }
929 }
930 }
931 }
932}
size_t m_stripIndentAmount
Definition htmlgen.h:88
#define Config_getInt(name)
Definition config.h:34
static constexpr auto hex
Definition htmlgen.cpp:71
size_t updateColumnCount(const char *s, size_t col)
Definition outputgen.cpp:60
const char * writeUTF8Char(TextStream &t, const char *s)
Writes the UTF8 character pointed to by s to stream t and returns a pointer to the next character.
Definition utf8.cpp:198

References Config_getInt, DString::data(), DString::empty(), hex, m_col, m_hide, m_stripIndentAmount, m_t, updateColumnCount(), and writeUTF8Char().

Referenced by _writeCodeLink(), writeLineNumber(), and writeTooltip().

◆ endCodeFragment()

void HtmlCodeGenerator::endCodeFragment ( const DString & )
overridevirtual

Implements OutputCodeIntf.

Definition at line 1163 of file htmlgen.cpp.

1164{
1165 //printf("endCodeFragment hide=%d open=%d\n",m_hide,m_lineOpen);
1166 bool wasHidden = m_hide;
1167 m_hide = false;
1168 //endCodeLine checks is there is still an open code line, if so closes it.
1169 endCodeLine();
1170 m_hide = wasHidden;
1171
1172 *m_t << "</div><!-- fragment -->";
1173}
void endCodeLine() override
Definition htmlgen.cpp:1124

References endCodeLine(), m_hide, and m_t.

◆ endCodeLine()

void HtmlCodeGenerator::endCodeLine ( )
overridevirtual

Implements OutputCodeIntf.

Definition at line 1124 of file htmlgen.cpp.

1125{
1126 //printf("endCodeLine hide=%d open=%d\n",m_hide,m_lineOpen);
1127 if (m_hide) return;
1128 if (m_col == 0)
1129 {
1130 *m_t << " ";
1131 m_col++;
1132 }
1133 if (m_lineOpen)
1134 {
1135 *m_t << "</div>\n";
1136 m_lineOpen = false;
1137 }
1138}

References m_col, m_hide, m_lineOpen, and m_t.

Referenced by endCodeFragment().

◆ endFold()

void HtmlCodeGenerator::endFold ( )
overridevirtual

Implements OutputCodeIntf.

Definition at line 1211 of file htmlgen.cpp.

1212{
1213 //printf("endFold open=%d\n",m_lineOpen);
1214 if (m_lineOpen) // if we have a hidden comment in a code fold, we need to end the line
1215 {
1216 *m_t << "</div>\n";
1217 }
1218 *m_t << "</div>\n";
1219 if (m_lineOpen)
1220 {
1222 }
1223}

References _startOpenLine(), m_lineOpen, and m_t.

◆ endFontClass()

void HtmlCodeGenerator::endFontClass ( )
overridevirtual

Implements OutputCodeIntf.

Definition at line 1146 of file htmlgen.cpp.

1147{
1148 if (m_hide) return;
1149 *m_t << "</span>";
1150}

References m_hide, and m_t.

◆ endSpecialComment()

void HtmlCodeGenerator::endSpecialComment ( )
overridevirtual

Implements OutputCodeIntf.

Definition at line 945 of file htmlgen.cpp.

946{
947 //*m_t << "[END]";
948 m_hide = false;
949}

References m_hide.

◆ fileName()

DString HtmlCodeGenerator::fileName ( )
inline

Definition at line 30 of file htmlgen.h.

30{ return m_fileName; }
DString m_fileName
Definition htmlgen.h:72

References m_fileName.

Referenced by _writeCodeLink(), setFileName(), and writeTooltip().

◆ setFileName()

void HtmlCodeGenerator::setFileName ( const DString fileName)
inline

Definition at line 29 of file htmlgen.h.

References fileName(), and m_fileName.

Referenced by HtmlGenerator::startFile().

◆ setRelativePath()

void HtmlCodeGenerator::setRelativePath ( const DString & path)

Definition at line 844 of file htmlgen.cpp.

845{
846 m_relPath = path;
847}

References m_relPath.

Referenced by HtmlGenerator::startFile().

◆ setStripIndentAmount()

void HtmlCodeGenerator::setStripIndentAmount ( size_t amount)
overridevirtual

Implements OutputCodeIntf.

Definition at line 951 of file htmlgen.cpp.

952{
953 m_stripIndentAmount = amount;
954}

References m_stripIndentAmount.

◆ setTextStream()

void HtmlCodeGenerator::setTextStream ( TextStream * t)
inline

Definition at line 28 of file htmlgen.h.

28{ m_t = t; }

References m_t.

Referenced by HtmlGenerator::HtmlGenerator(), and HtmlGenerator::operator=().

◆ startCodeFragment()

void HtmlCodeGenerator::startCodeFragment ( const DString & style)
overridevirtual

Implements OutputCodeIntf.

Definition at line 1158 of file htmlgen.cpp.

1159{
1160 *m_t << "<div class=\"fragment\">";
1161}

References m_t.

◆ startCodeLine()

void HtmlCodeGenerator::startCodeLine ( int )
overridevirtual

Implements OutputCodeIntf.

Definition at line 1112 of file htmlgen.cpp.

1113{
1114 //printf("startCodeLine open=%d\n",m_lineOpen);
1115 m_col=0;
1116 if (m_hide) return;
1117 if (!m_lineOpen)
1118 {
1119 *m_t << "<div class=\"line\">";
1120 m_lineOpen = true;
1121 }
1122}

References m_col, m_hide, m_lineOpen, and m_t.

◆ startFold()

void HtmlCodeGenerator::startFold ( int lineNr,
const DString & startMarker,
const DString & endMarker )
overridevirtual

Implements OutputCodeIntf.

Definition at line 1175 of file htmlgen.cpp.

1176{
1177 //printf("startFold open=%d\n",m_lineOpen);
1178 if (m_lineOpen) // if we have a hidden comment in a code fold, we need to end the line
1179 {
1180 *m_t << "</div>\n";
1181 }
1182 const int maxLineNrStr = 10;
1183 char lineNumber[maxLineNrStr];
1184 snprintf(lineNumber,maxLineNrStr,"%05d",lineNr);
1185 *m_t << "<div class=\"foldopen\" id=\"foldopen" << lineNumber <<
1186 "\" data-start=\"" << startMarker <<
1187 "\" data-end=\"" << endMarker <<
1188 "\">\n";
1189 if (m_lineOpen) // if we have a hidden comment in a code fold, we need to restart the line
1190 {
1191 *m_t << "<div class=\"line\">";
1192 }
1193 m_hide=false;
1194}

References m_hide, m_lineOpen, and m_t.

◆ startFontClass()

void HtmlCodeGenerator::startFontClass ( const DString & s)
overridevirtual

Implements OutputCodeIntf.

Definition at line 1140 of file htmlgen.cpp.

1141{
1142 if (m_hide) return;
1143 *m_t << "<span class=\"" << s << "\">";
1144}

References m_hide, and m_t.

◆ startSpecialComment()

void HtmlCodeGenerator::startSpecialComment ( )
overridevirtual

Implements OutputCodeIntf.

Definition at line 939 of file htmlgen.cpp.

940{
942 //*m_t << "[START]";
943}
bool m_stripCodeComments
Definition htmlgen.h:74

References m_hide, and m_stripCodeComments.

◆ stripCodeComments()

void HtmlCodeGenerator::stripCodeComments ( bool b)
overridevirtual

Implements OutputCodeIntf.

Definition at line 934 of file htmlgen.cpp.

935{
937}

References m_stripCodeComments.

◆ type()

OutputType HtmlCodeGenerator::type ( ) const
inlineoverridevirtual

Implements OutputCodeIntf.

Definition at line 32 of file htmlgen.h.

32{ return OutputType::Html; }

References Html.

Referenced by writeCodeLink().

◆ writeCodeAnchor()

void HtmlCodeGenerator::writeCodeAnchor ( const DString & anchor)
overridevirtual

Implements OutputCodeIntf.

Definition at line 1152 of file htmlgen.cpp.

1153{
1154 if (m_hide) return;
1155 *m_t << "<a id=\"" << anchor << "\" name=\"" << anchor << "\"></a>";
1156}

References m_hide, and m_t.

◆ writeCodeLink()

void HtmlCodeGenerator::writeCodeLink ( CodeSymbolType type,
const DString & ref,
const DString & file,
const DString & anchor,
const DString & name,
const DString & tooltip )
overridevirtual

Implements OutputCodeIntf.

Definition at line 988 of file htmlgen.cpp.

992{
993 if (m_hide) return;
994 const char *hl = codeSymbolType2Str(type);
995 DString hlClass = "code";
996 if (hl)
997 {
998 hlClass+=" hl_";
999 hlClass+=hl;
1000 }
1001 _writeCodeLink(hlClass,ref,f,anchor,name,tooltip);
1002}
void _writeCodeLink(const DString &className, const DString &ref, const DString &file, const DString &anchor, const DString &name, const DString &tooltip)
Definition htmlgen.cpp:1004
OutputType type() const override
Definition htmlgen.h:32
constexpr const char * codeSymbolType2Str(CodeSymbolType type) noexcept
Definition types.h:515

References _writeCodeLink(), codeSymbolType2Str(), m_hide, and type().

◆ writeLineNumber()

void HtmlCodeGenerator::writeLineNumber ( const DString & ref,
const DString & filename,
const DString & anchor,
int l,
bool writeLineAnchor )
overridevirtual

Implements OutputCodeIntf.

Definition at line 956 of file htmlgen.cpp.

958{
959 m_lastLineInfo = LineInfo(ref,filename,anchor,l,writeLineAnchor);
960 if (m_hide) return;
961 const int maxLineNrStr = 10;
962 char lineNumber[maxLineNrStr];
963 char lineAnchor[maxLineNrStr];
964 snprintf(lineNumber,maxLineNrStr,"%5d",l);
965 snprintf(lineAnchor,maxLineNrStr,"l%05d",l);
966
967 //printf("writeLineNumber open=%d\n",m_lineOpen);
968 if (!m_lineOpen)
969 {
970 *m_t << "<div class=\"line\">";
971 m_lineOpen = true;
972 }
973
974 if (writeLineAnchor) *m_t << "<a id=\"" << lineAnchor << "\" name=\"" << lineAnchor << "\"></a>";
975 *m_t << "<span class=\"lineno\">";
976 if (!filename.empty())
977 {
978 _writeCodeLink("line",ref,filename,anchor,lineNumber,DString());
979 }
980 else
981 {
982 codify(lineNumber);
983 }
984 *m_t << "</span>";
985 m_col=0;
986}

References _writeCodeLink(), codify(), DString::empty(), m_col, m_hide, m_lastLineInfo, m_lineOpen, and m_t.

Referenced by _startOpenLine().

◆ writeTooltip()

void HtmlCodeGenerator::writeTooltip ( const DString & id,
const DocLinkInfo & docInfo,
const DString & decl,
const DString & desc,
const SourceLinkInfo & defInfo,
const SourceLinkInfo & declInfo )
overridevirtual

Implements OutputCodeIntf.

Definition at line 1032 of file htmlgen.cpp.

1036{
1037 if (m_hide) return;
1038 *m_t << "<div class=\"ttc\" id=\"" << id << "\">";
1039 *m_t << "<div class=\"ttname\">";
1040 if (!docInfo.url.empty())
1041 {
1042 *m_t << "<a href=\"";
1043 DString fn = docInfo.url;
1045 *m_t << createHtmlUrl(m_relPath,docInfo.ref,
1046 fileName()==fn,fn,docInfo.anchor);
1047 *m_t << "\">";
1048 }
1049 codify(docInfo.name);
1050 if (!docInfo.url.empty())
1051 {
1052 *m_t << "</a>";
1053 }
1054 *m_t << "</div>";
1055
1056 if (!decl.empty())
1057 {
1058 *m_t << "<div class=\"ttdeci\">";
1059 codify(decl);
1060 *m_t << "</div>";
1061 }
1062
1063 if (!desc.empty())
1064 {
1065 *m_t << "<div class=\"ttdoc\">";
1066 codify(desc);
1067 *m_t << "</div>";
1068 }
1069
1070 if (!defInfo.file.empty())
1071 {
1072 *m_t << "<div class=\"ttdef\"><b>" << theTranslator->trDefinition() << "</b> ";
1073 if (!defInfo.url.empty())
1074 {
1075 *m_t << "<a href=\"";
1076 DString fn = defInfo.url;
1078 *m_t << createHtmlUrl(m_relPath,defInfo.ref,
1079 fileName()==fn,fn,defInfo.anchor);
1080 *m_t << "\">";
1081 }
1082 *m_t << defInfo.file << ":" << defInfo.line;
1083 if (!defInfo.url.empty())
1084 {
1085 *m_t << "</a>";
1086 }
1087 *m_t << "</div>";
1088 }
1089 if (!declInfo.file.empty())
1090 {
1091 *m_t << "<div class=\"ttdecl\"><b>" << theTranslator->trDeclaration() << "</b> ";
1092 if (!declInfo.url.empty())
1093 {
1094 *m_t << "<a href=\"";
1095 DString fn = declInfo.url;
1097 *m_t << createHtmlUrl(m_relPath,declInfo.ref,
1098 fileName()==fn,fn,declInfo.anchor);
1099 *m_t << "\">";
1100 }
1101 *m_t << declInfo.file << ":" << declInfo.line;
1102 if (!declInfo.url.empty())
1103 {
1104 *m_t << "</a>";
1105 }
1106 *m_t << "</div>";
1107 }
1108 *m_t << "</div>\n";
1109}
virtual DString trDeclaration()=0
virtual DString trDefinition()=0
Translator * theTranslator
Definition language.cpp:71

References addHtmlExtensionIfMissing(), DocLinkInfo::anchor, SourceLinkInfo::anchor, codify(), createHtmlUrl(), DString::empty(), SourceLinkInfo::file, fileName(), SourceLinkInfo::line, m_hide, m_relPath, m_t, DocLinkInfo::name, DocLinkInfo::ref, SourceLinkInfo::ref, theTranslator, Translator::trDeclaration(), Translator::trDefinition(), DocLinkInfo::url, and SourceLinkInfo::url.

Member Data Documentation

◆ m_col

size_t HtmlCodeGenerator::m_col = 0
private

Definition at line 70 of file htmlgen.h.

Referenced by _writeCodeLink(), codify(), endCodeLine(), startCodeLine(), and writeLineNumber().

◆ m_fileName

DString HtmlCodeGenerator::m_fileName
private

Definition at line 72 of file htmlgen.h.

Referenced by fileName(), and setFileName().

◆ m_hide

◆ m_lastLineInfo

LineInfo HtmlCodeGenerator::m_lastLineInfo
private

Definition at line 87 of file htmlgen.h.

Referenced by _startOpenLine(), and writeLineNumber().

◆ m_lineOpen

bool HtmlCodeGenerator::m_lineOpen = false
private

◆ m_relPath

DString HtmlCodeGenerator::m_relPath
private

Definition at line 71 of file htmlgen.h.

Referenced by _writeCodeLink(), HtmlCodeGenerator(), setRelativePath(), and writeTooltip().

◆ m_stripCodeComments

bool HtmlCodeGenerator::m_stripCodeComments = false
private

Definition at line 74 of file htmlgen.h.

Referenced by startSpecialComment(), and stripCodeComments().

◆ m_stripIndentAmount

size_t HtmlCodeGenerator::m_stripIndentAmount = 0
private

Definition at line 88 of file htmlgen.h.

Referenced by codify(), and setStripIndentAmount().

◆ m_t


The documentation for this class was generated from the following files: