60 lines
1.7 KiB
Rust
60 lines
1.7 KiB
Rust
|
use chrono::{DateTime, FixedOffset};
|
||
|
use markdown::{Block, Span};
|
||
|
|
||
|
#[derive(Debug)]
|
||
|
pub struct Doc {
|
||
|
pub title: Vec<Span>,
|
||
|
pub entries: Vec<Entry>,
|
||
|
}
|
||
|
|
||
|
impl Doc {
|
||
|
pub fn new(f: &str) -> Self {
|
||
|
let mut entries = Vec::new();
|
||
|
let mut doc_title = vec![Span::Text("Journal".to_owned())];
|
||
|
let toks = markdown::tokenize(f);
|
||
|
let mut current = None;
|
||
|
|
||
|
for tok in toks {
|
||
|
match tok {
|
||
|
Block::Header(title, 1) => doc_title = title,
|
||
|
Block::Header(entry_title, 2) => {
|
||
|
if let Some(cur) = current.take() {
|
||
|
entries.push(cur);
|
||
|
}
|
||
|
|
||
|
let Some(Span::Text(title)) = entry_title.first() else {
|
||
|
eprintln!("Error: Titles should be text.");
|
||
|
std::process::exit(1);
|
||
|
};
|
||
|
|
||
|
let (ts, entry_title) = title.split_once(": ").unwrap();
|
||
|
let ts = DateTime::parse_from_rfc3339(ts).unwrap();
|
||
|
// let ts = PrimitiveDateTime::parse(ts, &DT_FORMAT).unwrap();
|
||
|
|
||
|
current = Some(Entry {
|
||
|
timestamp: ts,
|
||
|
title: entry_title.to_owned(),
|
||
|
content: Vec::new(),
|
||
|
});
|
||
|
}
|
||
|
other => current.as_mut().unwrap().content.push(other),
|
||
|
}
|
||
|
}
|
||
|
if let Some(cur) = current {
|
||
|
entries.push(cur);
|
||
|
}
|
||
|
|
||
|
Self {
|
||
|
title: doc_title,
|
||
|
entries,
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#[derive(Debug)]
|
||
|
pub struct Entry {
|
||
|
pub timestamp: DateTime<FixedOffset>,
|
||
|
pub title: String,
|
||
|
pub content: Vec<Block>,
|
||
|
}
|