Skip to content

Commit

Permalink
Inline template elements in generate and build
Browse files Browse the repository at this point in the history
  • Loading branch information
Tate-CC committed Mar 19, 2024
1 parent f12c7d3 commit ba30ccc
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

## Unreleased

* Fixes an issue where `<template>` elements were being ignored by generate and build.

## v2.0.4 (March 16, 2023)

* `<title>`, `<script>` and `<style>` elements will no longer be wordwrapped when the `--wrap` flag is used.
Expand Down
31 changes: 30 additions & 1 deletion rosey/features/build/rosey-build-resilience.feature
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,33 @@ Feature: Rosey Build Resilience
When I run my program with the flags:
| build |
Then I should see a selector 'img' in "dist/translated_site/fr/index.html" with the attributes:
| src | https://ryancollins.website/images/rodents/souslik.png |
| src | https://ryancollins.website/images/rodents/souslik.png |

Scenario: Rosey builds from locales with templates
Given I have a "dist/site/index.html" file with the content:
"""
<html>
<body>
<template>
<p data-rosey="seal">Kiss From A Rose</p>
</template>
</body>
</html>
"""
And I have a "rosey/locales/em.json" file with the content:
"""
{
"seal": "👄🌹"
}
"""
When I run my program with the flags:
| build |
Then I should see a selector 'title' in "dist/translated_site/index.html" with the attributes:
| innerText | Redirecting... |
And I should see a selector 'a' in "dist/translated_site/index.html" with the attributes:
| href | /en/ |
| innerText | Click here if you are not redirected. |
And I should see '<template>' in "dist/translated_site/en/index.html"
And I should see '<p data-rosey="seal">Kiss From A Rose</p>' in "dist/translated_site/en/index.html"
And I should see '<template>' in "dist/translated_site/em/index.html"
And I should see '<p data-rosey="seal">👄🌹</p>' in "dist/translated_site/em/index.html"
30 changes: 30 additions & 0 deletions rosey/features/generate/rosey-generate-resilience.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Feature: Rosey Generate v2
Background:
Given I have the environment variables:
| ROSEY_SOURCE | dist/site |
| ROSEY_DEST | dist/translated_site |

Scenario: Rosey generates base.json files with templates
Given I have a "dist/site/index.html" file with the content:
"""
<html>
<body data-rosey-ns='home'>
<template>
<h1 data-rosey="title">Home page title</h1>
<div data-rosey-root="contact">
<p data-rosey="contact-us">Contact content</p>
<div data-rosey-ns="inner">
<p data-rosey="author">CloudCannon</p>
</div>
</div>
</template>
</body>
</html>
"""
When I run my program with the flags:
| generate |
Then I should see "rosey/base.json" containing the values:
| version | int:2 |
| keys.home:title.original | Home page title |
| keys.contact:contact-us.original | Contact content |
| keys.contact:inner:author.original | CloudCannon |
13 changes: 13 additions & 0 deletions rosey/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,3 +310,16 @@ impl RoseyLocale {
}
}
}

pub fn inline_templates(dom: &kuchiki::NodeRef) {
dom.inclusive_descendants().for_each(|node| {
if let Some(kuchiki::ElementData {
template_contents: Some(contents),
..
}) = node.as_element()
{
inline_templates(contents);
node.append(contents.clone());
}
})
}
4 changes: 3 additions & 1 deletion rosey/src/runners/builder/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ impl<'a> RoseyPage<'a> {
wrap_class: &'a Option<String>,
) -> Self {
let dom = kuchiki::parse_html().one(content);
crate::inline_templates(&dom);

RoseyPage {
dom,
Expand Down Expand Up @@ -286,7 +287,8 @@ impl<'a> RoseyPage<'a> {
});

let element_data = node.as_element().unwrap();
let should_prevent_wrap = UNSUPPORTED_WRAP_ELEMENTS.contains(&&element_data.name.local[..]);
let should_prevent_wrap =
UNSUPPORTED_WRAP_ELEMENTS.contains(&&element_data.name.local[..]);

if let Some(content) = translation.get(key) {
let content = if content.contains('<') {
Expand Down
1 change: 1 addition & 0 deletions rosey/src/runners/generator/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ impl RoseyGenerator {
pub fn process_html_file(&mut self, file: &Path) {
let config = &self.options.config;
let dom = kuchiki::parse_html().one(read_to_string(file).unwrap());
crate::inline_templates(&dom);
self.current_file =
String::from(file.strip_prefix(&config.source).unwrap().to_str().unwrap());
self.process_html_node(dom, None, None);
Expand Down

0 comments on commit ba30ccc

Please sign in to comment.