aboutsummaryrefslogtreecommitdiffstats
path: root/cms-backd/src/pagegen.rs
blob: 4836a84810f1d592e9baf7b5fde1862a783923b1 (plain)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// -*- coding: utf-8 -*-
//
// Simple CMS
//
// Copyright (C) 2011-2024 Michael Büsch <m@bues.ch>
//
// Licensed under the Apache License version 2.0
// or the MIT license, at your option.
// SPDX-License-Identifier: Apache-2.0 OR MIT

use crate::{
    anchor::Anchor,
    args::CmsGetArgs,
    config::CmsConfig,
    navtree::{NavElem, NavTree},
    reply::CmsReply,
    resolver::Resolver,
};
use anyhow::{self as ah, format_err as err};
use chrono::prelude::*;
use cms_ident::{CheckedIdent, UrlComp};
use std::{fmt::Write as _, sync::Arc, write as wr, writeln as ln};

const DEFAULT_HTML_ALLOC: usize = 1024 * 64;
const DEFAULT_INDEX_HTML_ALLOC: usize = 1024 * 4;
const MAX_INDENT: usize = 1024;

#[inline]
fn make_indent(indent: usize) -> &'static str {
    const TEMPLATE: &str = "                                        ";
    &TEMPLATE[..(indent * 4).min(TEMPLATE.len())]
}

pub struct PageGen<'a> {
    get: &'a CmsGetArgs,
    config: Arc<CmsConfig>,
}

impl<'a> PageGen<'a> {
    pub fn new(get: &'a CmsGetArgs, config: Arc<CmsConfig>) -> Self {
        Self { get, config }
    }

    #[allow(clippy::comparison_chain)]
    pub fn generate_index(&self, anchors: &[Anchor], resolver: &Resolver) -> ah::Result<String> {
        let mut html = String::with_capacity(DEFAULT_INDEX_HTML_ALLOC);

        ln!(html, r#"{}<ul>"#, make_indent(1))?;
        let mut indent = 0;

        for anchor in anchors {
            if anchor.no_index() || anchor.text().is_empty() {
                continue;
            }
            if let Some(aindent) = anchor.indent() {
                // Adjust indent.
                if aindent > indent {
                    if aindent > MAX_INDENT {
                        return Err(err!("Anchor indent too big"));
                    }
                    for _ in 0..(aindent - indent) {
                        indent += 1;
                        ln!(html, r#"{}<ul>"#, make_indent(indent + 1))?;
                    }
                } else if aindent < indent {
                    for _ in 0..(indent - aindent) {
                        ln!(html, r#"{}</ul>"#, make_indent(indent + 1))?;
                        indent -= 1;
                    }
                }
            }
            // Anchor data.
            ln!(
                html,
                r#"{}<li>{}</li>"#,
                make_indent(indent + 2),
                anchor.make_html(resolver, false)?,
            )?;
        }
        for _ in 0..(indent + 1) {
            ln!(html, r#"{}</ul>"#, make_indent(indent + 1))?;
        }
        Ok(html)
    }

    #[rustfmt::skip]
    #[allow(clippy::only_used_in_recursion)]
    pub fn generate_navelem(
        &self,
        b: &mut String,
        navelems: &[NavElem],
        indent: usize,
    ) -> ah::Result<()> {
        if navelems.is_empty() {
            return Ok(());
        }

        let c = &self.config;
        let ii = make_indent(indent + 1);

        if indent > 0 {
            ln!(b, r#"{ii}<div class="navelems">"#)?;
        }

        for navelem in navelems {
            let nav_label = navelem.nav_label().trim();
            if nav_label.is_empty() {
                continue;
            }
            let nav_href = navelem.path().url(UrlComp {
                protocol: None,
                domain: None,
                base: Some(c.url_base()),
            });
            let prio = navelem.prio();

            let cls = if indent > 0 { "navelem" } else { "navgroup" };
            ln!(b, r#"{ii}    <div class="{cls}"> <!-- {prio} -->"#)?;

            if indent == 0 {
                ln!(b, r#"{ii}        <div class="navhead">"#)?;
            }

            if navelem.active() {
                ln!(b, r#"{ii}        <div class="navactive">"#)?;
            }

            ln!(b, r#"{ii}        <a href="{nav_href}">{nav_label}</a>"#)?;

            if navelem.active() {
                ln!(b, r#"{ii}        </div>"#)?; // navactive
            }

            if indent == 0 {
                ln!(b, r#"{ii}        </div>"#)?; // navhead
            }

            self.generate_navelem(b, navelem.children(), indent + 2)?;

            ln!(b, r#"{ii}    </div>"#)?; // navelem / navgroup
        }

        if indent > 0 {
            ln!(b, r#"{ii}</div>"#)?; // navelems
        }
        Ok(())
    }

    #[rustfmt::skip]
    fn generate_nav(
        &self,
        b: &mut String,
        navtree: &NavTree,
        homestr: &str,
    ) -> ah::Result<()> {
        let c = &self.config;
        let nav_home_href = CheckedIdent::ROOT.url(UrlComp {
            protocol: None,
            domain: None,
            base: Some(c.url_base()),
        });
        let nav_home_text = homestr.trim();

        ln!(b, r#"<div class="navbar">"#)?;
        ln!(b, r#"    <div class="navgroups">"#)?;
        ln!(b, r#"        <div class="navhome">"#)?;
        if self.get.path.is_root() {
            ln!(b, r#"        <div class="navactive">"#)?;
        }
        ln!(b, r#"            <a href="{nav_home_href}">{nav_home_text}</a>"#)?;
        if self.get.path.is_root() {
            ln!(b, r#"        </div>"#)?; // navactive
        }
        ln!(b, r#"        </div>"#)?; // navhome

        self.generate_navelem(b, navtree.elems(), 0)?;

        ln!(b, r#"    </div>"#)?; // navgroups
        ln!(b, r#"</div>"#)?; // navbar

        Ok(())
    }

    #[rustfmt::skip]
    #[allow(clippy::too_many_arguments)]
    fn generate_body(
        &self,
        b: &mut String,
        path: Option<&CheckedIdent>,
        title: &str,
        page_content: &str,
        stamp: &DateTime<Utc>,
        navtree: &NavTree,
        homestr: &str,
    ) -> ah::Result<()> {
        let c = &self.config;
        let page_stamp = stamp.format("%A %d %B %Y %H:%M");

        ln!(b, r#"<div class="titlebar">"#)?;
        ln!(b, r#"    <div class="logo">"#)?;
        ln!(b, r#"        <a href="{}">"#, c.url_base())?;
        ln!(b, r#"            <img alt="logo" src="{}/__images/logo.png" />"#, c.url_base())?;
        ln!(b, r#"        </a>"#)?;
        ln!(b, r#"    </div>"#)?;
        ln!(b, r#"    <div class="title">{title}</div>"#)?;
        ln!(b, r#"</div>"#)?;
        self.generate_nav(b, navtree, homestr)?;
        ln!(b, r#"<div class="main">"#)?;
        ln!(b)?;
        ln!(b, r#"<!-- BEGIN: page content -->"#)?;
        ln!(b, r#"{page_content}"#)?;
        ln!(b, r#"<!-- END: page content -->"#)?;
        ln!(b)?;
        ln!(b, r#"<div class="modifystamp">"#)?;
        ln!(b, r#"    Updated: {page_stamp} (UTC)"#)?;
        ln!(b, r#"</div>"#)?;
        ln!(b)?;
        if let Some(path) = path {
            let url = path.url(UrlComp {
                protocol: Some("https"),
                domain: Some(self.config.domain()),
                base: Some(self.config.url_base()),
            });
            let mut url_enc = String::with_capacity(url.len() * 4);
            let url = url_escape::encode_component_to_string(url, &mut url_enc);

            ln!(b, r#"<div class="checker">"#)?;
            wr!(b, r#"    <a href="https://validator.w3.org/nu/"#)?;
            ln!(b, r#"?showsource=yes&amp;doc={url}">xhtml</a>"#)?;
            ln!(b, r#"    /"#)?;
            wr!(b, r#"    <a href="https://jigsaw.w3.org/css-validator/validator"#)?;
            wr!(b, r#"?uri={url}&amp;profile=css3svg&amp;usermedium=all&amp;warning=1"#)?;
            ln!(b, r#"&amp;vextwarning=&amp;lang=en">css</a>"#)?;
            ln!(b, r#"</div>"#)?;
        }
        ln!(b)?;
        ln!(b, r#"</div> <!-- class="main" -->"#)?;
        Ok(())
    }

    #[rustfmt::skip]
    #[allow(clippy::too_many_arguments)]
    pub fn generate_html(
        &self,
        path: Option<&CheckedIdent>,
        title: &str,
        headers: &str,
        data: &str,
        now: &DateTime<Utc>,
        stamp: &DateTime<Utc>,
        navtree: &NavTree,
        homestr: &str,
    ) -> ah::Result<String> {
        let c = &self.config;
        let mut b = String::with_capacity(DEFAULT_HTML_ALLOC);

        let title = title.trim();
        let now = now.to_rfc3339_opts(SecondsFormat::Secs, true);

        let headers = headers
            .lines()
            .fold(
                String::with_capacity(headers.len() * 2),
                |mut buf, line| {
                    let _ = ln!(buf, r#"    {line}"#);
                    buf
                }
            );

        ln!(b, r#"<?xml version="1.0" encoding="UTF-8" ?>"#)?;
        ln!(b, r#"<!DOCTYPE html>"#)?;
        ln!(b, r#"<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">"#)?;
        ln!(b, r#"<head>"#)?;
        ln!(b, r#"    <!--"#)?;
        ln!(b, r#"        Generated by: Simple Rust based CMS"#)?;
        ln!(b, r#"        https://bues.ch/cgit/cms.git/about/"#)?;
        ln!(b, r#"        https://github.com/mbuesch/cms"#)?;
        ln!(b, r#"    -->"#)?;
        ln!(b, r#"    <meta name="generator" content="Simple Rust based CMS" />"#)?;
        ln!(b, r#"    <meta name="date" content="{now}" />"#)?;
        ln!(b, r#"    <meta name="robots" content="all" />"#)?;
        ln!(b, r#"    <title>{title}</title>"#)?;
        ln!(b, r#"    <link rel="stylesheet" href="{}/__css/cms.css" type="text/css" />"#,
            c.url_base())?;
        ln!(b, r#"    <link rel="sitemap" type="application/xml" title="Sitemap" href="{}/__sitemap.xml" />"#,
            c.url_base())?;
        ln!(b, r#"    <!-- extra headers: -->"#)?;
        ln!(b, r#"{headers}"#)?;
        ln!(b, r#"</head>"#)?;
        ln!(b, r#"<body>"#)?;
        self.generate_body(&mut b, path, title, data, stamp, navtree, homestr)?;
        ln!(b, r#"</body>"#)?;
        ln!(b, r#"</html>"#)?;
        Ok(b)
    }

    #[allow(clippy::too_many_arguments)]
    pub fn generate(
        &self,
        path: Option<&CheckedIdent>,
        title: &str,
        headers: &str,
        data: &str,
        now: &DateTime<Utc>,
        stamp: &DateTime<Utc>,
        navtree: &NavTree,
        homestr: &str,
    ) -> CmsReply {
        if let Ok(b) = self.generate_html(path, title, headers, data, now, stamp, navtree, homestr)
        {
            CmsReply::ok(b.into_bytes(), "application/xhtml+xml; charset=UTF-8")
        } else {
            CmsReply::internal_error("PageGen failed")
        }
    }
}

// vim: ts=4 sw=4 expandtab
bues.ch cgit interface