Fix JSON examples presentation

This commit is contained in:
Pierre HUBERT 2023-03-01 09:41:23 +01:00
parent 277be04ae7
commit 47f79a9e1d

View File

@ -202,6 +202,20 @@ fn tex_adapt_name(i: &str) -> String {
fn tex_export(tree: &TreeNode) -> String {
let mut out = String::new();
writeln!(out, "% START OF EXPORT OF SCHEMA {}", tree.name).unwrap();
let box_name = format!("\\codeBox{}", tex_adapt_name(&tree.name));
if matches!(tree.r#type, NodeType::Object { .. }) {
// JSON export
out.push_str(&format!("\\newsavebox{{{box_name}}}\n"));
out.push_str(&format!("\\begin{{lrbox}}{{{box_name}}}\n"));
out.push_str("\\begin{lstlisting}[language=json]\n");
let json_doc = serde_json::to_string_pretty(&tree.example_value(1)).unwrap();
let replace_key = serde_json::to_string(REF_OBJECT).unwrap();
out.push_str(&json_doc.replace(&format!("{replace_key}:"), "$ref"));
out.push_str("\n\\end{lstlisting}\n");
out.push_str("\\end{lrbox}\n");
}
writeln!(
out,
"\\newcommand{{\\schemadef{}}}{{",
@ -235,12 +249,9 @@ fn tex_export(tree: &TreeNode) -> String {
}
out.push_str("\\end{schemaprops}\n");
// JSON export
out.push_str("\\begin{jsonsample}\n");
let json_doc = serde_json::to_string_pretty(&tree.example_value(1)).unwrap();
let replace_key = serde_json::to_string(REF_OBJECT).unwrap();
out.push_str(&json_doc.replace(&format!("{replace_key}:"), "$ref"));
out.push_str("\n\\end{jsonsample}\n");
out.push_str(&format!("\\usebox{{{box_name}}}\n"));
out.push_str("\\end{jsonsample}\n");
}
_ => tex_export_inner(
&ObjectChild {