update format methods

This commit is contained in:
yggverse 2026-03-19 06:38:01 +02:00
parent d0ad447016
commit 6b07499d10

View file

@ -7,6 +7,7 @@ use clap::Parser;
use config::Config; use config::Config;
use database::Database; use database::Database;
use html_to_markdown_rs::convert; use html_to_markdown_rs::convert;
use regex::Regex;
use std::{ use std::{
collections::HashMap, collections::HashMap,
fs::{File, create_dir_all, remove_dir_all}, fs::{File, create_dir_all, remove_dir_all},
@ -104,20 +105,23 @@ fn main() -> Result<()> {
file.write_all( file.write_all(
{ {
let mut page = Vec::new(); let mut page = Vec::new();
page.push(format!("# {}", discussion.title)); page.push(format!("# {}\n", discussion.title.trim()));
page.push({ page.push({
let mut content = Vec::new(); let mut content = Vec::new();
for post in discussion.posts { for post in discussion.posts {
content.push(format!( content.push(format!(
"_@{} / {}{}_", "_@{} / {}{}_\n",
users.get(&post.user_id).unwrap().username, users.get(&post.user_id).unwrap().username,
post.created_at, post.created_at,
post.edited_at post.edited_at
.map(|edited_at| format!(" / {}", edited_at)) .map(|edited_at| format!(" / {}", edited_at))
.unwrap_or_default() .unwrap_or_default()
)); ));
content.push("---".into()); content.push(post_format(&convert(
content.push(convert(strip_tags(&post.content).trim(), None)?) pre_format(&post.content).trim(),
None,
)?));
content.push("---\n".into())
} }
content.join("\n") content.join("\n")
}); });
@ -130,9 +134,7 @@ fn main() -> Result<()> {
Ok(()) Ok(())
} }
fn strip_tags(data: &str) -> String { fn pre_format(data: &str) -> String {
use regex::Regex;
let s = Regex::new(r"<s>[^<]+</s>").unwrap(); let s = Regex::new(r"<s>[^<]+</s>").unwrap();
let e = Regex::new(r"<e>[^<]+</e>").unwrap(); let e = Regex::new(r"<e>[^<]+</e>").unwrap();
@ -147,3 +149,10 @@ fn strip_tags(data: &str) -> String {
.replace("<r>", "") .replace("<r>", "")
.replace("</r>", "") .replace("</r>", "")
} }
fn post_format(data: &str) -> String {
Regex::new(r"\n{3,}")
.unwrap()
.replace(data, "\n\n")
.to_string()
}