Skip to content

Commit

Permalink
Use proper defines for files and directories
Browse files Browse the repository at this point in the history
  • Loading branch information
Coi-l committed Mar 26, 2024
1 parent 66bb88c commit b95aa5c
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/cmd/shelf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ use mdbook::config::Shelf;
use mdbook::errors::Result;
use mdbook::MDBook;

const SHELF_DIR: &str = "shelf";
const REPOS_DIR: &str = "repositories";
const INDEX_MD_FILE: &str = "index.md";
const INDEX_HTML_FILE: &str = "index.html";
const BOOKS_DIR: &str = "books";

pub fn make_subcommand() -> Command {
Command::new("shelf").about("Build a bookshelf from shelf.toml file")
}
Expand All @@ -23,15 +29,18 @@ fn process_book(path: &str, index_file: &mut File, summary: &mut File) -> Result
// Build book
let mut path = current_dir()?;
let title = book.config.book.title.clone().unwrap();
path.push("books/");
path.push(BOOKS_DIR);
path.push(title);
book.config.build.build_dir = path;
book.config.book.shelf_url = Some(PathBuf::from(r"../../shelf/book/index.html"));
book.build()?;

// Create post in index file
let title = book.config.book.title.unwrap_or_default();
writeln!(index_file, "## [{title}](<../../books/{title}/index.html>)")?;
writeln!(
index_file,
"## [{title}](<../../{BOOKS_DIR}/{title}/{INDEX_HTML_FILE}>)"
)?;
writeln!(index_file)?;
let desc = book.config.book.description.unwrap_or_default();
writeln!(index_file, "{desc}")?;
Expand All @@ -57,12 +66,12 @@ pub fn execute(_args: &ArgMatches) -> Result<()> {
file.read_to_string(&mut contents)?;
let shelf: Shelf = toml::from_str(&contents)?;

let _ = std::fs::remove_dir_all("shelf");
let _ = std::fs::remove_dir_all("repositories");
let shelf_book = MDBook::init("shelf").create_gitignore(false).build()?;
let _ = std::fs::remove_dir_all(SHELF_DIR);
let _ = std::fs::remove_dir_all(REPOS_DIR);
let shelf_book = MDBook::init(SHELF_DIR).create_gitignore(false).build()?;

let mut index_file_name = shelf_book.source_dir();
index_file_name.push("index.md");
index_file_name.push(INDEX_MD_FILE);
let mut index_file = File::create(index_file_name).unwrap();
writeln!(index_file, "# Bookshelf")?;
writeln!(index_file)?;
Expand All @@ -71,15 +80,15 @@ pub fn execute(_args: &ArgMatches) -> Result<()> {
summary_file_name.push("SUMMARY.md");
let mut summary = File::create(summary_file_name).unwrap();
writeln!(summary, "# Summary")?;
writeln!(summary, "- [Index](./index.md)")?;
writeln!(summary, "- [Index](./{INDEX_MD_FILE})")?;

for sb in &shelf.book {
if let Some(url) = &sb.git_url {
println!("{:?}", sb);
let path = sb.path.clone().unwrap_or("root".to_owned());
let repo_raw_name = url.split('/').last().unwrap_or(&path);
let repo_name = format!("{repo_raw_name}-{path}");
let mut checkout_path = PathBuf::from("repositories");
let mut checkout_path = PathBuf::from(REPOS_DIR);
checkout_path.push(repo_name);

let book_path = if let Some(path) = &sb.path {
Expand Down

0 comments on commit b95aa5c

Please sign in to comment.