From 9c074d5ca1a8448d1237d3be9ed0c9050f2cdc28 Mon Sep 17 00:00:00 2001 From: Ben Stern Date: Sun, 25 Feb 2024 13:11:10 -0500 Subject: [PATCH 1/3] implement transclusion --- Cargo.lock | 11 ++++++----- Cargo.toml | 1 + src/wiki.rs | 23 ++++++++++++++++++++++- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e8896ca..0f95304 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1398,13 +1398,13 @@ dependencies = [ [[package]] name = "regex" -version = "1.10.2" +version = "1.10.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "380b951a9c5e80ddfd6136919eef32310721aa4aacd4889a8d39124b026ab343" +checksum = "b62dbe01f0b06f9d8dc7d49e05a0785f153b00b2c227856282f671e0318c9b15" dependencies = [ "aho-corasick", "memchr", - "regex-automata 0.4.3", + "regex-automata 0.4.5", "regex-syntax 0.8.2", ] @@ -1419,9 +1419,9 @@ dependencies = [ [[package]] name = "regex-automata" -version = "0.4.3" +version = "0.4.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5f804c7828047e88b2d32e2d7fe5a105da8ee3264f01902f796c8e067dc2483f" +checksum = "5bb987efffd3c6d0d8f5f89510bb458559eab11e4f869acb20bf845e016259cd" dependencies = [ "aho-corasick", "memchr", @@ -1691,6 +1691,7 @@ dependencies = [ "pretty_env_logger", "pulldown-cmark", "rand", + "regex", "ring", "rocket", "serde", diff --git a/Cargo.toml b/Cargo.toml index 5c8f0c0..cbd738a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,6 +27,7 @@ once_cell = "1.18.0" pretty_env_logger = "0.5" pulldown-cmark = { version = "0.9", default-features = false } rand = "0.8.5" +regex = { version = "1.10.3", features = ["std"] } rocket = "0.5.0" serde = { version = "1.0", features = ["derive"] } strum = "0.25" diff --git a/src/wiki.rs b/src/wiki.rs index fc8c81d..e5c0f2a 100644 --- a/src/wiki.rs +++ b/src/wiki.rs @@ -1,5 +1,10 @@ +use std::str; use std::sync::Arc; +use lazy_static::lazy_static; + +use regex::bytes::{Captures, Regex}; + use tantivy::collector::TopDocs; use tantivy::query::QueryParser; use tantivy::schema::*; @@ -210,7 +215,23 @@ impl Wiki { } pub fn read_file(&self, file_path: &[&str]) -> Result, MyError> { - self.0.repository.read_file(file_path) + lazy_static! { + static ref RE: Regex = Regex::new(r"\[\[(.*?)\]\]").unwrap(); + } + let mut res = self.0.repository.read_file(file_path); + if let Ok(mut bytes) = res { + while RE.is_match(&bytes) { + bytes = RE.replace(&bytes, |caps: &Captures| { + if let Ok(filename) = str::from_utf8(&caps[1]) { + self.0.repository.read_file(&[filename]).unwrap_or(b"**read error**".to_vec()) + } else { + b"**conversion error**".to_vec() + } + }).to_vec(); + } + res = Ok(bytes) + } + res } pub fn write_file( From a881f69f8389e5825c3f7df7c87928e53624a02d Mon Sep 17 00:00:00 2001 From: Ben Stern Date: Sun, 25 Feb 2024 13:15:51 -0500 Subject: [PATCH 2/3] fix regex --- src/wiki.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wiki.rs b/src/wiki.rs index e5c0f2a..9372158 100644 --- a/src/wiki.rs +++ b/src/wiki.rs @@ -216,7 +216,7 @@ impl Wiki { pub fn read_file(&self, file_path: &[&str]) -> Result, MyError> { lazy_static! { - static ref RE: Regex = Regex::new(r"\[\[(.*?)\]\]").unwrap(); + static ref RE: Regex = Regex::new(r"\[\[(.+?)\]\]").unwrap(); } let mut res = self.0.repository.read_file(file_path); if let Ok(mut bytes) = res { From 6c53e73af9a57f7f891e6b593d5cb924cd07a555 Mon Sep 17 00:00:00 2001 From: Ben Stern Date: Mon, 4 Mar 2024 12:35:38 -0500 Subject: [PATCH 3/3] update regex --- src/wiki.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wiki.rs b/src/wiki.rs index 9372158..5318da2 100644 --- a/src/wiki.rs +++ b/src/wiki.rs @@ -216,7 +216,7 @@ impl Wiki { pub fn read_file(&self, file_path: &[&str]) -> Result, MyError> { lazy_static! { - static ref RE: Regex = Regex::new(r"\[\[(.+?)\]\]").unwrap(); + static ref RE: Regex = Regex::new(r"^{{(.+?)}}$").unwrap(); } let mut res = self.0.repository.read_file(file_path); if let Ok(mut bytes) = res {