Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle bad file names. #132

Merged
merged 1 commit into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ project adheres to
Thanks @kornelski!
* Fixed handling of `MULTI_WORD_CONSTANTS` in templates (Issue #129, PR #130).
Thanks @wezm!
* More ways to create a working rust symbol name from a "strange"
static file name. Illegal characters are replaced by `_`, and if
the file name starts with a number it is prefixed with `n` (Issue
#82, PR #132). Thanks @Aedius for reporting!
* Fixed more clippy lints (PR #123, #127). Thanks @vbrandl!
* Updated `rsass` to 0.28.0 and `itertools` to 0.11.0.

Expand Down
16 changes: 15 additions & 1 deletion examples/statics/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,24 @@ fn test_all_statics_known() {
use templates::statics::STATICS;
assert_eq!(
STATICS.iter().map(|s| s.name).collect::<Vec<_>>(),
["foo-JckCHvyv.css", "foo-R-7hhHLr.js", "style-o2rFo1lI.css"]
[
"17-C0lZWdZX.css",
"foo-JckCHvyv.css",
"foo-R-7hhHLr.js",
"style-o2rFo1lI.css"
]
);
}

/// Test for issue #82.
#[test]
fn test_num_gets_prefixed() {
// The rust name is usable in rust.
let style = &templates::statics::n17_css;
// Url name is not prefixed (but has a content hash).
assert_eq!(style.name, "17-C0lZWdZX.css");
}

#[cfg(test)]
fn r2s<Call>(call: Call) -> String
where
Expand Down
1 change: 1 addition & 0 deletions examples/statics/static/17.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
body { font-family: serif; }
11 changes: 10 additions & 1 deletion src/staticfiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,16 @@ impl StaticFile {
content: &impl Display,
suffix: &str,
) -> Result<&mut Self> {
let rust_name = rust_name.replace(['/', '-', '.'], "_");
let mut rust_name =
rust_name.replace(|c: char| !c.is_alphanumeric(), "_");
if rust_name
.as_bytes()
.first()
.map(|c| c.is_ascii_digit())
.unwrap_or(true)
{
rust_name.insert(0, 'n');
}
writeln!(
self.src,
"\n/// From {path:?}\
Expand Down