~neon/activity-graph

bca8fe0fccdadedfa0518f65ac67961c6360e6cf — Jens Pitkanen 2 years ago 8f26873
Add --external-css
3 files changed, 20 insertions(+), 13 deletions(-)

M src/main.rs
M src/render.rs
M src/server.rs
M src/main.rs => src/main.rs +6 -3
@@ 48,7 48,7 @@ pub struct Args {
    #[structopt(flatten)]
    gen: GenerationData,
    #[structopt(flatten)]
    ext: ExternalHtml,
    ext: ExternalResources,
}

#[derive(StructOpt, Default, Clone)]


@@ 68,7 68,7 @@ pub struct GenerationData {
}

#[derive(StructOpt, Clone, Default)]
pub struct ExternalHtml {
pub struct ExternalResources {
    /// A html file that will be pasted in the <head> element
    #[structopt(long)]
    external_head: Option<PathBuf>,


@@ 80,6 80,9 @@ pub struct ExternalHtml {
    /// element
    #[structopt(long)]
    external_footer: Option<PathBuf>,
    /// A css file that will be pasted at the end of the css
    #[structopt(long)]
    external_css: Option<PathBuf>,
}

#[derive(StructOpt)]


@@ 149,7 152,7 @@ fn main() {
                write_to_file(&html, output_html, "html");

                if let Some(css) = css {
                    let output_css = render::css();
                    let output_css = render::css(&args.ext);
                    write_to_file(&css, output_css, "css");
                }
            }

M src/render.rs => src/render.rs +13 -9
@@ 6,7 6,7 @@ use std::fs::File;
use std::io::{BufReader, Read};
use std::path::{Component, PathBuf};

use crate::{log, Day, ExternalHtml, ProjectMetadata, Year};
use crate::{log, Day, ExternalResources, ProjectMetadata, Year};

static HTML_HEAD: &str = include_str!("head.html");
static CSS: &str = include_str!("activity-graph.css");


@@ 63,14 63,20 @@ pub fn gather_years(

/// Renders a HTML visualization of the commits based on the
/// arguments.
pub fn html(ext: &ExternalHtml, html: &PathBuf, css: Option<&PathBuf>, years: &[Year]) -> String {
pub fn html(
    ext: &ExternalResources,
    html_path: &PathBuf,
    css_path: Option<&PathBuf>,
    years: &[Year],
) -> String {
    // Prepare the html scaffolding around the tables
    let external_head = read_optional_file(&ext.external_head).unwrap_or_else(String::new);
    let external_header = read_optional_file(&ext.external_header).unwrap_or_else(String::new);
    let external_footer = read_optional_file(&ext.external_footer).unwrap_or_else(String::new);
    let external_css = read_optional_file(&ext.external_css).unwrap_or_else(String::new);

    let mut style = None;
    if let (Some(base), Some(css_path)) = (html.parent(), &css) {
    if let (Some(base), Some(css_path)) = (html_path.parent(), &css_path) {
        if let Some(relative_path) = pathdiff::diff_paths(&css_path, base) {
            // Add the <link> element instead of <style> if using external css
            let path = create_web_path(relative_path);


@@ 78,7 84,7 @@ pub fn html(ext: &ExternalHtml, html: &PathBuf, css: Option<&PathBuf>, years: &[
        }
    }
    if style.is_none() {
        style = Some(format!("<style>\n{}</style>", CSS));
        style = Some(format!("<style>\n{}\n{}</style>", CSS, external_css));
    }
    let style = style.unwrap();



@@ 123,11 129,9 @@ pub fn html(ext: &ExternalHtml, html: &PathBuf, css: Option<&PathBuf>, years: &[
    result
}

/// Note: currently this just returns an owned version of a static
/// string, this function exists so it's consistent with
/// `render_html`.
pub fn css() -> String {
    CSS.to_string()
pub fn css(ext: &ExternalResources) -> String {
    let external_css = read_optional_file(&ext.external_css).unwrap_or_else(String::new);
    format!("{}\n{}", CSS, external_css)
}

/// Renders an ASCII visualization of the commits.

M src/server.rs => src/server.rs +1 -1
@@ 93,7 93,7 @@ async fn refresh_caches() {
                let html_path = PathBuf::from("/index");
                let css_path = PathBuf::from("/activity-graph.css");
                let output_html = render::html(&ext, &html_path, Some(&css_path), &years);
                let output_css = render::css();
                let output_css = render::css(&ext);
                if let Ok(mut html) = CACHED_HTML.write() {
                    *html = output_html;
                }