10 Commits

Author SHA1 Message Date
e058abfa96 added frontmatter to config
All checks were successful
Test / test (push) Successful in 49s
2026-04-15 17:41:05 -04:00
6b8b8a8f51 Merge pull request 'test' (#5) from workflow-test into main
All checks were successful
Test / test (push) Successful in 39s
Reviewed-on: #5
2026-04-15 00:43:30 -04:00
7f1162c999 Merge branch 'main' into workflow-test
All checks were successful
Test / test (push) Successful in 39s
Build / test (pull_request) Successful in 1m14s
2026-04-15 00:43:22 -04:00
e861bf99f7 more build actions testing
Some checks failed
Test / test (push) Has been cancelled
2026-04-15 00:42:58 -04:00
afb00ba57a Merge branch 'workflow-test'
All checks were successful
Test / test (push) Successful in 40s
2026-04-15 00:40:12 -04:00
887eb954a8 build workflow
All checks were successful
Test / test (push) Successful in 40s
2026-04-15 00:39:11 -04:00
1ec217732f build workflow
All checks were successful
Test / test (push) Successful in 41s
2026-04-15 00:29:05 -04:00
ce03c9c171 moved to comrak 0.52
All checks were successful
Test / test (push) Successful in 47s
2026-04-14 23:44:52 -04:00
181be6c4e3 actions test
Some checks failed
Test / test (release) (push) Failing after 10m32s
2026-04-14 14:22:44 -04:00
84d7ba45d3 moved to comrak v0.24.1 2024-10-10 20:08:26 -04:00
9 changed files with 371 additions and 45 deletions

View File

@@ -0,0 +1,20 @@
name: Build
on:
release:
types:
- published
pull_request:
types:
- opened
- edited
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v5
- uses: dtolnay/rust-toolchain@stable
- name: Run tests
run: cargo test
- name: Run tests
run: cargo build --release --target x86_64-unknown-linux-gnu

View File

@@ -0,0 +1,11 @@
name: Test
on: [push]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v5
- uses: dtolnay/rust-toolchain@stable
- name: Run tests
run: cargo test

View File

@@ -8,7 +8,7 @@ edition = "2021"
[dependencies] [dependencies]
chrono = "0.4.26" chrono = "0.4.26"
clap = { version = "4.5.1", features = ["derive"] } clap = { version = "4.5.1", features = ["derive"] }
comrak = "0.18.0" comrak = "~0.52.0"
figment = { version = "0.10.10", features = ["env", "serde_json", "json"] } figment = { version = "0.10.10", features = ["env", "serde_json", "json"] }
regex = "1.8.4" regex = "1.8.4"
serde = { version = "1.0.164", features = ["serde_derive"] } serde = { version = "1.0.164", features = ["serde_derive"] }
@@ -16,3 +16,4 @@ serde_json = "1.0.97"
resolve-path = "0.1.0" resolve-path = "0.1.0"
simple_logger = "4.3.3" simple_logger = "4.3.3"
log = "0.4.21" log = "0.4.21"
indexmap = "2.2.6"

View File

@@ -1,8 +1,6 @@
- [ ] Obsidian properties - [ ] Obsidian properties
- [ ] encoding in YAML (using Serde) - [x] config for default properties
- [ ] config for default properties
- [ ] formatting for properties such as dates - [ ] formatting for properties such as dates
- [ ] update rendering to use comrak (it's been update) - [ ] figure out what frontmatter obsidian uses
- [ ] generate title

View File

@@ -10,6 +10,9 @@ pub struct Args {
/// show current config file /// show current config file
#[arg(short = 'C', long)] #[arg(short = 'C', long)]
pub current_config: bool, pub current_config: bool,
// generate config file (output to stdout)
#[arg(long, default_value_t = true)]
pub gen_config: bool,
/// view a specific date's file (YYYY-MM-DD) /// view a specific date's file (YYYY-MM-DD)
#[arg(short, long)] #[arg(short, long)]

View File

@@ -4,6 +4,7 @@ extern crate serde_json;
use figment::providers::{Env, Format, Json, Serialized}; use figment::providers::{Env, Format, Json, Serialized};
use figment::Figment; use figment::Figment;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::env::var; use std::env::var;
use std::fs::File; use std::fs::File;
use std::io::Write; use std::io::Write;
@@ -13,7 +14,9 @@ use std::path::PathBuf;
pub struct Config { pub struct Config {
pub editor: String, pub editor: String,
pub sections: Vec<String>, pub sections: Vec<String>,
pub scratch_section: String,
pub notes_dir: String, pub notes_dir: String,
pub frontmatter: HashMap<String, String>,
} }
impl Default for Config { impl Default for Config {
@@ -21,7 +24,9 @@ impl Default for Config {
Config { Config {
editor: "nano".into(), editor: "nano".into(),
sections: vec!["Daily".into(), "Weekly".into(), "Monthly".into()], sections: vec!["Daily".into(), "Weekly".into(), "Monthly".into()],
scratch_section: "".into(),
notes_dir: "~/Notes".into(), notes_dir: "~/Notes".into(),
frontmatter: HashMap::new(),
} }
} }
} }

View File

@@ -2,9 +2,11 @@ use crate::todo::{File as TodoFile, Status as TaskStatus};
use crate::NaiveDate; use crate::NaiveDate;
use crate::TaskGroup; use crate::TaskGroup;
use chrono::Datelike; use chrono::Datelike;
use comrak::nodes::{AstNode, NodeValue}; use comrak::nodes::{Ast, AstNode, LineColumn, NodeHeading, NodeTaskItem, NodeValue};
use comrak::parse_document; use comrak::options::{Extension, Parse};
use comrak::{Arena, ComrakExtensionOptions, ComrakOptions, ComrakParseOptions}; use comrak::{parse_document, Arena, Options};
use indexmap::IndexMap;
use regex::Regex;
use std::collections::HashMap; use std::collections::HashMap;
use std::fs::{read, File}; use std::fs::{read, File};
use std::io::Write; use std::io::Write;
@@ -61,17 +63,17 @@ pub fn load_file(file: &TodoFile) -> String {
} }
/// Parse contents of markdown file with Comrak ( relaxed tasklist matching is enabled) /// Parse contents of markdown file with Comrak ( relaxed tasklist matching is enabled)
pub fn parse_todo_file<'a>(contents: &String, arena: &'a Arena<AstNode<'a>>) -> &'a AstNode<'a> { pub fn parse_todo_file<'a>(contents: &String, arena: &'a Arena<'a>) -> &'a AstNode<'a> {
let options = &ComrakOptions { let mut extension_options = Extension::default();
extension: ComrakExtensionOptions { extension_options.tasklist = true;
tasklist: true,
..ComrakExtensionOptions::default() let mut parse_options = Parse::default();
}, parse_options.relaxed_tasklist_matching = true;
parse: ComrakParseOptions {
relaxed_tasklist_matching: true, let options = &Options {
..ComrakParseOptions::default() extension: extension_options,
}, parse: parse_options,
..ComrakOptions::default() ..Options::default()
}; };
parse_document(arena, contents, options) parse_document(arena, contents, options)
} }
@@ -117,10 +119,185 @@ pub fn extract_secitons<'a>(
groups groups
} }
fn remove_heading<'a>(node: &'a AstNode<'a>, level: u8) {
let mut following = node.following_siblings();
let _ = following.next().unwrap();
for sib in following {
let node_ref = sib.data.borrow();
if let NodeValue::Heading(heading) = node_ref.value {
if heading.level == level {
break;
}
} else {
sib.detach();
}
}
node.detach();
}
/// recursively removes nodes from List
fn remove_task_nodes<'a>(root: &'a AstNode<'a>) {
for node in root.children() {
for child_node in node.children() {
remove_task_nodes(child_node)
}
match node.data.borrow().value {
NodeValue::TaskItem(NodeTaskItem {
symbol: Some(status),
symbol_sourcepos: _,
}) if status == 'x' || status == 'X' => node.detach(),
_ => continue,
}
}
}
fn create_title<'a>(arena: &'a Arena<'a>, date: &str) -> &'a AstNode<'a> {
let mut text = String::new();
text.push_str("Today's tasks ");
text.push_str(date);
create_heading(arena, 1, &text)
}
fn create_heading<'a>(arena: &'a Arena<'a>, level: u8, text: &str) -> &'a AstNode<'a> {
let heading_node = arena.alloc(AstNode::new(
Ast::new(
NodeValue::Heading(NodeHeading {
level,
setext: false,
closed: false,
}),
LineColumn { line: 0, column: 0 },
)
.into(),
));
let text_node = arena.alloc(AstNode::new(
Ast::new(
NodeValue::Text(text.to_string().into()),
LineColumn { line: 0, column: 2 },
)
.into(),
));
heading_node.append(text_node);
heading_node
}
pub fn create_new_doc<'a>(
arena: &'a Arena<'a>,
new_date: &str,
sections: IndexMap<String, Option<Vec<&'a AstNode<'a>>>>,
) -> &'a AstNode<'a> {
let doc = arena.alloc(AstNode::new(
Ast::new(NodeValue::Document, LineColumn { line: 0, column: 0 }).into(),
));
let title = create_title(&arena, new_date);
doc.append(title);
for (section, value) in sections.iter() {
let heading = create_heading(arena, 2, &section);
doc.append(heading);
match value {
Some(nodes) => {
for node in nodes.iter() {
doc.append(node);
}
}
_ => (),
}
}
doc
}
pub fn extract_sections<'a>(
root: &'a AstNode<'a>,
sections: &Vec<String>,
) -> IndexMap<String, Option<Vec<&'a AstNode<'a>>>> {
let mut section_map: IndexMap<String, Option<Vec<&'a AstNode<'a>>>> = IndexMap::new();
sections.iter().for_each(|section| {
section_map.insert(section.to_string(), None);
});
for node in root.reverse_children() {
let node_ref = node.data.borrow();
match node_ref.value {
NodeValue::Heading(heading) => {
let heading_content_node = if let Some(child) = node.first_child() {
child
} else {
continue;
};
let mut heading_content_ref = heading_content_node.data.borrow_mut();
if let NodeValue::Text(text) = &mut heading_content_ref.value {
if sections.contains(&text.to_string()) {
let mut content = Vec::new();
let mut following = node.following_siblings();
let _ = following.next().unwrap();
for sib in following {
remove_task_nodes(sib);
let node_ref = sib.data.borrow();
if let NodeValue::Heading(inner_heading) = node_ref.value {
if heading.level == inner_heading.level {
break;
}
} else {
content.push(sib);
}
}
section_map.insert(text.to_string(), Some(content));
remove_heading(node, heading.level);
};
}
}
_ => continue,
}
}
section_map
}
pub fn process_doc_tree<'a>(root: &'a AstNode<'a>, new_date: &str, sections: &Vec<String>) {
for node in root.reverse_children() {
let node_ref = node.data.borrow();
match node_ref.value {
NodeValue::Heading(heading) => {
let heading_content_node = if let Some(child) = node.first_child() {
child
} else {
continue;
};
let mut heading_content_ref = heading_content_node.data.borrow_mut();
if let NodeValue::Text(text) = &mut heading_content_ref.value {
let re = Regex::new(r"Today's tasks \d+-\d+-\d+")
.expect("title regex is not parsable");
if matches!(re.find(text), Some(_)) {
let text_mut = text.to_mut();
text_mut.clear();
text_mut.push_str("Today's tasks ");
text_mut.push_str(new_date);
} else if !sections.contains(&text.to_string()) {
remove_heading(node, heading.level);
};
}
}
NodeValue::List(_list) => remove_task_nodes(node),
_ => continue,
}
}
eprintln!("{:#?}", root);
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;
use crate::todo::{Status, Task}; use crate::todo::Status;
use crate::todo::Task;
use comrak::format_commonmark;
#[test] #[test]
fn test_extract_sections() { fn test_extract_sections() {
@@ -278,4 +455,89 @@ mod test {
"; ";
assert_eq!(result, expected); assert_eq!(result, expected);
} }
#[test]
fn test_node_removal() {
let md = "
# Today's tasks 2024-01-01
## Tasks
- [ ] task 1
- [X] task 2
- [x] task 2
- [>] task 3
- [!] task 3
## Long Term
- [ ] task 1
- [X] task 2
- [ ] all of these subtasks should be removed
- [x] subtasks
- [x] sub task to remove
- [!] task 3
- [ ] sub task to keep
- [x] sub task to remove
## Todays Notes
- some notes here
- these can go
";
let new_date = "2024-01-02";
let groups = vec![
"Tasks".to_string(),
"Other".to_string(),
"Long Term".to_string(),
"Last".to_string(),
];
let arena = Arena::new();
let mut extension_options = Extension::default();
extension_options.tasklist = true;
let mut parse_options = Parse::default();
parse_options.relaxed_tasklist_matching = true;
let options = &Options {
extension: extension_options,
parse: parse_options,
..Options::default()
};
let ast = parse_document(&arena, md, options);
let sections = extract_sections(ast, &groups);
let new_doc = create_new_doc(&arena, new_date, sections);
process_doc_tree(ast, new_date, &groups);
let mut output = String::new(); //BufWriter::new(Vec::new());
assert!(format_commonmark(new_doc, options, &mut output).is_ok());
assert_eq!(
"\
# Today's tasks 2024-01-02
## Tasks
- [ ] task 1
- [>] task 3
- [!] task 3
## Other
## Long Term
- [ ] task 1
- [!] task 3
- [ ] sub task to keep
## Last
",
output
);
}
} }

View File

@@ -5,20 +5,23 @@ mod logging;
mod todo; mod todo;
use chrono::naive::NaiveDate; use chrono::naive::NaiveDate;
use chrono::{Local, TimeDelta}; use chrono::{Datelike, Local, TimeDelta};
use clap::Parser; use clap::Parser;
use cli::Args; use cli::Args;
use comrak::Arena; use comrak::options::{Extension, Parse};
use config::Config; use comrak::{format_commonmark, Arena, Options};
use config::{Config, ConfigError};
use log; use log;
use logging::get_logging_level; use logging::get_logging_level;
use resolve_path::PathResolveExt; use resolve_path::PathResolveExt;
use simple_logger::init_with_level; use simple_logger::init_with_level;
use std::fs; use std::fs;
use std::path::Path; use std::path::Path;
use std::process::Command; use std::process::{exit, Command};
use todo::{File as TodoFile, TaskGroup}; use todo::{File as TodoFile, TaskGroup};
use crate::file::{extract_sections, process_doc_tree};
fn main() { fn main() {
// setup // setup
let args = Args::parse(); let args = Args::parse();
@@ -48,6 +51,15 @@ fn main() {
} }
} }
if args.gen_config {
let buf = match serde_json::to_string_pretty(&Config::default()) {
Ok(text) => text,
_ => panic!("Could not generate config text"),
};
println!("{}", buf);
return;
}
// set witch config file to load // set witch config file to load
let cfg_file = match args.config { let cfg_file = match args.config {
Some(file) => file, Some(file) => file,
@@ -121,6 +133,17 @@ fn main() {
let current_file = match latest_file { let current_file = match latest_file {
// copy old file if the user specifies today's notes but it does not exist // copy old file if the user specifies today's notes but it does not exist
Some(todo_file) if todo_file.date < today && args.previous == 0 => { Some(todo_file) if todo_file.date < today && args.previous == 0 => {
let mut extension_options = Extension::default();
extension_options.tasklist = true;
let mut parse_options = Parse::default();
parse_options.relaxed_tasklist_matching = true;
let options = &Options {
extension: extension_options,
parse: parse_options,
..Options::default()
};
let sections = &cfg.sections; let sections = &cfg.sections;
log::info!("looking for sections: {:?}", sections); log::info!("looking for sections: {:?}", sections);
let arena = Arena::new(); let arena = Arena::new();
@@ -131,29 +154,27 @@ fn main() {
"loading and parsing file: {}", "loading and parsing file: {}",
todo_file.file.to_string_lossy() todo_file.file.to_string_lossy()
); );
let contents = file::load_file(&todo_file); let contents = file::load_file(&todo_file);
let root = file::parse_todo_file(&contents, &arena); let root = comrak::parse_document(&arena, &contents, options);
root root
}; };
log::trace!("file loaded"); log::trace!("file loaded");
// extract sections specified in config
let groups = file::extract_secitons(root, sections); let sect = extract_sections(root, &sections);
log::trace!("sections extracted"); let date = format!("{}-{:02}-{:02}", today.year(), today.month(), today.day());
// create new sections and generate empty sections for any that are missing
let level = groups.values().map(|group| group.level).min().unwrap_or(2);
let data = sections
.iter()
.map(|section| match groups.get(section) {
Some(group) => group.clone(),
None => TaskGroup::empty(section.to_string(), level),
})
.collect();
// generate string for new file and write to filesystem // generate string for new file and write to filesystem
let content = file::generate_file_content(&data, &today); let new_doc = file::create_new_doc(&arena, &date, sect);
process_doc_tree(root, &date, &sections);
let mut new_content = String::new();
format_commonmark(new_doc, options, &mut new_content);
let file_path = file::get_filepath(&data_dir, &today); let file_path = file::get_filepath(&data_dir, &today);
log::info!("writing to file: {}", file_path.to_string_lossy()); log::info!("writing to file: {}", file_path.to_string_lossy());
file::write_file(&file_path, &content); file::write_file(&file_path, &new_content);
// return file name // return file name
file_path file_path
} }

View File

@@ -1,7 +1,6 @@
use std::borrow::Borrow; use std::borrow::Borrow;
use comrak::nodes::AstNode; use comrak::nodes::{AstNode, NodeTaskItem, NodeValue};
use comrak::nodes::NodeValue;
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub struct TaskGroup { pub struct TaskGroup {
@@ -46,7 +45,7 @@ impl Task {
for child in node.children() { for child in node.children() {
let child_data_ref = child.data.borrow(); let child_data_ref = child.data.borrow();
let t = match &child_data_ref.borrow().value { let t = match &child_data_ref.borrow().value {
NodeValue::Text(contents) => contents.clone(), NodeValue::Text(contents) => contents.to_string(),
NodeValue::Emph if child.first_child().is_some() => { NodeValue::Emph if child.first_child().is_some() => {
format!("*{}*", Self::extract_text(child.first_child().unwrap())?) format!("*{}*", Self::extract_text(child.first_child().unwrap())?)
} }
@@ -101,8 +100,14 @@ impl<'a> TryFrom<&'a AstNode<'a>> for Task {
.ok_or(TaskError::ParsingError("No childern of node found"))?, .ok_or(TaskError::ParsingError("No childern of node found"))?,
)?; )?;
let status = match ch { let status = match ch {
Some(c) if c == 'x' || c == 'X' => Status::Done(c), NodeTaskItem {
Some(c) => Status::Todo(c), symbol: Some(c),
symbol_sourcepos: _,
} if c == 'x' || c == 'X' => Status::Done(c),
NodeTaskItem {
symbol: Some(c),
symbol_sourcepos: _,
} => Status::Todo(c),
_ => Status::Empty, _ => Status::Empty,
}; };
let subtasks = node let subtasks = node