Skip to content

Commit

Permalink
refator: optimise regex parse, and update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
MR-Addict committed Jan 11, 2024
1 parent 0167b8f commit 7310a48
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mdbook-embedify"
version = "0.0.2"
version = "0.1.0"
edition = "2021"
license = "MIT"
readme = "README.md"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ This is a [mdbook](https://rust-lang.github.io/mdBook) preprocessor plugin that

## 1. Installation

First, you need to install mdbook-embedify to your computer.You can install it from crates.io using cargo.
First, you need to install mdbook-embedify to your computer. You can install it from crates.io using cargo.

```sh
cargo install mdbook-embedify
Expand Down
2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ <h1>Mdbook Embedify</h1>
<h2>1. Installation</h2>

<p>
First, you need to install mdbook-embedify to your computer.You can install it from crates.io using cargo.
First, you need to install mdbook-embedify to your computer. You can install it from crates.io using cargo.
</p>

<pre><code>cargo install mdbook-embedify</code></pre>
Expand Down
15 changes: 11 additions & 4 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,17 @@ pub fn render_template(app: &str, placeholders: &[(String, String)]) -> String {
let mut result = template.to_string();
// replace the key with the value
for (key, value) in placeholders {
// replace {key} or {key|default} with value
let pattern = format!(r"\{{{}\|?[^}}]*}}", key);
let re = Regex::new(&pattern).unwrap();
result = re.replace_all(&result, value).to_string();
// replace {key} with value
let re = Regex::new(&format!(r"\{{{}}}", key)).unwrap();
let replaced_result = re.replace_all(&result, value).to_string();
// replacement was successful
if replaced_result != result {
result = replaced_result;
} else {
//replacement does not success, try replace {key|default} with value
let re = Regex::new(&format!(r"\{{{}\|[^}}]*}}", key)).unwrap();
result = re.replace_all(&result, value).to_string();
}
}
// check if there are any placeholders left, this makes the process faster
if result.contains('{') && result.contains('}') {
Expand Down

0 comments on commit 7310a48

Please sign in to comment.