~not/mdblog

71bb6570afe0115aa71a6bb9289bd94f0c95d861 — b123400 3 months ago 460c4c8
Read h1 as title if <title> is not found
1 files changed, 18 insertions(+), 0 deletions(-)

M src/post.rs
M src/post.rs => src/post.rs +18 -0
@@ 117,6 117,13 @@ pub async fn read_title(path: &Path) -> Option<String> {

static TITLE_REGEX: &str = r#"<title>([^<]*)</title>"#;
pub async fn read_html_title(path: &Path) -> Option<String> {
    let t = read_html_by_title(&path).await;
    match t {
        Some(_)=> t,
        None => read_html_by_h1(&path).await
    }
}
pub async fn read_html_by_title(path: &Path) -> Option<String> {
    let title_regex: regex::bytes::Regex = Regex::new(TITLE_REGEX).unwrap();
    let mut file = tokio::fs::File::open(path).await.ok()?;
    let mut buffer = [0; 500];


@@ 126,6 133,17 @@ pub async fn read_html_title(path: &Path) -> Option<String> {
    let title_bytes = captures.get(1)?.as_bytes();
    Some(std::str::from_utf8(title_bytes).ok()?.trim().to_string())
}
static H1_REGEX: &str = r#"<h1>([^<]*)</h1>"#;
pub async fn read_html_by_h1(path: &Path) -> Option<String> {
    let title_regex: regex::bytes::Regex = Regex::new(H1_REGEX).unwrap();
    let mut file = tokio::fs::File::open(path).await.ok()?;
    let mut buffer = [0; 500];
    // read up to 500 bytes
    file.read(&mut buffer).await.ok()?;
    let captures = title_regex.captures(&buffer)?;
    let title_bytes = captures.get(1)?.as_bytes();
    Some(std::str::from_utf8(title_bytes).ok()?.trim().to_string())
}

pub async fn read_post(path: PathBuf) -> Option<Post> {
    if path.is_file() {