Compare commits

...

3 Commits

Author SHA1 Message Date
la Fleur 133e1e4aac typo in index 2020-11-11 02:11:10 +01:00
la Fleur cbdfe627b8 use the cwd as site title 2020-11-11 02:07:21 +01:00
la Fleur 980d02a946 reorganize content and templates 2020-11-11 02:06:51 +01:00
7 changed files with 29 additions and 25 deletions

3
content/cat.md Normal file
View File

@ -0,0 +1,3 @@
Let's say this page talks about cats.
We will want to _insist_ on some.

1
content/sunny.md Normal file
View File

@ -0,0 +1 @@
This could be any sunny day.

View File

@ -9,38 +9,40 @@ use rocket::request::{Request, FromParam};
use rocket::http::RawStr;
use rocket_contrib::templates::Template;
const CONTENT_DIR:&str = "content";
#[derive(serde::Serialize, Debug)]
struct User {
name: String,
struct Content {
title: String,
content: String,
}
impl<'r> FromParam<'r> for User {
impl<'r> FromParam<'r> for Content {
type Error = &'r RawStr;
fn from_param(param: &'r RawStr) -> Result<Self, Self::Error> {
let filename = format!("users/{}.md", param);
let filename = format!("{}/{}.md", CONTENT_DIR, param);
let content = fs::read_to_string(&filename);
match content {
Ok(value) => Ok(User{ name: param.to_string(), content: value }),
Ok(value) => Ok(Content{ title: param.to_string(), content: value }),
Err(_) => Err(param),
}
}
}
fn list_users() -> io::Result<Vec<String>> {
let mut users: Vec<String> = vec!();
for path in fs::read_dir("users")? {
fn list_contents() -> io::Result<Vec<String>> {
let mut contents: Vec<String> = vec!();
for path in fs::read_dir(CONTENT_DIR)? {
let file = path?;
let user = format!("{:?}", file.file_name()).replace("\"", "").replace(".md", "");
users.push(user);
let content = format!("{:?}", file.file_name()).replace("\"", "").replace(".md", "");
contents.push(content);
}
Ok(users)
Ok(contents)
}
#[derive(serde::Serialize)]
struct IndexContext {
name: &'static str,
title: String,
items: Vec<String>
}
@ -54,16 +56,18 @@ fn not_found(req: &Request<'_>) -> Template {
Template::render("error/404", &map)
}
#[get("/<user>")]
fn show(user: User) -> Template {
//format!("{:#?}", user)
Template::render("user", &user)
#[get("/<content>")]
fn show(content: Content) -> Template {
Template::render("content", &content)
}
#[get("/")]
fn index() -> io::Result<Template> {
let users = list_users()?;
let context = IndexContext { name: "home", items: users };
let cwd = std::env::current_dir()?;
let title = String::from(cwd.to_str().unwrap().split("/").last().unwrap());
//println!("{}", title);
let contents = list_contents()?;
let context = IndexContext { title: title, items: contents };
Ok(Template::render("index", &context))
}

View File

@ -1,7 +1,7 @@
{% extends "base" %}
{% block content %}
<h1>{{ name }}</h1>
<h1>{{ title }}</h1>
<p>
{{ content }}
</p>

View File

@ -1,8 +1,8 @@
{% extends "base" %}
{% block content %}
<h1>Here is {{name}}</h1>
<h3>Here are the registered users :</h3>
<h1>This is {{ title }}</h1>
<h3>Here are the available pages :</h3>
<ul>
{% for s in items %}
<li>

View File

@ -1,3 +0,0 @@
# Cat
Hi, I'm Cat, that's my preferred username.

View File

@ -1 +0,0 @@
This is Sunny's landing page.