1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-06-17 22:57:35 +02:00

Rename directory to match crate name.

This commit is contained in:
Andrew Walbran
2023-04-03 16:10:06 +01:00
parent 3e224e6f55
commit e204b5060f
5 changed files with 1 additions and 1 deletions

View File

@ -0,0 +1,75 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use log::{info, trace};
use pulldown_cmark::{Event, Parser, Tag};
use std::{
fs::{create_dir_all, File},
io::Write,
path::Path,
};
const FILENAME_START: &str = "<!-- File ";
const FILENAME_END: &str = " -->";
pub fn process(output_directory: &Path, input_contents: &str) -> anyhow::Result<()> {
let parser = Parser::new(input_contents);
// Find a specially-formatted comment followed by a code block, and then call `write_output`
// with the contents of the code block, to write to a file named by the comment. Code blocks
// without matching comments will be ignored, as will comments which are not followed by a code
// block.
let mut next_filename: Option<String> = None;
let mut current_file: Option<File> = None;
for event in parser {
trace!("{:?}", event);
match event {
Event::Html(html) => {
let html = html.trim();
if html.starts_with(FILENAME_START) && html.ends_with(FILENAME_END) {
next_filename = Some(
html[FILENAME_START.len()..html.len() - FILENAME_END.len()]
.to_string(),
);
info!("Next file: {:?}:", next_filename);
}
}
Event::Start(Tag::CodeBlock(x)) => {
info!("Start {:?}", x);
if let Some(filename) = &next_filename {
let full_filename = output_directory.join(filename);
info!("Opening {:?}", full_filename);
if let Some(directory) = full_filename.parent() {
create_dir_all(directory)?;
}
current_file = Some(File::create(full_filename)?);
next_filename = None;
}
}
Event::Text(text) => {
info!("Text: {:?}", text);
if let Some(output_file) = &mut current_file {
output_file.write(text.as_bytes())?;
}
}
Event::End(Tag::CodeBlock(x)) => {
info!("End {:?}", x);
current_file = None;
}
_ => {}
}
}
Ok(())
}

View File

@ -0,0 +1,70 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use anyhow::Context;
use log::trace;
use mdbook::{book::Book, renderer::RenderContext, BookItem};
use mdbook_exerciser::process;
use std::{
fs::{create_dir, remove_dir_all},
io::stdin,
path::Path,
};
fn main() -> anyhow::Result<()> {
pretty_env_logger::init();
let context = RenderContext::from_json(&mut stdin()).context("Parsing stdin")?;
let config = context
.config
.get_renderer("exerciser")
.context("Missing output.exerciser configuration")?;
let output_directory = Path::new(
config
.get("output-directory")
.context("Missing output.exerciser.output-directory configuration value")?
.as_str()
.context("Expected a string for output.exerciser.output-directory")?,
);
let _ = remove_dir_all(output_directory);
create_dir(output_directory).with_context(|| {
format!("Failed to create output directory {:?}", output_directory)
})?;
process_all(&context.book, output_directory)?;
Ok(())
}
fn process_all(book: &Book, output_directory: &Path) -> anyhow::Result<()> {
for item in book.iter() {
if let BookItem::Chapter(chapter) = item {
trace!("Chapter {:?} / {:?}", chapter.path, chapter.source_path);
if let Some(chapter_path) = &chapter.path {
// Put the exercises in a subdirectory named after the chapter file, without its
// parent directories.
let chapter_output_directory =
output_directory.join(chapter_path.file_stem().with_context(
|| format!("Chapter {:?} has no file stem", chapter_path),
)?);
process(&chapter_output_directory, &chapter.content)?;
}
}
}
Ok(())
}