aboutsummaryrefslogtreecommitdiffstats
path: root/cms-backd/src/anchor.rs
blob: 78c9ec28c659eafb20d92887dffd7b91d82faa10 (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
// -*- 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::resolver::Resolver;
use anyhow as ah;

#[derive(Clone, Debug)]
pub struct Anchor {
    name: String,
    text: String,
    indent: Option<usize>,
    no_index: bool,
}

impl Anchor {
    pub fn new(name: &str, text: &str, indent: i64, no_index: bool) -> Self {
        let indent = if indent >= 0 {
            Some(indent.clamp(0, u8::MAX.into()).try_into().unwrap())
        } else {
            None
        };
        Self {
            name: name.to_string(),
            text: text.to_string(),
            indent,
            no_index,
        }
    }

    pub fn name(&self) -> &str {
        &self.name
    }

    pub fn text(&self) -> &str {
        &self.text
    }

    pub fn indent(&self) -> Option<usize> {
        self.indent
    }

    pub fn no_index(&self) -> bool {
        self.no_index
    }

    fn make_url(&self, resolver: &Resolver) -> ah::Result<String> {
        let ident = resolver.expand_variable("CMS_PAGEIDENT")?;
        let name = self.name();
        Ok(format!("{ident}#{name}"))
    }

    pub fn make_html(&self, resolver: &Resolver, with_id: bool) -> ah::Result<String> {
        let name = self.name();
        let text = self.text();
        let url = self.make_url(resolver)?;
        if with_id {
            Ok(format!(r#"<a id="{name}" href="{url}">{text}</a>"#))
        } else {
            Ok(format!(r#"<a href="{url}">{text}</a>"#))
        }
    }
}

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