jrnl: markdown generation

This commit is contained in:
Schrottkatze 2024-04-22 11:44:26 +02:00
parent 0bf5ed0c76
commit b967f6e90e
No known key found for this signature in database
2 changed files with 27 additions and 2 deletions

View file

@ -2,7 +2,10 @@
use clap::{Parser, Subcommand};
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 md;
@ -35,7 +38,8 @@ fn main() {
// TODO: handle btter
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 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
)
}
}