Skip to content

Commit

Permalink
Lossy read filecontent as UTF-8
Browse files Browse the repository at this point in the history
Signed-off-by: Bastian Rinsche <[email protected]>
  • Loading branch information
brinsche committed Mar 13, 2018
1 parent 1b8f3f5 commit 3f82410
Showing 1 changed file with 27 additions and 24 deletions.
51 changes: 27 additions & 24 deletions qlhighlight/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ mod quicklook;

use std::fmt::Write;
use std::io::Cursor;
use std::io::prelude::*;
use std::fs::File;
use std::panic::{self, AssertUnwindSafe};
use std::path::Path;

Expand All @@ -16,7 +18,7 @@ use core_foundation::string::CFStringRef;
use core_foundation::dictionary::CFDictionaryRef;

use syntect::highlighting::{Color, ThemeSet};
use syntect::html::highlighted_snippet_for_file;
use syntect::html::highlighted_snippet_for_string;
use syntect::parsing::SyntaxSet;

use quicklook::QLPreviewRequestRef;
Expand Down Expand Up @@ -69,34 +71,38 @@ pub extern "C" fn GeneratePreviewForURL(
"pre {{ font-size: {}px; font-family: {}; }}",
conf.font_size, conf.font_family
);
let c = theme.settings.background.unwrap_or(Color::WHITE);
let bg = theme.settings.background.unwrap_or(Color::WHITE);

let mut filecontent: Vec<u8> = Vec::new();
let mut file = File::open(&path).expect("Unable to open the file");
file.read_to_end(&mut filecontent)
.expect("Unable to read the file");

let content = String::from_utf8_lossy(&filecontent);

let mut buffer = String::new();
write!(
buffer,
"<body style=\"background-color:#{:02x}{:02x}{:02x};\">\n",
c.r, c.g, c.b
bg.r, bg.g, bg.b
);
write!(buffer, "<head><style>{}</style></head>", style);

let first_try = panic::catch_unwind(AssertUnwindSafe(|| {
if let Ok(html) = highlighted_snippet_for_file(&path, &syntax_set, theme) {
write!(buffer, "{}", html);
} else {
write!(
buffer,
"<pre><span style=\"color:#{:02x}{:02x}{:02x}\">{}</span></pre>\n",
c.r, c.g, c.b, "IOError encountered."
);
let syntax = match syntax_set.find_syntax_for_file(&path) {
Ok(found) => match found {
Some(syntax) => syntax,
None => syntax_set.find_syntax_plain_text(),
},
Err(_) => syntax_set.find_syntax_plain_text(),
};

let html = highlighted_snippet_for_string(&content, &syntax, theme);
write!(buffer, "{}", html);
}));

if first_try.is_err() {
// Force plaintext syntax after first try panicked
let mut plain_syntax = SyntaxSet::new();
plain_syntax.load_plain_text_syntax();
plain_syntax.link_syntaxes();

let c = Color {
r: 255,
g: 0,
Expand All @@ -111,15 +117,12 @@ pub extern "C" fn GeneratePreviewForURL(
);

let _retry = panic::catch_unwind(AssertUnwindSafe(|| {
if let Ok(html) = highlighted_snippet_for_file(&path, &plain_syntax, &theme) {
write!(buffer, "{}", html);
} else {
write!(
buffer,
"<pre><span style=\"color:#{:02x}{:02x}{:02x}\">{}</span></pre>\n",
c.r, c.g, c.b, "IOError encountered."
);
};
let html = highlighted_snippet_for_string(
&content,
&syntax_set.find_syntax_plain_text(),
&theme,
);
write!(buffer, "{}", html);
}));
}

Expand Down

0 comments on commit 3f82410

Please sign in to comment.