Mejora el renderizado condicional de Favicon
This commit is contained in:
parent
9611a80275
commit
a09f3aba7b
1 changed files with 37 additions and 37 deletions
|
|
@ -1,7 +1,7 @@
|
||||||
use crate::html::{html, Markup, PreEscaped};
|
use crate::html::{html, Markup};
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct Favicon(Vec<String>);
|
pub struct Favicon(Vec<Markup>);
|
||||||
|
|
||||||
impl Favicon {
|
impl Favicon {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
|
|
@ -11,66 +11,66 @@ impl Favicon {
|
||||||
// Favicon BUILDER.
|
// Favicon BUILDER.
|
||||||
|
|
||||||
pub fn with_icon(self, image: &str) -> Self {
|
pub fn with_icon(self, image: &str) -> Self {
|
||||||
self.add_item("icon", image, "", "")
|
self.add_icon_item("icon", image, None, None)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_icon_for_sizes(self, image: &str, sizes: &str) -> Self {
|
pub fn with_icon_for_sizes(self, image: &str, sizes: &str) -> Self {
|
||||||
self.add_item("icon", image, sizes, "")
|
self.add_icon_item("icon", image, Some(sizes), None)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_apple_touch_icon(self, image: &str, sizes: &str) -> Self {
|
pub fn with_apple_touch_icon(self, image: &str, sizes: &str) -> Self {
|
||||||
self.add_item("apple-touch-icon", image, sizes, "")
|
self.add_icon_item("apple-touch-icon", image, Some(sizes), None)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_mask_icon(self, image: &str, color: &str) -> Self {
|
pub fn with_mask_icon(self, image: &str, color: &str) -> Self {
|
||||||
self.add_item("mask-icon", image, "", color)
|
self.add_icon_item("mask-icon", image, None, Some(color))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_manifest(self, file: &str) -> Self {
|
pub fn with_manifest(self, file: &str) -> Self {
|
||||||
self.add_item("manifest", file, "", "")
|
self.add_icon_item("manifest", file, None, None)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_theme_color(mut self, color: &str) -> Self {
|
pub fn with_theme_color(mut self, color: &str) -> Self {
|
||||||
self.0
|
self.0.push(html! { meta name="theme-color" content=(color); });
|
||||||
.push(format!("<meta name=\"theme-color\" content=\"{}\">", color));
|
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_ms_tile_color(mut self, color: &str) -> Self {
|
pub fn with_ms_tile_color(mut self, color: &str) -> Self {
|
||||||
self.0.push(format!(
|
self.0.push(html! { meta name="msapplication-TileColor" content=(color); });
|
||||||
"<meta name=\"msapplication-TileColor\" content=\"{}\">",
|
|
||||||
color
|
|
||||||
));
|
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_ms_tile_image(mut self, image: &str) -> Self {
|
pub fn with_ms_tile_image(mut self, image: &str) -> Self {
|
||||||
self.0.push(format!(
|
self.0.push(html! { meta name="msapplication-TileImage" content=(image); });
|
||||||
"<meta name=\"msapplication-TileImage\" content=\"{}\">",
|
|
||||||
image
|
|
||||||
));
|
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_item(mut self, rel: &str, source: &str, sizes: &str, color: &str) -> Self {
|
fn add_icon_item(
|
||||||
let mut link: String = format!("<link rel=\"{}\"", rel);
|
mut self,
|
||||||
if let Some(i) = source.rfind('.') {
|
icon_rel: &str,
|
||||||
link = match source[i..].to_owned().to_lowercase().as_str() {
|
icon_source: &str,
|
||||||
".gif" => format!("{} type=\"image/gif\"", link),
|
icon_sizes: Option<&str>,
|
||||||
".ico" => format!("{} type=\"image/x-icon\"", link),
|
icon_color: Option<&str>,
|
||||||
".jpg" => format!("{} type=\"image/jpg\"", link),
|
) -> Self {
|
||||||
".png" => format!("{} type=\"image/png\"", link),
|
let icon_type = match icon_source.rfind('.') {
|
||||||
".svg" => format!("{} type=\"image/svg+xml\"", link),
|
Some(i) => match icon_source[i..].to_owned().to_lowercase().as_str() {
|
||||||
_ => link,
|
".gif" => Some("image/gif"),
|
||||||
};
|
".ico" => Some("image/x-icon"),
|
||||||
}
|
".jpg" => Some("image/jpg"),
|
||||||
if !sizes.is_empty() {
|
".png" => Some("image/png"),
|
||||||
link = format!("{} sizes=\"{}\"", link, sizes);
|
".svg" => Some("image/svg+xml"),
|
||||||
}
|
_ => None,
|
||||||
if !color.is_empty() {
|
},
|
||||||
link = format!("{} color=\"{}\"", link, color);
|
_ => None,
|
||||||
}
|
};
|
||||||
self.0.push(format!("{} href=\"{}\">", link, source));
|
self.0.push(html! {
|
||||||
|
link
|
||||||
|
rel=(icon_rel)
|
||||||
|
type=[(icon_type)]
|
||||||
|
sizes=[(icon_sizes)]
|
||||||
|
color=[(icon_color)]
|
||||||
|
href=(icon_source);
|
||||||
|
});
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -79,7 +79,7 @@ impl Favicon {
|
||||||
pub(crate) fn render(&self) -> Markup {
|
pub(crate) fn render(&self) -> Markup {
|
||||||
html! {
|
html! {
|
||||||
@for item in &self.0 {
|
@for item in &self.0 {
|
||||||
(PreEscaped(item))
|
(item)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue