1 #include "xml-template.h"
4 #include <libxml/parser.h>
8 Replace::Replace(const string &str)
11 void Replace::process(xmlNode *node, bool clean) {
12 node->children = xmlNewTextLen(reinterpret_cast<const xmlChar *>(str.data()), str.size());
15 Substitute::Substitute(const unordered_map<string, Directive*> &substitution_map)
16 : substitution_map(substitution_map) {}
18 void Substitute::process(xmlNode *node, bool clean) {
19 for (xmlNode *child = node->children; child != NULL; child = child->next) {
20 bool processed = false;
22 if (child->type == XML_ELEMENT_NODE) {
23 // Find the ID, if any.
25 for (xmlAttr *attr = child->properties; attr != NULL; attr = attr->next) {
26 if (strcmp(reinterpret_cast<const char *>(attr->ns->href), "http://template.sesse.net/") == 0 &&
27 strcmp(reinterpret_cast<const char *>(attr->name), "id") == 0) {
28 id = reinterpret_cast<const char *>(xmlNodeGetContent(attr->children));
31 if (attr->prev == NULL) {
32 child->properties = attr->next;
34 attr->prev->next = attr->next;
40 // Check all substitutions to see if we found anything appropriate.
41 for (auto it : substitution_map) {
42 if (it.first == reinterpret_cast<const char *>(child->name) ||
43 (!id.empty() && it.first == ("#" + id))) {
44 it.second->process(child, clean);
52 process(child, clean);
57 void process_file(const string &input_filename,
58 const string &output_filename,
59 Directive *root_directive)
63 xmlDocPtr doc = xmlParseFile(input_filename.c_str());
64 root_directive->process(xmlDocGetRootElement(doc), true);
65 xmlSaveFile(output_filename.c_str(), doc);