Compare commits
3 commits
70d5aa194b
...
1096dd1db8
Author | SHA1 | Date | |
---|---|---|---|
1096dd1db8 | |||
a5016082fc | |||
2edc3082d3 |
2 changed files with 26 additions and 18 deletions
|
@ -97,14 +97,19 @@ fn format_attachments(depth: usize, attachments: &[Attachment]) -> String {
|
||||||
// line, breaking the desired formatting
|
// line, breaking the desired formatting
|
||||||
fn apply_block_quote<S: AsRef<str>>(depth: usize, content: S) -> String {
|
fn apply_block_quote<S: AsRef<str>>(depth: usize, content: S) -> String {
|
||||||
let prefix = quote_prefix(depth);
|
let prefix = quote_prefix(depth);
|
||||||
|
// TODO use OnceLock
|
||||||
let empty = Regex::new("\n\\s").expect("Failed to compiled regex for nested empty lines!");
|
let empty = Regex::new("\n\\s").expect("Failed to compiled regex for nested empty lines!");
|
||||||
let non_empty =
|
let non_empty =
|
||||||
Regex::new("\n(?P<initial>[^>\\s])").expect("Failed to compile regex for nested lines!");
|
Regex::new("\n(?P<initial>[^>\\s])").expect("Failed to compile regex for nested lines!");
|
||||||
|
// TODO break into separate function
|
||||||
// parsing some multiline content adds paragraph tags, convert those back to new lines to apply
|
// parsing some multiline content adds paragraph tags, convert those back to new lines to apply
|
||||||
// the block quote level to each line
|
// the block quote level to each line
|
||||||
let content = content.as_ref().replace("</p><p>", "\n\n");
|
let content = content.as_ref().replace("</p><p>", "\n\n");
|
||||||
let content = content.replace("<p>", "");
|
let content = content.replace("<p>", "");
|
||||||
let content = content.replace("</p>", "");
|
let content = content.replace("</p>", "");
|
||||||
|
// TODO refactor into separate function
|
||||||
|
let content = content.replace("\\*", "*");
|
||||||
|
let content = content.replace(":totoro:", "\\:totoro\\:");
|
||||||
// replace separately to avoid trailing spaces when replacing empty lines with the prefix
|
// replace separately to avoid trailing spaces when replacing empty lines with the prefix
|
||||||
let content = empty.replace_all(content.as_ref(), format!("\n{}\n", prefix.trim()));
|
let content = empty.replace_all(content.as_ref(), format!("\n{}\n", prefix.trim()));
|
||||||
let content = non_empty.replace_all(&content, |c: &Captures| {
|
let content = non_empty.replace_all(&content, |c: &Captures| {
|
||||||
|
|
39
src/main.rs
39
src/main.rs
|
@ -127,7 +127,7 @@ async fn main() -> Result<()> {
|
||||||
Ok(exists) if exists => {
|
Ok(exists) if exists => {
|
||||||
debug!("Appending {}", output);
|
debug!("Appending {}", output);
|
||||||
let mut file = File::options().append(true).open(&output)?;
|
let mut file = File::options().append(true).open(&output)?;
|
||||||
file.write("\n".as_bytes())?;
|
file.write("\n\n".as_bytes())?;
|
||||||
file
|
file
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
|
@ -138,30 +138,23 @@ async fn main() -> Result<()> {
|
||||||
.open(&output)
|
.open(&output)
|
||||||
.with_context(|| format!("Failed to create {}", output))?;
|
.with_context(|| format!("Failed to create {}", output))?;
|
||||||
file.write(format!("# {}\n\n", day.end.format("%Y-%m-%d")).as_bytes())?;
|
file.write(format!("# {}\n\n", day.end.format("%Y-%m-%d")).as_bytes())?;
|
||||||
// TODO extract into a more general function
|
|
||||||
let week_ago = day.end - Duration::days(7);
|
// TODO move to separate function
|
||||||
// TODO check if the file exists
|
file.write(create_back_link(&day.end, "[One week ago](diary:{})\n", 7).as_bytes())?;
|
||||||
file.write(
|
file.write(
|
||||||
format!("[One week ago](diary:{})\n", week_ago.format("%Y-%m-%d")).as_bytes(),
|
create_back_link(&day.end, "[One month ago](diary:{})\n", 30).as_bytes(),
|
||||||
)?;
|
)?;
|
||||||
let month_ago = day.end - Duration::days(30);
|
|
||||||
// TODO check if the file exists
|
|
||||||
file.write(
|
file.write(
|
||||||
format!("[One month ago](diary:{})\n", month_ago.format("%Y-%m-%d")).as_bytes(),
|
create_back_link(&day.end, "[Six months ago](diary:{})\n", 6 * 30).as_bytes(),
|
||||||
)?;
|
)?;
|
||||||
let half_year_ago = day.end - Duration::days(6 * 30);
|
|
||||||
// TODO check if the file exists
|
|
||||||
file.write(
|
file.write(
|
||||||
format!(
|
create_back_link(&day.end, "[One year ago](diary:{})\n", 365).as_bytes(),
|
||||||
"[Six months ago](diary:{})\n",
|
|
||||||
half_year_ago.format("%Y-%m-%d")
|
|
||||||
)
|
|
||||||
.as_bytes(),
|
|
||||||
)?;
|
)?;
|
||||||
let year_ago = day.end - Duration::days(365);
|
|
||||||
// TODO check if the file exists
|
|
||||||
file.write(
|
file.write(
|
||||||
format!("[One year ago](diary:{})\n", year_ago.format("%Y-%m-%d")).as_bytes(),
|
create_back_link(&day.end, "[Two years ago](diary:{})\n", 365 * 2).as_bytes(),
|
||||||
|
)?;
|
||||||
|
file.write(
|
||||||
|
create_back_link(&day.end, "[Three years ago](diary:{})\n", 365 * 3).as_bytes(),
|
||||||
)?;
|
)?;
|
||||||
file
|
file
|
||||||
}
|
}
|
||||||
|
@ -175,6 +168,16 @@ async fn main() -> Result<()> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn create_back_link(day_end: &DateTime<Local>, anchor_text: &str, ago: i64) -> String {
|
||||||
|
let prior_date = *day_end - Duration::days(ago);
|
||||||
|
// TODO check if the file exists
|
||||||
|
format!(
|
||||||
|
"[{}](diary:{})\n",
|
||||||
|
anchor_text,
|
||||||
|
prior_date.format("%Y-%m-%d")
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
enum NextIter {
|
enum NextIter {
|
||||||
Skip,
|
Skip,
|
||||||
Stop,
|
Stop,
|
||||||
|
|
Loading…
Reference in a new issue