start: templating

new-idea
Avril 4 years ago
parent bca5c2264a
commit b2caa126f9
Signed by: flanchan
GPG Key ID: 284488987C31F630

54
Cargo.lock generated

@ -783,6 +783,35 @@ version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08"
[[package]]
name = "maud"
version = "0.22.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da0784808b0c06f80365c1071048df9b6414a83fc56b8d4b305859e39f5162fa"
dependencies = [
"maud_htmlescape",
"maud_macros",
]
[[package]]
name = "maud_htmlescape"
version = "0.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d0fb85bccffc42302ad1e1ed8679f6a39d1317f775a37fbc3f79bdfbe054bfb7"
[[package]]
name = "maud_macros"
version = "0.22.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73a00158abd4671407a3d8bd2c8577e9c7dc8d0b8a8e5523bdaba7d486655b1e"
dependencies = [
"maud_htmlescape",
"proc-macro-error",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "memchr"
version = "2.3.4"
@ -1101,6 +1130,30 @@ dependencies = [
"log",
]
[[package]]
name = "proc-macro-error"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
dependencies = [
"proc-macro-error-attr",
"proc-macro2",
"quote",
"syn",
"version_check",
]
[[package]]
name = "proc-macro-error-attr"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
dependencies = [
"proc-macro2",
"quote",
"version_check",
]
[[package]]
name = "proc-macro-hack"
version = "0.5.19"
@ -1921,6 +1974,7 @@ dependencies = [
"khash",
"lazy_static",
"log",
"maud",
"mopa",
"once_cell",
"pretty_env_logger",

@ -24,6 +24,7 @@ getrandom = "0.2.1"
khash = "2.0.0"
lazy_static = "1.4.0"
log = "0.4.11"
maud = "0.22.2"
mopa = "0.2.2"
once_cell = "1.5.2"
pretty_env_logger = "0.4.0"

@ -6,7 +6,7 @@ pub const POST_ID_MAX_LEN: usize = 32;
/// The salt to use for tripcode hashes
pub const TRIPCODE_SALT: khash::salt::Salt = khash::salt::Salt::internal();
/// The tripcode algorithm to use
pub const TRIPCODE_ALGO: khash::ctx::Algorithm = khash::ctx::Algorithm::Sha256;
pub const TRIPCODE_ALGO: khash::ctx::Algorithm = khash::ctx::Algorithm::Sha256Truncated;
/// What timezone to represent data in.
pub type Timezone = chrono::offset::Utc;

@ -10,6 +10,7 @@
#[macro_use] extern crate mopa;
#[macro_use] extern crate cfg_if;
#[macro_use] extern crate ad_hoc_iter;
#[macro_use] extern crate maud;
#[allow(unused_imports)]
use std::convert::{TryFrom, TryInto};

@ -1,6 +1,9 @@
use super::*;
use cryptohelpers::sha256::Sha256Hash;
mod render;
pub use render::*;
use hard_format::formats::{
MaxLenString,
Base64FormattedString,
@ -34,6 +37,39 @@ pub struct Ident
email: Option<IDMaxString>,
}
impl Ident
{
/// Create a new ident object
///
/// # Panics
/// If `name` or `email` are longer than `defaults::POST_ID_MAX_LEN`.
pub fn new(name: Option<&str>, tripcode: Option<Tripcode>, email: Option<&str>) -> Self
{
Self {
name: name.map(|x| IDMaxString::new(x.to_owned()).expect("Name too long")),
email: email.map(|x| IDMaxString::new(x.to_owned()).expect("Email too long")),
tripcode
}
}
/// The name of this user ident
pub fn name(&self) -> &str
{
self.name.as_ref().map(|x| x.as_str()).unwrap_or(defaults::ANON_NAME)
}
/// The tripcode of this user ident
pub fn tripcode(&self) -> Option<&Tripcode>
{
self.tripcode.as_ref()
}
/// The email of this user ident
pub fn email(&self) -> Option<&str>
{
self.email.as_ref().map(|x| x.as_str())
}
}
/// A single completed post.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct Post
@ -194,6 +230,7 @@ mod tests
edited: Default::default(),
expires_in: None,
};
eprintln!("Post as html: {}", html! { body { (post) } }.into_string());
println!("Post is: {:?}", post);
let post_json = serde_json::to_vec(&post).expect("Serialise");
println!("Post json: {}", std::str::from_utf8(&post_json[..]).unwrap());

@ -0,0 +1,64 @@
use super::*;
use maud::{html, Markup, Render};
impl Render for Ident
{
fn render(&self) -> Markup
{
html! {
b.ident {
@if let Some(email) = self.email() {
a href=(format!("mailto:{}", email)) { (self.name()) }
} @else {
(self.name())
}
@if let Some(tripcode) = self.tripcode() {
" "
code {
"!" (tripcode)
}
}
}
}
}
}
impl Render for Post
{
fn render(&self) -> Markup
{
let id = self.id.0;
html! {
article#(id) {
header {
(self.ident)
" "
time datetime=(self.created()) {
(self.created()) //TODO format the DateTime properly
}
nav {
a href=(format!("#{}", id)) { "No." }
a href=(format!("#q{}", id)) { (id) }
}
}
blockquote {
(self.body())
}
//TODO: Signature and hash and other things
}
}
}
}
#[cfg(test)]
mod tests
{
#[test]
fn ident_htmlrender()
{
let ident = super::Ident::new(Some("Name"), Some(crate::tripcode::Tripcode::generate("mwee").unwrap()), Some("user@example.com"));
use maud::Render;
assert_eq!(r#"<b class="ident"><a href="mailto:user@example.com">Name</a> <code>!えナセッゲよラで</code></b>"#, &ident.render().into_string());
}
}

@ -0,0 +1,3 @@
use super::*;
use maud::DOCTYPE;
Loading…
Cancel
Save