Coverage for src / rtflite / text_conversion / __init__.py: 100%

9 statements  

« prev     ^ index     » next       coverage.py v7.12.0, created at 2025-11-28 05:09 +0000

1""" 

2Text conversion module for LaTeX to Unicode conversion. 

3 

4This module provides functionality to convert LaTeX mathematical symbols 

5and commands to their Unicode equivalents, matching the behavior of the 

6r2rtf package. 

7 

8The main entry point is the `convert_text` function which handles the 

9text_convert parameter found throughout RTF components. 

10""" 

11 

12from ..text_convert import text_convert # For backward compatibility 

13from .converter import TextConverter 

14from .symbols import LaTeXSymbolMapper 

15 

16 

17# Main public interface 

18def convert_text(text: str | None, enable_conversion: bool = True) -> str | None: 

19 """ 

20 Convert LaTeX symbols in text to Unicode characters. 

21 

22 This function provides the main text conversion interface used throughout 

23 the RTF encoding pipeline. It respects the enable_conversion flag to 

24 allow selective enabling/disabling of conversion. 

25 

26 Args: 

27 text: Input text that may contain LaTeX commands 

28 enable_conversion: Whether to perform LaTeX to Unicode conversion 

29 

30 Returns: 

31 Text with LaTeX commands converted to Unicode (if enabled) 

32 

33 Examples: 

34 >>> convert_text("Area: \\pm 0.05", True) 

35 "Area: +/- 0.05" 

36 

37 >>> convert_text("\\alpha + \\beta", False) 

38 "\\alpha + \\beta" 

39 """ 

40 if not enable_conversion or not text: 

41 return text 

42 

43 converter = TextConverter() 

44 return converter.convert_latex_to_unicode(text) 

45 

46 

47__all__ = [ 

48 "convert_text", 

49 "TextConverter", 

50 "LaTeXSymbolMapper", 

51 "text_convert", # backward compatibility 

52]