aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJames Campos <james.r.campos@gmail.com>2020-09-23 18:40:15 -0700
committerJames Campos <james.r.campos@gmail.com>2020-09-23 18:40:15 -0700
commit2a48081b1a683b6ac22fe5bd706b619be8520872 (patch)
treee35c951a118ab7aebb1455d1e89c2d840d697a4e
parent139c3d96fc7ee8cd29f2c704913e8f57ea84f867 (diff)
downloadbk-2a48081b1a683b6ac22fe5bd706b619be8520872.tar.gz
render external links as plain text
-rw-r--r--src/epub.rs32
-rw-r--r--src/view.rs11
2 files changed, 17 insertions, 26 deletions
diff --git a/src/epub.rs b/src/epub.rs
index 25d4cd6..b3e6c49 100644
--- a/src/epub.rs
+++ b/src/epub.rs
@@ -153,11 +153,14 @@ impl Chapter {
fn render(&mut self, n: Node, open: Attribute, close: Attribute) {
self.state.set(open);
self.attrs.push((self.text.len(), open, self.state));
+ self.render_text(n);
+ self.state.unset(open);
+ self.attrs.push((self.text.len(), close, self.state));
+ }
+ fn render_text(&mut self, n: Node) {
for child in n.children() {
render(child, self);
}
- self.state.unset(open);
- self.attrs.push((self.text.len(), close, self.state));
}
}
@@ -186,9 +189,14 @@ fn render(n: Node, c: &mut Chapter) {
"img" => c.text.push_str("\n[IMG]\n"),
"a" => {
if let Some(url) = n.attribute("href") {
- let start = c.text.len();
- c.render(n, Attribute::Underlined, Attribute::NoUnderline);
- c.links.push((start, c.text.len(), url.to_string()));
+ if url.starts_with("http") {
+ // TODO open in browser
+ c.render_text(n)
+ } else {
+ let start = c.text.len();
+ c.render(n, Attribute::Underlined, Attribute::NoUnderline);
+ c.links.push((start, c.text.len(), url.to_string()));
+ }
}
}
"em" => c.render(n, Attribute::Italic, Attribute::NoItalic),
@@ -200,23 +208,15 @@ fn render(n: Node, c: &mut Chapter) {
}
"blockquote" | "p" | "tr" => {
c.text.push('\n');
- for child in n.children() {
- render(child, c);
- }
+ c.render_text(n);
c.text.push('\n');
}
"li" => {
c.text.push_str("\n- ");
- for child in n.children() {
- render(child, c);
- }
+ c.render_text(n);
c.text.push('\n');
}
- _ => {
- for child in n.children() {
- render(child, c);
- }
- }
+ _ => c.render_text(n),
}
}
diff --git a/src/view.rs b/src/view.rs
index 11eaccd..7593eea 100644
--- a/src/view.rs
+++ b/src/view.rs
@@ -244,16 +244,7 @@ impl View for Page {
if let Ok(i) = r {
let url = &c.links[i].2;
- let mut link = bk.links.get(url);
- // FIXME hack
- if link.is_none() {
- let url = url.split('#').next().unwrap();
- link = bk.links.get(url);
- if link.is_none() {
- return;
- }
- }
- let &(chapter, byte) = link.unwrap();
+ let &(chapter, byte) = bk.links.get(url).expect(url);
let line = get_line(&bk.chapters[chapter].lines, byte);
bk.jump((chapter, line));
}