Compare commits
14 Commits
v0.1.3
...
workflow-t
| Author | SHA1 | Date | |
|---|---|---|---|
| 587a0be9cf | |||
| 7c499c1e93 | |||
| 03e830575d | |||
| 4733dd12ed | |||
| 72973791db | |||
| 59fba3ef7a | |||
| 807d75ac31 | |||
| e861bf99f7 | |||
| 887eb954a8 | |||
| ce03c9c171 | |||
| 181be6c4e3 | |||
| 84d7ba45d3 | |||
| 9cc32fe65a | |||
| 319cbbef4d |
21
.gitea/workflows/build.yaml
Normal file
21
.gitea/workflows/build.yaml
Normal file
@@ -0,0 +1,21 @@
|
||||
name: Build
|
||||
on:
|
||||
release:
|
||||
types:
|
||||
- published
|
||||
pull_request:
|
||||
|
||||
jobs:
|
||||
test:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v5
|
||||
- uses: dtolnay/rust-toolchain@stable
|
||||
- name: Run tests
|
||||
run: cargo test --release
|
||||
- name: Run build
|
||||
run: cargo build --release --target x86_64-unknown-linux-gnu
|
||||
- uses: https://github.com/christopherHX/gitea-upload-artifact@v4
|
||||
with:
|
||||
name: rusty-task
|
||||
path: target/x86_64-unknown-linux-gnu/release/rusty-tasks
|
||||
11
.gitea/workflows/test.yaml
Normal file
11
.gitea/workflows/test.yaml
Normal 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
|
||||
@@ -8,7 +8,7 @@ edition = "2021"
|
||||
[dependencies]
|
||||
chrono = "0.4.26"
|
||||
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"] }
|
||||
regex = "1.8.4"
|
||||
serde = { version = "1.0.164", features = ["serde_derive"] }
|
||||
@@ -16,3 +16,4 @@ serde_json = "1.0.97"
|
||||
resolve-path = "0.1.0"
|
||||
simple_logger = "4.3.3"
|
||||
log = "0.4.21"
|
||||
indexmap = "2.2.6"
|
||||
|
||||
8
TODO.md
Normal file
8
TODO.md
Normal file
@@ -0,0 +1,8 @@
|
||||
- [ ] Obsidian properties
|
||||
- [ ] encoding in YAML (using Serde)
|
||||
- [ ] config for default properties
|
||||
- [ ] formatting for properties such as dates
|
||||
- [x] update rendering to use comrak (it's been update)
|
||||
|
||||
|
||||
|
||||
292
src/file/mod.rs
292
src/file/mod.rs
@@ -2,9 +2,11 @@ use crate::todo::{File as TodoFile, Status as TaskStatus};
|
||||
use crate::NaiveDate;
|
||||
use crate::TaskGroup;
|
||||
use chrono::Datelike;
|
||||
use comrak::nodes::{AstNode, NodeValue};
|
||||
use comrak::parse_document;
|
||||
use comrak::{Arena, ComrakExtensionOptions, ComrakOptions, ComrakParseOptions};
|
||||
use comrak::nodes::{Ast, AstNode, LineColumn, NodeHeading, NodeTaskItem, NodeValue};
|
||||
use comrak::options::{Extension, Parse};
|
||||
use comrak::{parse_document, Arena, Options};
|
||||
use indexmap::IndexMap;
|
||||
use regex::Regex;
|
||||
use std::collections::HashMap;
|
||||
use std::fs::{read, File};
|
||||
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)
|
||||
pub fn parse_todo_file<'a>(contents: &String, 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()
|
||||
pub fn parse_todo_file<'a>(contents: &String, arena: &'a Arena<'a>) -> &'a AstNode<'a> {
|
||||
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()
|
||||
};
|
||||
parse_document(arena, contents, options)
|
||||
}
|
||||
@@ -117,10 +119,185 @@ pub fn extract_secitons<'a>(
|
||||
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, §ion);
|
||||
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)]
|
||||
mod test {
|
||||
use super::*;
|
||||
use crate::todo::{Status, Task};
|
||||
use crate::todo::Status;
|
||||
use crate::todo::Task;
|
||||
use comrak::format_commonmark;
|
||||
|
||||
#[test]
|
||||
fn test_extract_sections() {
|
||||
@@ -278,4 +455,89 @@ mod test {
|
||||
";
|
||||
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
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
47
src/main.rs
47
src/main.rs
@@ -5,10 +5,11 @@ mod logging;
|
||||
mod todo;
|
||||
|
||||
use chrono::naive::NaiveDate;
|
||||
use chrono::{Local, TimeDelta};
|
||||
use chrono::{Datelike, Local, TimeDelta};
|
||||
use clap::Parser;
|
||||
use cli::Args;
|
||||
use comrak::Arena;
|
||||
use comrak::options::{Extension, Parse};
|
||||
use comrak::{format_commonmark, Arena, Options};
|
||||
use config::Config;
|
||||
use log;
|
||||
use logging::get_logging_level;
|
||||
@@ -19,7 +20,8 @@ use std::path::Path;
|
||||
use std::process::Command;
|
||||
use todo::{File as TodoFile, TaskGroup};
|
||||
|
||||
// TODO: add options to use specific date instead of args.previous
|
||||
use crate::file::{extract_sections, process_doc_tree};
|
||||
|
||||
fn main() {
|
||||
// setup
|
||||
let args = Args::parse();
|
||||
@@ -122,6 +124,17 @@ fn main() {
|
||||
let current_file = match latest_file {
|
||||
// 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 => {
|
||||
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;
|
||||
log::info!("looking for sections: {:?}", sections);
|
||||
let arena = Arena::new();
|
||||
@@ -132,29 +145,27 @@ fn main() {
|
||||
"loading and parsing file: {}",
|
||||
todo_file.file.to_string_lossy()
|
||||
);
|
||||
|
||||
let contents = file::load_file(&todo_file);
|
||||
let root = file::parse_todo_file(&contents, &arena);
|
||||
let root = comrak::parse_document(&arena, &contents, options);
|
||||
root
|
||||
};
|
||||
log::trace!("file loaded");
|
||||
// extract sections specified in config
|
||||
let groups = file::extract_secitons(root, sections);
|
||||
log::trace!("sections extracted");
|
||||
// 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();
|
||||
|
||||
let sect = extract_sections(root, §ions);
|
||||
let date = format!("{}-{:02}-{:02}", today.year(), today.month(), today.day());
|
||||
|
||||
// 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, §ions);
|
||||
|
||||
let mut new_content = String::new();
|
||||
format_commonmark(new_doc, options, &mut new_content);
|
||||
|
||||
let file_path = file::get_filepath(&data_dir, &today);
|
||||
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
|
||||
file_path
|
||||
}
|
||||
|
||||
@@ -2,4 +2,4 @@ mod file;
|
||||
mod tasks;
|
||||
|
||||
pub use file::File;
|
||||
pub use tasks::{Status, TaskGroup};
|
||||
pub use tasks::{Status, Task, TaskGroup};
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
use std::borrow::Borrow;
|
||||
|
||||
use comrak::nodes::AstNode;
|
||||
use comrak::nodes::NodeValue;
|
||||
use comrak::nodes::{AstNode, NodeTaskItem, NodeValue};
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct TaskGroup {
|
||||
@@ -46,7 +45,7 @@ impl Task {
|
||||
for child in node.children() {
|
||||
let child_data_ref = child.data.borrow();
|
||||
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() => {
|
||||
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"))?,
|
||||
)?;
|
||||
let status = match ch {
|
||||
Some(c) if c == 'x' || c == 'X' => Status::Done(c),
|
||||
Some(c) => Status::Todo(c),
|
||||
NodeTaskItem {
|
||||
symbol: Some(c),
|
||||
symbol_sourcepos: _,
|
||||
} if c == 'x' || c == 'X' => Status::Done(c),
|
||||
NodeTaskItem {
|
||||
symbol: Some(c),
|
||||
symbol_sourcepos: _,
|
||||
} => Status::Todo(c),
|
||||
_ => Status::Empty,
|
||||
};
|
||||
let subtasks = node
|
||||
|
||||
Reference in New Issue
Block a user