mindmap.helpers module

mindmap.helpers.escape_mermaid_text(text: str) str

Escape special characters in text for Mermaid compatibility.

Parameters:

text (str) – Text to escape

Returns:

Escaped text for use in Mermaid diagrams

Return type:

str

mindmap.helpers.replace_unicode_for_markdown(text: str) str

Replace Unicode characters for compatibility with Markdown.

Parameters:

text (str) – Text containing Unicode characters

Returns:

Text with Unicode characters replaced for Markdown compatibility

Return type:

str

mindmap.helpers.replace_unicode_in_obj(obj)

Recursively replace Unicode characters in an object.

Parameters:

obj – Object potentially containing Unicode strings

Returns:

Object with Unicode characters replaced

Return type:

object

Source code for helpers.py
 1import re
 2
 3def escape_mermaid_text(text: str) -> str:
 4    """Escape special characters in text for Mermaid compatibility.
 5    
 6    Args:
 7        text (str): Text to escape
 8        
 9    Returns:
10        str: Escaped text for use in Mermaid diagrams
11    """
12    if not isinstance(text, str):
13        return text
14    escaped = text.replace("\\", "\\\\") \
15                  .replace("\n", "\\n") \
16                  .replace("\r", "") \
17                  .replace("\"", "\\\"")
18    result = []
19    for ch in escaped:
20        if ord(ch) > 127:
21            result.append("\\u{:04x}".format(ord(ch)))
22        else:
23            result.append(ch)
24    return "".join(result)
25
26def replace_unicode_for_markdown(text: str) -> str:
27    """Replace Unicode characters for compatibility with Markdown.
28    
29    Args:
30        text (str): Text containing Unicode characters
31        
32    Returns:
33        str: Text with Unicode characters replaced for Markdown compatibility
34    """
35    return text
36
37def replace_unicode_in_obj(obj):
38    """Recursively replace Unicode characters in an object.
39    
40    Args:
41        obj: Object potentially containing Unicode strings
42        
43    Returns:
44        object: Object with Unicode characters replaced
45    """
46    if isinstance(obj, str):
47        return replace_unicode_for_markdown(obj)
48    elif isinstance(obj, list):
49        return [replace_unicode_in_obj(item) for item in obj]
50    elif isinstance(obj, dict):
51        return {key: replace_unicode_in_obj(value) for key, value in obj.items()}
52    else:
53        return obj