From 6d83b07aaa9db110b5e1ff610e3cfa1d0e2d257e Mon Sep 17 00:00:00 2001 From: Butter Cat Date: Tue, 9 Apr 2024 18:33:13 -0400 Subject: [PATCH 01/11] Update embedding Reddit preview links to include captions where applicable --- src/utils.rs | 26 +++++++++++++++++++++----- static/style.css | 16 ++++++++++++---- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/utils.rs b/src/utils.rs index 63e679d..e0ce24c 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -901,14 +901,30 @@ pub fn rewrite_urls(input_text: &str) -> String { let formatted_url = format_url(REDDIT_PREVIEW_REGEX.find(&text1).map(|x| x.as_str()).unwrap_or_default()); let image_url = REDLIB_PREVIEW_LINK_REGEX.find(&formatted_url).map_or("", |m| m.as_str()).to_string(); - let image_text = REDLIB_PREVIEW_TEXT_REGEX.find(&formatted_url).map_or("", |m| m.as_str()).to_string(); + let mut image_text = REDLIB_PREVIEW_TEXT_REGEX.find(&formatted_url).map_or("", |m| m.as_str()).to_string(); + if !image_text.is_empty() { + image_text.remove(0); + image_text.pop(); + image_text.pop(); + image_text.pop(); + image_text.pop(); + } - let image_to_replace = format!(">", ">"); - let image_replacement = format!(""); + let image_to_replace = format!("

"); + + image_text = image_text.replace("\\"", "\""); + + let mut _image_replacement = String::new(); + + if REDDIT_PREVIEW_REGEX.find(&image_text).is_none() { + _image_replacement = format!("
{image_text}
"); + } else { + _image_replacement = format!("
"); + } text1 = REDDIT_PREVIEW_REGEX .replace(&text1, formatted_url) - .replace(&image_to_replace, &image_replacement) + .replace(&image_to_replace, &_image_replacement) .to_string() } } @@ -1169,6 +1185,6 @@ fn test_rewriting_image_links() {

https://preview.redd.it/bdfdxkjj2xo31.png?width=2560&format=png&auto=webp&s=d0fa420ece27605e882e89cb4711d75d774322ac

caption 1

caption 2

"#; - let output = r#"

"#; + let output = r#"
caption 1
caption 2
"#; assert_eq!(rewrite_urls(input), output); } diff --git a/static/style.css b/static/style.css index a85ab75..6c6a546 100644 --- a/static/style.css +++ b/static/style.css @@ -187,6 +187,15 @@ nav #redlib { vertical-align: -2px; } +figure { + margin: 0; +} + +figcaption { + margin-top: 5px; + text-align: center; +} + #settings_link { opacity: 0.8; margin-left: 10px; @@ -979,10 +988,6 @@ a.search_subreddit:hover { vertical-align: bottom; } -.gallery figcaption { - margin-top: 5px; -} - .gallery .outbound_url { color: var(--accent); text-overflow: ellipsis; @@ -1010,6 +1015,9 @@ a.search_subreddit:hover { .post_body img { max-width: 100%; + display: block; + margin-left: auto; + margin-right: auto; } .post_poll { From 2c8f5a7ac16abf88d46132a43777eba7310c4344 Mon Sep 17 00:00:00 2001 From: Butter Cat Date: Tue, 9 Apr 2024 18:51:32 -0400 Subject: [PATCH 02/11] Make figure margins only apply to comments to bring embedded previews more in line with gallery posts --- static/style.css | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/static/style.css b/static/style.css index 6c6a546..a533dd6 100644 --- a/static/style.css +++ b/static/style.css @@ -187,10 +187,6 @@ nav #redlib { vertical-align: -2px; } -figure { - margin: 0; -} - figcaption { margin-top: 5px; text-align: center; @@ -1195,6 +1191,10 @@ a.search_subreddit:hover { } } +.comment figure { + margin: 0; +} + .comment_left, .comment_right { display: flex; flex-direction: column; From e581f432ddb00febbc4c0e396614a98dc28de396 Mon Sep 17 00:00:00 2001 From: Butter Cat Date: Wed, 10 Apr 2024 10:47:24 -0400 Subject: [PATCH 03/11] Use substring instead of .remove and .pop, change image_text to image_caption to better reflect its usage, only replace quotes in image_caption when needed, and add comments for what some of the code does --- src/utils.rs | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/utils.rs b/src/utils.rs index e0ce24c..6f70fa5 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -901,23 +901,26 @@ pub fn rewrite_urls(input_text: &str) -> String { let formatted_url = format_url(REDDIT_PREVIEW_REGEX.find(&text1).map(|x| x.as_str()).unwrap_or_default()); let image_url = REDLIB_PREVIEW_LINK_REGEX.find(&formatted_url).map_or("", |m| m.as_str()).to_string(); - let mut image_text = REDLIB_PREVIEW_TEXT_REGEX.find(&formatted_url).map_or("", |m| m.as_str()).to_string(); - if !image_text.is_empty() { - image_text.remove(0); - image_text.pop(); - image_text.pop(); - image_text.pop(); - image_text.pop(); - } + let mut image_caption = REDLIB_PREVIEW_TEXT_REGEX.find(&formatted_url).map_or("", |m| m.as_str()).to_string(); - let image_to_replace = format!("

"); + /* Remove first and last four characters of image_text to leave us with just the text in the caption without any HTML. + This makes it possible to enclose it in a
later on without having stray HTML breaking it */ + image_caption = image_caption[1..image_caption.len() - 4].to_string(); - image_text = image_text.replace("\\"", "\""); + // image_url contains > at the end of it, and right above this we remove image_text's front >, leaving us with just a single > between them + let image_to_replace = format!("

"); + // _image_replacement needs to be in scope for the replacement at the bottom of the loop let mut _image_replacement = String::new(); - if REDDIT_PREVIEW_REGEX.find(&image_text).is_none() { - _image_replacement = format!("
{image_text}
"); + /* We don't want to show a caption that's just the image's link, so we check if we find a Reddit preview link within the image's caption. + If we don't find one we must have actual text, so we include a
block that contains it. + Otherwise we don't include the
block as we don't need it. */ + if REDDIT_PREVIEW_REGEX.find(&image_caption).is_none() { + // Without this " would show as \" instead. "\"" is how the quotes are formatted within image_text beforehand + image_caption = image_caption.replace("\\"", "\""); + + _image_replacement = format!("
{image_caption}
"); } else { _image_replacement = format!("
"); } From 3f863c8991648b60b0a650bedf4c91dc9a7576ee Mon Sep 17 00:00:00 2001 From: Butter Cat Date: Sun, 14 Apr 2024 17:26:43 -0400 Subject: [PATCH 04/11] Prevent panic if image_caption is empty, don't replace

's in case text is inside them with the image, update test to reflect change in image replacing --- src/utils.rs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/utils.rs b/src/utils.rs index 6f70fa5..d65478a 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -903,12 +903,16 @@ pub fn rewrite_urls(input_text: &str) -> String { let image_url = REDLIB_PREVIEW_LINK_REGEX.find(&formatted_url).map_or("", |m| m.as_str()).to_string(); let mut image_caption = REDLIB_PREVIEW_TEXT_REGEX.find(&formatted_url).map_or("", |m| m.as_str()).to_string(); - /* Remove first and last four characters of image_text to leave us with just the text in the caption without any HTML. + /* As long as image_caption isn't empty remove first and last four characters of image_text to leave us with just the text in the caption without any HTML. This makes it possible to enclose it in a

later on without having stray HTML breaking it */ - image_caption = image_caption[1..image_caption.len() - 4].to_string(); + if !image_caption.is_empty() { + image_caption = image_caption[1..image_caption.len() - 4].to_string(); + } // image_url contains > at the end of it, and right above this we remove image_text's front >, leaving us with just a single > between them - let image_to_replace = format!("

"); + let image_to_replace = format!(""); + + //println!("{}", image_to_replace); // _image_replacement needs to be in scope for the replacement at the bottom of the loop let mut _image_replacement = String::new(); @@ -928,6 +932,7 @@ pub fn rewrite_urls(input_text: &str) -> String { text1 = REDDIT_PREVIEW_REGEX .replace(&text1, formatted_url) .replace(&image_to_replace, &_image_replacement) + .replace("

", "") .to_string() } } @@ -1183,11 +1188,7 @@ async fn test_fetching_ws() { #[test] fn test_rewriting_image_links() { - let input = r#"

https://preview.redd.it/zq21ggkj2xo31.png?width=2560&format=png&auto=webp&s=539d8050628ec1190cac26468fe99cc66b6071ab

-

https://preview.redd.it/vty9ocij2xo31.png?width=2560&format=png&auto=webp&s=fc7c7ef993a5e9ef656d5f5d9cf8290a0a1df877

-

https://preview.redd.it/bdfdxkjj2xo31.png?width=2560&format=png&auto=webp&s=d0fa420ece27605e882e89cb4711d75d774322ac

-

caption 1

-

caption 2

"#; - let output = r#"
caption 1
caption 2
"#; + let input = r#"

caption 1

"#; + let output = r#"

caption 1

"#; assert_eq!(rewrite_urls(input), output); } From 6484ebf8976f99002ddb1c2202046f921a31f1b3 Mon Sep 17 00:00:00 2001 From: Butter Cat Date: Sun, 14 Apr 2024 17:32:10 -0400 Subject: [PATCH 05/11] Fix failing check --- src/utils.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/utils.rs b/src/utils.rs index d65478a..d1b84a3 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1188,7 +1188,8 @@ async fn test_fetching_ws() { #[test] fn test_rewriting_image_links() { - let input = r#"

caption 1

"#; + let input = + r#"

caption 1

"#; let output = r#"

caption 1

"#; assert_eq!(rewrite_urls(input), output); } From 50fad938dd8daa53aa1203a9cf4dbd66df44c301 Mon Sep 17 00:00:00 2001 From: Butter Cat Date: Wed, 22 May 2024 16:31:07 -0400 Subject: [PATCH 06/11] Fix infinite loop when replacing text that contains dollar signs --- src/utils.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils.rs b/src/utils.rs index d1b84a3..a0cdbe8 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -930,7 +930,7 @@ pub fn rewrite_urls(input_text: &str) -> String { } text1 = REDDIT_PREVIEW_REGEX - .replace(&text1, formatted_url) + .replace(&text1, "/preview/pre$2") .replace(&image_to_replace, &_image_replacement) .replace("

", "") .to_string() @@ -1190,6 +1190,6 @@ async fn test_fetching_ws() { fn test_rewriting_image_links() { let input = r#"

caption 1

"#; - let output = r#"

caption 1

"#; + let output = r#"

caption 1
Date: Wed, 22 May 2024 16:34:39 -0400 Subject: [PATCH 07/11] Remove useless replace --- src/utils.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/utils.rs b/src/utils.rs index a0cdbe8..e08e09d 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -932,7 +932,6 @@ pub fn rewrite_urls(input_text: &str) -> String { text1 = REDDIT_PREVIEW_REGEX .replace(&text1, "/preview/pre$2") .replace(&image_to_replace, &_image_replacement) - .replace("

", "") .to_string() } } From b22fb7cd7be8839f32e8703b9707fcbe394be149 Mon Sep 17 00:00:00 2001 From: Butter Cat Date: Wed, 22 May 2024 16:40:57 -0400 Subject: [PATCH 08/11] Fix embedded preview images having a gap from the top of a comment --- static/style.css | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/static/style.css b/static/style.css index a533dd6..6c561d4 100644 --- a/static/style.css +++ b/static/style.css @@ -1496,6 +1496,15 @@ input[type="submit"] { margin-top: 20px; } +.md > figure:first-of-type { + margin-top: 5px; + margin-bottom: 0px; +} + +.md > figure:not(:first-of-type) { + margin-top: 10px; +} + .md h1 { font-size: 22px; } .md h2 { font-size: 20px; } .md h3 { font-size: 18px; } From 2f2cded671e92ef3aa12eb4527cab59a03c7f45d Mon Sep 17 00:00:00 2001 From: Butter Cat Date: Wed, 22 May 2024 17:22:10 -0400 Subject: [PATCH 09/11] Make sure new system can handle both normal and external previews --- src/utils.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/utils.rs b/src/utils.rs index e08e09d..a00b6e3 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -929,8 +929,18 @@ pub fn rewrite_urls(input_text: &str) -> String { _image_replacement = format!("
"); } + /* In order to know if we're dealing with a normal or external preview we need to take a look at the first capture group of REDDIT_PREVIEW_REGEX + if it's preview we're dealing with something that needs /preview/pre, otherwise it needs /preview/external-pre */ + let reddit_preview_regex_captures = REDDIT_PREVIEW_REGEX.captures(&text1).unwrap(); + let mut _preview_type = String::new(); + if reddit_preview_regex_captures.get(1).map_or("", |m| m.as_str()).to_string() == "preview" { + _preview_type = "/preview/pre".to_string(); + } else { + _preview_type = "/preview/external-pre".to_string(); + } + text1 = REDDIT_PREVIEW_REGEX - .replace(&text1, "/preview/pre$2") + .replace(&text1, format!("{_preview_type}$2")) .replace(&image_to_replace, &_image_replacement) .to_string() } From e9af28b6eb11de35cc223ecea376d0042bbc19ed Mon Sep 17 00:00:00 2001 From: Butter Cat Date: Thu, 23 May 2024 20:31:40 -0400 Subject: [PATCH 10/11] Make sure that the extra

at the bottom of a comment containing only an image doesn't get a margin --- static/style.css | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/static/style.css b/static/style.css index 6c561d4..ffc977e 100644 --- a/static/style.css +++ b/static/style.css @@ -1492,7 +1492,7 @@ input[type="submit"] { width: 100%; } -.md > *:not(:first-child) { +.md > p:not(:first-child):not(:last-child) { margin-top: 20px; } From 62b791bb24cdcdbd8d81d3525e7327f5acda1422 Mon Sep 17 00:00:00 2001 From: Butter Cat Date: Thu, 23 May 2024 21:29:36 -0400 Subject: [PATCH 11/11] Add in support for embedding i.redd.it images and gifs and remove leftover println --- src/utils.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/utils.rs b/src/utils.rs index a00b6e3..98a2415 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -875,9 +875,9 @@ pub fn format_url(url: &str) -> String { // These are links we want to replace in-body static REDDIT_REGEX: Lazy = Lazy::new(|| Regex::new(r#"href="(https|http|)://(www\.|old\.|np\.|amp\.|new\.|)(reddit\.com|redd\.it)/"#).unwrap()); -static REDDIT_PREVIEW_REGEX: Lazy = Lazy::new(|| Regex::new(r"https?://(external-preview|preview)\.redd\.it(.*)[^?]").unwrap()); +static REDDIT_PREVIEW_REGEX: Lazy = Lazy::new(|| Regex::new(r"https?://(external-preview|preview|i)\.redd\.it(.*)[^?]").unwrap()); static REDDIT_EMOJI_REGEX: Lazy = Lazy::new(|| Regex::new(r"https?://(www|).redditstatic\.com/(.*)").unwrap()); -static REDLIB_PREVIEW_LINK_REGEX: Lazy = Lazy::new(|| Regex::new(r#"/preview/(pre|external-pre)/(.*?)>"#).unwrap()); +static REDLIB_PREVIEW_LINK_REGEX: Lazy = Lazy::new(|| Regex::new(r#"/(img|preview/)(pre|external-pre)?/(.*?)>"#).unwrap()); static REDLIB_PREVIEW_TEXT_REGEX: Lazy = Lazy::new(|| Regex::new(r">(.*?)
").unwrap()); // Rewrite Reddit links to Redlib in body of text @@ -912,8 +912,6 @@ pub fn rewrite_urls(input_text: &str) -> String { // image_url contains > at the end of it, and right above this we remove image_text's front >, leaving us with just a single > between them let image_to_replace = format!(""); - //println!("{}", image_to_replace); - // _image_replacement needs to be in scope for the replacement at the bottom of the loop let mut _image_replacement = String::new(); @@ -930,13 +928,15 @@ pub fn rewrite_urls(input_text: &str) -> String { } /* In order to know if we're dealing with a normal or external preview we need to take a look at the first capture group of REDDIT_PREVIEW_REGEX - if it's preview we're dealing with something that needs /preview/pre, otherwise it needs /preview/external-pre */ - let reddit_preview_regex_captures = REDDIT_PREVIEW_REGEX.captures(&text1).unwrap(); + if it's preview we're dealing with something that needs /preview/pre, external-preview is /preview/external-pre, and i is /img */ + let reddit_preview_regex_capture = REDDIT_PREVIEW_REGEX.captures(&text1).unwrap().get(1).map_or("", |m| m.as_str()).to_string(); let mut _preview_type = String::new(); - if reddit_preview_regex_captures.get(1).map_or("", |m| m.as_str()).to_string() == "preview" { + if reddit_preview_regex_capture == "preview" { _preview_type = "/preview/pre".to_string(); - } else { + } else if reddit_preview_regex_capture == "external-preview" { _preview_type = "/preview/external-pre".to_string(); + } else { + _preview_type = "/img".to_string(); } text1 = REDDIT_PREVIEW_REGEX