aboutsummaryrefslogtreecommitdiffstats
path: root/askama_shared
diff options
context:
space:
mode:
authorLibravatar René Kijewski <kijewski@library.vetmed.fu-berlin.de>2022-01-07 00:53:54 +0100
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2022-01-07 13:18:03 +0100
commite3ede6a71ed96cfe78c2883a2fac1d0f9425b668 (patch)
tree4db9edad80a352a253a81eacd2fb32d1d8e5beb5 /askama_shared
parentaf552ee3744016ac1db741888318649a64dfee50 (diff)
downloadaskama-e3ede6a71ed96cfe78c2883a2fac1d0f9425b668.tar.gz
askama-e3ede6a71ed96cfe78c2883a2fac1d0f9425b668.tar.bz2
askama-e3ede6a71ed96cfe78c2883a2fac1d0f9425b668.zip
Move extension_to_mime_type() to askama_shared
Diffstat (limited to '')
-rw-r--r--askama_shared/Cargo.toml2
-rw-r--r--askama_shared/src/input.rs27
-rw-r--r--askama_shared/src/lib.rs1
3 files changed, 30 insertions, 0 deletions
diff --git a/askama_shared/Cargo.toml b/askama_shared/Cargo.toml
index 7f476b1..be00ae4 100644
--- a/askama_shared/Cargo.toml
+++ b/askama_shared/Cargo.toml
@@ -18,6 +18,8 @@ yaml = ["serde", "serde_yaml"]
[dependencies]
askama_escape = { version = "0.10.2", path = "../askama_escape" }
humansize = { version = "1.1.0", optional = true }
+mime = "0.3"
+mime_guess = "2"
nom = "7"
num-traits = { version = "0.2.6", optional = true }
proc-macro2 = "1"
diff --git a/askama_shared/src/input.rs b/askama_shared/src/input.rs
index 03c62d0..7d28a7e 100644
--- a/askama_shared/src/input.rs
+++ b/askama_shared/src/input.rs
@@ -3,6 +3,7 @@ use crate::{CompileError, Config, Syntax};
use std::path::{Path, PathBuf};
use std::str::FromStr;
+use mime::Mime;
use quote::ToTokens;
pub struct TemplateInput<'a> {
@@ -239,6 +240,32 @@ impl FromStr for Print {
}
}
+#[doc(hidden)]
+pub fn extension_to_mime_type(ext: &str) -> Mime {
+ let basic_type = mime_guess::from_ext(ext).first_or_octet_stream();
+ for (simple, utf_8) in &TEXT_TYPES {
+ if &basic_type == simple {
+ return utf_8.clone();
+ }
+ }
+ basic_type
+}
+
+const TEXT_TYPES: [(Mime, Mime); 6] = [
+ (mime::TEXT_PLAIN, mime::TEXT_PLAIN_UTF_8),
+ (mime::TEXT_HTML, mime::TEXT_HTML_UTF_8),
+ (mime::TEXT_CSS, mime::TEXT_CSS_UTF_8),
+ (mime::TEXT_CSV, mime::TEXT_CSV_UTF_8),
+ (
+ mime::TEXT_TAB_SEPARATED_VALUES,
+ mime::TEXT_TAB_SEPARATED_VALUES_UTF_8,
+ ),
+ (
+ mime::APPLICATION_JAVASCRIPT,
+ mime::APPLICATION_JAVASCRIPT_UTF_8,
+ ),
+];
+
#[cfg(test)]
mod tests {
use super::*;
diff --git a/askama_shared/src/lib.rs b/askama_shared/src/lib.rs
index f6b94dd..911367a 100644
--- a/askama_shared/src/lib.rs
+++ b/askama_shared/src/lib.rs
@@ -11,6 +11,7 @@ use std::{env, fmt, fs};
#[cfg(feature = "serde")]
use serde::Deserialize;
+pub use crate::input::extension_to_mime_type;
pub use askama_escape::MarkupDisplay;
mod error;