rewrite html to remove previously required html templating, resolves #4

This commit is contained in:
zyl 2024-11-13 11:53:32 -08:00
parent 4c6064a5a1
commit 6c31ccb9d5
Signed by: zyl
SSH key fingerprint: SHA256:uxxbSXbdroP/OnKBGnEDk5q7EKB2razvstC/KmzdXXs
4 changed files with 34 additions and 80 deletions

View file

@ -19,55 +19,7 @@ all templates are given the following variables:
### `title`
the page's title. this should be rendered in the `<head>` html element like so:
```html
<head>
<title>{{ title }}</title>
</head>
```
this variable will no longer be required to set the page title before the first crates.io release.
### `head`
the page's extra head data. this should be rendered in the `<head>` html element like so:
```html
<head>
{{ head | safe }}
</head>
```
this variable will be removed before the first crates.io release.
### `scripts`
the page's extra scripts. this should be rendered in the `<head>` html element like so:
```html
<head>
{% for script in scripts %}
<script type="text/javascript" src="{{script}}" defer></script>
{% endfor %}
</head>
```
this variable will be removed before the first crates.io release.
### `styles`
the page's extra stylesheets. this should be rendered in the `<head>` html element like so:
```html
<head>
{% for style in styles %}
<link rel="stylesheet" href="/styles/{{style}}" />
{% endfor %}
</head>
```
this variable will be removed before the first crates.io release.
the page's full title, as shown in the browser tab.
### `page`

View file

@ -5,14 +5,6 @@
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="referrer" content="no-referrer">
<link rel="stylesheet" href="/styles/index.css">
<title>{{ title }}</title>
{{ head | safe }}
{% for script in scripts %}
<script type="text/javascript" src="{{script}}" defer></script>
{% endfor %}
{% for style in styles %}
<link rel="stylesheet" href="/styles/{{style}}">
{% endfor %}
</head>
<body>

View file

@ -22,12 +22,6 @@ struct TemplateData<'a, T> {
pub page: &'a str,
/// The page's title.
pub title: &'a str,
/// Custom head data for the page.
pub head: Option<String>,
/// The page's custom scripts.
pub scripts: &'a [String],
/// the page's custom styles.
pub styles: &'a [String],
/// Custom template data.
pub data: T,
}
@ -159,7 +153,14 @@ impl SiteBuilder {
}
/// Function to rewrite HTML wow.
pub fn rewrite_html(&self, html: String) -> eyre::Result<String> {
pub fn rewrite_html(
&self,
html: String,
title: &str,
head: &Option<String>,
scripts: &[String],
styles: &[String],
) -> eyre::Result<String> {
let mut output = Vec::new();
let mut rewriter = HtmlRewriter::new(
Settings {
@ -172,6 +173,24 @@ impl SiteBuilder {
}),
element!("head", |el| {
el.prepend(r#"<meta charset="utf-8">"#, ContentType::Html);
el.append(&format!("<title>{title}</title>"), ContentType::Html);
if let Some(head) = head {
el.append(head, ContentType::Html);
}
for script in scripts {
el.append(
&format!(
r#"<script type="text/javascript" src="{script}" defer></script>"#
),
ContentType::Html,
);
}
for style in styles {
el.append(
&format!(r#"<link rel="stylesheet" href="/styles/{style}">"#),
ContentType::Html,
);
}
el.append(
r#"<script type="text/javascript" src="/webdog/webdog.js" defer></script>"#,
ContentType::Html,
@ -300,15 +319,18 @@ impl SiteBuilder {
&tera::Context::from_serialize(TemplateData {
page: page_html,
title: &title,
head,
scripts: &page_metadata.scripts,
styles: &page_metadata.styles,
data: extra_data,
})?,
)?;
// Modify HTML output
let mut out = self.rewrite_html(out)?;
let mut out = self.rewrite_html(
out,
&title,
&head,
&page_metadata.scripts,
&page_metadata.styles,
)?;
if let Some(data) = extra {
if let Some(extra) = crate::extras::get_extra(&data.name) {

View file

@ -5,18 +5,6 @@
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="referrer" content="no-referrer">
<link rel="stylesheet" href="/styles/index.css">
<title>{{ title }}</title>
{# header information from webdog #}
{{ head | safe }}
{# include scripts defined in the page frontmatter #}
{% for script in scripts %}
<script type="text/javascript" src="{{script}}" defer></script>
{% endfor %}
{# include styles defined in the page frontmatter #}
{% for style in styles %}
<link rel="stylesheet" href="/styles/{{style}}">
{% endfor %}
</head>
<body>