From 35d5467d03f2442ee2d86cf4fe8baeac215ab094 Mon Sep 17 00:00:00 2001 From: Andrei Stoica Date: Thu, 15 Jun 2023 12:14:36 -0400 Subject: [PATCH] copy via markdown parsing --- Cargo.toml | 1 + src/main.rs | 37 +++++++++++++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7c0595a..b87f4d8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,4 +7,5 @@ edition = "2021" [dependencies] chrono = "0.4.26" +comrak = "0.18.0" regex = "1.8.4" diff --git a/src/main.rs b/src/main.rs index 08eefdb..1757f0c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,13 +3,21 @@ mod todo_file; use crate::todo_file::TodoFile; use chrono::naive::NaiveDate; use chrono::{Datelike, Local}; +use comrak::nodes::AstNode; +use comrak::{ + format_commonmark, parse_document, Arena, ComrakExtensionOptions, ComrakOptions, + ComrakParseOptions, +}; use std::env; -use std::fs::{copy, read_dir}; +use std::fs::{copy, read, read_dir, File}; +use std::io::Write; use std::path::{Path, PathBuf}; use std::process::Command; +use std::str; //TODO handle unwraps and errors more uniformly //TODO clean up verbose printing +//TODO create config for passing options to different files fn main() { let data_dir = get_data_dir("notes"); @@ -35,7 +43,14 @@ fn main() { let mut today_file_path = data_dir.clone(); today_file_path.push(today_file_name); - copy(latest_file.file.path(), today_file_path.clone()).unwrap(); + let arena = Arena::new(); + let root = parse_todo_file(&latest_file, &arena); + //copy(latest_file.file.path(), today_file_path.clone()).unwrap(); + let mut new_doc = vec![]; + format_commonmark(root, &ComrakOptions::default(), &mut new_doc); + + let mut new_file = File::create(today_file_path.clone()).unwrap(); + new_file.write_all(&new_doc); editor .args([today_file_path]) @@ -53,6 +68,24 @@ fn main() { } } +fn parse_todo_file<'a>(file: &TodoFile, arena: &'a Arena>) -> &'a AstNode<'a> { + let options = &ComrakOptions { + extension: ComrakExtensionOptions { + tasklist: true, + ..ComrakExtensionOptions::default() + }, + parse: ComrakParseOptions { + relaxed_tasklist_matching: true, + ..ComrakParseOptions::default() + }, + ..ComrakOptions::default() + }; + + let contents = read(file.file.path()).unwrap(); + + parse_document(arena, str::from_utf8(&contents).unwrap(), options) +} + fn get_editor(fallback: String) -> String { match env::var("EDITOR") { Ok(editor) => editor,