-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fb02d44
commit 52312ec
Showing
4 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
source "https://rubygems.org" | ||
ruby "3.3.5" | ||
gem 'optparse' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
GEM | ||
remote: https://rubygems.org/ | ||
specs: | ||
optparse (0.3.1) | ||
|
||
PLATFORMS | ||
arm64-darwin-23 | ||
ruby | ||
|
||
DEPENDENCIES | ||
optparse | ||
|
||
BUNDLED WITH | ||
2.5.10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
require 'fileutils' | ||
require 'optparse' | ||
require 'date' | ||
require 'yaml' | ||
|
||
# Parse command-line options | ||
options = {} | ||
OptionParser.new do |opts| | ||
opts.banner = "Usage: obsidian-journal-cleaner.rb [options]" | ||
|
||
opts.on("-d", "--dry-run", "Perform a dry run without moving files") do |d| | ||
options[:dry_run] = d | ||
end | ||
|
||
opts.on("-r", "--remove-original", "Remove original files after moving") do |r| | ||
options[:remove_original] = r | ||
end | ||
end.parse! | ||
|
||
# Define source and destination directories | ||
source_dir = 'EG.ObsidianVault/Journals' | ||
destination_dir = 'EG.ObsidianVault/Journals/01 Daily' | ||
|
||
# Iterate through all the files in the source directory | ||
Dir.glob("#{source_dir}/[0-9][0-9][0-9][0-9]/**/*.md").each do |file| | ||
# Extract the date from the file path | ||
date_match = file.match(%r{(\d{4})/(\d{4}-\d{2}-\d{2})\.md}) | ||
next unless date_match | ||
|
||
year = date_match[1] | ||
date_str = date_match[2] | ||
|
||
# Parse the date | ||
date = Date.parse(date_str) | ||
created_at = date.strftime('%Y-%m-%d %H:%M') | ||
weekly = date.strftime('%Y-W%U') | ||
monthly = date.strftime('%Y-%m-M') | ||
yearly = date.strftime('%Y-Y') | ||
date_short = date.strftime('%A %d. %B') | ||
aliases = "#{date_short} #{year}" | ||
journal_start_date = date_str | ||
journal_end_date = date_str | ||
month = date.strftime('%m') | ||
|
||
# Define the properties to add | ||
properties = { | ||
'date' => date_str, | ||
'createdAt' => created_at, | ||
'weekly' => weekly, | ||
'monthly' => [monthly], | ||
'yearly' => yearly, | ||
'dateShort' => date_short, | ||
'aliases' => [aliases], | ||
'journal' => 'personal', | ||
'journal-start-date' => journal_start_date, | ||
'journal-end-date' => journal_end_date, | ||
'journal-section' => 'day' | ||
} | ||
|
||
# Convert properties to YAML front matter | ||
yaml_front_matter = properties.to_yaml | ||
yaml_front_matter = "#{yaml_front_matter}---\n" | ||
|
||
# Construct the new path with month subdirectory | ||
new_path = "#{destination_dir}/#{year}/#{month}/#{date_str}.md" | ||
|
||
if options[:dry_run] | ||
# Print the source and destination paths | ||
puts "Would move: #{file} -> #{new_path}" | ||
else | ||
# Create the destination directory if it doesn't exist | ||
FileUtils.mkdir_p(File.dirname(new_path)) | ||
|
||
# Read the content of the file | ||
content = File.read(file) | ||
|
||
# Prepend the YAML front matter | ||
updated_content = "#{yaml_front_matter}\n## Summary\n#{content}" | ||
|
||
# Write the updated content to the new path | ||
File.write(new_path, updated_content) | ||
|
||
# Remove the original file if the option is set | ||
if options[:remove_original] | ||
FileUtils.rm(file) | ||
end | ||
|
||
puts "Moved and updated: #{file} -> #{new_path}" | ||
end | ||
end |