CXX=g++
CXXFLAGS=-std=gnu++0x -g -Wall $(shell xml2-config --cflags)
+OBJS=simple.o xml-template.o
-simple: simple.o
- $(CXX) -o $@ $< $(shell xml2-config --libs)
+simple: $(OBJS)
+ $(CXX) -o $@ $(OBJS) $(shell xml2-config --libs)
#include <string.h>
#include <libxml/parser.h>
-#include <string>
-#include <utility>
-#include <unordered_map>
+#include "xml-template.h"
using namespace std;
-class Directive {
- public:
- virtual void process(xmlNode *node, bool clean) = 0;
-};
-
-class Replace : public Directive {
- public:
- Replace(const string &str) : str(str) {}
- virtual void process(xmlNode *node, bool clean) {
- node->children = xmlNewTextLen(reinterpret_cast<const xmlChar *>(str.data()), str.size());
- }
-
- private:
- const string str;
-};
-
-class Substitute : public Directive {
- public:
- Substitute(const unordered_map<string, Directive*> &substitution_map)
- : substitution_map(substitution_map) {}
-
- virtual void process(xmlNode *node, bool clean) {
- for (xmlNode *child = node->children; child != NULL; child = child->next) {
- bool processed = false;
-
- if (child->type == XML_ELEMENT_NODE) {
- // Find the ID, if any.
- string id;
- for (xmlAttr *attr = child->properties; attr != NULL; attr = attr->next) {
- if (strcmp(reinterpret_cast<const char *>(attr->ns->href), "http://template.sesse.net/") == 0 &&
- strcmp(reinterpret_cast<const char *>(attr->name), "id") == 0) {
- id = reinterpret_cast<const char *>(xmlNodeGetContent(attr->children));
- }
- }
-
- // Check all substitutions to see if we found anything appropriate.
- for (auto it : substitution_map) {
- if (it.first == reinterpret_cast<const char *>(child->name) ||
- (!id.empty() && it.first == ("#" + id))) {
- it.second->process(child, clean);
- processed = true;
- break;
- }
- }
- }
-
- if (!processed) {
- process(child, clean);
- }
- }
- }
-
- private:
- const unordered_map<string, Directive*> &substitution_map;
-};
-
int main(int argc, char **argv)
{
LIBXML_TEST_VERSION
--- /dev/null
+#include "xml-template.h"
+
+#include <string.h>
+
+using namespace std;
+
+Replace::Replace(const string &str)
+ : str(str) {}
+
+void Replace::process(xmlNode *node, bool clean) {
+ node->children = xmlNewTextLen(reinterpret_cast<const xmlChar *>(str.data()), str.size());
+}
+
+Substitute::Substitute(const unordered_map<string, Directive*> &substitution_map)
+ : substitution_map(substitution_map) {}
+
+void Substitute::process(xmlNode *node, bool clean) {
+ for (xmlNode *child = node->children; child != NULL; child = child->next) {
+ bool processed = false;
+
+ if (child->type == XML_ELEMENT_NODE) {
+ // Find the ID, if any.
+ string id;
+ for (xmlAttr *attr = child->properties; attr != NULL; attr = attr->next) {
+ if (strcmp(reinterpret_cast<const char *>(attr->ns->href), "http://template.sesse.net/") == 0 &&
+ strcmp(reinterpret_cast<const char *>(attr->name), "id") == 0) {
+ id = reinterpret_cast<const char *>(xmlNodeGetContent(attr->children));
+ }
+ }
+
+ // Check all substitutions to see if we found anything appropriate.
+ for (auto it : substitution_map) {
+ if (it.first == reinterpret_cast<const char *>(child->name) ||
+ (!id.empty() && it.first == ("#" + id))) {
+ it.second->process(child, clean);
+ processed = true;
+ break;
+ }
+ }
+ }
+
+ if (!processed) {
+ process(child, clean);
+ }
+ }
+}
--- /dev/null
+#ifndef _XML_TEMPLATE_H
+#define _XML_TEMPLATE_H 1
+
+#include <libxml/tree.h>
+
+#include <string>
+#include <utility>
+#include <unordered_map>
+
+class Directive {
+ public:
+ virtual void process(xmlNode *node, bool clean) = 0;
+};
+
+class Replace : public Directive {
+ public:
+ Replace(const std::string &str);
+ virtual void process(xmlNode *node, bool clean);
+
+ private:
+ const std::string str;
+};
+
+class Substitute : public Directive {
+ public:
+ Substitute(const std::unordered_map<std::string, Directive*> &substitution_map);
+
+ virtual void process(xmlNode *node, bool clean);
+
+ private:
+ const std::unordered_map<std::string, Directive*> &substitution_map;
+};
+
+#endif // !defined(_XML_TEMPLATE_H)