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

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

826 : m_t(t)
827{
828 //printf("%p:HtmlCodeGenerator()\n",(void*)this);
829}

References m_t.

Member Function Documentation

◆ _startOpenLine()

void HtmlCodeGenerator::_startOpenLine ( )
private

Definition at line 1189 of file htmlgen.cpp.

1190{
1191 //printf("_startOpenLine open=%d\n",m_lineOpen);
1192 *m_t << "<div class=\"line\">";
1193 bool wasHidden=m_hide;
1194 m_hide = false;
1195 m_lineOpen = true;
1201 m_hide = wasHidden;
1202}
void writeLineNumber(const DString &, const DString &, const DString &, int, bool) override
Definition htmlgen.cpp:949
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 997 of file htmlgen.cpp.

1001{
1002 m_col+=name.length();
1003 if (m_hide) return;
1004 if (!ref.empty())
1005 {
1006 *m_t << "<a class=\"" << className << "Ref\" ";
1007 *m_t << externalLinkTarget();
1008 }
1009 else
1010 {
1011 *m_t << "<a class=\"" << className << "\" ";
1012 }
1013 *m_t << "href=\"";
1014 DString fn = f;
1016 *m_t << createHtmlUrl(m_relPath,ref,
1017 fileName()==fn,fn,anchor);
1018 *m_t << "\"";
1019 if (!tooltip.empty()) *m_t << " title=\"" << convertToHtml(tooltip) << "\"";
1020 *m_t << ">";
1021 codify(name);
1022 *m_t << "</a>";
1023}
bool empty() const
Returns true iff the string is empty (std::string compatible alias for isEmpty()).
Definition dstring.h:148
size_t length() const
Returns the length of the string, not counting the 0-terminator.
Definition dstring.h:151
void codify(const DString &text) override
Definition htmlgen.cpp:842
DString fileName()
Definition htmlgen.h:30
DString externalLinkTarget(const bool parent)
Definition util.cpp:4491
DString convertToHtml(const DString &s, bool keepEntities)
Definition util.cpp:3291
void addHtmlExtensionIfMissing(DString &fName)
Definition util.cpp:3931
DString createHtmlUrl(const DString &relPath, const DString &ref, bool isLocalFile, const DString &targetFileName, const DString &anchor)
Definition util.cpp:4502

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

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

1157{
1158 //printf("endCodeFragment hide=%d open=%d\n",m_hide,m_lineOpen);
1159 bool wasHidden = m_hide;
1160 m_hide = false;
1161 //endCodeLine checks is there is still an open code line, if so closes it.
1162 endCodeLine();
1163 m_hide = wasHidden;
1164
1165 *m_t << "</div><!-- fragment -->";
1166}
void endCodeLine() override
Definition htmlgen.cpp:1117

References endCodeLine(), m_hide, and m_t.

◆ endCodeLine()

void HtmlCodeGenerator::endCodeLine ( )
overridevirtual

Implements OutputCodeIntf.

Definition at line 1117 of file htmlgen.cpp.

1118{
1119 //printf("endCodeLine hide=%d open=%d\n",m_hide,m_lineOpen);
1120 if (m_hide) return;
1121 if (m_col == 0)
1122 {
1123 *m_t << " ";
1124 m_col++;
1125 }
1126 if (m_lineOpen)
1127 {
1128 *m_t << "</div>\n";
1129 m_lineOpen = false;
1130 }
1131}

References m_col, m_hide, m_lineOpen, and m_t.

Referenced by endCodeFragment().

◆ endFold()

void HtmlCodeGenerator::endFold ( )
overridevirtual

Implements OutputCodeIntf.

Definition at line 1204 of file htmlgen.cpp.

1205{
1206 //printf("endFold open=%d\n",m_lineOpen);
1207 if (m_lineOpen) // if we have a hidden comment in a code fold, we need to end the line
1208 {
1209 *m_t << "</div>\n";
1210 }
1211 *m_t << "</div>\n";
1212 if (m_lineOpen)
1213 {
1215 }
1216}

References _startOpenLine(), m_lineOpen, and m_t.

◆ endFontClass()

void HtmlCodeGenerator::endFontClass ( )
overridevirtual

Implements OutputCodeIntf.

Definition at line 1139 of file htmlgen.cpp.

1140{
1141 if (m_hide) return;
1142 *m_t << "</span>";
1143}

References m_hide, and m_t.

◆ endSpecialComment()

void HtmlCodeGenerator::endSpecialComment ( )
overridevirtual

Implements OutputCodeIntf.

Definition at line 938 of file htmlgen.cpp.

939{
940 //*m_t << "[END]";
941 m_hide = false;
942}

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

838{
839 m_relPath = path;
840}

References m_relPath.

Referenced by HtmlGenerator::startFile().

◆ setStripIndentAmount()

void HtmlCodeGenerator::setStripIndentAmount ( size_t amount)
overridevirtual

Implements OutputCodeIntf.

Definition at line 944 of file htmlgen.cpp.

945{
946 m_stripIndentAmount = amount;
947}

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

1152{
1153 *m_t << "<div class=\"fragment\">";
1154}

References m_t.

◆ startCodeLine()

void HtmlCodeGenerator::startCodeLine ( int )
overridevirtual

Implements OutputCodeIntf.

Definition at line 1105 of file htmlgen.cpp.

1106{
1107 //printf("startCodeLine open=%d\n",m_lineOpen);
1108 m_col=0;
1109 if (m_hide) return;
1110 if (!m_lineOpen)
1111 {
1112 *m_t << "<div class=\"line\">";
1113 m_lineOpen = true;
1114 }
1115}

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

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

References m_hide, m_lineOpen, and m_t.

◆ startFontClass()

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

Implements OutputCodeIntf.

Definition at line 1133 of file htmlgen.cpp.

1134{
1135 if (m_hide) return;
1136 *m_t << "<span class=\"" << s << "\">";
1137}

References m_hide, and m_t.

◆ startSpecialComment()

void HtmlCodeGenerator::startSpecialComment ( )
overridevirtual

Implements OutputCodeIntf.

Definition at line 932 of file htmlgen.cpp.

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

928{
930}

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

1146{
1147 if (m_hide) return;
1148 *m_t << "<a id=\"" << anchor << "\" name=\"" << anchor << "\"></a>";
1149}

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

985{
986 if (m_hide) return;
987 const char *hl = codeSymbolType2Str(type);
988 DString hlClass = "code";
989 if (hl)
990 {
991 hlClass+=" hl_";
992 hlClass+=hl;
993 }
994 _writeCodeLink(hlClass,ref,f,anchor,name,tooltip);
995}
void _writeCodeLink(const DString &className, const DString &ref, const DString &file, const DString &anchor, const DString &name, const DString &tooltip)
Definition htmlgen.cpp:997
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 949 of file htmlgen.cpp.

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

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

1029{
1030 if (m_hide) return;
1031 *m_t << "<div class=\"ttc\" id=\"" << id << "\">";
1032 *m_t << "<div class=\"ttname\">";
1033 if (!docInfo.url.empty())
1034 {
1035 *m_t << "<a href=\"";
1036 DString fn = docInfo.url;
1038 *m_t << createHtmlUrl(m_relPath,docInfo.ref,
1039 fileName()==fn,fn,docInfo.anchor);
1040 *m_t << "\">";
1041 }
1042 codify(docInfo.name);
1043 if (!docInfo.url.empty())
1044 {
1045 *m_t << "</a>";
1046 }
1047 *m_t << "</div>";
1048
1049 if (!decl.empty())
1050 {
1051 *m_t << "<div class=\"ttdeci\">";
1052 codify(decl);
1053 *m_t << "</div>";
1054 }
1055
1056 if (!desc.empty())
1057 {
1058 *m_t << "<div class=\"ttdoc\">";
1059 codify(desc);
1060 *m_t << "</div>";
1061 }
1062
1063 if (!defInfo.file.empty())
1064 {
1065 *m_t << "<div class=\"ttdef\"><b>" << theTranslator->trDefinition() << "</b> ";
1066 if (!defInfo.url.empty())
1067 {
1068 *m_t << "<a href=\"";
1069 DString fn = defInfo.url;
1071 *m_t << createHtmlUrl(m_relPath,defInfo.ref,
1072 fileName()==fn,fn,defInfo.anchor);
1073 *m_t << "\">";
1074 }
1075 *m_t << defInfo.file << ":" << defInfo.line;
1076 if (!defInfo.url.empty())
1077 {
1078 *m_t << "</a>";
1079 }
1080 *m_t << "</div>";
1081 }
1082 if (!declInfo.file.empty())
1083 {
1084 *m_t << "<div class=\"ttdecl\"><b>" << theTranslator->trDeclaration() << "</b> ";
1085 if (!declInfo.url.empty())
1086 {
1087 *m_t << "<a href=\"";
1088 DString fn = declInfo.url;
1090 *m_t << createHtmlUrl(m_relPath,declInfo.ref,
1091 fileName()==fn,fn,declInfo.anchor);
1092 *m_t << "\">";
1093 }
1094 *m_t << declInfo.file << ":" << declInfo.line;
1095 if (!declInfo.url.empty())
1096 {
1097 *m_t << "</a>";
1098 }
1099 *m_t << "</div>";
1100 }
1101 *m_t << "</div>\n";
1102}
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: