Skip to content

Commit

Permalink
Bake templates into binary
Browse files Browse the repository at this point in the history
  • Loading branch information
simoncozens committed Jul 17, 2024
1 parent cdd37f7 commit 80a3542
Show file tree
Hide file tree
Showing 15 changed files with 334 additions and 327 deletions.
102 changes: 101 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,5 @@ ucd = "0.1.1"
unicode_names2 = "0.6.0"
brotli = "6.0.0"
lazy_static = "1.4.0"
zeno = "0.3.1"
zeno = "0.3.1"
homedir = "0.3.3"
68 changes: 60 additions & 8 deletions src/html.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::{collections::HashMap, path::Path};
use std::{
collections::HashMap,
path::{Path, PathBuf},
};

use lazy_static::lazy_static;
use serde::Serialize;
Expand Down Expand Up @@ -95,16 +98,65 @@ impl CSSFontStyle {

lazy_static! {
pub static ref TEMPLATES: Tera = {
match Tera::new("templates/*") {
Ok(t) => t,
Err(e) => {
eprintln!("Parsing error(s): {}", e);
::std::process::exit(1);
}
}
let homedir = create_user_home_templates_directory();
Tera::new(&format!("{}/*", homedir.to_str().unwrap())).unwrap_or_else(|e| {
println!("Problem parsing templates: {:?}", e);
std::process::exit(1)
})
};
}

pub fn create_user_home_templates_directory() -> PathBuf {
let home = homedir::my_home()
.expect("Couldn't got home directory")
.expect("No home directory found");
let templates_dir = home.join(".diffenator3/templates");
if !templates_dir.exists() {
std::fs::create_dir_all(&templates_dir)
.expect(format!("Couldn't create {}", templates_dir.to_str().unwrap()).as_str());
}
let all_templates = [
["_base.html", include_str!("templates/_base.html")],
[
"CSSFontFace.partial.html",
include_str!("templates/CSSFontFace.partial.html"),
],
[
"CSSFontStyle.partial.html",
include_str!("templates/CSSFontStyle.partial.html"),
],
[
"Glyph.partial.html",
include_str!("templates/Glyph.partial.html"),
],
[
"GlyphDiff.partial.html",
include_str!("templates/GlyphDiff.partial.html"),
],
[
"Word.partial.html",
include_str!("templates/Word.partial.html"),
],
[
"WordDiff.partial.html",
include_str!("templates/WordDiff.partial.html"),
],
["script.js", include_str!("templates/script.js")],
["style.css", include_str!("templates/style.css")],
["diffenator.html", include_str!("templates/diffenator.html")],
];
for template in all_templates.iter() {
let path = templates_dir.join(template[0]);
if !path.exists() {
std::fs::write(&path, template[1]).expect(&format!(
"Couldn't write template file {}",
path.to_str().unwrap()
));
}
}
templates_dir
}

pub fn render_output(
value: &serde_json::Value,
font_face_old: CSSFontFace,
Expand Down
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::collections::HashMap;

use cfg_if::cfg_if;

pub mod dfont;
Expand All @@ -14,6 +12,7 @@ cfg_if! {

cfg_if! {
if #[cfg(target_family = "wasm")] {
use std::collections::HashMap;
use dfont::DFont;
use render::{test_font_glyphs, test_font_words};
use serde_json::json;
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
61 changes: 61 additions & 0 deletions src/templates/_base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<!DOCTYPE html>
<html lang="en" dir="auto">
<head>
{% block head %}
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>GFR - {% block title %}{% endblock %}</title>
{% endblock %}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<style type="text/css">
{% include "style.css" %}
{% for font_face in font_faces_old %}
{% include "CSSFontFace.partial.html" %}
{% endfor %}

{% for font_face in font_faces_new %}
{% include "CSSFontFace.partial.html" %}
{% endfor %}

{% for font_face in font_faces %}
{% include "CSSFontFace.partial.html" %}
{% endfor %}

{% for font_class in font_styles_old %}
{% include "CSSFontStyle.partial.html" %}
{% endfor %}

{% for font_class in font_styles_new %}
{% include "CSSFontStyle.partial.html" %}
{% endfor %}

{% for font_class in font_styles %}
{% include "CSSFontStyle.partial.html" %}
{% endfor %}
</style>
</head>
<body>
<div id="nav">
{% if include_ui %}
<div class="nav-item" id="font-toggle">old</div>
{% endif %}
<div class="nav-item" id="ot-button">OT features</div>
<div id="ot-panel">
</div>
{% block nav %}{% endblock %}
</div>
<div id="content">
<h2>{% block content_name %}{% endblock %}</h2>
{% block content %}
{% endblock %}
</div>

<script>
{% include "script.js" %}
{% block js %}
{% endblock %}

</script>
</body>

</html>

4 changes: 2 additions & 2 deletions templates/diffenator.html → src/templates/diffenator.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
}
{% endblock %}
{% block content_name %}
<b>Diffenator</b>
<b>Diffenator3</b>
{% endblock %}
{% block content %}
{% for font_class in font_styles_old %}
Expand Down Expand Up @@ -245,4 +245,4 @@
}

insertBreaks()
{% endblock %}
{% endblock %}
Loading

0 comments on commit 80a3542

Please sign in to comment.