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< OutputCodeIntf > clone () 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

TextStream * m_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 837 of file htmlgen.cpp.

838 : m_t(t), m_relPath(relPath)
839{
840 //printf("%p:HtmlCodeGenerator()\n",(void*)this);
841}
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 832 of file htmlgen.cpp.

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

References m_t.

Member Function Documentation

◆ _startOpenLine()

void HtmlCodeGenerator::_startOpenLine ( )
private

Definition at line 1195 of file htmlgen.cpp.

1196{
1197 //printf("_startOpenLine open=%d\n",m_lineOpen);
1198 *m_t << "<div class=\"line\">";
1199 bool wasHidden=m_hide;
1200 m_hide = false;
1201 m_lineOpen = true;
1207 m_hide = wasHidden;
1208}
void writeLineNumber(const DString &, const DString &, const DString &, int, bool) override
Definition htmlgen.cpp:955
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 1003 of file htmlgen.cpp.

1007{
1008 m_col+=name.length();
1009 if (m_hide) return;
1010 if (!ref.empty())
1011 {
1012 *m_t << "<a class=\"" << className << "Ref\" ";
1013 *m_t << externalLinkTarget();
1014 }
1015 else
1016 {
1017 *m_t << "<a class=\"" << className << "\" ";
1018 }
1019 *m_t << "href=\"";
1020 DString fn = f;
1022 *m_t << createHtmlUrl(m_relPath,ref,
1023 fileName()==fn,fn,anchor);
1024 *m_t << "\"";
1025 if (!tooltip.empty()) *m_t << " title=\"" << convertToHtml(tooltip) << "\"";
1026 *m_t << ">";
1027 codify(name);
1028 *m_t << "</a>";
1029}
bool empty() const
Returns true iff the string is empty (std::string compatible alias for isEmpty()).
Definition dstring.h:148
void codify(const DString &text) override
Definition htmlgen.cpp:848
DString fileName()
Definition htmlgen.h:30
const char * name(int level)
Definition latexgen.h:368
DString externalLinkTarget(const bool parent)
Definition util.cpp:4500
DString convertToHtml(const DString &s, bool keepEntities)
Definition util.cpp:3300
void addHtmlExtensionIfMissing(DString &fName)
Definition util.cpp:3940
DString createHtmlUrl(const DString &relPath, const DString &ref, bool isLocalFile, const DString &targetFileName, const DString &anchor)
Definition util.cpp:4511

References addHtmlExtensionIfMissing(), codify(), convertToHtml(), createHtmlUrl(), DString::empty(), externalLinkTarget(), fileName(), 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 848 of file htmlgen.cpp.

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

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 1162 of file htmlgen.cpp.

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

References endCodeLine(), m_hide, and m_t.

◆ endCodeLine()

void HtmlCodeGenerator::endCodeLine ( )
overridevirtual

Implements OutputCodeIntf.

Definition at line 1123 of file htmlgen.cpp.

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

References m_col, m_hide, m_lineOpen, and m_t.

Referenced by endCodeFragment().

◆ endFold()

void HtmlCodeGenerator::endFold ( )
overridevirtual

Implements OutputCodeIntf.

Definition at line 1210 of file htmlgen.cpp.

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

References _startOpenLine(), m_lineOpen, and m_t.

◆ endFontClass()

void HtmlCodeGenerator::endFontClass ( )
overridevirtual

Implements OutputCodeIntf.

Definition at line 1145 of file htmlgen.cpp.

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

References m_hide, and m_t.

◆ endSpecialComment()

void HtmlCodeGenerator::endSpecialComment ( )
overridevirtual

Implements OutputCodeIntf.

Definition at line 944 of file htmlgen.cpp.

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

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 843 of file htmlgen.cpp.

844{
845 m_relPath = path;
846}

References m_relPath.

Referenced by HtmlGenerator::startFile().

◆ setStripIndentAmount()

void HtmlCodeGenerator::setStripIndentAmount ( size_t amount)
overridevirtual

Implements OutputCodeIntf.

Definition at line 950 of file htmlgen.cpp.

951{
952 m_stripIndentAmount = amount;
953}

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 1157 of file htmlgen.cpp.

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

References m_t.

◆ startCodeLine()

void HtmlCodeGenerator::startCodeLine ( int )
overridevirtual

Implements OutputCodeIntf.

Definition at line 1111 of file htmlgen.cpp.

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

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 1174 of file htmlgen.cpp.

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

References m_hide, m_lineOpen, and m_t.

◆ startFontClass()

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

Implements OutputCodeIntf.

Definition at line 1139 of file htmlgen.cpp.

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

References m_hide, and m_t.

◆ startSpecialComment()

void HtmlCodeGenerator::startSpecialComment ( )
overridevirtual

Implements OutputCodeIntf.

Definition at line 938 of file htmlgen.cpp.

939{
941 //*m_t << "[START]";
942}
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 933 of file htmlgen.cpp.

934{
936}

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 1151 of file htmlgen.cpp.

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

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 987 of file htmlgen.cpp.

991{
992 if (m_hide) return;
993 const char *hl = codeSymbolType2Str(type);
994 DString hlClass = "code";
995 if (hl)
996 {
997 hlClass+=" hl_";
998 hlClass+=hl;
999 }
1000 _writeCodeLink(hlClass,ref,f,anchor,name,tooltip);
1001}
void _writeCodeLink(const DString &className, const DString &ref, const DString &file, const DString &anchor, const DString &name, const DString &tooltip)
Definition htmlgen.cpp:1003
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 955 of file htmlgen.cpp.

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

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 1031 of file htmlgen.cpp.

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

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: