Compare commits

...

2 commits

Author SHA1 Message Date
Schrottkatze b967f6e90e
jrnl: markdown generation 2024-04-22 11:44:26 +02:00
Schrottkatze 0bf5ed0c76
jrnl: simplify (or complexify, if you dont like iterators and zero-copy) the parsing 2024-04-22 11:15:45 +02:00
5 changed files with 79 additions and 58 deletions

7
Cargo.lock generated
View file

@ -412,6 +412,7 @@ dependencies = [
"owo-colors", "owo-colors",
"petgraph", "petgraph",
"ratatui", "ratatui",
"temp-file",
"termsize", "termsize",
] ]
@ -799,6 +800,12 @@ dependencies = [
"unicode-ident", "unicode-ident",
] ]
[[package]]
name = "temp-file"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f210bda61d003f311d95611d1b68361df8fe8e732c3609f945441bde881321d"
[[package]] [[package]]
name = "termion" name = "termion"
version = "1.5.6" version = "1.5.6"

View file

@ -13,4 +13,5 @@ markdown = "0.3.0"
owo-colors = "4.0.0" owo-colors = "4.0.0"
petgraph = "0.6.4" petgraph = "0.6.4"
ratatui = "0.26.2" ratatui = "0.26.2"
temp-file = "0.1.8"
termsize = "0.1.6" termsize = "0.1.6"

View file

@ -5,16 +5,20 @@ use crate::md::Doc;
pub fn list_entries(path: PathBuf) { pub fn list_entries(path: PathBuf) {
let file = fs::read_to_string(path).unwrap(); let file = fs::read_to_string(path).unwrap();
let doc = Doc::new(&file);
for (i, entry) in doc.entries.into_iter().enumerate() { if let Some(doc) = Doc::new(&file) {
let n = format!("{:>2}", i + 1); for (i, entry) in doc.entries.into_iter().enumerate() {
let r = format!(". {}", entry.title,); let n = format!("{:>2}", i + 1);
let l = format!(" {} ", crate::utils::format_datetime(entry.timestamp)); let r = format!(". {}", entry.title,);
let termsize::Size { cols, .. } = termsize::get().unwrap(); let l = format!(" {} ", crate::utils::format_datetime(entry.timestamp));
let termsize::Size { cols, .. } = termsize::get().unwrap();
let padding = " ".repeat(cols as usize - (n.len() + r.len() + l.len())); let padding = " ".repeat(cols as usize - (n.len() + r.len() + l.len()));
println!("{}{r}{padding}{}", n.cyan(), l.white()) println!("{}{r}{padding}{}", n.cyan(), l.white())
}
} else {
eprintln!("Parsing error...");
std::process::exit(1);
} }
} }

View file

@ -1,7 +1,11 @@
#![feature(iter_collect_into)]
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;
@ -34,8 +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 = Doc::new(&file); let doc = Doc::new(&file).unwrap();
dbg!(doc); println!("{}", doc.to_md())
} }
} }
} }

View file

@ -1,59 +1,64 @@
use chrono::{DateTime, FixedOffset}; use chrono::{DateTime, FixedOffset};
use markdown::{Block, Span}; use markdown::{Block, Span};
use std::convert::identity;
#[derive(Debug)] pub trait ToMd {
pub struct Doc { fn to_md(&self) -> String;
pub title: Vec<Span>,
pub entries: Vec<Entry>,
} }
impl Doc { #[derive(Debug)]
pub fn new(f: &str) -> Self { pub struct Doc<'src> {
let mut entries = Vec::new(); pub entries: Vec<Entry<'src>>,
let mut doc_title = vec![Span::Text("Journal".to_owned())]; }
let toks = markdown::tokenize(f);
let mut current = None;
for tok in toks { impl<'src> Doc<'src> {
match tok { // TODO: better parsing errors?
Block::Header(title, 1) => doc_title = title, pub fn new(f: &'src str) -> Option<Self> {
Block::Header(entry_title, 2) => { let entries = f
if let Some(cur) = current.take() { .split("\n## ")
entries.push(cur); .map(|s| s.split_once("\n"))
} .skip(1)
.filter_map(identity)
let Some(Span::Text(title)) = entry_title.first() else { .map(|(title, content)| (title.split_once(": "), content))
eprintln!("Error: Titles should be text."); .map(|(title, content)| {
std::process::exit(1); if let Some((ts, title)) = title {
}; Some(Entry {
timestamp: DateTime::parse_from_rfc3339(ts).unwrap(),
let (ts, entry_title) = title.split_once(": ").unwrap(); title,
let ts = DateTime::parse_from_rfc3339(ts).unwrap(); content: content.trim_matches('\n'),
// let ts = PrimitiveDateTime::parse(ts, &DT_FORMAT).unwrap(); })
} else {
current = Some(Entry { None
timestamp: ts,
title: entry_title.to_owned(),
content: Vec::new(),
});
} }
other => current.as_mut().unwrap().content.push(other), })
} .collect::<Vec<_>>();
}
if let Some(cur) = current {
entries.push(cur);
}
Self { entries.iter().all(|it| it.is_some()).then_some(Self {
title: doc_title, entries: entries.into_iter().filter_map(identity).collect(),
entries, })
}
} }
} }
#[derive(Debug)] impl ToMd for Doc<'_> {
pub struct Entry { fn to_md(&self) -> String {
pub timestamp: DateTime<FixedOffset>, let mut r = "# Journal\n\n".to_owned();
pub title: String,
pub content: Vec<Block>, 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
)
}
} }