Doxygen
Loading...
Searching...
No Matches
htmlentity.h
Go to the documentation of this file.
1/******************************************************************************
2 *
3 * Copyright (C) 1997-2015 by Dimitri van Heesch.
4 *
5 * Permission to use, copy, modify, and distribute this software and its
6 * documentation under the terms of the GNU General Public License is hereby
7 * granted. No representations are made about the suitability of this software
8 * for any purpose. It is provided "as is" without express or implied warranty.
9 * See the GNU General Public License for more details.
10 *
11 * Documents produced by Doxygen are derivative works derived from the
12 * input used in their production; they are not affected by this license.
13 *
14 */
15#ifndef HTMLENTITY_H
16#define HTMLENTITY_H
17
18#include <cassert>
19#include <functional>
20#include <string>
21#include <unordered_map>
22
23#include "dstring.h"
24#include "construct.h"
25
26class TextStream;
27
28/** @brief Singleton helper class to map html entities to other formats */
30{
31 public:
32 enum SymType { Sym_Unknown = -1,
84
85 /* doxygen extensions */
87
88 /* doxygen commands mapped */
93 };
97 };
98 static HtmlEntityMapper &instance();
99 SymType name2sym(const DString &symName) const;
100 const char *utf8(SymType symb,bool useInPrintf=false) const;
101 const char *html(SymType symb,bool useInPrintf=false) const;
102 const char *xml(SymType symb) const;
103 const char *docbook(SymType symb) const;
104 const char *latex(SymType symb) const;
105 const char *man(SymType symb) const;
106 const char *rtf(SymType symb) const;
107 struct PerlSymb
108 {
109 const char *symb;
111 };
112 const PerlSymb *perl(SymType symb) const;
113 void writeXMLSchema(TextStream &t);
115
116 using HtmlEntityMapperFunc = std::function<DString(SymType)>;
117
118 /*! Writes an HTML entity for the current symbol and advances the input pointer.
119 * \tparam T Type of the output sink used to write encoded output.
120 * \param result Output target receiving the encoded entity or fallback text.
121 * \param s Pointer to the start of a potential HTML entity in the input text.
122 * \param mapper Callback that maps a entity symbol type to its HTML entity string.
123 * \param fallback Fallback string written when no entity mapping is available.
124 * \return Pointer to the position after the processed HTML entity in the input text.
125 */
126 template<class T>
127 const char *writeHtmlEntity(T &result, const char *s, HtmlEntityMapperFunc &&mapper, const char *fallback)
128 {
129 assert(s!=nullptr);
130 assert(s[0]=='&');
131 const char *q = s+1;
132 size_t cnt = 2; // we have to count & and ; as well
133 while ((*q >= 'a' && *q <= 'z') || (*q >= 'A' && *q <= 'Z') || (*q >= '0' && *q <= '9'))
134 {
135 cnt++;
136 q++;
137 }
138 if (*q == ';') // valid entity name
139 {
140 SymType res = name2sym(DString(s).left(cnt));
141 if (res!=Sym_Unknown)
142 {
143 result += mapper(res);
144 return q+1;
145 }
146 }
147 result += fallback;
148 return s+1;
149 }
150
151 private:
152 void validate();
157 std::unordered_map<std::string,SymType> m_name2sym;
158};
159
160#endif
A String class for use with Doxygen wrapping std::string and adding some additional functionality off...
Definition dstring.h:88
Singleton helper class to map html entities to other formats.
Definition htmlentity.h:30
SymType name2sym(const DString &symName) const
Give code of the requested HTML entity name.
const char * latex(SymType symb) const
Access routine to the LaTeX code of the HTML entity.
const PerlSymb * perl(SymType symb) const
Access routine to the perl struct with the perl code of the HTML entity.
const char * writeHtmlEntity(T &result, const char *s, HtmlEntityMapperFunc &&mapper, const char *fallback)
Definition htmlentity.h:127
const char * xml(SymType symb) const
Access routine to the XML code of the HTML entity.
const char * utf8(SymType symb, bool useInPrintf=false) const
Access routine to the UTF8 code of the HTML entity.
void writeXMLSchema(TextStream &t)
static HtmlEntityMapper & instance()
Returns the one and only instance of the HTML entity mapper.
std::unordered_map< std::string, SymType > m_name2sym
Definition htmlentity.h:157
const char * rtf(SymType symb) const
Access routine to the RTF code of the HTML entity.
void validate()
Routine to check if the entries of the html_entities are numbered correctly.
const char * docbook(SymType symb) const
Access routine to the docbook code of the HTML entity.
const char * html(SymType symb, bool useInPrintf=false) const
Access routine to the html code of the HTML entity.
DString convertCharEntitiesToUTF8(const DString &s) const
const char * man(SymType symb) const
Access routine to the man code of the HTML entity.
std::function< DString(SymType)> HtmlEntityMapperFunc
Definition htmlentity.h:116
static HtmlEntityMapper * s_instance
Definition htmlentity.h:156
Text streaming class that buffers data.
Definition textstream.h:36
#define NON_COPYABLE(cls)
Macro to help implementing the rule of 5 for a non-copyable & movable class.
Definition construct.h:37
Definition dstring.h:917