templating rewrite start

This commit is contained in:
Schrottkatze 2023-03-03 23:52:16 +01:00
parent 3f2bdfdaed
commit 1f2589976d
5 changed files with 103 additions and 11 deletions

View file

@ -31,8 +31,8 @@ pub struct Args {
pub editable: bool, pub editable: bool,
/// The text displayed in the browser navigation bar. /// The text displayed in the browser navigation bar.
#[clap(long, env = "MICROBIN_TITLE")] #[clap(long, env = "MICROBIN_TITLE", default_value = " MicroBin")]
pub title: Option<String>, pub title: String,
/// The web interfaces' footer text. /// The web interfaces' footer text.
#[clap(long, env = "MICROBIN_FOOTER_TEXT")] #[clap(long, env = "MICROBIN_FOOTER_TEXT")]

View file

@ -7,18 +7,21 @@ use crate::endpoints::{
use crate::pasta::Pasta; use crate::pasta::Pasta;
use crate::util::dbio; use crate::util::dbio;
use actix_web::middleware::Condition; use actix_web::middleware::Condition;
use actix_web::{middleware, web, App, HttpServer}; use actix_web::{middleware, web, App, HttpServer, HttpResponse, get};
use actix_web_httpauth::middleware::HttpAuthentication; use actix_web_httpauth::middleware::HttpAuthentication;
use chrono::Local; use chrono::Local;
use env_logger::Builder; use env_logger::Builder;
use futures::lock::Mutex; use futures::lock::Mutex;
use log::LevelFilter; use log::LevelFilter;
use templates::BasePage;
use std::fs; use std::fs;
use std::io::Write; use std::io::Write;
pub mod args; pub mod args;
pub mod pasta; pub mod pasta;
pub mod templates;
pub mod util { pub mod util {
pub mod pasta_id_converter; pub mod pasta_id_converter;
pub mod auth; pub mod auth;
@ -108,6 +111,7 @@ async fn main() -> std::io::Result<()> {
.wrap(middleware::Logger::default()) .wrap(middleware::Logger::default())
.service(remove::remove) .service(remove::remove)
.service(pastalist::list) .service(pastalist::list)
.service(testtemplate)
.wrap(Condition::new( .wrap(Condition::new(
ARGS.auth_username.is_some(), ARGS.auth_username.is_some(),
HttpAuthentication::basic(util::auth::auth_validator), HttpAuthentication::basic(util::auth::auth_validator),
@ -118,3 +122,14 @@ async fn main() -> std::io::Result<()> {
.run() .run()
.await .await
} }
#[get("/testtemplate")]
async fn testtemplate() -> HttpResponse {
HttpResponse::Ok()
.content_type("text/html")
.body(BasePage {
body: markup::new! {
p "Test content!"
}
}.to_string())
}

80
src/templates/mod.rs Normal file
View file

@ -0,0 +1,80 @@
use crate::args::ARGS;
markup::define!{
Create() {
@BasePage {
body: markup::new! {
form [
id = "pasta-form",
action = "upload",
method = "POST",
enctype = "multipart/form-data",
] {
br;
}
}
}
}
BasePage<Body: markup::Render>( body: Body ) {
@markup::doctype()
html {
head {
title { @ARGS.title }
meta[charset = "utf-8"];
meta[
name = "viewport",
content = "width=device-width, initial-scale=1.0"
];
link[
rel = "stylesheet",
href = if let Some(custom_css_url) = ARGS.custom_css.as_ref() {
custom_css_url.clone()
} else {
format!("{}/static/water.css", ARGS.public_path)
}
];
}
body[
style = format!(
"margin:auto;padding-left: 0.5rem;padding-right: 0.5rem;line-height:1.5;font-size: 1.1em;{}",
if ARGS.wide { "1080px" } else { "800px" }
)
] {
br;
b[ style = "margin-right: 0.5rem" ] {
@if !ARGS.hide_logo {
a[ href = format!("{}/", ARGS.public_path) ] {
img[
width = 26,
style = "margin-bottom: -6px; margin-right: 0.5rem;",
src = format!("{}/static/logo.png", ARGS.public_path)
];
}
@ARGS.title
}
}
a[ href = format!("{}/", ARGS.public_path), style = "margin-right: 0.5rem; margin-left: 0.5rem" ] "New"
a[ href = format!("{}/pastalist", ARGS.public_path), style = "margin-right: 0.5rem; margin-left: 0.5rem" ] "List"
a[ href = format!("{}/info", ARGS.public_path), style = "margin-right: 0.5rem; margin-left: 0.5rem" ] "Info"
hr;
@body
hr;
// TODO: footer text but like with proper markdown and customizability
p[ style = "font-size:smaller" ] {
@if let Some(footer_text) = ARGS.footer_text.as_ref() {
@footer_text
} else {
a[ href = "https://microbin.eu" ] "MicroBin"
" by Dániel Szabó and the FOSS Community."
"Let's keep the Web "
b "compact" ", "
b "accessible" " and "
b "humane" "!"
}
}
}
}
}
}

View file

@ -15,3 +15,4 @@
</body> </body>
</html> </html>

View file

@ -2,11 +2,7 @@
<html> <html>
<head> <head>
{% if args.title.as_ref().is_none() %} <title>MicroBin</title>
<title>MicroBin</title>
{%- else %}
<title>{{ args.title.as_ref().unwrap() }}</title>
{%- endif %}
<meta charset="utf-8"> <meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
@ -55,8 +51,8 @@
src="{{ args.public_path }}/static/logo.png" src="{{ args.public_path }}/static/logo.png"
> >
</a> </a>
{%- endif %} {% if args.title.as_ref().is_none() %} {%- endif %}
MicroBin {%- else %} {{ args.title.as_ref().unwrap() }} {%- endif %} MicroBin
</b> </b>
<a href="{{ args.public_path }}/" style="margin-right: 0.5rem; margin-left: 0.5rem">New <a href="{{ args.public_path }}/" style="margin-right: 0.5rem; margin-left: 0.5rem">New