jrnl: markdown generation

This commit is contained in:
Schrottkatze 2024-04-22 11:44:26 +02:00
parent 0bf5ed0c76
commit b967f6e90e
Signed by: schrottkatze
SSH key fingerprint: SHA256:hXb3t1vINBFCiDCmhRABHX5ocdbLiKyCdKI4HK2Rbbc
2 changed files with 27 additions and 2 deletions

View file

@ -2,6 +2,10 @@ use chrono::{DateTime, FixedOffset};
use markdown::{Block, Span};
use std::convert::identity;
pub trait ToMd {
fn to_md(&self) -> String;
}
#[derive(Debug)]
pub struct Doc<'src> {
pub entries: Vec<Entry<'src>>,
@ -35,9 +39,26 @@ impl<'src> Doc<'src> {
}
}
impl ToMd for Doc<'_> {
fn to_md(&self) -> String {
let mut r = "# Journal\n\n".to_owned();
self.entries.iter().fold(r, |mut r, it| r + &it.to_md())
}
}
#[derive(Debug, Clone)]
pub struct Entry<'src> {
pub timestamp: DateTime<FixedOffset>,
pub title: &'src str,
pub content: &'src str,
}
impl ToMd for Entry<'_> {
fn to_md(&self) -> String {
format!(
"## {}: {}\n\n{}\n\n",
self.timestamp, self.title, self.content
)
}
}