diff options
author | Minijackson <minijackson@riseup.net> | 2021-11-28 16:35:37 +0100 |
---|---|---|
committer | Minijackson <minijackson@riseup.net> | 2021-11-28 16:35:37 +0100 |
commit | e1d7691ebd38e27b14f2549e01e59c08ea84d821 (patch) | |
tree | 362af8e5f606ad0a8eff910fc6aff5d0d57f82a6 | |
parent | 17b10fab16bc5df3a969826150e92f50e88a99b9 (diff) | |
download | pandoc-docbook-e1d7691ebd38e27b14f2549e01e59c08ea84d821.tar.gz pandoc-docbook-e1d7691ebd38e27b14f2549e01e59c08ea84d821.zip |
filters: relativize links when inside inline element
-rw-r--r-- | src/filters.rs | 18 |
1 files changed, 14 insertions, 4 deletions
diff --git a/src/filters.rs b/src/filters.rs index 8dbe578..9e37371 100644 --- a/src/filters.rs +++ b/src/filters.rs | |||
@@ -1,6 +1,6 @@ | |||
1 | use std::{collections::HashMap, path::Path}; | 1 | use std::{collections::HashMap, path::Path}; |
2 | 2 | ||
3 | use log::trace; | 3 | use log::{trace, warn}; |
4 | use pandoc_ast::MutVisitor; | 4 | use pandoc_ast::MutVisitor; |
5 | 5 | ||
6 | // If a link points to `./a/b/c.ext`, and a file in the output directory `pdbook/./a/b/c.html` | 6 | // If a link points to `./a/b/c.ext`, and a file in the output directory `pdbook/./a/b/c.html` |
@@ -13,10 +13,13 @@ pub struct RelativizeUrls<'a> { | |||
13 | } | 13 | } |
14 | 14 | ||
15 | impl<'a> pandoc_ast::MutVisitor for RelativizeUrls<'a> { | 15 | impl<'a> pandoc_ast::MutVisitor for RelativizeUrls<'a> { |
16 | fn walk_inline(&mut self, inline: &mut pandoc_ast::Inline) { | 16 | fn visit_inline(&mut self, inline: &mut pandoc_ast::Inline) { |
17 | let link = match inline { | 17 | let link = match inline { |
18 | pandoc_ast::Inline::Link(_, _, target) => &mut target.0, | 18 | pandoc_ast::Inline::Link(_, _, target) => &mut target.0, |
19 | _ => return, | 19 | _ => { |
20 | self.walk_inline(inline); | ||
21 | return; | ||
22 | } | ||
20 | }; | 23 | }; |
21 | 24 | ||
22 | if link.starts_with('#') || link.contains("://") { | 25 | if link.starts_with('#') || link.contains("://") { |
@@ -26,6 +29,7 @@ impl<'a> pandoc_ast::MutVisitor for RelativizeUrls<'a> { | |||
26 | let link_path = self.source_dir.join(&link); | 29 | let link_path = self.source_dir.join(&link); |
27 | 30 | ||
28 | if link_path.is_absolute() { | 31 | if link_path.is_absolute() { |
32 | warn!("Link is absolute: '{}'", link_path.display()); | ||
29 | return; | 33 | return; |
30 | } | 34 | } |
31 | 35 | ||
@@ -46,7 +50,13 @@ impl<'a> pandoc_ast::MutVisitor for RelativizeUrls<'a> { | |||
46 | .expect("Path constructed from UTF-8 valid strings in not UTF-8 valid") | 50 | .expect("Path constructed from UTF-8 valid strings in not UTF-8 valid") |
47 | .to_string(); | 51 | .to_string(); |
48 | 52 | ||
49 | trace!("Relativizing link '{}' -> into '{}'", link_path.display(), link); | 53 | trace!( |
54 | "Relativizing link '{}' -> into '{}'", | ||
55 | link_path.display(), | ||
56 | link | ||
57 | ); | ||
58 | } else { | ||
59 | warn!("Could not relativize link '{}'", link_path.display()); | ||
50 | } | 60 | } |
51 | } | 61 | } |
52 | } | 62 | } |