(&self, block: Block, meta: ParserMetadata, output: Option<String>)
| 359 | }); |
| 360 | let base_dir = base_dir.unwrap_or_else(|err| { |
| 361 | Message::new_err_msg("Couldn't get the absolute path to the provided input file") |
| 362 | .comment(err.to_string()) |
| 363 | .show(); |
| 364 | std::process::exit(1); |
| 365 | }); |
| 366 | let ast_forest = self.get_sorted_ast_forest(block, &meta); |
| 367 | let mut paths = vec![]; |
| 368 | for (path, block) in ast_forest { |
| 369 | let dep_path = { |
| 370 | let dep_path = match fs::canonicalize(PathBuf::from(path)) { |
| 371 | Ok(path) => path, |
| 372 | Err(_) => continue, |
| 373 | }; |
| 374 | if !dep_path.starts_with(&base_dir) { |
| 375 | continue; |
| 376 | } |
| 377 | dep_path |
| 378 | }; |
| 379 | let document = block.document(&meta); |
| 380 | // Check if an output directory was specified. |
| 381 | if let Some(output) = &output { |
| 382 | // Save to file; replace the base directory if the output |
| 383 | // path is absolute, otherwise append the output path. |
| 384 | let dir_path = { |
| 385 | let file_path = dep_path.strip_prefix(&base_dir).unwrap(); |
| 386 | let file_dir = file_path.parent().unwrap(); |
| 387 | base_dir.join(output).join(file_dir) |
| 388 | }; |
| 389 | if let Err(err) = fs::create_dir_all(dir_path.clone()) { |
| 390 | let message = format!( |
| 391 | "Couldn't create directory `{}`. Do you have sufficient permissions?", |
| 392 | dir_path.display() |
| 393 | ); |
| 394 | Message::new_err_msg(message) |
| 395 | .comment(err.to_string()) |
| 396 | .show(); |
| 397 | std::process::exit(1); |
| 398 | } |
| 399 | let filename = dep_path.file_stem().unwrap().to_string_lossy(); |
| 400 | let path = dir_path.join(format!("{filename}.md")); |
| 401 | let mut file = File::create(path.clone()).unwrap(); |
| 402 | file.write_all(document.as_bytes()).unwrap(); |
| 403 | paths.push(String::from(path.to_string_lossy())); |
| 404 | } else { |
| 405 | // Write to standard output. |
| 406 | std::io::stdout().write_all(document.as_bytes()).unwrap(); |
| 407 | } |
| 408 | } |
| 409 | if !paths.is_empty() { |
| 410 | let files = pluralize(paths.len(), "File", "Files"); |
| 411 | let message = once(format!("{files} generated at:")) |
| 412 | .chain(paths) |
| 413 | .join("\n"); |
| 414 | Message::new_info_msg(message).show(); |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | pub fn typecheck( |
no test coverage detected