Skip to content

Commit

Permalink
Bundle bulma CSS
Browse files Browse the repository at this point in the history
To support restricted or offline environments, bundle the bulma CSS with
grcov instead of loading it from the jsdelivr CDN.

HTML reports will now create `bulma.min.css` in the root directory and
relatively reference it in all the HTML files.

Fixes mozilla#1079.
  • Loading branch information
legoktm committed Aug 2, 2023
1 parent 322fc39 commit 7770c22
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
1 change: 1 addition & 0 deletions src/bulma.min.css

Large diffs are not rendered by default.

41 changes: 25 additions & 16 deletions src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ impl ConfigFile {
}
}

static BULMA_VERSION: &str = "0.9.1";

fn load_template(path: &str) -> String {
fs::read_to_string(path).unwrap()
}
Expand Down Expand Up @@ -260,14 +258,6 @@ fn get_dirs_result(global: Arc<Mutex<HtmlGlobalStats>>, rel_path: &Path, stats:

use tera::{Context, Tera};

fn make_context() -> Context {
let mut ctx = Context::new();
let ver = std::env::var("BULMA_VERSION").map_or(BULMA_VERSION.into(), |v| v);
ctx.insert("bulma_version", &ver);

ctx
}

pub fn gen_index(
tera: &Tera,
global: &HtmlGlobalStats,
Expand All @@ -286,11 +276,12 @@ pub fn gen_index(
Ok(f) => f,
};

let mut ctx = make_context();
let mut ctx = Context::new();
let empty: &[&str] = &[];
ctx.insert("date", &conf.date);
ctx.insert("current", "top_level");
ctx.insert("parents", empty);
ctx.insert("bulma_path", "bulma.min.css");
ctx.insert("stats", &global.stats);
ctx.insert("precision", &precision);
ctx.insert("items", &global.dirs);
Expand All @@ -304,6 +295,22 @@ pub fn gen_index(
return;
}

let bulma_file = output.join("bulma.min.css");
let mut bulma_stream = match File::create(&bulma_file) {
Err(_) => {
eprintln!("Cannot create file {:?}", output_file);
return;
}
Ok(f) => f,
};
if bulma_stream
.write_all(include_bytes!("bulma.min.css"))
.is_err()
{
eprintln!("Cannot write the file {:?}", bulma_file);
return;
}

for (dir_name, dir_stats) in global.dirs.iter() {
gen_dir_index(
tera,
Expand All @@ -329,6 +336,7 @@ pub fn gen_dir_index(
let index = Path::new(dir_name).join("index.html");
let layers = index.components().count() - 1;
let prefix = "../".repeat(layers) + "index.html";
let bulma_path = "../".repeat(layers) + "bulma.min.css";
let output_file = output.join(index);
create_parent(&output_file);
let mut output = match File::create(&output_file) {
Expand All @@ -339,9 +347,9 @@ pub fn gen_dir_index(
Ok(f) => f,
};

let mut ctx = make_context();
let mut ctx = Context::new();
ctx.insert("date", &conf.date);
ctx.insert("bulma_version", BULMA_VERSION);
ctx.insert("bulma_path", &bulma_path);
ctx.insert("current", dir_name);
ctx.insert("parents", &[(prefix, "top_level")]);
ctx.insert("stats", &dir_stats.stats);
Expand Down Expand Up @@ -395,12 +403,13 @@ fn gen_html(
let base_url = get_base(rel_path);
let filename = rel_path.file_name().unwrap().to_str().unwrap();
let parent = rel_path.parent().unwrap().to_str().unwrap().to_string();
let bulma_path = format!("{base_url}bulma.min.css");
let mut index_url = base_url;
index_url.push_str("index.html");

let mut ctx = make_context();
let mut ctx = Context::new();
ctx.insert("date", &conf.date);
ctx.insert("bulma_version", BULMA_VERSION);
ctx.insert("bulma_path", &bulma_path);
ctx.insert("current", filename);
ctx.insert(
"parents",
Expand Down Expand Up @@ -540,7 +549,7 @@ pub fn gen_badge(tera: &Tera, stats: &HtmlStats, conf: &Config, output: &Path, s
Ok(f) => f,
};

let mut ctx = make_context();
let mut ctx = Context::new();
ctx.insert(
"current",
&(get_percentage_of_covered_lines(stats.covered_lines, stats.total_lines) as usize),
Expand Down
2 changes: 1 addition & 1 deletion src/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{% block title %}{% endblock title %}</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@{{ bulma_version }}/css/bulma.min.css">
<link rel="stylesheet" href="{{ bulma_path }}">
{%- endblock head -%}
</head>
<body>
Expand Down

0 comments on commit 7770c22

Please sign in to comment.