one StringBuilder

This commit is contained in:
Svetlana Kulikova
2020-07-28 12:07:56 +03:00
parent c8961a8d3b
commit 14e49aeb9a
2 changed files with 23 additions and 35 deletions

View File

@ -21,7 +21,7 @@ static std::string special_handling = "|html|body|";
static std::string no_entity_sub = "|script|style|"; static std::string no_entity_sub = "|script|style|";
static std::string treat_like_inline = "|p|"; static std::string treat_like_inline = "|p|";
static std::string prettyprint(GumboNode*); static void prettyprint(GumboNode*, NSStringUtils::CStringBuilderA& oBuilder);
static std::wstring htmlToXhtml(const std::wstring& sFile) static std::wstring htmlToXhtml(const std::wstring& sFile)
{ {
@ -56,7 +56,9 @@ static std::wstring htmlToXhtml(const std::wstring& sFile)
GumboOutput* output = gumbo_parse_with_options(&options, sFileContent.data(), sFileContent.length()); GumboOutput* output = gumbo_parse_with_options(&options, sFileContent.data(), sFileContent.length());
// prettyprint // prettyprint
std::string sR = prettyprint(output->document); NSStringUtils::CStringBuilderA oBuilder;
prettyprint(output->document, oBuilder);
std::string sR = oBuilder.GetData();
// Вставка кодировки в файл // Вставка кодировки в файл
if(sR.length() > 5) if(sR.length() > 5)
@ -161,9 +163,8 @@ static void build_attributes(GumboAttribute* at, bool no_entities, NSStringUtils
} }
static std::string prettyprint_contents(GumboNode* node) static void prettyprint_contents(GumboNode* node, NSStringUtils::CStringBuilderA& contents)
{ {
NSStringUtils::CStringBuilderA contents;
std::string key = "|" + get_tag_name(node) + "|"; std::string key = "|" + get_tag_name(node) + "|";
bool no_entity_substitution = no_entity_sub.find(key) != std::string::npos; bool no_entity_substitution = no_entity_sub.find(key) != std::string::npos;
bool keep_whitespace = preserve_whitespace.find(key) != std::string::npos; bool keep_whitespace = preserve_whitespace.find(key) != std::string::npos;
@ -184,8 +185,7 @@ static std::string prettyprint_contents(GumboNode* node)
} }
else if ((child->type == GUMBO_NODE_ELEMENT) || (child->type == GUMBO_NODE_TEMPLATE)) else if ((child->type == GUMBO_NODE_ELEMENT) || (child->type == GUMBO_NODE_TEMPLATE))
{ {
std::string val = prettyprint(child); prettyprint(child, contents);
contents.WriteString(val);
} }
else if (child->type == GUMBO_NODE_WHITESPACE) else if (child->type == GUMBO_NODE_WHITESPACE)
{ {
@ -199,59 +199,47 @@ static std::string prettyprint_contents(GumboNode* node)
// fprintf(stderr, "unknown element of type: %d\n", child->type); // fprintf(stderr, "unknown element of type: %d\n", child->type);
} }
} }
return contents.GetData();
} }
static std::string prettyprint(GumboNode* node) static void prettyprint(GumboNode* node, NSStringUtils::CStringBuilderA& oBuilder)
{ {
NSStringUtils::CStringBuilderA oBuilder;
// special case the document node // special case the document node
if (node->type == GUMBO_NODE_DOCUMENT) if (node->type == GUMBO_NODE_DOCUMENT)
{ {
build_doctype(node, oBuilder); build_doctype(node, oBuilder);
oBuilder.WriteString(prettyprint_contents(node)); prettyprint_contents(node, oBuilder);
return oBuilder.GetData(); return;
} }
std::string close = ""; std::string close = "";
std::string closeTag = ""; std::string closeTag = "";
NSStringUtils::CStringBuilderA atts;
std::string tagname = get_tag_name(node); std::string tagname = get_tag_name(node);
std::string key = "|" + tagname + "|"; std::string key = "|" + tagname + "|";
bool is_empty_tag = empty_tags.find(key) != std::string::npos; bool is_empty_tag = empty_tags.find(key) != std::string::npos;
bool no_entity_substitution = no_entity_sub.find(key) != std::string::npos; bool no_entity_substitution = no_entity_sub.find(key) != std::string::npos;
// build attr string
const GumboVector * attribs = &node->v.element.attributes;
for (int i = 0; i < attribs->length; ++i)
{
GumboAttribute* at = static_cast<GumboAttribute*>(attribs->data[i]);
build_attributes(at, no_entity_substitution, atts);
}
// determine closing tag type // determine closing tag type
if (is_empty_tag) if (is_empty_tag)
close = "/"; close = "/";
else else
closeTag = "</" + tagname + ">"; closeTag = "</" + tagname + ">";
// prettyprint your contents
std::string contents = prettyprint_contents(node);
char last_char = ' ';
if (!contents.empty())
last_char = contents.at(contents.length() - 1);
// build results // build results
NSStringUtils::CStringBuilderA results; oBuilder.WriteString("<" + tagname);
results.WriteString("<" + tagname + atts.GetData() + close + ">");
results.WriteString(contents);
results.WriteString(closeTag);
return results.GetData(); // build attr string
const GumboVector * attribs = &node->v.element.attributes;
for (int i = 0; i < attribs->length; ++i)
{
GumboAttribute* at = static_cast<GumboAttribute*>(attribs->data[i]);
build_attributes(at, no_entity_substitution, oBuilder);
}
oBuilder.WriteString(close + ">");
// prettyprint your contents
prettyprint_contents(node, oBuilder);
oBuilder.WriteString(closeTag);
} }
#endif // HTMLTOXHTML_H #endif // HTMLTOXHTML_H

View File

@ -15,7 +15,7 @@ void readFile( XmlUtils::CXmlLiteReader& oLightReader)
int main() int main()
{ {
// Файл, который открываем // Файл, который открываем
std::wstring sFile = NSFile::GetProcessDirectory() + L"/../../../examples/test3.html"; std::wstring sFile = NSFile::GetProcessDirectory() + L"/../../../examples/test2.html";
// Директория, где будем создавать xhtml // Директория, где будем создавать xhtml
std::wstring sOutputDirectory = NSFile::GetProcessDirectory() + L"/res"; std::wstring sOutputDirectory = NSFile::GetProcessDirectory() + L"/res";