Repository navigation
Expand file tree
/
Copy pathmain.rs
More file actions
139 lines (118 loc) · 5.22 KB
/
Copy pathmain.rs
File metadata and controls
139 lines (118 loc) · 5.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use std::env;
use std::fs;
use std::path::{Path, PathBuf};
use std::collections::HashMap;
use std::error::Error;
mod slug;
mod file_utils;
use file_utils::process_file;
mod handle_link_index;
use handle_link_index::convert_to_lower_case;
mod enrich_with_blog;
mod enrich_with_book;
mod enrich_with_memories;
mod merge_search_index;
mod note_status;
mod svg_generator;
use svg_generator::{ImageConfig, generate_og_image, extract_title_from_md};
mod base_parser;
mod base_query;
mod base_renderer;
fn main() -> Result<(), Box<dyn Error>> {
let args: Vec<String> = env::args().collect();
if args.len() > 1 && args[1] == "convert_to_lower_case" {
//handle hugo linkindexes
println!("Handling link indexes");
let link_index_path = Path::new("assets/indices/linkIndex.json");
convert_to_lower_case(link_index_path)?;
println!("Handling link indexes: DONE");
} else if args.len() > 1 && args[1] == "enrich-with-blog" {
enrich_with_blog::run()?;
} else if args.len() > 1 && args[1] == "enrich-with-book" {
enrich_with_book::run()?;
} else if args.len() > 1 && args[1] == "enrich-with-memories" {
enrich_with_memories::run()?;
} else if args.len() > 1 && args[1] == "merge-search-index" {
merge_search_index::run()?;
} else if args.len() > 1 && args[1] == "note-status" {
note_status::run()?;
} else {
let second_brain_path = env::var("secondbrain")?;
let public_folder_path_copy = env::var("public_secondbrain")?;
let public_brain_image_path = env::var("public_secondbrain")?;
let mut images_map: HashMap<String, PathBuf> = HashMap::new();
build_images_map(Path::new(&second_brain_path), &mut images_map)?;
match visit_dirs(Path::new(&second_brain_path), &public_folder_path_copy, &public_brain_image_path, &images_map, &second_brain_path) {
Ok(_) => (),
Err(e) => println!("An error occurred: {}", e),
}
// Needs every note on disk: an alias can only be checked against the
// other notes' URLs once they all exist.
file_utils::resolve_alias_collisions(&public_folder_path_copy)?;
}
Ok(())
}
fn visit_dirs(dir: &Path, public_folder: &str, public_brain_image_path: &str, images_map: &HashMap<String, PathBuf>, vault_root: &str) -> std::io::Result<()> {
// println!("Visiting directory: {}", dir.display());
if dir.is_dir() {
for entry in fs::read_dir(dir)? {
let entry = entry?;
let path = entry.path();
if path.is_dir() {
// ignore symlinked folder that could contain #publish that breaks the build
if path.ends_with("Book") || path.ends_with("Blog") {
println!("Visiting directory: {}", path.display());
continue;
}
visit_dirs(&path, public_folder, public_brain_image_path, images_map, vault_root)?;
} else {
////DEBUG:
////skip if file is not "Folder Structure PARA.md"
//if let Some(file_name) = path.file_name() {
// if file_name != "Data Orchestrators.md" {
// continue;
// }
//}
if let Some(extension) = path.extension() {
if extension == "md" {
process_file(&path, public_folder, public_brain_image_path, images_map)?;
} else if extension == "base" {
// Check if BASE file has publish: true property
use crate::base_parser::parse_base_file;
if let Ok(base_file) = parse_base_file(&path) {
if base_file.publish {
// Query from vault_root to get all matching files (published or not)
// The BASE table is a self-contained view that doesn't require
// individual files to be published
if let Err(e) = file_utils::process_base_file(&path, public_folder, &PathBuf::from(vault_root)) {
eprintln!("Error processing BASE file: {}", e);
}
}
}
}
}
}
}
}
Ok(())
}
fn build_images_map(dir: &Path, images_map: &mut HashMap<String, PathBuf>) -> std::io::Result<()> {
if dir.is_dir() {
for entry in fs::read_dir(dir)? {
let entry = entry?;
let path = entry.path();
if path.is_dir() {
build_images_map(&path, images_map)?;
} else {
if let Some(extension) = path.extension() {
if extension == "png" || extension == "jpg" || extension == "jpeg" || extension == "gif" || extension == "webp" || extension == "svg" || extension == "mp4" {
if let Some(file_name) = path.file_name() {
images_map.insert(file_name.to_str().unwrap().to_string(), path.clone());
}
}
}
}
}
}
Ok(())
}