From cd9dd8b34bd40164851c7dfe8858d731eaeee2a9 Mon Sep 17 00:00:00 2001 From: blank X Date: Mon, 26 Apr 2021 10:35:35 +0700 Subject: [PATCH] Add titles --- src/main.rs | 4 ++-- src/utils.rs | 26 ++++++++++++++++++-------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5599080..81079ba 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,11 +32,11 @@ async fn async_main(port: u16) { .and_then(handle_album); let media_path = warp::path!(String).and(client).and_then(handle_media); let root_handler = warp::path::end().map(|| warp::reply::html( - format!("
ImgurX v{}
An alternative JS-less Imgur frontend
", env!("CARGO_PKG_VERSION")) + format!("ImgurX v{0}
ImgurX v{0}
An alternative JS-less Imgur frontend
", env!("CARGO_PKG_VERSION")) )); let index_handler = warp::path("index.html").and(root_handler); let not_found_handler = warp::any().map(|| warp::reply::with_status( - warp::reply::html("
404: Not Found
"), + warp::reply::html("404: Not Found
404: Not Found
"), 404.try_into().unwrap(), )); let routes = warp::filters::method::get().and( diff --git a/src/utils.rs b/src/utils.rs index 5459d6d..b938ebf 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -48,6 +48,24 @@ pub fn generate_html(album: Result) -> (u16, Vec) { let elem = BytesStart::owned(b"head".to_vec(), 4); writer.write_event(Event::Start(elem)).unwrap(); + let (show_details, title) = match album { + Ok(ref album) if !album.title.is_empty() => (false, Some(album.title.clone())), + Err(Error::APIErrors(ref err)) if err.errors.len() == 1 && err.errors[0].code == "404" => { + (false, Some(format!("404: {}", &err.errors[0].detail))) + } + _ => (true, None), + }; + if let Some(title) = title { + let elem = BytesStart::owned(b"title".to_vec(), 5); + writer.write_event(Event::Start(elem)).unwrap(); + + let elem = BytesText::from_plain_str(&title); + writer.write_event(Event::Text(elem)).unwrap(); + + let elem = BytesEnd::owned(b"title".to_vec()); + writer.write_event(Event::End(elem)).unwrap(); + } + let elem = BytesStart::owned(b"style".to_vec(), 5); writer.write_event(Event::Start(elem)).unwrap(); @@ -155,14 +173,6 @@ pub fn generate_html(album: Result) -> (u16, Vec) { 200 } Err(err) => { - let show_details = match err { - Error::APIErrors(ref err) - if err.errors.len() == 1 && err.errors[0].code == "404" => - { - false - } - _ => true, - }; let mut elem = BytesStart::owned(b"div".to_vec(), 3); if show_details { elem.push_attribute(("style", "text-align: left;"));