diff --git a/Cargo.toml b/Cargo.toml index 8febedd..48fb261 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,3 +10,5 @@ serde = { version = "1.0.140", features = ["derive"] } toml = "0.5.9" xmltree = "0.10.3" mustache = { git = "https://git.shadamethyst.xyz/adri326/rust-mustache.git" } +clap = { version = "3.2", features = ["derive"] } +mkdirp = "1.0.0" diff --git a/species/blobfox/assets/reach_aww.svg b/species/blobfox/assets/reach_aww.svg new file mode 100644 index 0000000..8af0283 --- /dev/null +++ b/species/blobfox/assets/reach_aww.svg @@ -0,0 +1,194 @@ + + + + + blobfox_reach_aww + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Blobfox team (https://git.shadamethyst.xyz/adri326/blobfox), licensed under the Apache 2.0 Licenseblobfox_reach_awwFeuerfuchshttps://git.shadamethyst.xyz/adri326/blobfoxShad Amethyst + + diff --git a/species/blobfox/species.toml b/species/blobfox/species.toml index f13fa38..dc894a6 100644 --- a/species/blobfox/species.toml +++ b/species/blobfox/species.toml @@ -1 +1,2 @@ # Add options in here as needs be +name = "blobfox" diff --git a/species/blobfox/templates/base.mustache b/species/blobfox/templates/base.mustache index cbeaa72..e653823 100644 --- a/species/blobfox/templates/base.mustache +++ b/species/blobfox/templates/base.mustache @@ -1,7 +1,7 @@ - {{#base}}body{{/base}} - {{#base}}left-ear{{/base}} - {{#base}}hair{{/base}} - {{#base}}right-ear{{/base}} - {{#base}}right-ear-fluff{{/base}} + {{#base}}#body{{/base}} + {{#base}}#left-ear{{/base}} + {{#base}}#hair{{/base}} + {{#base}}#right-ear{{/base}} + {{#base}}#right-ear-fluff{{/base}} diff --git a/species/blobfox/templates/eyes-aww.mustache b/species/blobfox/templates/eyes-aww.mustache new file mode 100644 index 0000000..7a818b2 --- /dev/null +++ b/species/blobfox/templates/eyes-aww.mustache @@ -0,0 +1,4 @@ + + {{#reach_aww}}#left-eye{{/reach_aww}} + {{#reach_aww}}#right-eye{{/reach_aww}} + diff --git a/species/blobfox/templates/eyes.mustache b/species/blobfox/templates/eyes.mustache index 0581ed4..935bf81 100644 --- a/species/blobfox/templates/eyes.mustache +++ b/species/blobfox/templates/eyes.mustache @@ -1,4 +1,4 @@ - {{#base}}left-eye{{/base}} - {{#base}}right-eye{{/base}} + {{#base}}#left-eye{{/base}} + {{#base}}#right-eye{{/base}} diff --git a/species/blobfox/templates/footer.mustache b/species/blobfox/templates/footer.mustache index b590cc4..c2667cb 100644 --- a/species/blobfox/templates/footer.mustache +++ b/species/blobfox/templates/footer.mustache @@ -1 +1,2 @@ + diff --git a/species/blobfox/templates/hands-reach.mustache b/species/blobfox/templates/hands-reach.mustache new file mode 100644 index 0000000..591bb43 --- /dev/null +++ b/species/blobfox/templates/hands-reach.mustache @@ -0,0 +1,4 @@ + + {{#reach_aww}}#left-hand{{/reach_aww}} + {{#reach_aww}}#right-hand{{/reach_aww}} + diff --git a/species/blobfox/templates/header.mustache b/species/blobfox/templates/header.mustache index 05199ce..54ae65c 100644 --- a/species/blobfox/templates/header.mustache +++ b/species/blobfox/templates/header.mustache @@ -1,2 +1,3 @@ {{variant_name}} + diff --git a/species/blobfox/templates/mouth-w.mustache b/species/blobfox/templates/mouth-w.mustache index c0e10bc..8e1d73a 100644 --- a/species/blobfox/templates/mouth-w.mustache +++ b/species/blobfox/templates/mouth-w.mustache @@ -1 +1 @@ -{{#base}}mouth{{/base}} +{{#base}}#mouth{{/base}} diff --git a/species/blobfox/templates/nose-aww.mustache b/species/blobfox/templates/nose-aww.mustache new file mode 100644 index 0000000..11abd48 --- /dev/null +++ b/species/blobfox/templates/nose-aww.mustache @@ -0,0 +1,4 @@ + + {{#base}}#nose{{/base}} + {{#reach_aww}}#nose-outline{{/reach_aww}} + diff --git a/species/blobfox/templates/nose.mustache b/species/blobfox/templates/nose.mustache index 815ce1e..ea89fd8 100644 --- a/species/blobfox/templates/nose.mustache +++ b/species/blobfox/templates/nose.mustache @@ -1,4 +1,4 @@ - {{#base}}nose-outline{{/base}} - {{#base}}nose{{/base}} + {{#base}}#nose-outline{{/base}} + {{#base}}#nose{{/base}} diff --git a/species/blobfox/variants/base.mustache b/species/blobfox/variants/base.mustache index 30488d9..c1ab55d 100644 --- a/species/blobfox/variants/base.mustache +++ b/species/blobfox/variants/base.mustache @@ -1,6 +1,10 @@ {{>header}} - {{>base}} - {{>eyes}} - {{>nose}} - {{>mouth-w}} + + {{>base}} + + + {{>eyes}} + {{>nose}} + {{>mouth-w}} + {{>footer}} diff --git a/species/blobfox/variants/owo.mustache b/species/blobfox/variants/owo.mustache new file mode 100644 index 0000000..d262dac --- /dev/null +++ b/species/blobfox/variants/owo.mustache @@ -0,0 +1,10 @@ +{{>header}} + + {{>base}} + + + {{>eyes-aww}} + {{>nose-aww}} + {{>mouth-w}} + +{{>footer}} diff --git a/species/blobfox/variants/reach.mustache b/species/blobfox/variants/reach.mustache new file mode 100644 index 0000000..56fa5ea --- /dev/null +++ b/species/blobfox/variants/reach.mustache @@ -0,0 +1,11 @@ +{{>header}} + + {{>base}} + {{>hands-reach}} + + + {{>eyes}} + {{>nose}} + {{>mouth-w}} + +{{>footer}} diff --git a/species/blobfox/variants/reach_aww.mustache b/species/blobfox/variants/reach_aww.mustache new file mode 100644 index 0000000..695f33e --- /dev/null +++ b/species/blobfox/variants/reach_aww.mustache @@ -0,0 +1,11 @@ +{{>header}} + + {{>base}} + {{>hands-reach}} + + + {{>eyes-aww}} + {{>nose-aww}} + {{>mouth-w}} + +{{>footer}} diff --git a/src/main.rs b/src/main.rs index 9b82bfa..4fc5398 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,6 @@ use std::sync::Arc; +use clap::Parser; +use std::path::PathBuf; pub mod parse; use parse::*; @@ -7,10 +9,56 @@ pub mod template; use template::*; fn main() { - let species = Arc::new(dbg!(load_species("species/blobfox")).unwrap()); + let args = Args::parse(); + + let species = Arc::new(load_species(args.decl).unwrap()); let context = RenderingContext::new(species); - let template = context.compile("species/blobfox/variants/base.svg").unwrap(); - let rendered = template.render_data_to_string(&context.get_data()).unwrap(); - println!("{}", rendered); - std::fs::write("./test.svg", rendered).unwrap(); + + let output_dir = args.output_dir.unwrap_or(PathBuf::from("output/vector/")); + + mkdirp::mkdirp(output_dir.clone()).unwrap(); + + if args.names.is_empty() { + for name in context.species().variants.keys() { + generate_variant(&context, name, &output_dir); + } + } else { + for name in args.names.iter() { + generate_variant(&context, name, &output_dir); + } + } +} + +fn generate_variant(context: &RenderingContext, name: &str, output_dir: &PathBuf) { + if let Some(path) = context.species().variants.get(name) { + match context.compile(path).and_then(|template| { + template.render_data_to_string(&context.get_data()) + }) { + Ok(rendered) => { + let output = output_dir.join(&format!("{}_{}.svg", context.species().name, name)); + std::fs::write(output, rendered).unwrap(); + } + Err(err) => { + eprintln!("Error while rendering {}: {}", name, err); + } + } + } else { + eprintln!("No variant named {}!", name); + } +} + +#[derive(Parser, Debug)] +#[clap(author, version, about, long_about = None)] +struct Args { + /// A folder containing the declaration from which the emotes should be generated + #[clap(short, long, value_parser)] + decl: PathBuf, + + /// List of the emote names to export + #[clap(value_parser)] + names: Vec, + + /// Output directory + #[clap(short, long, value_parser)] + output_dir: Option, } diff --git a/src/parse.rs b/src/parse.rs index ea2214b..f438616 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -1,4 +1,4 @@ -use xmltree::{XMLNode, Element}; +use xmltree::{Element}; use serde::{Serialize, Deserialize}; use std::path::{PathBuf, Path}; use std::collections::HashMap; @@ -28,6 +28,9 @@ pub struct SpeciesDecl { /// Imports xml and svg files from this folder if they aren't found pub base: Option, + /// The name of the species + pub name: String, + #[serde(skip)] pub templates: HashMap, diff --git a/src/template.rs b/src/template.rs index 4bf21e1..16ace95 100644 --- a/src/template.rs +++ b/src/template.rs @@ -111,6 +111,10 @@ impl RenderingContext { None } } + + pub fn species(&self) -> Arc { + Arc::clone(&self.species) + } } impl PartialLoader for RenderingContext { @@ -133,11 +137,13 @@ pub fn query_selector(svg: Element, pattern: &str) -> Option { for child in svg.children { if let XMLNode::Element(child) = child { - if child.attributes.get("id").map(|id| id == pattern).unwrap_or(false) { - return Some(child); - } else if child.children.len() > 0 { - if let Some(res) = query_selector(child, pattern) { - return Some(res); + if let ("#", pattern_id) = pattern.split_at(1) { + if child.attributes.get("id").map(|id| id == pattern_id).unwrap_or(false) { + return Some(child); + } else if child.children.len() > 0 { + if let Some(res) = query_selector(child, pattern) { + return Some(res); + } } } } @@ -152,7 +158,7 @@ pub fn xml_to_string(element: Element) -> Option { config.perform_indent = true; config.write_document_declaration = false; - element.write_with_config(&mut s, config); + element.write_with_config(&mut s, config).ok()?; String::from_utf8(s).ok() } diff --git a/vector/blobfox_reach_aww.svg b/vector/blobfox_reach_aww.svg index adaea0e..8af0283 100644 --- a/vector/blobfox_reach_aww.svg +++ b/vector/blobfox_reach_aww.svg @@ -2,159 +2,193 @@ blobfox_reach_awwBlobfox team (https://git.shadamethyst.xyz/adri326/blobfox), licensed under the Apache 2.0 Licenseblobfox_reach_awwFeuerfuchshttps://git.shadamethyst.xyz/adri326/blobfoxShad Amethyst + width="128" + height="128" + viewBox="0 0 33.866668 33.866668" + version="1.1" + id="svg5" + inkscape:version="1.2.1 (9c6d41e410, 2022-07-14)" + sodipodi:docname="blobfox_reach_aww.svg" + xml:space="preserve" + xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" + xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" + xmlns:xlink="http://www.w3.org/1999/xlink" + xmlns="http://www.w3.org/2000/svg" + xmlns:svg="http://www.w3.org/2000/svg" + xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" + xmlns:cc="http://creativecommons.org/ns#" + xmlns:dc="http://purl.org/dc/elements/1.1/"> + blobfox_reach_aww + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Blobfox team (https://git.shadamethyst.xyz/adri326/blobfox), licensed under the Apache 2.0 Licenseblobfox_reach_awwFeuerfuchshttps://git.shadamethyst.xyz/adri326/blobfoxShad Amethyst + +