Tex export more flexible

This commit is contained in:
Pierre HUBERT 2023-02-06 09:13:16 +01:00
parent 8c93ab9b78
commit 94ec495057

View File

@ -22,7 +22,7 @@ struct Args {
action: Action, action: Action,
} }
#[derive(Subcommand, Debug)] #[derive(Subcommand, Debug, Eq, PartialEq)]
enum Action { enum Action {
/// Dump as JSON /// Dump as JSON
Json, Json,
@ -31,7 +31,11 @@ enum Action {
Graph, Graph,
/// Dump as Tex list /// Dump as Tex list
Tex, Tex {
/// Dump a single entry
#[arg(short, long)]
single: bool,
},
} }
fn main() { fn main() {
@ -45,13 +49,23 @@ fn main() {
}; };
let schema = parse_schema(&file_content); let schema = parse_schema(&file_content);
let tree = build_tree(&args.struct_name, schema.components.as_ref().unwrap()); let components = schema.components.as_ref().unwrap();
if args.action == (Action::Tex { single: false }) {
for entry in components.schemas.keys() {
let tree = build_tree(entry, components);
println!("{}", tex_export(&tree));
}
return;
}
let tree = build_tree(&args.struct_name, components);
match args.action { match args.action {
Action::Json => println!("{}", serde_json::to_string(&tree).unwrap()), Action::Json => println!("{}", serde_json::to_string(&tree).unwrap()),
Action::Graph => println!("{}", graphviz_export(&tree)), Action::Graph => println!("{}", graphviz_export(&tree)),
Action::Tex { .. } => println!("{}", tex_export(&tree)),
Action::Tex => println!("{}", tex_export(&tree)),
} }
} }
@ -93,7 +107,6 @@ fn tex_escape_str(s: &str) -> String {
} }
fn tex_export_inner(tree: &ObjectChild, out: &mut String, required: bool) { fn tex_export_inner(tree: &ObjectChild, out: &mut String, required: bool) {
out.push_str("\\begin{minipage}{0.45\\textwidth}\n");
let type_str = match &tree.node.r#type { let type_str = match &tree.node.r#type {
NodeType::Null => "NULL".to_string(), NodeType::Null => "NULL".to_string(),
NodeType::Boolean => "bool".to_string(), NodeType::Boolean => "bool".to_string(),
@ -104,45 +117,45 @@ fn tex_export_inner(tree: &ObjectChild, out: &mut String, required: bool) {
NodeType::Integer => "integer".to_string(), NodeType::Integer => "integer".to_string(),
}; };
write!(out, "\\textbf{{{}}}", tex_escape_str(&tree.name)).unwrap(); writeln!(
if required {
out.push_str("\\textcolor{red}{*}");
}
out.push_str("\n\\newline");
write!(
out, out,
"\\textit{{\\textcolor{{gray}}{{{}}}}}", "\\schemaprop{{{}}}{{{}}}{{{}}}{{{}}}{{{}}}",
tex_escape_str(&type_str) tex_escape_str(&tree.name),
match required {
true => "true",
false => "false",
},
tex_escape_str(&type_str),
match &tree.node.description {
None => "".to_string(),
Some(d) => tex_escape_str(d),
},
match tree.node.examples.get(0) {
None => "".to_string(),
Some(e) => tex_escape_str(e),
}
) )
.unwrap(); .unwrap();
if let Some(description) = &tree.node.description {
write!(
out,
"\\newline\n\\textcolor{{gray}}{{{}}}",
tex_escape_str(description)
)
.unwrap();
}
if let Some(example) = &tree.node.examples.get(0) {
write!(
out,
"\\newline\n\\textcolor{{gray}}{{Exemple : {}}}",
tex_escape_str(example)
)
.unwrap();
}
out.push_str("\\end{minipage}\n");
} }
fn tex_export(tree: &TreeNode) -> String { fn tex_export(tree: &TreeNode) -> String {
let mut out = String::new(); let mut out = String::new();
writeln!(out, "% START OF EXPORT OF STRUCTURE {}", tree.name).unwrap(); writeln!(out, "% START OF EXPORT OF SCHEMA {}", tree.name).unwrap();
writeln!(out, "\\newcommand{{\\schemadef{}}}{{", tree.name).unwrap();
match &tree.r#type { match &tree.r#type {
NodeType::Object { children, required } => { NodeType::Object { children, required } => {
writeln!(out, "\\schemaname{{{}}}", tex_escape_str(&tree.name)).unwrap();
if let Some(description) = &tree.description {
writeln!(
out,
"\\schemadescription{{{}}}",
tex_escape_str(description)
)
.unwrap();
}
for child in children { for child in children {
tex_export_inner( tex_export_inner(
child, child,
@ -152,8 +165,6 @@ fn tex_export(tree: &TreeNode) -> String {
.map(|r| r.contains(&child.name)) .map(|r| r.contains(&child.name))
.unwrap_or(false), .unwrap_or(false),
); );
out.push_str("\n\n \\vspace{0.2cm} \n\n");
} }
} }
_ => tex_export_inner( _ => tex_export_inner(
@ -166,6 +177,7 @@ fn tex_export(tree: &TreeNode) -> String {
), ),
} }
writeln!(out, "% END OF EXPORT OF STRUCTURE {}", tree.name).unwrap(); out.push_str("}\n");
writeln!(out, "% END OF EXPORT OF SCHEMAS {}", tree.name).unwrap();
out out
} }