Skip to content

Commit 03f4581

Browse files
db48xrdmsr
authored andcommitted
allow the user to use globs in the list of extra files
and only issue a warning if any of them cannot be read, so that the rest of the documentation is still generated.
1 parent 2cb50de commit 03f4581

File tree

4 files changed

+45
-42
lines changed

4 files changed

+45
-42
lines changed

CONFIG.md

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Configuration options
2-
The configuration file is a TOML file with the following sections:
2+
The configuration file is a TOML file named `config.toml` with the following sections:
33

44
## `project`
55
- `name`: Project name.
@@ -9,7 +9,6 @@ The configuration file is a TOML file with the following sections:
99
- `glob`: Glob to use for finding documented files.
1010
- `compiler_arguments`: List of arguments to pass to libclang while parsing code.
1111

12-
1312
## `output`
1413
- `static_dir`: Path to a directory containing static files that will be copied in the output directory, this is where `style.css` will typically be located.
1514
- `path`: Path to the output directory.
@@ -18,7 +17,7 @@ The configuration file is a TOML file with the following sections:
1817

1918
## `pages`
2019
- `index` (optional): Markdown file to use as the index file, if an index page is not specified, the root namespace's comment will be used instead.
21-
- `extra` (optional): List of file paths to serve as extra documentation pages.
20+
- `extra` (optional): List of file paths to serve as extra documentation pages. These may be globs.
2221

2322
## `doctests` (optional)
2423
- `enable`: Whether to enable documentation tests or not.
@@ -51,4 +50,3 @@ enable = false
5150
run = true
5251
compiler_invocation = ["clang++", "{file}", "-o", "{out}", "-Iinclude", "-std=c++20"]
5352
```
54-

src/main.rs

+36-36
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use glob::glob;
33
use indicatif::{ProgressBar, ProgressStyle};
44
use render::get_path_for_name;
55
use serde::Serialize;
6-
use std::time::Duration;
6+
use std::{path::Path, time::Duration};
77

88
mod comment;
99
mod config;
@@ -13,7 +13,7 @@ mod render;
1313
mod report;
1414
mod templates;
1515

16-
use report::report_error;
16+
use report::{report_error, report_warning};
1717

1818
#[derive(Parser, Debug)]
1919
struct Cli {
@@ -75,17 +75,17 @@ fn main() {
7575
let bar = ProgressBar::new_spinner();
7676

7777
for file in glob(&config.input.glob).expect("Failed to read glob pattern") {
78-
let file = match file {
79-
Ok(file) => file,
78+
match file {
79+
Ok(file) => {
80+
bar.set_message(format!("Parsing {}", file.to_str().unwrap()));
81+
parser.parse(&config, file.to_str().unwrap(), &mut output);
82+
bar.tick();
83+
},
8084
Err(e) => {
81-
report_error(&format!("Error reading file: {}", e));
82-
std::process::exit(1);
85+
report_warning(&format!("Error reading input file: {e:}"));
8386
}
8487
};
8588

86-
bar.set_message(format!("Parsing {}", file.to_str().unwrap()));
87-
parser.parse(&config, file.to_str().unwrap(), &mut output);
88-
bar.tick();
8989
}
9090

9191
bar.finish_and_clear();
@@ -125,25 +125,30 @@ fn main() {
125125

126126
let mut extra_pages = Vec::new();
127127

128-
for page_path in config.pages.extra.clone().unwrap_or_default() {
129-
let page_source = match std::fs::read_to_string(&page_path) {
130-
Ok(source) => source,
131-
Err(e) => {
132-
report_error(&format!("Error reading file: {}", e));
133-
std::process::exit(1);
134-
}
135-
};
136-
137-
let mut page =
138-
render::process_markdown(&page_source, &output.index, &mut doctests, &config);
139-
140-
if page.title.is_empty() {
141-
page.title = page_path.split("/").last().unwrap().to_string();
128+
for g in &config.pages.extra.clone().unwrap_or_default() {
129+
for file in glob(g).expect("Failed to read glob pattern") {
130+
match file {
131+
Ok(page_path) => {
132+
match std::fs::read_to_string(&page_path) {
133+
Ok(source) => {
134+
let mut page =
135+
render::process_markdown(&source, &output.index, &mut doctests, &config);
136+
if page.title.is_empty() {
137+
page.title = page_path.file_name().unwrap().to_string_lossy().into_owned();
138+
}
139+
page.path = page_path;
140+
extra_pages.push(page);
141+
},
142+
Err(e) => {
143+
report_warning(&format!("Error reading extra file “{page_path:?}”: {e}"));
144+
}
145+
};
146+
},
147+
Err(e) => {
148+
report_warning(&format!("Error reading extra file “{g}”: {e}"));
149+
}
150+
};
142151
}
143-
144-
page.path = page_path.to_string();
145-
146-
extra_pages.push(page);
147152
}
148153

149154
let pages = Pages {
@@ -192,13 +197,8 @@ fn main() {
192197
.unwrap();
193198

194199
for page in &pages.extra {
195-
let path = page.path.split("/").collect::<Vec<&str>>();
196-
std::fs::create_dir_all(format!(
197-
"{}/{}",
198-
config.output.path,
199-
path[..path.len() - 1].join("/")
200-
))
201-
.map_err(|e| {
200+
let path = Path::new(&config.output.path).join(page.path.parent().unwrap_or_else(|| &Path::new("")));
201+
std::fs::create_dir_all(path).map_err(|e| {
202202
report_error(&format!("Error creating output directory: {}", e));
203203
std::process::exit(1);
204204
})
@@ -217,7 +217,7 @@ fn main() {
217217
context.insert("title", &page.title);
218218

219219
std::fs::write(
220-
format!("{}/{}.html", config.output.path, page.path),
220+
format!("{}/{}.html", config.output.path, page.path.display()),
221221
tera.render("docpage", &context).unwrap(),
222222
)
223223
.map_err(|e| {
@@ -284,7 +284,7 @@ fn main() {
284284
index.push(SearchIndex {
285285
id,
286286
name: page.title.clone(),
287-
link: page.path.clone(),
287+
link: page.path.to_string_lossy().into_owned(),
288288
kind: "page".to_string(),
289289
});
290290

src/render.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::report::report_warning;
55

66
use serde::Serialize;
77
use std::collections::HashMap;
8+
use std::path::PathBuf;
89

910
use pulldown_cmark::{CodeBlockKind, Event, Tag, TagEnd};
1011
use pygmentize::HtmlFormatter;
@@ -13,7 +14,7 @@ use pygmentize::HtmlFormatter;
1314
pub struct Page {
1415
pub title: String,
1516
pub content: String,
16-
pub path: String,
17+
pub path: PathBuf,
1718
}
1819

1920
pub fn get_path_for_name(name: &str, index: &HashMap<String, String>) -> Option<String> {
@@ -177,7 +178,7 @@ pub fn process_markdown(
177178
Page {
178179
content: html_output,
179180
title,
180-
path: "".to_string(),
181+
path: PathBuf::new()
181182
}
182183
}
183184

src/report.rs

+4
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,7 @@ pub fn report_error(error: &str) {
22
eprintln!("\x1b[1;31mError\x1b[0m: {}", error);
33
std::process::exit(1);
44
}
5+
6+
pub fn report_warning(error: &str) {
7+
eprintln!("\x1b[1;33mWarning\x1b[0m: {}", error);
8+
}

0 commit comments

Comments
 (0)