Skip to content

Commit

Permalink
implement transclusion (#61)
Browse files Browse the repository at this point in the history
* implement transclusion

* fix regex

* update regex

---------

Co-authored-by: Ben Stern <[email protected]>
  • Loading branch information
therealbstern and Ben Stern authored Mar 11, 2024
1 parent 7f6a1b8 commit 8a99558
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
11 changes: 6 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
23 changes: 22 additions & 1 deletion src/wiki.rs
Original file line number Diff line number Diff line change
@@ -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::*;
Expand Down Expand Up @@ -210,7 +215,23 @@ impl Wiki {
}

pub fn read_file(&self, file_path: &[&str]) -> Result<Vec<u8>, 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(
Expand Down

0 comments on commit 8a99558

Please sign in to comment.