copy via markdown parsing

This commit is contained in:
Andrei Stoica 2023-06-15 12:14:36 -04:00
parent 2caa88ad43
commit 35d5467d03
2 changed files with 36 additions and 2 deletions

View File

@ -7,4 +7,5 @@ edition = "2021"
[dependencies] [dependencies]
chrono = "0.4.26" chrono = "0.4.26"
comrak = "0.18.0"
regex = "1.8.4" regex = "1.8.4"

View File

@ -3,13 +3,21 @@ mod todo_file;
use crate::todo_file::TodoFile; use crate::todo_file::TodoFile;
use chrono::naive::NaiveDate; use chrono::naive::NaiveDate;
use chrono::{Datelike, Local}; use chrono::{Datelike, Local};
use comrak::nodes::AstNode;
use comrak::{
format_commonmark, parse_document, Arena, ComrakExtensionOptions, ComrakOptions,
ComrakParseOptions,
};
use std::env; 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::path::{Path, PathBuf};
use std::process::Command; use std::process::Command;
use std::str;
//TODO handle unwraps and errors more uniformly //TODO handle unwraps and errors more uniformly
//TODO clean up verbose printing //TODO clean up verbose printing
//TODO create config for passing options to different files
fn main() { fn main() {
let data_dir = get_data_dir("notes"); let data_dir = get_data_dir("notes");
@ -35,7 +43,14 @@ fn main() {
let mut today_file_path = data_dir.clone(); let mut today_file_path = data_dir.clone();
today_file_path.push(today_file_name); 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 editor
.args([today_file_path]) .args([today_file_path])
@ -53,6 +68,24 @@ fn main() {
} }
} }
fn parse_todo_file<'a>(file: &TodoFile, arena: &'a Arena<AstNode<'a>>) -> &'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 { fn get_editor(fallback: String) -> String {
match env::var("EDITOR") { match env::var("EDITOR") {
Ok(editor) => editor, Ok(editor) => editor,