From 85e0c3abc592a2fe4e52cba1eef29ba7bf0588d3 Mon Sep 17 00:00:00 2001 From: Pierre Hubert Date: Fri, 3 Feb 2023 15:36:52 +0100 Subject: [PATCH] Tex export functional --- src/main.rs | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index 4a2fe3d..b7e0b78 100644 --- a/src/main.rs +++ b/src/main.rs @@ -85,7 +85,15 @@ fn graphviz_export(tree: &TreeNode) -> String { out } +fn tex_escape_str(s: &str) -> String { + s.replace('_', "\\_") + .trim_start_matches('\n') + .replace("\n\n", "\n") + .replace('\n', "\\newline\n") +} + 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(), @@ -96,25 +104,37 @@ fn tex_export_inner(tree: &ObjectChild, out: &mut String, required: bool) { NodeType::Integer => "integer".to_string(), }; - write!(out, "\\textbf{{{}}}", tree.name).unwrap(); + write!(out, "\\textbf{{{}}}", tex_escape_str(&tree.name)).unwrap(); if required { out.push_str("\\textcolor{red}{*}"); } out.push_str("\n\\newline"); - write!(out, "\\textit{{\\textcolor{{gray}}{{{type_str}}}}}").unwrap(); + write!( + out, + "\\textit{{\\textcolor{{gray}}{{{}}}}}", + tex_escape_str(&type_str) + ) + .unwrap(); if let Some(description) = &tree.node.description { - write!(out, "\\newline\n\textcolor{{gray}}{{{}}}", description).unwrap(); + 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 : {}}}", - example + "\\newline\n\\textcolor{{gray}}{{Exemple : {}}}", + tex_escape_str(example) ) .unwrap(); } + + out.push_str("\\end{minipage}\n"); } fn tex_export(tree: &TreeNode) -> String { @@ -133,7 +153,7 @@ fn tex_export(tree: &TreeNode) -> String { .unwrap_or(false), ); - out.push_str("\n\n"); + out.push_str("\n\n \\vspace{0.2cm} \n\n"); } } _ => tex_export_inner(