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,7 +2,10 @@
use clap::{Parser, Subcommand}; use clap::{Parser, Subcommand};
use std::{fs, path::PathBuf}; use std::{fs, path::PathBuf};
use crate::{commands::list_entries::list_entries, md::Doc}; use crate::{
commands::list_entries::list_entries,
md::{Doc, ToMd},
};
mod commands; mod commands;
mod md; mod md;
@ -35,7 +38,8 @@ fn main() {
// TODO: handle btter // TODO: handle btter
let file = fs::read_to_string(cli.s10e_jrnl_file_loc).unwrap(); let file = fs::read_to_string(cli.s10e_jrnl_file_loc).unwrap();
let doc = dbg!(Doc::new(&file)); let doc = Doc::new(&file).unwrap();
println!("{}", doc.to_md())
} }
} }
} }

View file

@ -2,6 +2,10 @@ use chrono::{DateTime, FixedOffset};
use markdown::{Block, Span}; use markdown::{Block, Span};
use std::convert::identity; use std::convert::identity;
pub trait ToMd {
fn to_md(&self) -> String;
}
#[derive(Debug)] #[derive(Debug)]
pub struct Doc<'src> { pub struct Doc<'src> {
pub entries: Vec<Entry<'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)] #[derive(Debug, Clone)]
pub struct Entry<'src> { pub struct Entry<'src> {
pub timestamp: DateTime<FixedOffset>, pub timestamp: DateTime<FixedOffset>,
pub title: &'src str, pub title: &'src str,
pub content: &'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
)
}
}