summaryrefslogtreecommitdiffstats
path: root/rust/wolfree_libredirect_patch/src/main.rs
blob: 65f1b93cc7164cc3f38019266050bbc40ba7ed0b (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
//! The Rust crate provides lightweight wrappers around the `fs::write` and `fs::remove_dir_all` functions from the standard library's `fs` module.
//! The crate is designed to simplify file writing and directory removal operations by encapsulating the error handling logic and providing a more convenient interface.
//! It is particularly useful for tasks that involve writing multiple files or removing directories with their contents.

#![allow(clippy::blanket_clippy_restriction_lints)]
#![allow(clippy::exit)]
#![allow(clippy::print_stderr)]
#![allow(clippy::implicit_return)]

/* SPDX-License-Identifier: AGPL-3.0-or-later
 * This file is part of Wolfree.
 * This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 */

use markdown::to_html_with_options;
use markdown::Options;
use std::fs;

/// A lightweight wrapper around `fs::write`.
fn wolfree_write(file_path: &str, contents: &str) {
    match fs::write(file_path, contents) {
        Ok(_) => (),
        Err(err) => {
            eprintln!("Error: {err}",);
            std::process::exit(1);
        }
    };
}

/// A lightweight wrapper around `fs::remove_dir_all`.
fn wolfree_remove_dir_all(dir_path: &str) {
    match fs::remove_dir_all(dir_path) {
        Ok(_) => (),
        Err(err) => {
            eprintln!("Error: {err}",);
            std::process::exit(1);
        }
    };
}

/// A lightweight wrapper around `markdown::to_html_with_options`.
fn wolfree_to_html_with_options(value: &str) -> String {
    match to_html_with_options(value, &Options::gfm()) {
        Ok(result) => result,
        Err(err) => {
            eprintln!("Error: {err}",);
            std::process::exit(1);
        }
    }
}

/// Entry point of the program.
fn main() {
    wolfree_write(
        "./docusaurus/build/ajax/libs/wolfree/23.7.8/js/Entrypoint.js",
        "export default () => {}",
    );
    wolfree_write(
        "./docusaurus/build/index.html",
        &format!(
            "{}{}",
            include_str!("head.html"),
            wolfree_to_html_with_options(include_str!("body.md")),
        ),
    );
    wolfree_write(
        "./docusaurus/build/instances.json",
        include_str!("instances.json"),
    );
    wolfree_write(
        "./docusaurus/build/404.html",
        "<script>location='/'</script>",
    );
    wolfree_write(
        "./docusaurus/build/acknowledgment/index.html",
        "<script>location='/'</script>",
    );
    wolfree_write(
        "./docusaurus/build/community/index.html",
        "<script>location='/'</script>",
    );
    wolfree_write(
        "./docusaurus/build/dmca/index.html",
        "<script>location='/'</script>",
    );
    wolfree_write(
        "./docusaurus/build/mirror/index.html",
        "<script>location='/'</script>",
    );
    wolfree_write(
        "./docusaurus/build/source/index.html",
        "<script>location='/'</script>",
    );
    wolfree_remove_dir_all("./docusaurus/build/assets/");
}