| 1 | //! Writes sections as the website Export as Website makes, each section under the folders |
| 2 | //! its path names below `ROOT`. `export_site NEW-FOLDER ROOT SECTION.one...` |
| 3 | |
| 4 | use canvas::{layout::TextEngine, site}; |
| 5 | use onestore::{Arena, Section}; |
| 6 | use std::{env, fs, path::Path}; |
| 7 | |
| 8 | fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 9 | let args: Vec<String> = env::args().skip(1).collect(); |
| 10 | let [destination, root, sections @ ..] = args.as_slice() else { |
| 11 | return Err("Expected a new folder, a root and sections".into()); |
| 12 | }; |
| 13 | let mut listed = Vec::new(); |
| 14 | let mut read = Vec::new(); |
| 15 | for path in sections { |
| 16 | let arena = Arena::default(); |
| 17 | let bytes = fs::read(path)?; |
| 18 | let identity = onestore::Header::parse(bytes.get(..1024).ok_or("Too short")?)?.file_id; |
| 19 | let mut section = Section::open(&arena, bytes)?; |
| 20 | let (mut listings, mut pages) = (Vec::new(), Vec::new()); |
| 21 | for (space, title, level) in section.pages()? { |
| 22 | let page = section.page(space)?; |
| 23 | listings.push(site::Listing { |
| 24 | title, |
| 25 | level, |
| 26 | identity: page.identity, |
| 27 | }); |
| 28 | pages.push(page); |
| 29 | } |
| 30 | let relative = Path::new(path).strip_prefix(root)?; |
| 31 | let groups = (relative.parent().into_iter()) |
| 32 | .flat_map(|parent| parent.iter()) |
| 33 | .map(|group| group.to_string_lossy().into_owned()) |
| 34 | .collect(); |
| 35 | let name = relative.file_stem().unwrap_or_default().to_string_lossy(); |
| 36 | listed.push(site::Section { |
| 37 | groups, |
| 38 | name: name.into_owned(), |
| 39 | color: None, |
| 40 | identity: Some(identity), |
| 41 | pages: Some(listings), |
| 42 | }); |
| 43 | read.push(pages); |
| 44 | } |
| 45 | let destination = Path::new(destination); |
| 46 | let mut write = |name: &str, bytes: &[u8]| { |
| 47 | let path = destination.join(name); |
| 48 | fs::create_dir_all(path.parent().unwrap_or(destination))?; |
| 49 | fs::write(path, bytes) |
| 50 | }; |
| 51 | let mut site = site::Site::new("Corpus", listed); |
| 52 | let mut engine = TextEngine::default(); |
| 53 | for (section, pages) in read.into_iter().enumerate() { |
| 54 | for (at, page) in pages.into_iter().enumerate() { |
| 55 | site.page([section, at], page, &mut engine, &mut write)?; |
| 56 | } |
| 57 | } |
| 58 | site.finish(&mut write)?; |
| 59 | Ok(()) |
| 60 | } |