Skip to content

Commit

Permalink
Change Request 3
Browse files Browse the repository at this point in the history
Signed-off-by: Hollow Man <[email protected]>
  • Loading branch information
HollowMan6 committed Nov 30, 2023
1 parent b6b7fb5 commit 7c25eff
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 28 deletions.
24 changes: 5 additions & 19 deletions src/renderer/html_handlebars/hbs_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,7 @@ fn make_data(
Ok(data)
}

/// Goes through part of the rendered print page HTML,
/// Go through the rendered print page HTML,
/// add path id prefix to all the elements id as well as footnote links.
fn build_print_element_id(html: &str, path_id: &str) -> String {
static ALL_ID: Lazy<Regex> = Lazy::new(|| Regex::new(r#"(<[^>]*?id=")([^"]+?)""#).unwrap());
Expand All @@ -842,27 +842,13 @@ fn build_print_element_id(html: &str, path_id: &str) -> String {
.unwrap()
});

if path_id.is_empty() {
return html.to_string();
}

let temp_html = ALL_ID
.replace_all(html, |caps: &Captures<'_>| {
let mut fixed = String::new();
fixed.push_str(&path_id);
fixed.push_str("-");
fixed.push_str(&caps[2]);
format!("{}{}\"", &caps[1], fixed)
})
.into_owned();
let temp_html = ALL_ID.replace_all(html, |caps: &Captures<'_>| {
format!("{}{}-{}\"", &caps[1], path_id, &caps[2])
});

FOOTNOTE_ID
.replace_all(&temp_html, |caps: &Captures<'_>| {
let mut fixed = String::new();
fixed.push_str(&path_id);
fixed.push_str("-");
fixed.push_str(&caps[2]);
format!("{}{}\"", &caps[1], fixed)
format!("{}{}-{}\"", &caps[1], path_id, &caps[2])
})
.into_owned()
}
Expand Down
22 changes: 13 additions & 9 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ fn adjust_links<'a>(
static HTML_MD_LINK: Lazy<Regex> =
Lazy::new(|| Regex::new(r"(?P<link>.*)\.(html|md)(?P<anchor>#.*)?").unwrap());

fn add_base(mut fixed_link: String, path: Option<&Path>) -> String {
fn add_base(path: Option<&Path>) -> String {
let mut fixed_link = String::new();
if let Some(path) = path {
let base = path
.parent()
Expand Down Expand Up @@ -240,8 +241,11 @@ fn adjust_links<'a>(
if SCHEME_LINK.is_match(&dest) {
return dest;
}

let closure = |path: Option<&Path>| add_base(path);

// This is a relative link, adjust it as necessary.
let mut fixed_link = add_base(String::new(), path);
let mut fixed_link = closure(path);
fixed_link.push_str(&dest);
CowStr::from(fixed_link)
}
Expand Down Expand Up @@ -276,8 +280,10 @@ fn adjust_links<'a>(
return dest;
}

let closure = |path: Option<&Path>| add_base(path);

// This is a relative link, adjust it as necessary.
let mut fixed_link = add_base(String::new(), path);
let mut fixed_link = closure(path);

if let Some(caps) = HTML_MD_LINK.captures(&dest) {
fixed_link.push_str(&caps["link"]);
Expand Down Expand Up @@ -321,12 +327,10 @@ fn adjust_links<'a>(
static IMG_LINK: Lazy<Regex> =
Lazy::new(|| Regex::new(r#"(<img [^>]*?src=")([^"]+?)""#).unwrap());

let temp_html = IMG_LINK
.replace_all(&html, |caps: &regex::Captures<'_>| {
let fixed = fix_resource_links(caps[2].into(), path);
format!("{}{}\"", &caps[1], fixed)
})
.into_owned();
let temp_html = IMG_LINK.replace_all(&html, |caps: &regex::Captures<'_>| {
let fixed = fix_resource_links(caps[2].into(), path);
format!("{}{}\"", &caps[1], fixed)
});

A_LINK
.replace_all(&temp_html, |caps: &regex::Captures<'_>| {
Expand Down

0 comments on commit 7c25eff

Please sign in to comment.