31 #ifndef WRAPPER_TIDYDOC_HPP_ 32 #define WRAPPER_TIDYDOC_HPP_ 36 #include "../Helper/SilentInclude/tidy.h" 37 #include "../Helper/Strings.hpp" 38 #include "../Main/Exception.hpp" 82 [[nodiscard]] std::string
getOutput(std::queue<std::string>& warningsTo);
88 void setOption(TidyOptionId option,
bool value);
89 void setOption(TidyOptionId option,
int value);
90 void setOption(TidyOptionId option, ulong value);
91 void setOption(TidyOptionId option,
const std::string& value);
97 void parse(
const std::string& in, std::queue<std::string>& warningsTo);
147 [[nodiscard]]
static bool isVersionBelow5_7_18();
168 if(TidyDoc::isVersionBelow5_7_18()) {
169 tidySetLanguage(tidyGetLanguage());
173 this->doc = tidyCreate();
174 this->created =
true;
177 if(tidySetErrorBuffer(this->doc, this->errors.
get()) != 0) {
178 throw Exception(
"Could not set error buffer");
185 tidyRelease(this->doc);
188 this->created =
false;
224 switch(tidySaveBuffer(this->doc, buffer.
get())) {
232 if(this->errors.
valid()) {
233 std::queue<std::string> warnings(
241 while(!warnings.empty()) {
242 warningsTo.emplace(warnings.front());
247 this->errors.
clear();
254 if(this->errors.
valid() && !(this->errors.
empty())) {
258 throw Exception(
"Could not write to buffer");
265 return std::string();
297 if(tidyOptSetBool(this->doc, option, value ? yes : no) == 0) {
299 "Could not set tidy option #" 300 + std::to_string(option)
302 + (value ?
"yes" :
"no")
330 if(tidyOptSetInt(this->doc, option, value) == 0) {
332 "Could not set tidy option #" 333 + std::to_string(option)
335 + std::to_string(value)
363 if(tidyOptSetInt(this->doc, option, value) == 0) {
365 "Could not set tidy option #" 366 + std::to_string(option)
367 +
" to unsigned integer " 368 + std::to_string(value)
396 if(tidyOptSetValue(this->doc, option, value.c_str()) == 0) {
398 "Could not set tidy option #" 399 + std::to_string(option)
441 inline void TidyDoc::parse(
const std::string& in, std::queue<std::string>& warningsTo) {
442 switch(tidyParseString(this->doc, in.c_str())) {
450 if(this->errors.
valid()) {
451 std::queue<std::string> warnings(
459 while(!warnings.empty()) {
460 warningsTo.emplace(warnings.front());
465 this->errors.
clear();
472 if(this->errors.
valid() && !(this->errors.
empty())) {
506 switch(tidyCleanAndRepair(this->doc)) {
514 if(this->errors.
valid()) {
515 std::queue<std::string> warnings(
523 while(!warnings.empty()) {
524 warningsTo.emplace(warnings.front());
529 this->errors.
clear();
536 if(this->errors.
valid() && !(this->errors.
empty())) {
540 throw Exception(
"Could not clean and repair HTML");
545 inline bool TidyDoc::isVersionBelow5_7_18() {
546 constexpr
auto firstDotPosition{1};
547 constexpr
auto secondDotPosition{3};
550 const std::string version(tidyLibraryVersion());
552 if(version.substr(0, firstDotPosition) >
"5") {
556 if(version.size() > firstDotPosition && version[firstDotPosition] !=
'.') {
560 if(version.substr(0, secondDotPosition) >
"5.7") {
564 if(version.substr(0, firstDotPosition) <
"5") {
568 if(version.size() > secondDotPosition && version[secondDotPosition] !=
'.') {
572 if(version >=
"5.7.18" && version.size() >=
length) {
576 if(version.substr(0, secondDotPosition) <
"5.7") {
580 if(version.size() >
length) {
::TidyBuffer * get() noexcept
Gets a pointer to the underlying buffer.
Definition: TidyBuffer.hpp:119
bool valid() const noexcept
Checks whether the underlying buffer is valid.
Definition: TidyBuffer.hpp:155
bool empty() const noexcept
Checks whether the underlying buffer is empty.
Definition: TidyBuffer.hpp:195
virtual ~TidyDoc()
Destructor releasing the underlying tidy-html5 document.
Definition: TidyDoc.hpp:183
void setOption(TidyOptionId option, bool value)
Sets a boolean option.
Definition: TidyDoc.hpp:296
#define MAIN_EXCEPTION_CLASS()
Macro used to easily define classes for general exceptions.
Definition: Exception.hpp:50
RAII wrapper for documents used by the tidy-html5 API.
Definition: TidyDoc.hpp:70
RAII wrapper for buffers used by the tidy-html5 API.
Definition: TidyBuffer.hpp:53
std::queue< std::string > splitToQueue(std::string_view str, char delimiter, bool removeEmpty)
Splits a string into a queue of strings using the given delimiter.
Definition: Strings.hpp:794
std::string getString() const noexcept
Copies the content of the underlying buffer into a string.
Definition: TidyBuffer.hpp:136
void clear() noexcept
Frees the underlying buffer.
Definition: TidyBuffer.hpp:209
Namespace for RAII wrappers and Wrapper::Database.
Definition: Database.hpp:109
std::size_t length(std::string_view str)
Definition: Utf8.hpp:327
TidyDoc()
Constructor creating an empty tidy-html5 document.
Definition: TidyDoc.hpp:166
void parse(const std::string &in, std::queue< std::string > &warningsTo)
Parses the given markup.
Definition: TidyDoc.hpp:441
TidyDoc & operator=(TidyDoc &)=delete
Deleted copy assignment operator.
std::string getOutput(std::queue< std::string > &warningsTo)
Gets the processed text from the tidy-html5 document.
Definition: TidyDoc.hpp:221
Class for tidy-html5 document exceptions.
Definition: TidyDoc.hpp:118
void cleanAndRepair(std::queue< std::string > &warningsTo)
Cleans and repairs the previously parsed content of the underlying tidy-html5 document.
Definition: TidyDoc.hpp:505