-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(l10n): add sizes to fallback imgs
- Loading branch information
Showing
4 changed files
with
129 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
use std::error::Error; | ||
use std::path::Path; | ||
|
||
use lol_html::html_content::Element; | ||
use lol_html::HandlerResult; | ||
use rari_types::locale::default_locale; | ||
use tracing::warn; | ||
use url::{ParseOptions, Url}; | ||
|
||
use crate::issues::get_issue_counter; | ||
use crate::pages::page::{Page, PageLike}; | ||
|
||
type ImgSize = (Option<String>, Option<String>); | ||
|
||
pub fn handle_img( | ||
el: &mut Element, | ||
page: &impl PageLike, | ||
data_issues: bool, | ||
base: &Url, | ||
base_url: &ParseOptions, | ||
) -> HandlerResult { | ||
// Leave dimensions alone if we have a `width` attribute | ||
if el.get_attribute("width").is_some() { | ||
return Ok(()); | ||
} | ||
if let Some(src) = el.get_attribute("src") { | ||
let url = base_url.parse(&src)?; | ||
if url.host() == base.host() && !url.path().starts_with("/assets/") { | ||
el.set_attribute("src", url.path())?; | ||
let mut file = page.full_path().parent().unwrap().join(&src); | ||
if !file.try_exists().unwrap_or_default() { | ||
if let Ok(en_us_page) = | ||
Page::from_url_with_locale_and_fallback(page.url(), default_locale()) | ||
{ | ||
file = en_us_page.full_path().parent().unwrap().join(&src); | ||
} | ||
} | ||
let (width, height) = img_size(el, &src, &file, data_issues)?; | ||
if let Some(width) = width { | ||
el.set_attribute("width", &width)?; | ||
} | ||
if let Some(height) = height { | ||
el.set_attribute("height", &height)?; | ||
} | ||
} | ||
} | ||
Ok(()) | ||
} | ||
|
||
pub fn img_size( | ||
el: &mut Element, | ||
src: &str, | ||
file: &Path, | ||
data_issues: bool, | ||
) -> Result<ImgSize, Box<dyn Error + Send + Sync>> { | ||
let (width, height) = if src.ends_with(".svg") { | ||
match svg_metadata::Metadata::parse_file(file) { | ||
// If only width and viewbox are given, use width and scale | ||
// the height according to the viewbox size ratio. | ||
// If width and height are given, use these. | ||
// If only a viewbox is given, use the viewbox values. | ||
// If only height and viewbox are given, use height and scale | ||
// the height according to the viewbox size ratio. | ||
Ok(meta) => { | ||
let width = meta.width.map(|w| w.width); | ||
let height = meta.height.map(|h| h.height); | ||
let view_box = meta.view_box; | ||
|
||
let (final_width, final_height) = match (width, height, view_box) { | ||
// Both width and height are given | ||
(Some(w), Some(h), _) => (Some(w), Some(h)), | ||
// Only width and viewbox are given | ||
(Some(w), None, Some(vb)) => (Some(w), Some(w * vb.height / vb.width)), | ||
// Only height and viewbox are given | ||
(None, Some(h), Some(vb)) => (Some(h * vb.width / vb.height), Some(h)), | ||
// Only viewbox is given | ||
(None, None, Some(vb)) => (Some(vb.width), Some(vb.height)), | ||
// Only width is given | ||
(Some(w), None, None) => (Some(w), None), | ||
// Only height is given | ||
(None, Some(h), None) => (None, Some(h)), | ||
// Neither width, height, nor viewbox are given | ||
(None, None, None) => (None, None), | ||
}; | ||
|
||
( | ||
final_width.map(|w| format!("{:.0}", w)), | ||
final_height.map(|h| format!("{:.0}", h)), | ||
) | ||
} | ||
Err(e) => { | ||
let ic = get_issue_counter(); | ||
warn!( | ||
source = "image-check", | ||
ic = ic, | ||
"Error parsing {}: {e}", | ||
file.display() | ||
); | ||
if data_issues { | ||
el.set_attribute("data-flaw", &ic.to_string())?; | ||
} | ||
(None, None) | ||
} | ||
} | ||
} else { | ||
match imagesize::size(file) { | ||
Ok(dim) => (Some(dim.width.to_string()), Some(dim.height.to_string())), | ||
Err(e) => { | ||
let ic = get_issue_counter(); | ||
warn!( | ||
source = "image-check", | ||
ic = ic, | ||
"Error opening {}: {e}", | ||
file.display() | ||
); | ||
if data_issues { | ||
el.set_attribute("data-flaw", &ic.to_string())?; | ||
} | ||
|
||
(None, None) | ||
} | ||
} | ||
}; | ||
Ok((width, height)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
pub mod bubble_up; | ||
mod fix_img; | ||
mod fix_link; | ||
pub mod links; | ||
pub mod modifier; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters