|
9 | 9 |
|
10 | 10 | use std::collections::HashMap;
|
11 | 11 | use std::io;
|
| 12 | +use std::path::Path; |
12 | 13 | use std::process;
|
13 | 14 |
|
14 |
| -use anyhow::Context; |
| 15 | +use anyhow::{anyhow, Context}; |
15 | 16 | use clap::{App, Arg, ArgMatches, SubCommand};
|
16 | 17 | use mdbook::book::Book;
|
17 | 18 | use mdbook::errors::Error;
|
@@ -97,7 +98,9 @@ impl Preprocessor for Pandocs {
|
97 | 98 | renderer != "not-supported"
|
98 | 99 | }
|
99 | 100 |
|
100 |
| - fn run(&self, _: &PreprocessorContext, mut book: Book) -> Result<Book, Error> { |
| 101 | + fn run(&self, ctx: &PreprocessorContext, mut book: Book) -> Result<Book, Error> { |
| 102 | + let config = ctx.config.get_preprocessor("pandocs"); |
| 103 | + |
101 | 104 | let mut sections = HashMap::new();
|
102 | 105 | for item in book.iter() {
|
103 | 106 | if let BookItem::Chapter(ref chapter) = item {
|
@@ -130,11 +133,20 @@ impl Preprocessor for Pandocs {
|
130 | 133 | abort_if_err!(self.process_admonitions(chapter));
|
131 | 134 |
|
132 | 135 | if chapter.name == "Foreword" {
|
133 |
| - let commit = abort_if_err!(Commit::rev_parse("HEAD")); |
134 |
| - chapter.content.push_str(&format!( |
135 |
| - "<small>This document version was produced from git commit [`{}`](https://github.com/gbdev/pandocs/tree/{}) ({}). </small>", |
136 |
| - commit.short_hash(), commit.hash(), commit.timestamp(), |
137 |
| - )); |
| 136 | + // If the `.git` directory exists, we're very likely on a dev machine, |
| 137 | + // so it's safe to assume the command is installed. |
| 138 | + if Path::new(".git").exists() { |
| 139 | + let commit = abort_if_err!(Commit::rev_parse("HEAD")); |
| 140 | + chapter.content.push_str(&format!( |
| 141 | + "<small>This document version was produced from git commit [`{}`](https://github.com/gbdev/pandocs/tree/{}) ({}).</small>", |
| 142 | + commit.short_hash(), commit.hash(), commit.timestamp(), |
| 143 | + )); |
| 144 | + } else if matches!(config.and_then(|config| config.get("out-of-repo")).and_then(|value| value.as_bool()), Some(true)) { |
| 145 | + // OK, just don't add anything. |
| 146 | + } else { |
| 147 | + res = Err(anyhow!("Git metadata is missing, but out-of-repo builds are not enabled!\n\tYou can enable them by setting `preprocessor.pandocs.out-of-repo` to `true`.\n\t(Consider using an environment variable for this:\n\t https://rust-lang.github.io/mdBook/format/configuration/environment-variables.html)")); |
| 148 | + return; |
| 149 | + } |
138 | 150 | }
|
139 | 151 | }
|
140 | 152 | });
|
|
0 commit comments