Coverage for src / local_deep_research / exporters / latex_exporter.py: 100%

31 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-02-25 01:07 +0000

1"""LaTeX export service. 

2 

3This module provides the LaTeXExporter class that wraps the existing 

4LaTeXExporter implementation from text_optimization.citation_formatter. 

5""" 

6 

7from typing import Optional 

8 

9from loguru import logger 

10 

11from .base import BaseExporter, ExportOptions, ExportResult 

12from .registry import ExporterRegistry 

13 

14# Maximum content size (50 MB) to prevent OOM errors 

15MAX_CONTENT_SIZE = 50 * 1024 * 1024 

16 

17 

18@ExporterRegistry.register 

19class LaTeXExporter(BaseExporter): 

20 """LaTeX exporter using the existing LaTeXExporter implementation. 

21 

22 This exporter converts markdown content to LaTeX format suitable for 

23 compilation with pdflatex or similar LaTeX processors. 

24 """ 

25 

26 def __init__(self): 

27 """Initialize LaTeX exporter.""" 

28 # Import here to avoid circular imports 

29 from ..text_optimization.citation_formatter import ( 

30 LaTeXExporter as LegacyLaTeXExporter, 

31 ) 

32 

33 self._legacy = LegacyLaTeXExporter() 

34 

35 @property 

36 def format_name(self) -> str: 

37 return "latex" 

38 

39 @property 

40 def file_extension(self) -> str: 

41 return ".tex" 

42 

43 @property 

44 def mimetype(self) -> str: 

45 return "text/plain" 

46 

47 def export( 

48 self, 

49 markdown_content: str, 

50 options: Optional[ExportOptions] = None, 

51 ) -> ExportResult: 

52 """Convert markdown content to LaTeX. 

53 

54 Args: 

55 markdown_content: The markdown text to convert 

56 options: Optional export options (title, metadata) 

57 

58 Returns: 

59 ExportResult with LaTeX content as UTF-8 bytes, filename, and mimetype 

60 

61 Raises: 

62 ValueError: If content exceeds maximum size limit 

63 """ 

64 try: 

65 # Check content size limit to prevent OOM errors 

66 if len(markdown_content) > MAX_CONTENT_SIZE: 

67 raise ValueError( 

68 f"Content exceeds maximum size of " 

69 f"{MAX_CONTENT_SIZE // (1024 * 1024)} MB" 

70 ) 

71 

72 options = options or ExportOptions() 

73 

74 content = self._legacy.export_to_latex(markdown_content) 

75 filename = self._generate_safe_filename(options.title) 

76 

77 logger.info( 

78 f"Generated LaTeX in memory, size: {len(content)} bytes" 

79 ) 

80 

81 return ExportResult( 

82 content=content.encode("utf-8"), 

83 filename=filename, 

84 mimetype=self.mimetype, 

85 ) 

86 

87 except Exception: 

88 logger.exception("Error generating LaTeX") 

89 raise