Skip to content

A fast file manager for Windows and macOS that never freezes: tabs, dual panes, archives as folders, Quick Look-style previews. A File Explorer and Finder alternative written in Rust.

DirectLook no longer closes on a web page: HTML is shown as text with its links (802)

gelenmedyabdc8afd12499
diff --git a/crates/rf-app/src/look/viewers/document.rs b/crates/rf-app/src/look/viewers/document.rs
index 797179e1..6af993b4 100644
--- a/crates/rf-app/src/look/viewers/document.rs
+++ b/crates/rf-app/src/look/viewers/document.rs
@@ -30,7 +30,10 @@ impl Render for DocView {
None => div().size_full().p_6().text_sm().text_color(p.text_secondary).child(t!("Açılıyor…")).into_any_element(),
Some(Err(e)) => div().size_full().p_4().text_color(p.danger).child(crate::i18n::td(e)).into_any_element(),
Some(Ok(Doc::Markdown(md))) => page.child(TextView::markdown("dl-doc", SharedString::from(md.clone()))).into_any_element(),
- Some(Ok(Doc::Html(html))) => page.child(TextView::html("dl-doc", SharedString::from(html.clone()))).into_any_element(),
+ // (as Markdown: the toolkit's HTML parser closes the app on many pages, see `html_page_as_text`, issue 802)
+ Some(Ok(Doc::Html(html))) => {
+ page.child(TextView::markdown("dl-doc", SharedString::from(super::markdown::html_page_as_text(html)))).into_any_element()
+ }
}
}
}
diff --git a/crates/rf-app/src/look/viewers/markdown.rs b/crates/rf-app/src/look/viewers/markdown.rs
index 5d3e1bab..88891010 100644
--- a/crates/rf-app/src/look/viewers/markdown.rs
+++ b/crates/rf-app/src/look/viewers/markdown.rs
@@ -67,6 +67,55 @@ pub(crate) fn html_blocks_as_text(md: &str) -> String {
out
}
+/// A whole HTML page as Markdown text, for DirectLook's document view.
+///
+/// WHY (issue 802: DirectLook closed on a saved web page, its crash.log `inline_flow.rs:498:32: index out of bounds`). It is the
+/// panic of issue 955: the toolkit's HTML parser gives every paragraph the same element id. 955's way round it covered Markdown
+/// only, and an .html file still went to that parser whole. Here the page goes the same way as a README's HTML blocks: every
+/// block-level tag starts a line of its own, each line loses its tags (links, bold, italic and headings stay, pictures leave their
+/// alt text), and the result is drawn as Markdown. Tables and layout are lost; the text and its links are kept, and nothing can
+/// close the app.
+pub(crate) fn html_page_as_text(html: &str) -> String {
+ // a line break before each block-level tag, so that a heading or a paragraph is a line of its own
+ const BREAK_BEFORE: &[&str] = &[
+ "p", "div", "h1", "h2", "h3", "h4", "h5", "h6", "li", "tr", "br", "table", "ul", "ol", "blockquote", "section", "article", "pre",
+ "hr", "header", "footer", "nav", "main", "aside", "figure", "figcaption", "dt", "dd", "details", "summary",
+ ];
+ let mut spaced = String::with_capacity(html.len() + html.len() / 16);
+ let mut rest = html;
+ while let Some(i) = rest.find('<') {
+ spaced.push_str(&rest[..i]);
+ let after = &rest[i + 1..];
+ let after = after.strip_prefix('/').unwrap_or(after);
+ let name: String = after.chars().take_while(|c| c.is_ascii_alphanumeric()).collect::<String>().to_ascii_lowercase();
+ if BREAK_BEFORE.contains(&name.as_str()) {
+ spaced.push_str("\n\n");
+ }
+ spaced.push('<');
+ rest = &rest[i + 1..];
+ }
+ spaced.push_str(rest);
+ let mut out = String::with_capacity(spaced.len());
+ let mut in_comment = false;
+ let mut blank = true;
+ for line in spaced.lines() {
+ let text = strip_tags(line, &mut in_comment);
+ // (a line indented by the page's source would read as a Markdown code block: the indentation goes)
+ let text = text.trim();
+ if text.is_empty() {
+ if !blank {
+ out.push('\n');
+ blank = true;
+ }
+ continue;
+ }
+ out.push_str(text);
+ out.push('\n');
+ blank = false;
+ }
+ out
+}
+
/// Whether a line opens an HTML block: a comment, or a tag at the start of a line. A tag of the block kind (`<p>`, `<div>`, a
/// heading, a table…) opens one even right under a paragraph's line; any other (`<a>`, `<img>`, `<b>`) only after a blank line, as
/// CommonMark has it.
@@ -223,6 +272,19 @@ impl Viewer for MarkdownViewer {
mod tests {
use super::html_blocks_as_text as tame;
+ /// Issue 802: a saved web page reaches DirectLook as Markdown with no tag left; headings, links and paragraphs stay.
+ #[test]
+ fn a_web_page_becomes_markdown_with_no_tag_left() {
+ let html = "<html><body>\n<!-- saved by SingleFile -->\n<div class=\"x\"><h2>Title</h2><p>First <b>bold</b> line,\n with an <a href=\"https://e.com/a\">anchor</a>.</p><p>Second</p>\n<ul><li>one</li><li>two</li></ul>\n<img alt=\"Chart\" src=\"data:image/png;base64,AAAA\"></div></body></html>";
+ let got = super::html_page_as_text(html);
+ assert!(got.contains("## Title"), "{got}");
+ assert!(got.contains("**bold**"), "{got}");
+ assert!(got.contains("[anchor](https://e.com/a)"), "{got}");
+ assert!(got.contains("Second") && got.contains("one") && got.contains("two") && got.contains("Chart"), "{got}");
+ assert!(!got.contains('<') && !got.contains("SingleFile"), "no tag and no comment is left: {got}");
+ assert!(!got.lines().any(|l| l.starts_with(" ")), "no line reads as a code block: {got}");
+ }
+
/// Issue 955: a README's centred header (several HTML blocks) closed the app; it reaches the view as Markdown.
#[test]
fn html_blocks_become_markdown_and_the_rest_stays_as_it_is() {