about summary refs log tree commit diff
path: root/templates/simple.c
blob: eeee6db8ec042a964205908d9521fdc04f9aa515 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <core.h>
#include <template.h>
#include <config.h>
#include <cgiutil.h>
#include <stringutil.h>
#include <timeutil.h>
#include <xml.h>

static struct xml_context ctx;

void output_entry_time(struct xml_context *ctx, struct entry entry) {
    char strtime[MAX_TIMESTR_SIZE];

    if(flocaltime(strtime, HTML_TIME_FORMAT_READABLE, MAX_TIMESTR_SIZE, &entry.time) > 0) {
        xml_open_tag_attrs(ctx, "time", 1, "datetime", strtime);
        xml_raw(ctx, strtime);
        xml_close_tag(ctx, "time");
    }
}

void template_header(void) {
    new_xml_context(&ctx);
    ctx.warn = stderr;
    ctx.closing_slash = 0;

    xml_raw(&ctx, "<!doctype html>");
    xml_open_tag_attrs(&ctx, "html", 1, "lang", "en");

    xml_open_tag(&ctx, "head");

    xml_empty_tag(&ctx, "meta", 1, "charset", "utf-8");

   #ifdef BLOG_CSS
    xml_empty_tag(&ctx, "link", 3,
                  "rel", "stylesheet",
                  "type", "text/css",
                  "href", BLOG_CSS);
   #endif

    xml_open_tag(&ctx, "title");
    xml_raw(&ctx, BLOG_TITLE);
    xml_close_tag(&ctx, "title");

    xml_close_tag(&ctx, "head");

    xml_open_tag(&ctx, "body");
    xml_open_tag(&ctx, "header");
    xml_open_tag(&ctx, "h1");
    xml_raw(&ctx, BLOG_TITLE);
    xml_close_including(&ctx, "header");

    xml_open_tag(&ctx, "main");
}

void template_footer(void) {
    xml_close_tag(&ctx, "main");

    xml_open_tag(&ctx, "footer");

    char *script_name = getenv("SCRIPT_NAME");
    char *rss_link = catn_alloc(2, script_name, "/rss.xml");
    char *atom_link = catn_alloc(2, script_name, "/atom.xml");

    if(rss_link != NULL) {
        xml_open_tag_attrs(&ctx, "a", 1, "href", rss_link);
        xml_escaped(&ctx, "RSS Feed");
        xml_close_tag(&ctx, "a");

        free(rss_link);
    }

    if(atom_link != NULL) {
        xml_raw(&ctx, " &bull; ");

        xml_open_tag_attrs(&ctx, "a", 1, "href", atom_link);
        xml_escaped(&ctx, "Atom Feed");
        xml_close_tag(&ctx, "a");

        free(atom_link);
    }

    xml_close_all(&ctx);

    del_xml_context(&ctx);
}

void template_single_entry(struct entry entry) {
    xml_open_tag(&ctx, "article");

    if(entry.text_size > 0) {
        xml_open_tag_attrs(&ctx, "div", 1, "class", "content");
        xml_raw(&ctx, entry.text);
        xml_close_tag(&ctx, "div");
    }

    char *script_name = getenv("SCRIPT_NAME");

    xml_open_tag_attrs(&ctx, "div", 1, "class", "meta");
    xml_open_tag(&ctx, "ul");

    // time of publishing
    xml_open_tag(&ctx, "li");
    output_entry_time(&ctx, entry);
    xml_close_tag(&ctx, "li");

    // link back to index
    xml_open_tag(&ctx, "li");
    xml_open_tag_attrs(&ctx, "a", 1, "href", script_name);
    xml_raw(&ctx, "Back");
    xml_close_tag(&ctx, "a");
    xml_close_tag(&ctx, "li");

    xml_close_including(&ctx, "article");
}

void template_index_entry(struct entry entry) {
    // TODO time?
    xml_open_tag_attrs(&ctx, "article", 1, "id", entry.title);
    if(entry.text_size > 0) {
        xml_open_tag_attrs(&ctx, "div", 1, "class", "content index");
        xml_raw(&ctx, entry.text);
        xml_close_tag(&ctx, "div");
    }

    xml_open_tag_attrs(&ctx, "div", 1, "class", "meta index");
    xml_open_tag(&ctx, "p");
    xml_open_tag_attrs(&ctx, "a", 1, "href", entry.link);
    xml_raw(&ctx, "Permalink");

    xml_close_including(&ctx, "article");
}

void template_error(int status) {
    xml_open_tag_attrs(&ctx, "div", 1, "class", "error-page");
    xml_open_tag(&ctx, "h2");
    xml_raw(&ctx, "An error occured while handling your request");
    xml_close_tag(&ctx, "h2");

    xml_open_tag(&ctx, "p");

    if(status == 500) {
        xml_raw(&ctx, "Something is wrong with this application and/or its server (error 500).");
    } else if(status == 404) {
        xml_raw(&ctx, "What you requested doesn't exist (error 404).");
    } else {
       xml_raw(&ctx, "The error encoutered is: ");
       xml_raw(&ctx, http_status_line(status));
    }

    xml_close_tag(&ctx, "p");

    char *script_name = getenv("SCRIPT_NAME");

    xml_open_tag(&ctx, "nav");
    xml_open_tag(&ctx, "p");
    xml_open_tag_attrs(&ctx, "a", 1, "href", script_name);
    xml_raw(&ctx, "Back");

    xml_close_including(&ctx, "div");
}