Casey Rodarmor f4f7a69069
Add co-authored-by field to commit metadata
This field is used by GitHub to credit co-authors.

type: development
co-authored-by: y7y <pair@rodarmor.com>
2020-05-01 01:28:40 -07:00
..
2020-04-30 00:22:43 -07:00
2020-04-29 00:36:25 -07:00
2020-04-29 00:36:25 -07:00
2020-04-30 00:22:43 -07:00
2020-04-19 23:19:29 -07:00
2020-04-19 23:19:29 -07:00
2020-04-30 00:22:43 -07:00
2020-04-29 00:36:25 -07:00
2020-04-30 00:22:43 -07:00
2020-04-17 23:28:08 -07:00
2020-04-17 23:28:08 -07:00
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()),
    }
  }
}