Remove some code duplication
This commit is contained in:
parent
8a9ecd212d
commit
e7b69114ed
1 changed files with 25 additions and 33 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
//! Support for writing static files.
|
//! Support for writing static files.
|
||||||
|
|
||||||
use log::{debug, warn};
|
use log::{debug, warn};
|
||||||
|
use once_cell::sync::Lazy;
|
||||||
|
|
||||||
use crate::config::HtmlConfig;
|
use crate::config::HtmlConfig;
|
||||||
use crate::errors::*;
|
use crate::errors::*;
|
||||||
|
|
@ -231,27 +232,32 @@ impl StaticFiles {
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
// The `{{ resource "name" }}` directive in static resources look like
|
// The `{{ resource "name" }}` directive in static resources look like
|
||||||
// handlebars syntax, even if they technically aren't.
|
// handlebars syntax, even if they technically aren't.
|
||||||
let resource = Regex::new(r#"\{\{ resource "([^"]+)" \}\}"#).unwrap();
|
static RESOURCE: Lazy<Regex> =
|
||||||
for static_file in self.static_files {
|
Lazy::new(|| Regex::new(r#"\{\{ resource "([^"]+)" \}\}"#).unwrap());
|
||||||
|
fn replace_all<'a>(
|
||||||
|
hash_map: &HashMap<String, String>,
|
||||||
|
data: &'a [u8],
|
||||||
|
filename: &str,
|
||||||
|
) -> Cow<'a, [u8]> {
|
||||||
|
RESOURCE.replace_all(data, move |captures: &Captures<'_>| {
|
||||||
|
let name = captures
|
||||||
|
.get(1)
|
||||||
|
.expect("capture 1 in resource regex")
|
||||||
|
.as_bytes();
|
||||||
|
let name = std::str::from_utf8(name).expect("resource name with invalid utf8");
|
||||||
|
let resource_filename = hash_map.get(name).map(|s| &s[..]).unwrap_or(&name);
|
||||||
|
let path_to_root = utils::fs::path_to_root(&filename);
|
||||||
|
format!("{}{}", path_to_root, resource_filename)
|
||||||
|
.as_bytes()
|
||||||
|
.to_owned()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
for static_file in &self.static_files {
|
||||||
match static_file {
|
match static_file {
|
||||||
StaticFile::Builtin { filename, data } => {
|
StaticFile::Builtin { filename, data } => {
|
||||||
debug!("Writing builtin -> {}", filename);
|
debug!("Writing builtin -> {}", filename);
|
||||||
let hash_map = &self.hash_map;
|
|
||||||
let data = if filename.ends_with(".css") || filename.ends_with(".js") {
|
let data = if filename.ends_with(".css") || filename.ends_with(".js") {
|
||||||
resource.replace_all(&data, |captures: &Captures<'_>| {
|
replace_all(&self.hash_map, data, filename)
|
||||||
let name = captures
|
|
||||||
.get(1)
|
|
||||||
.expect("capture 1 in resource regex")
|
|
||||||
.as_bytes();
|
|
||||||
let name =
|
|
||||||
std::str::from_utf8(name).expect("resource name with invalid utf8");
|
|
||||||
let resource_filename =
|
|
||||||
hash_map.get(name).map(|s| &s[..]).unwrap_or(&name);
|
|
||||||
let path_to_root = utils::fs::path_to_root(&filename);
|
|
||||||
format!("{}{}", path_to_root, resource_filename)
|
|
||||||
.as_bytes()
|
|
||||||
.to_owned()
|
|
||||||
})
|
|
||||||
} else {
|
} else {
|
||||||
Cow::Borrowed(&data[..])
|
Cow::Borrowed(&data[..])
|
||||||
};
|
};
|
||||||
|
|
@ -272,25 +278,11 @@ impl StaticFiles {
|
||||||
.with_context(|| format!("Unable to create {}", parent.display()))?;
|
.with_context(|| format!("Unable to create {}", parent.display()))?;
|
||||||
}
|
}
|
||||||
if filename.ends_with(".css") || filename.ends_with(".js") {
|
if filename.ends_with(".css") || filename.ends_with(".js") {
|
||||||
let hash_map = &self.hash_map;
|
|
||||||
let mut file = File::open(input_location)?;
|
let mut file = File::open(input_location)?;
|
||||||
let mut data = Vec::new();
|
let mut data = Vec::new();
|
||||||
file.read_to_end(&mut data)?;
|
file.read_to_end(&mut data)?;
|
||||||
let data = resource.replace_all(&data, |captures: &Captures<'_>| {
|
let data = replace_all(&self.hash_map, &data, filename);
|
||||||
let name = captures
|
write_file(destination, filename, &data)?;
|
||||||
.get(1)
|
|
||||||
.expect("capture 1 in resource regex")
|
|
||||||
.as_bytes();
|
|
||||||
let name =
|
|
||||||
std::str::from_utf8(name).expect("resource name with invalid utf8");
|
|
||||||
let resource_filename =
|
|
||||||
hash_map.get(name).map(|s| &s[..]).unwrap_or(&name);
|
|
||||||
let path_to_root = utils::fs::path_to_root(&filename);
|
|
||||||
format!("{}{}", path_to_root, resource_filename)
|
|
||||||
.as_bytes()
|
|
||||||
.to_owned()
|
|
||||||
});
|
|
||||||
write_file(destination, &filename, &data)?;
|
|
||||||
} else {
|
} else {
|
||||||
fs::copy(&input_location, &output_location).with_context(|| {
|
fs::copy(&input_location, &output_location).with_context(|| {
|
||||||
format!(
|
format!(
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue