intermodal/bin/gen/src
Casey Rodarmor 3ed449ce93
Generate reference sections with bin/gen
type: documentation
2020-04-18 19:14:54 -07:00
..
bin.rs Merge documentation and changelog generation 2020-04-17 21:31:54 -07:00
changelog.rs Merge documentation and changelog generation 2020-04-17 21:31:54 -07:00
cmd.rs Merge documentation and changelog generation 2020-04-17 21:31:54 -07:00
command_ext.rs Merge documentation and changelog generation 2020-04-17 21:31:54 -07:00
common.rs Generate reference sections with bin/gen 2020-04-18 19:14:54 -07:00
config.rs Generate reference sections with bin/gen 2020-04-18 19:14:54 -07:00
entry.rs Merge documentation and changelog generation 2020-04-17 21:31:54 -07:00
example.rs Generate reference sections with bin/gen 2020-04-18 19:14:54 -07:00
exit_status_ext.rs Merge documentation and changelog generation 2020-04-17 21:31:54 -07:00
introduction.rs Merge documentation and changelog generation 2020-04-17 21:31:54 -07:00
kind.rs Merge documentation and changelog generation 2020-04-17 21:31:54 -07:00
main.rs Generate reference sections with bin/gen 2020-04-18 19:14:54 -07:00
metadata.rs Generate reference sections with bin/gen 2020-04-18 19:14:54 -07:00
opt.rs Generate reference sections with bin/gen 2020-04-18 19:14:54 -07:00
package.rs Generate reference sections with bin/gen 2020-04-18 19:14:54 -07:00
project.rs Merge documentation and changelog generation 2020-04-17 21:31:54 -07:00
readme.rs Readme improvements 2020-04-17 23:28:08 -07:00
reference_section.rs Generate reference sections with bin/gen 2020-04-18 19:14:54 -07:00
reference.rs Generate reference sections with bin/gen 2020-04-18 19:14:54 -07:00
release.rs Merge documentation and changelog generation 2020-04-17 21:31:54 -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 Generate reference sections with bin/gen 2020-04-18 19:14:54 -07:00
summary.rs Generate reference sections with bin/gen 2020-04-18 19:14:54 -07:00
table.rs Readme improvements 2020-04-17 23:28:08 -07:00
template_ext.rs Generate reference sections with bin/gen 2020-04-18 19:14:54 -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)?;

    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()),
    }
  }
}