coloooors

This commit is contained in:
2024-09-18 18:36:54 +02:00
parent fb63a2011b
commit 0937de96dd
14 changed files with 469 additions and 155 deletions

View File

@ -53,8 +53,6 @@ async fn main() {
// TODO Socials
// - fotos
// TODO ul li article styles
// TODO header height difference
// TODO Colors
// TODO print css and other 404 css linked in base.html
// TODO go live pipeline

View File

@ -5,6 +5,7 @@ use std::{
use anyhow::Context;
use image::{image_dimensions, ImageReader};
use indoc::formatdoc;
use super::{
export_format::ExportFormat, image_generator::generate_images,
@ -66,7 +67,7 @@ pub fn generate_picture_markup(
.map(|format| {
let srcset = generate_srcset(&path_to_generated, format, &resolutions);
let format_type = format.get_type();
format!(
formatdoc!(
r#"<source
srcset="{srcset}"
type="{format_type}"
@ -81,7 +82,7 @@ pub fn generate_picture_markup(
resolutions.first().expect("Should this error ever happen?"),
exported_formats.last().expect("Can this one ever happen?"),
);
let image_tag = format!(
let image_tag = formatdoc!(
r#"<img
src="{image_path}"
width="{width}"
@ -90,7 +91,7 @@ pub fn generate_picture_markup(
>"#
);
let result = format!(
let result = formatdoc!(
r#"<picture>
{source_tags}
{image_tag}
@ -272,10 +273,12 @@ fn test_generate_srcset() {
#[test]
fn test_generate_picture_markup() {
use indoc::indoc;
let width = 300;
let height = 200;
let orig_img_path = "/images/uploads/2020-03-23_20-24-06_393.jpg";
let result = r#"<picture>
let result = indoc! {
r#"<picture>
<source
srcset="/generated_images/images/uploads/2020-03-23_20-24-06_393_300x200.avif 1x, /generated_images/images/uploads/2020-03-23_20-24-06_393_450x300.avif 1.5x, /generated_images/images/uploads/2020-03-23_20-24-06_393_600x400.avif 2x, /generated_images/images/uploads/2020-03-23_20-24-06_393_900x600.avif 3x, /generated_images/images/uploads/2020-03-23_20-24-06_393_1200x800.avif 4x"
type="image/avif"
@ -290,7 +293,8 @@ fn test_generate_picture_markup() {
height="200"
alt="Testing image alt"
>
</picture>"#;
</picture>"#,
};
assert_eq!(
generate_picture_markup(orig_img_path, width, height, "Testing image alt", false)
.expect("picture markup has to be generated"),

View File

@ -4,6 +4,7 @@ use axum::http::StatusCode;
use chrono::{DateTime, Utc};
use gray_matter::{engine::YAML, Matter};
use image::image_dimensions;
use indoc::formatdoc;
use pulldown_cmark::{CodeBlockKind, Event, Options, Parser, Tag, TagEnd};
use serde::{de::DeserializeOwned, Deserialize, Deserializer};
use syntect::{highlighting::ThemeSet, html::highlighted_html_for_string, parsing::SyntaxSet};
@ -71,6 +72,7 @@ pub async fn parse_post<'de, Metadata: DeserializeOwned>(
enum TextKind {
Text,
Heading(Option<String>),
Code(String),
}
@ -102,7 +104,7 @@ pub fn parse_html(markdown: &str, generate_images: bool) -> String {
}) => {
if !dest_url.starts_with("/") {
return Event::Html(
format!(
formatdoc!(
r#"<img
alt="{title}"
src="{dest_url}"
@ -125,7 +127,7 @@ pub fn parse_html(markdown: &str, generate_images: bool) -> String {
// Place image into the content with scaled reso to a boundary
let picture_markup =
generate_picture_markup(&dest_url, max_width, max_height, &title, generate_images)
.unwrap_or(format!(
.unwrap_or(formatdoc!(
r#"
<img
alt="{alt}"
@ -139,7 +141,7 @@ pub fn parse_html(markdown: &str, generate_images: bool) -> String {
link_type, dest_url, title, id
);
Event::Html(
format!(
formatdoc!(
r#"<figure>
{picture_markup}
<figcaption>
@ -168,14 +170,45 @@ pub fn parse_html(markdown: &str, generate_images: bool) -> String {
.unwrap();
Event::Html(highlighted.into())
}
TextKind::Heading(provided_id) => {
let heading_id = provided_id.clone().unwrap_or({
text.to_lowercase()
.replace(|c: char| !c.is_alphanumeric(), "-")
});
Event::Html(
formatdoc!(
r##"
<a name="{heading_id}" class="anchor" href="#{heading_id}">
<span class="header-link"></span>
</a>
{text}
"##
)
.into(),
)
}
_ => Event::Text(text),
},
Event::Start(Tag::Heading {
level,
id,
classes: _,
attrs: _,
}) => {
let id_str = id.map(|id| id.to_string());
text_kind = TextKind::Heading(id_str);
Event::Html(format!("<{level}>").into())
}
Event::Start(_) => event,
Event::End(TagEnd::Image) => Event::Html("</figcaption></figure>".into()),
Event::End(TagEnd::CodeBlock) => {
text_kind = TextKind::Text;
Event::End(TagEnd::CodeBlock)
}
Event::End(TagEnd::Heading(heading_level)) => {
text_kind = TextKind::Text;
Event::End(TagEnd::Heading(heading_level))
}
_ => event,
});