summaryrefslogtreecommitdiff
path: root/src/document.cc
diff options
context:
space:
mode:
authorJoel Klinghed <the_jk@spawned.biz>2021-11-17 22:34:57 +0100
committerJoel Klinghed <the_jk@spawned.biz>2021-11-17 22:34:57 +0100
commit6232d13f5321b87ddf12a1aa36b4545da45f173d (patch)
tree23f3316470a14136debd9d02f9e920ca2b06f4cc /src/document.cc
Travel3: Simple image and video display site
Reads the images and videos from filesystem and builds a site in memroy.
Diffstat (limited to 'src/document.cc')
-rw-r--r--src/document.cc66
1 files changed, 66 insertions, 0 deletions
diff --git a/src/document.cc b/src/document.cc
new file mode 100644
index 0000000..498e50f
--- /dev/null
+++ b/src/document.cc
@@ -0,0 +1,66 @@
+#include "common.hh"
+
+#include "document.hh"
+#include "hash_method.hh"
+#include "htmlutil.hh"
+#include "tag.hh"
+
+#include <vector>
+
+namespace {
+
+class DocumentImpl : public Document {
+public:
+ explicit DocumentImpl(std::string title)
+ : html_(Tag::create("html")), head_(html_->add_tag("head")),
+ title_(head_->add_tag("title", std::move(title))),
+ body_(html_->add_tag("body")) {
+ }
+
+ void add_style(std::string rel_path) override {
+ head_->add_tag("link")
+ ->attr("rel", "stylesheet")
+ ->attr("href", std::move(rel_path));
+ }
+
+ void add_script(std::string src_path) override {
+ head_->add_tag("script")
+ ->attr("type", "text/javascript")
+ ->attr("src", std::move(src_path));
+ }
+
+ void add_script(std::unique_ptr<Tag> script) override {
+ if (!script->has_attr("type"))
+ script->attr("type", "text/javascript");
+ head_->add(std::move(script));
+ }
+
+ Tag* body() override {
+ return body_;
+ }
+
+ std::unique_ptr<Transport::Response> build(Transport* transport) override {
+ std::string data;
+ html_->render(&data);
+ auto sha256 = HashMethod::sha256();
+ sha256->update(data.data(), data.size());
+ auto etag = "\"" + sha256->finish() + "\"";
+ auto resp = transport->create_ok_data(std::move(data));
+ resp->add_header("Content-Type", "text/html; charset=utf-8");
+ resp->add_header("ETag", etag);
+ return resp;
+ }
+
+private:
+ std::unique_ptr<Tag> html_;
+ Tag* head_;
+ Tag* title_;
+ Tag* body_;
+};
+
+} // namespace
+
+std::unique_ptr<Document> Document::create(std::string title) {
+ return std::make_unique<DocumentImpl>(std::move(title));
+}
+