Skip to content

Commit 85debda

Browse files
committed
use enumeration for HTML standard
1 parent da56e2e commit 85debda

File tree

10 files changed

+109
-114
lines changed

10 files changed

+109
-114
lines changed

code/bbcode/BBCodeParser.cpp

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
-------------------------------------------------------------------------------
33
This file is part of the Private Message Database.
4-
Copyright (C) 2012, 2013 Dirk Stolle
4+
Copyright (C) 2012, 2013, 2025 Dirk Stolle
55
66
This program is free software: you can redistribute it and/or modify
77
it under the terms of the GNU General Public License as published by
@@ -36,58 +36,51 @@ BBCodeParser::BBCodeParser()
3636
{
3737
}
3838

39-
std::string BBCodeParser::parse(std::string text, const std::string& forumURL, const bool isXHTML, const bool nl2br) const
39+
std::string BBCodeParser::parse(std::string text, const std::string& forumURL, const HTMLStandard standard, const bool nl2br) const
4040
{
4141
#ifndef NO_PREPROCESSORS_IN_PARSER
42-
//handle pre processors
43-
std::vector<TextProcessor*>::const_iterator pre_iter = m_PreProcs.begin();
44-
while (pre_iter!=m_PreProcs.end())
42+
// handle pre processors
43+
for (const auto * pre_ptr: m_PreProcs)
4544
{
46-
(*pre_iter)->applyToText(text);
47-
++pre_iter;
48-
}//while
45+
pre_ptr->applyToText(text);
46+
}
4947
#endif
5048

51-
//handle line breaks
49+
// handle line breaks
5250
if (nl2br)
5351
{
5452
std::string::size_type pos = text.find("\n");
55-
while (pos!=std::string::npos)
53+
const bool isXHTML = standard == HTMLStandard::XHTML;
54+
while (pos != std::string::npos)
5655
{
5756
text.replace(pos, 1, isXHTML ? "<br />\n" : "<br>\n");
58-
pos = text.find("\n", isXHTML ? pos+7 : pos+5);
59-
}//while
60-
}//if
57+
pos = text.find("\n", isXHTML ? pos + 7 : pos + 5);
58+
}
59+
} // if
6160

62-
//handle bb codes
63-
std::vector<BBCode*>::const_iterator iter = m_Codes.begin();
64-
while (iter!=m_Codes.end())
61+
// handle bb codes
62+
for (const BBCode* code: m_Codes)
6563
{
66-
(*iter)->applyToText(text);
67-
++iter;
68-
}//while
64+
code->applyToText(text);
65+
}
6966

7067
#ifndef NO_SMILIES_IN_PARSER
71-
//handle smilies
72-
std::vector<Smilie>::const_iterator sm_iter = m_Smilies.begin();
73-
while (sm_iter!=m_Smilies.end())
68+
// handle smilies
69+
for (const auto& sm: m_Smilies)
7470
{
75-
sm_iter->applyToText(text, forumURL, isXHTML);
76-
++sm_iter;
77-
}//while
71+
sm.applyToText(text, forumURL, standard);
72+
}
7873
#endif
7974

80-
//handle quotes
75+
// handle quotes
8176
text = handleQuotes(text, forumURL);
8277

8378
#ifndef NO_POSTPROCESSORS_IN_PARSER
84-
//handle post processors
85-
std::vector<TextProcessor*>::const_iterator post_iter = m_PostProcs.begin();
86-
while (post_iter!=m_PostProcs.end())
79+
// handle post processors
80+
for (const auto* post_ptr: m_PostProcs)
8781
{
88-
(*post_iter)->applyToText(text);
89-
++post_iter;
90-
}//while
82+
post_ptr->applyToText(text);
83+
}
9184
#endif
9285

9386
return text;
@@ -96,14 +89,14 @@ std::string BBCodeParser::parse(std::string text, const std::string& forumURL, c
9689
#ifndef NO_PREPROCESSORS_IN_PARSER
9790
void BBCodeParser::addPreProcessor(TextProcessor* preProc)
9891
{
99-
if (preProc!=NULL)
92+
if (preProc != nullptr)
10093
m_PreProcs.push_back(preProc);
10194
}
10295
#endif
10396

10497
void BBCodeParser::addCode(BBCode* code)
10598
{
106-
if (code!=NULL)
99+
if (code != nullptr)
107100
m_Codes.push_back(code);
108101
}
109102

@@ -117,7 +110,7 @@ void BBCodeParser::addSmilie(const Smilie& sm)
117110
#ifndef NO_POSTPROCESSORS_IN_PARSER
118111
void BBCodeParser::addPostProcessor(TextProcessor* postProc)
119112
{
120-
if (postProc!=NULL)
113+
if (postProc != nullptr)
121114
m_PostProcs.push_back(postProc);
122115
}
123116
#endif

code/bbcode/BBCodeParser.hpp

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
-------------------------------------------------------------------------------
33
This file is part of the Private Message Database.
4-
Copyright (C) 2012, 2013, 2014 Dirk Stolle
4+
Copyright (C) 2012, 2013, 2014, 2025 Dirk Stolle
55
66
This program is free software: you can redistribute it and/or modify
77
it under the terms of the GNU General Public License as published by
@@ -36,6 +36,7 @@
3636
#include <string>
3737
#include <vector>
3838
#include "BBCode.hpp"
39+
#include "HTMLStandard.hpp"
3940
#ifndef NO_SMILIES_IN_PARSER
4041
#include "Smilie.hpp"
4142
#endif
@@ -44,34 +45,32 @@
4445
#endif
4546

4647
/** \brief BBCodeParser:
47-
transforms a given text containing BB codes to HTML code via the
48+
Transforms a given text containing BB codes to (X)HTML code via the
4849
parse() function. All codes that should be replaced have to be added
4950
before parsing. Use addCode() for that. Smilies added via addSmilie()
5051
will be replaced, too.
5152
*/
5253
class BBCodeParser
5354
{
5455
public:
55-
/** constructor */
5656
BBCodeParser();
5757

5858

59-
/** \brief transforms BB codes in text to HTML codes (still incomplete)
59+
/** \brief Transforms BB codes in text to (X)HTML codes (still incomplete).
6060
*
6161
* \param text the original text
6262
* \param forumURL the base URL of the forum (some BB codes might require
63-
this URL for proper transformation of code to HTML)
64-
* \param isXHTML if set to true, smilie transformations will produce XHTML
65-
image tags
63+
this URL for proper transformation of code to (X)HTML)
64+
* \param standard the HTML standard to use during transformation (HTML or XHTML)
6665
* \param nl2br if set to true, new line characters will be converted to
6766
the corresponding (X)HTML code for line breaks
6867
* \return Returns the transformed/parsed text.
6968
*/
70-
std::string parse(std::string text, const std::string& forumURL, const bool isXHTML, const bool nl2br) const;
69+
std::string parse(std::string text, const std::string& forumURL, const HTMLStandard standard, const bool nl2br) const;
7170

7271

7372
#ifndef NO_PREPROCESSORS_IN_PARSER
74-
/** \brief adds a new text preprocessor to the parser
73+
/** \brief Adds a new text preprocessor to the parser.
7574
*
7675
* \param preProc pointer to the text processor object that should be added
7776
* \remarks The passed pointers must live for the whole lifetime of the
@@ -92,7 +91,7 @@ class BBCodeParser
9291
#endif
9392

9493

95-
/** \brief adds a new bb code to the parser
94+
/** \brief Adds a new BB code to the parser.
9695
*
9796
* \param code pointer to the BB code object that should be added
9897
* \remarks
@@ -113,16 +112,16 @@ class BBCodeParser
113112

114113

115114
#ifndef NO_SMILIES_IN_PARSER
116-
/** \brief adds a new smilie to the parser
115+
/** \brief Adds a new smilie to the parser.
117116
*
118-
* \param sm the smilie
117+
* \param sm the smilie to add
119118
*/
120119
void addSmilie(const Smilie& sm);
121120
#endif
122121

123122

124123
#ifndef NO_POSTPROCESSORS_IN_PARSER
125-
/** \brief adds a new text postprocessor to the parser
124+
/** \brief Adds a new text postprocessor to the parser.
126125
*
127126
* \param postProc pointer to the text processor object that should be added
128127
*
@@ -145,26 +144,26 @@ class BBCodeParser
145144
#endif
146145

147146
#ifndef NO_PREPROCESSORS_IN_PARSER
148-
/** clears all added preprocessors */
147+
/** Clears all added preprocessors. */
149148
inline void clearPreProcessors()
150149
{
151150
m_PreProcs.clear();
152151
}
153152
#endif
154153

155154

156-
/** \brief clears all added BB codes
155+
/** \brief Clears all added BB codes.
157156
*
158157
* \remarks
159158
* Clearing codes right before calling parse() without any addCode()
160159
* calls in between will result in no BB codes being parsed. However,
161-
* smilies might still get parsed.
160+
* smilies might still get parsed, if there are any.
162161
*/
163162
void clearCodes();
164163

165164

166165
#ifndef NO_SMILIES_IN_PARSER
167-
/** \brief clears all added smilies
166+
/** \brief Clears all added smilies.
168167
*
169168
* \remarks
170169
* Clearing smilies right before calling parse() without any addSmile()
@@ -175,7 +174,7 @@ class BBCodeParser
175174
#endif
176175

177176
#ifndef NO_POSTPROCESSORS_IN_PARSER
178-
/** clears all added postprocessors */
177+
/** Clears all added postprocessors. */
179178
inline void clearPostProcessors()
180179
{
181180
m_PostProcs.clear();
@@ -192,6 +191,6 @@ class BBCodeParser
192191
std::vector<TextProcessor*> m_PostProcs;
193192
#endif
194193
std::vector<BBCode*> m_Codes;
195-
};//class
194+
}; // class
196195

197196
#endif // BBCODEPARSER_HPP

code/bbcode/HorizontalRuleBBCode.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
-------------------------------------------------------------------------------
33
This file is part of the Private Message Database.
4-
Copyright (C) 2012, 2015, 2016 Dirk Stolle
4+
Copyright (C) 2012, 2015, 2016, 2025 Dirk Stolle
55
66
This program is free software: you can redistribute it and/or modify
77
it under the terms of the GNU General Public License as published by
@@ -21,17 +21,17 @@
2121
#include "HorizontalRuleBBCode.hpp"
2222
#include "../../libstriezel/common/StringUtils.hpp"
2323

24-
HorizontalRuleBBCode::HorizontalRuleBBCode(const std::string& code, const bool isXHTML)
25-
: BBCode(code), m_isXHTML(isXHTML)
24+
HorizontalRuleBBCode::HorizontalRuleBBCode(const std::string& code, const HTMLStandard standard)
25+
: BBCode(code), m_standard(standard)
2626
{ }
2727

2828
void HorizontalRuleBBCode::applyToText(std::string& text) const
2929
{
30-
const std::string code = "["+getName()+"][/"+getName()+"]";
30+
const std::string code = "[" + getName() + "][/" + getName() + "]";
3131
std::string::size_type pos = find_ci(text, code);
32-
while (pos!=std::string::npos)
32+
while (pos != std::string::npos)
3333
{
34-
text.replace(pos, code.length(), m_isXHTML ? "<hr />" : "<hr>");
34+
text.replace(pos, code.length(), m_standard == HTMLStandard::XHTML ? "<hr />" : "<hr>");
3535
pos = find_ci(text, code);
36-
}//while
36+
}
3737
}

code/bbcode/HorizontalRuleBBCode.hpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
-------------------------------------------------------------------------------
33
This file is part of the Private Message Database.
4-
Copyright (C) 2012, 2014, 2015 Dirk Stolle
4+
Copyright (C) 2012, 2014, 2015, 2025 Dirk Stolle
55
66
This program is free software: you can redistribute it and/or modify
77
it under the terms of the GNU General Public License as published by
@@ -23,28 +23,30 @@
2323

2424
#include <string>
2525
#include "BBCode.hpp"
26+
#include "HTMLStandard.hpp"
2627

2728
/** \brief HorizontalRuleBBCode:
2829
struct for hr BB code
2930
*/
3031
struct HorizontalRuleBBCode: public BBCode
3132
{
3233
public:
33-
/** constructor
34+
/** Creates a new instance.
3435
*
35-
* \param code "name" of the code, i.e. "b" for [B]bold text[/B]
36+
* \param code "name" of the code, i.e. "b" for [B]bold text[/B]
37+
* \param standard the HTML standard to use during transformation (HTML or XHTML)
3638
*/
37-
HorizontalRuleBBCode(const std::string& code, const bool isXHTML);
39+
HorizontalRuleBBCode(const std::string& code, const HTMLStandard standard);
3840

3941

40-
/** \brief "applies" the BB code to the given text, i.e. transforms the BB code
41-
* into its HTML representation
42+
/** \brief "Applies" the BB code to the given text, i.e. transforms the BB code
43+
* into its (X)HTML representation.
4244
*
4345
* \param text the message text that (may) contain the BB code
4446
*/
4547
virtual void applyToText(std::string& text) const;
4648
private:
47-
bool m_isXHTML;
48-
};//struct HorizontalRuleBBCode
49+
HTMLStandard m_standard;
50+
}; // struct
4951

5052
#endif // HORIZONTALRULEBBCODE_HPP

code/bbcode/Smilie.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ Smilie::Smilie(const std::string& code, const std::string& url, const UrlType ur
2525
{
2626
}
2727

28-
void Smilie::applyToText(std::string& text, const std::string& forumURL, const bool isXHTML) const
28+
void Smilie::applyToText(std::string& text, const std::string& forumURL, const HTMLStandard standard) const
2929
{
3030
std::string::size_type pos = text.find(m_Code);
3131

3232
const std::string replacement = "<img src=\""
3333
+ (type_of_url == UrlType::Relative ? forumURL + m_URL : m_URL) + "\" alt=\"" + m_Code
34-
+ (isXHTML ? "\" border=\"0\" />" : "\" border=\"0\">");
34+
+ (standard == HTMLStandard::XHTML ? "\" border=\"0\" />" : "\" border=\"0\">");
3535
while (pos != std::string::npos)
3636
{
3737
text.replace(pos, m_Code.length(), replacement);

code/bbcode/Smilie.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
-------------------------------------------------------------------------------
33
This file is part of the Private Message Database.
4-
Copyright (C) 2012 Dirk Stolle
4+
Copyright (C) 2012, 2025 Dirk Stolle
55
66
This program is free software: you can redistribute it and/or modify
77
it under the terms of the GNU General Public License as published by
@@ -22,6 +22,7 @@
2222
#define SMILIE_HPP
2323

2424
#include <string>
25+
#include "HTMLStandard.hpp"
2526

2627
enum class UrlType: bool
2728
{
@@ -48,10 +49,9 @@ struct Smilie
4849
*
4950
* \param text the message text that (may) contain the smilie code
5051
* \param forumURL base URL of the forum
51-
* \param isXHTML if set to true, the replacement will XHTML code. Otherwise
52-
* HTML code is used.
52+
* \param standard Indicates the HTML standard to use for the replacement (HTML or XHTML).
5353
*/
54-
void applyToText(std::string& text, const std::string& forumURL, const bool isXHTML) const;
54+
void applyToText(std::string& text, const std::string& forumURL, const HTMLStandard standard) const;
5555
private:
5656
std::string m_Code;
5757
std::string m_URL;

0 commit comments

Comments
 (0)