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,
}
#[derive(Subcommand, Debug)]
#[derive(Subcommand, Debug, Eq, PartialEq)]
enum Action {
/// Dump as JSON
Json,
@ -31,7 +31,11 @@ enum Action {
Graph,
/// Dump as Tex list
Tex,
Tex {
/// Dump a single entry
#[arg(short, long)]
single: bool,
},
}
fn main() {
@ -45,13 +49,23 @@ fn main() {
};
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 {
Action::Json => println!("{}", serde_json::to_string(&tree).unwrap()),
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) {
out.push_str("\\begin{minipage}{0.45\\textwidth}\n");
let type_str = match &tree.node.r#type {
NodeType::Null => "NULL".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(),
};
write!(out, "\\textbf{{{}}}", tex_escape_str(&tree.name)).unwrap();
if required {
out.push_str("\\textcolor{red}{*}");
}
out.push_str("\n\\newline");
write!(
writeln!(
out,
"\\textit{{\\textcolor{{gray}}{{{}}}}}",
tex_escape_str(&type_str)
"\\schemaprop{{{}}}{{{}}}{{{}}}{{{}}}{{{}}}",
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();
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 {
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 {
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 {
tex_export_inner(
child,
@ -152,8 +165,6 @@ fn tex_export(tree: &TreeNode) -> String {
.map(|r| r.contains(&child.name))
.unwrap_or(false),
);
out.push_str("\n\n \\vspace{0.2cm} \n\n");
}
}
_ => 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
}