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

9 statements  

« prev     ^ index     » next       coverage.py v7.10.3, created at 2025-08-14 16:35 +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 .converter import TextConverter 

13from .symbols import LaTeXSymbolMapper 

14 

15 

16# Main public interface 

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

18 """ 

19 Convert LaTeX symbols in text to Unicode characters. 

20 

21 This function provides the main text conversion interface used throughout 

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

23 allow selective enabling/disabling of conversion. 

24 

25 Args: 

26 text: Input text that may contain LaTeX commands 

27 enable_conversion: Whether to perform LaTeX to Unicode conversion 

28 

29 Returns: 

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

31 

32 Examples: 

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

34 "Area: +/- 0.05" 

35 

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

37 "\\alpha + \\beta" 

38 """ 

39 if not enable_conversion or not text: 

40 return text 

41 

42 converter = TextConverter() 

43 return converter.convert_latex_to_unicode(text) 

44 

45 

46# For backward compatibility with existing imports 

47from ..text_convert import text_convert 

48 

49__all__ = [ 

50 "convert_text", 

51 "TextConverter", 

52 "LaTeXSymbolMapper", 

53 "text_convert", # backward compatibility 

54]