intermodal/bin/gen/src
Casey Rodarmor 39dcb5e183
Don't treat head as special in changelog
The changelog is no longer comitted, so we no longer need to avoid
including the commit hash.

type: documentation
2020-09-06 23:21:01 -07:00
..
arguments.rs Move all output from bin/gen to target/gen 2020-05-01 00:29:40 -07:00
bin_subcommand.rs Move all output from bin/gen to target/gen 2020-05-01 00:29:40 -07:00
bin.rs Move all output from bin/gen to target/gen 2020-05-01 00:29:40 -07:00
changelog.rs Don't treat head as special in changelog 2020-09-06 23:21:01 -07:00
cmd.rs Merge documentation and changelog generation 2020-04-17 21:31:54 -07:00
command_ext.rs Improve bin/gen error messages 2020-04-29 00:36:25 -07:00
common.rs Move all output from bin/gen to target/gen 2020-05-01 00:29:40 -07:00
config.rs Improve bin/gen error messages 2020-04-29 00:36:25 -07:00
entry.rs Don't treat head as special in changelog 2020-09-06 23:21:01 -07:00
error.rs Add --no-git flag to gen all 2020-06-23 21:51:13 -07:00
example.rs Generate reference sections with bin/gen 2020-04-18 19:14:54 -07:00
faq_entry.rs Create FAQ 2020-04-19 23:19:29 -07:00
faq.rs Create FAQ 2020-04-19 23:19:29 -07:00
introduction.rs Merge documentation and changelog generation 2020-04-17 21:31:54 -07:00
kind.rs Benchmark and improve hashing performance 2020-05-26 00:15:09 -07:00
main.rs Move all output from bin/gen to target/gen 2020-05-01 00:29:40 -07:00
metadata.rs Add co-authored-by field to commit metadata 2020-05-01 01:28:40 -07:00
options.rs Move all output from bin/gen to target/gen 2020-05-01 00:29:40 -07:00
package.rs Generate reference sections with bin/gen 2020-04-18 19:14:54 -07:00
project.rs Add --no-git flag to gen all 2020-06-23 21:51:13 -07:00
readme.rs Improve bin/gen error messages 2020-04-29 00:36:25 -07:00
reference_section.rs Move all output from bin/gen to target/gen 2020-05-01 00:29:40 -07:00
reference.rs Generate reference sections with bin/gen 2020-04-18 19:14:54 -07:00
release.rs Add changelog to book 2020-04-30 00:22:43 -07:00
row.rs Readme improvements 2020-04-17 23:28:08 -07:00
slug.rs Generate reference sections with bin/gen 2020-04-18 19:14:54 -07:00
subcommand.rs Add --no-git flag to gen book 2020-06-24 03:33:50 -07:00
summary.rs Add --no-git flag to gen book 2020-06-24 03:33:50 -07:00
table.rs Readme improvements 2020-04-17 23:28:08 -07:00
template_ext.rs Improve bin/gen error messages 2020-04-29 00:36:25 -07:00

use crate::common::*;

#[derive(Template)]
#[template(path = "README.md")]
pub(crate) struct Readme {
  pub(crate) table_of_contents: String,
  pub(crate) packages: Table<Package>,
}

const HEADING_PATTERN: &str = "(?m)^(?P<MARKER>#+) (?P<TEXT>.*)$";

impl Readme {
  #[throws]
  pub(crate) fn load(config: &Config, template: &Path) -> Readme {
    let text = fs::read_to_string(template).context(error::Filesystem { path: template })?;

    let header_re = Regex::new(HEADING_PATTERN)?;

    let mut lines = Vec::new();

    for captures in header_re.captures_iter(&text).skip(2) {
      let marker = captures.name("MARKER").unwrap().as_str();
      let text = captures.name("TEXT").unwrap().as_str();
      let level = marker.len();
      let indentation = " ".repeat((level - 2) * 2);
      let slug = text
        .to_lowercase()
        .replace(' ', "-")
        .replace('.', "")
        .replace('&', "");
      lines.push(format!("{}- [{}](#{})", indentation, text, slug));
    }

    Readme {
      table_of_contents: lines.join("\n"),
      packages: Table::new(config.packages.clone()),
    }
  }
}