From b967f6e90edb523c0a57ebfe0b57607c2271084b Mon Sep 17 00:00:00 2001 From: Schrottkatze Date: Mon, 22 Apr 2024 11:44:26 +0200 Subject: [PATCH] jrnl: markdown generation --- programs/jrnl/src/main.rs | 8 ++++++-- programs/jrnl/src/md.rs | 21 +++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/programs/jrnl/src/main.rs b/programs/jrnl/src/main.rs index b9ba013..22faae3 100644 --- a/programs/jrnl/src/main.rs +++ b/programs/jrnl/src/main.rs @@ -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()) } } } diff --git a/programs/jrnl/src/md.rs b/programs/jrnl/src/md.rs index 80ba946..cd3ab43 100644 --- a/programs/jrnl/src/md.rs +++ b/programs/jrnl/src/md.rs @@ -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>, @@ -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, 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 + ) + } +}