aboutsummaryrefslogtreecommitdiff
path: root/src/modules.rs
diff options
context:
space:
mode:
authorFuwn <[email protected]>2022-03-30 03:04:48 -0700
committerFuwn <[email protected]>2022-03-30 03:04:48 -0700
commit0495bd1837087a91a36036dbaab71c83c3acc197 (patch)
tree6f1744db3bf9a7eb07da70971f0b2aa39741a0b0 /src/modules.rs
downloadlocus-0495bd1837087a91a36036dbaab71c83c3acc197.tar.xz
locus-0495bd1837087a91a36036dbaab71c83c3acc197.zip
feat: 0.0.0 :star:
Diffstat (limited to 'src/modules.rs')
-rw-r--r--src/modules.rs177
1 files changed, 177 insertions, 0 deletions
diff --git a/src/modules.rs b/src/modules.rs
new file mode 100644
index 0000000..0067266
--- /dev/null
+++ b/src/modules.rs
@@ -0,0 +1,177 @@
+// This file is part of Locus <https://github.com/gemrest/locus>.
+// Copyright (C) 2022-2022 Fuwn <[email protected]>
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, version 3.
+//
+// This program is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
+//
+// Copyright (C) 2022-2022 Fuwn <[email protected]>
+// SPDX-License-Identifier: GPL-3.0-only
+
+use std::{
+ collections::HashMap,
+ fs::{self, read_dir},
+ io::Read,
+};
+
+use rand::seq::SliceRandom;
+use windmark::Response;
+
+use crate::{success, Main, QUOTES};
+
+#[allow(clippy::too_many_lines)]
+pub fn multi_blog(router: &mut windmark::Router) {
+ let paths = read_dir("content/pages/blog").unwrap();
+ let mut blogs: HashMap<String, HashMap<_, _>> = HashMap::new();
+
+ for path in paths {
+ let blog = path.unwrap().path().display().to_string();
+ let blog_paths = read_dir(&blog).unwrap();
+ let mut entries: HashMap<_, String> = HashMap::new();
+
+ blog_paths
+ .map(|e| e.unwrap().path().display().to_string())
+ .for_each(|file| {
+ let mut contents = String::new();
+
+ fs::File::open(&file)
+ .unwrap()
+ .read_to_string(&mut contents)
+ .unwrap();
+
+ entries.insert(
+ file.replace(&blog, "").replace(".gmi", "").replace(
+ {
+ #[cfg(windows)]
+ {
+ '\\'
+ }
+
+ #[cfg(unix)]
+ {
+ '/'
+ }
+ },
+ "",
+ ),
+ contents,
+ );
+ });
+
+ blogs.insert(
+ blog
+ .replace(
+ {
+ #[cfg(windows)]
+ {
+ "content/pages/blog\\"
+ }
+
+ #[cfg(unix)]
+ {
+ "content/pages/blog/"
+ }
+ },
+ "",
+ )
+ .split('_')
+ .map(|s| {
+ // https://gist.github.com/jpastuszek/2704f3c5a3864b05c48ee688d0fd21d7
+ let mut c = s.chars();
+
+ match c.next() {
+ None => String::new(),
+ Some(f) =>
+ f.to_uppercase()
+ .chain(c.flat_map(char::to_lowercase))
+ .collect(),
+ }
+ })
+ .collect::<Vec<_>>()
+ .join(" "),
+ entries,
+ );
+ }
+
+ let blog_clone = blogs.clone();
+
+ router.mount(
+ "/blog",
+ Box::new(move |context| {
+ success!(
+ &format!(
+ "# BLOGS ({})\n\n{}",
+ blog_clone.len(),
+ blog_clone
+ .iter()
+ .map(|(title, _)| title.clone())
+ .collect::<Vec<_>>()
+ .into_iter()
+ .map(|i| {
+ format!(
+ "=> {} {}",
+ format_args!("/blog/{}", i.replace(' ', "_").to_lowercase(),),
+ i
+ )
+ })
+ .collect::<Vec<_>>()
+ .join("\n")
+ ),
+ context
+ )
+ }),
+ );
+
+ for (blog, entries) in blogs {
+ let fixed_blog_name = blog.replace(' ', "_").to_lowercase();
+ let entries_clone = entries.clone();
+ let fixed_blog_name_clone = fixed_blog_name.clone();
+
+ router.mount(
+ &format!("/blog/{}", fixed_blog_name),
+ Box::new(move |context| {
+ success!(
+ &format!(
+ "# {} ({})\n\n{}",
+ blog.to_uppercase(),
+ entries_clone.len(),
+ entries_clone
+ .iter()
+ .map(|(title, _)| title.clone())
+ .collect::<Vec<_>>()
+ .into_iter()
+ .map(|i| {
+ format!(
+ "=> {} {}",
+ format_args!(
+ "/blog/{}/{}",
+ fixed_blog_name_clone,
+ i.to_lowercase()
+ ),
+ i
+ )
+ })
+ .collect::<Vec<_>>()
+ .join("\n")
+ ),
+ context
+ )
+ }),
+ );
+
+ for (title, contents) in entries {
+ router.mount(
+ &format!("/blog/{}/{}", fixed_blog_name, title.to_lowercase()),
+ Box::new(move |context| success!(contents, context)),
+ );
+ }
+ }
+}