From 453baaa4030c05238689ebf3e33c621f4cbbac2f Mon Sep 17 00:00:00 2001 From: Wryhder Date: Sun, 16 Feb 2025 22:25:40 +0100 Subject: [PATCH] refactor(rust): merge functions for enriching tags Signed-off-by: Wryhder --- .../rust/ockam/ockam_command/src/docs.rs | 37 ++++++++----------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/implementations/rust/ockam/ockam_command/src/docs.rs b/implementations/rust/ockam/ockam_command/src/docs.rs index 47e475137d2..15adcfe23be 100644 --- a/implementations/rust/ockam/ockam_command/src/docs.rs +++ b/implementations/rust/ockam/ockam_command/src/docs.rs @@ -154,38 +154,33 @@ impl FencedCodeBlockHighlighter<'_> { } } -/// Enrich the `[Preview]` tag with html -fn enrich_preview_tag(text: &str) -> Option { - if !text.contains(PREVIEW_TAG) { +/// Enrich a specific tag with HTML +fn enrich_tag(text: &str, tag: &str, tooltip_text: &str, display_text: &str) -> Option { + if !text.contains(tag) { return None; } - // Converts [Preview] to
Preview
..
+ // Converts [Tag_name] to
Tag_name
..
let mut tooltip = String::new(); - for line in PREVIEW_TOOLTIP_TEXT.trim_end().lines() { + for line in tooltip_text.trim_end().lines() { tooltip.push_str(&format!("

{}

", line)); } tooltip = format!("
{tooltip}
"); - let preview_tag = "Preview"; - let container = format!("
{}{}
\n", preview_tag, tooltip); - Some(text.replace(PREVIEW_TAG, &container)) + let container = format!( + "
{}{}
\n", + display_text, tooltip + ); + Some(text.replace(tag, &container)) +} + +/// Enrich the `[Preview]` tag with html +fn enrich_preview_tag(text: &str) -> Option { + enrich_tag(text, PREVIEW_TAG, PREVIEW_TOOLTIP_TEXT, "Preview") } /// Enrich the `[Unsafe]` tag with html fn enrich_unsafe_tag(text: &str) -> Option { - if !text.contains(UNSAFE_TAG) { - return None; - } - - // Converts [Unsafe] to
Unsafe
..
- let mut tooltip = String::new(); - for line in UNSAFE_TOOLTIP_TEXT.trim_end().lines() { - tooltip.push_str(&format!("

{}

", line)); - } - tooltip = format!("
{tooltip}
"); - let unsafe_tag = "Unsafe"; - let container = format!("
{}{}
\n", unsafe_tag, tooltip); - Some(text.replace(UNSAFE_TAG, &container)) + enrich_tag(text, UNSAFE_TAG, UNSAFE_TOOLTIP_TEXT, "Unsafe") } #[cfg(test)]