From 47f79a9e1da5dfd88d33d8db2e0b65b477cda456 Mon Sep 17 00:00:00 2001 From: Pierre Hubert Date: Wed, 1 Mar 2023 09:41:23 +0100 Subject: [PATCH] Fix JSON examples presentation --- src/main.rs | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index cab9b9c..0c81c3b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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 {