# rraldev/doc2tex architecture

> Architecture diagram of the GitHub repository rraldev/doc2tex, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/rraldev/doc2tex
- Repository: https://github.com/rraldev/doc2tex

## Overview

1. **What this repository is**
   - `doc2tex` is a **local document conversion tool** that translates between **DOCX and LaTeX** in both directions.
   - The architecture is centered on a **core conversion library** with two thin entrypoints: a **CLI** and a **Flask-based web UI**.

2. **Primary subsystem: core converter package**
   - The `doc2tex/` package is the main business logic boundary.
   - `converter.py` appears to be the **orchestrator/facade**, coordinating direction selection and shared conversion flow.
   - `latex.py` contains the **DOCX → LaTeX** conversion logic.
   - `docx.py` contains the **LaTeX → DOCX** conversion logic.

3. **Shared support layer**
   - `options.py`, `utils.py`, and `errors.py` are supporting modules for configuration, helper routines, and domain-specific failure handling.
   - These modules likely keep parsing/conversion code isolated from input/output concerns.

4. **CLI entrypoint**
   - `cli.py` is the command-line boundary.
   - It likely accepts a file path, invokes the core converter, and writes the output document alongside the source file.
   - This path is meant for direct local batch conversion without any UI dependencies.

5. **Web entrypoint**
   - `web.py` is the server boundary and runs the application locally.
   - It uses **Flask** to expose conversion through HTTP, probably for file upload and download interactions.
   - The server is not described as cloud-backed; conversion stays on the local machine.

6. **Frontend boundary**
   - `templates/index.html` is the main web page template.
   - `static/script.js` and `static/style.css` provide browser-side behavior and presentation.
   - This UI layer is thin and exists only to drive the conversion service exposed by Flask.

7. **Conversion data flow**
   - User submits a DOCX or LaTeX file through either the CLI or web UI.
   - The entrypoint hands the request to the core converter.
   - The converter routes to the appropriate direction-specific module (`latex.py` or `docx.py`).
   - The resulting document is written back to the local filesystem or returned through the web response.

8. **Document-processing responsibilities**
   - The tool is intended to preserve **formatting, tables, images, equations, footnotes, and multilingual content** as much as possible.
   - The README also notes some support for **bibliography extraction**, but in a basic form.
   - This suggests the core conversion layer is a document-structure transformer, not just text substitution.

9. **File and asset handling**
   - Images are treated as local assets, with the README noting they need to be available in the `.tex` output folder.
   - That means the architecture depends on the **filesystem as a working boundary** for intermediate and output artifacts.

10. **Runtime and external dependencies**
   - The project is Python-based.
   - It depends on **python-docx** for DOCX handling, **Pillow (PIL)** for image handling, and **Flask** for the web interface.
   - These libraries materially shape the architecture by providing document parsing, media processing, and HTTP serving.

11. **Testing boundary**
   - `tests/test_basic.py` provides basic verification of conversion behavior.
   - The tests likely exercise the core library rather than the UI, which is consistent with the architecture: core logic is separated from frontends.

12. **Architectural summary for a diagram**
   - Draw this as three layers:
     - **Presentation layer**: CLI and Flask web UI
     - **Application/core layer**: converter + DOCX/LaTeX transformation modules
     - **Infrastructure layer**: local filesystem and Python document/image libraries
   - There are **no external network services** in the intended path; all conversion is local.

Main entry points: [`doc2tex/converter.py`](https://github.com/rraldev/doc2tex/blob/main/doc2tex/converter.py) (Converter), [`web.py`](https://github.com/rraldev/doc2tex/blob/main/web.py) (Web app), [`doc2tex/latex.py`](https://github.com/rraldev/doc2tex/blob/main/doc2tex/latex.py) (DOCX→LaTeX), [`doc2tex/docx.py`](https://github.com/rraldev/doc2tex/blob/main/doc2tex/docx.py) (LaTeX→DOCX)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_presentation["Presentation"]
  node_cli["CLI<br/>entrypoint<br/>[cli.py]"]
  node_web["Web app<br/>entrypoint<br/>[web.py]"]
  node_ui_template["Page template<br/>[index.html]"]
  node_ui_script["Browser script<br/>asset<br/>[script.js]"]
  node_ui_style["Stylesheet<br/>asset<br/>[style.css]"]
end

subgraph group_core["Core"]
  node_converter["Converter<br/>orchestrator<br/>[converter.py]"]
  node_latex_conv["DOCX→LaTeX<br/>transform<br/>[latex.py]"]
  node_docx_conv["LaTeX→DOCX<br/>transform<br/>[docx.py]"]
  node_options["Options<br/>config<br/>[options.py]"]
  node_utils["Utilities<br/>helpers<br/>[utils.py]"]
  node_errors["Errors<br/>domain<br/>[errors.py]"]
  node_pkg_init(("Package init<br/>[__init__.py]"))
end

subgraph group_infrastructure["Infrastructure"]
  node_filesystem[("Filesystem<br/>runtime")]
  node_python_docx{{"python-docx"}}
  node_pillow{{"Pillow"}}
  node_flask{{"Flask"}}
  node_tests_basic["Basic tests<br/>[test_basic.py]"]
end

node_cli -->|"invoke"| node_converter
node_web -->|"invoke"| node_converter
node_web -->|"runs on"| node_flask
node_web -->|"serves"| node_ui_template
node_web -->|"serves"| node_ui_script
node_web -->|"serves"| node_ui_style
node_converter -->|"selects"| node_latex_conv
node_converter -->|"selects"| node_docx_conv
node_converter -->|"uses"| node_options
node_converter -->|"uses"| node_utils
node_converter -->|"raises"| node_errors
node_latex_conv -->|"reads DOCX"| node_python_docx
node_docx_conv -->|"writes DOCX"| node_python_docx
node_latex_conv -->|"uses images"| node_pillow
node_docx_conv -->|"uses images"| node_pillow
node_latex_conv -->|"reads/writes"| node_filesystem
node_docx_conv -->|"reads/writes"| node_filesystem
node_ui_template -->|"stored on"| node_filesystem
node_ui_script -->|"stored on"| node_filesystem
node_ui_style -->|"stored on"| node_filesystem
node_tests_basic -->|"verifies"| node_converter
node_pkg_init -->|"exports"| node_converter

click node_cli "https://github.com/rraldev/doc2tex/blob/main/cli.py"
click node_web "https://github.com/rraldev/doc2tex/blob/main/web.py"
click node_ui_template "https://github.com/rraldev/doc2tex/blob/main/templates/index.html"
click node_ui_script "https://github.com/rraldev/doc2tex/blob/main/static/script.js"
click node_ui_style "https://github.com/rraldev/doc2tex/blob/main/static/style.css"
click node_converter "https://github.com/rraldev/doc2tex/blob/main/doc2tex/converter.py"
click node_latex_conv "https://github.com/rraldev/doc2tex/blob/main/doc2tex/latex.py"
click node_docx_conv "https://github.com/rraldev/doc2tex/blob/main/doc2tex/docx.py"
click node_options "https://github.com/rraldev/doc2tex/blob/main/doc2tex/options.py"
click node_utils "https://github.com/rraldev/doc2tex/blob/main/doc2tex/utils.py"
click node_errors "https://github.com/rraldev/doc2tex/blob/main/doc2tex/errors.py"
click node_pkg_init "https://github.com/rraldev/doc2tex/blob/main/doc2tex/__init__.py"
click node_tests_basic "https://github.com/rraldev/doc2tex/blob/main/tests/test_basic.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_cli,node_web,node_ui_template,node_ui_script,node_ui_style toneBlue
class node_converter,node_latex_conv,node_docx_conv,node_options,node_utils,node_errors,node_pkg_init toneAmber
class node_filesystem,node_python_docx,node_pillow,node_flask,node_tests_basic toneMint
```

## Components

### Presentation

User-facing entrypoints and web assets

- [CLI](https://github.com/rraldev/doc2tex/blob/main/cli.py): entrypoint · Command-line conversion interface · `cli.py`
- [Web app](https://github.com/rraldev/doc2tex/blob/main/web.py): entrypoint · Flask server entrypoint · `web.py`
- [Page template](https://github.com/rraldev/doc2tex/blob/main/templates/index.html): template · Main web page markup · `templates/index.html`
- [Browser script](https://github.com/rraldev/doc2tex/blob/main/static/script.js): asset · Client-side interaction logic · `static/script.js`
- [Stylesheet](https://github.com/rraldev/doc2tex/blob/main/static/style.css): asset · Web UI presentation styles · `static/style.css`

### Core

Conversion orchestration and document transforms

- [Converter](https://github.com/rraldev/doc2tex/blob/main/doc2tex/converter.py): orchestrator · Routes conversion direction and shared flow · `doc2tex/converter.py`
- [DOCX→LaTeX](https://github.com/rraldev/doc2tex/blob/main/doc2tex/latex.py): transform · DOCX to LaTeX conversion logic · `doc2tex/latex.py`
- [LaTeX→DOCX](https://github.com/rraldev/doc2tex/blob/main/doc2tex/docx.py): transform · LaTeX to DOCX conversion logic · `doc2tex/docx.py`
- [Options](https://github.com/rraldev/doc2tex/blob/main/doc2tex/options.py): config · Conversion settings and flags · `doc2tex/options.py`
- [Utilities](https://github.com/rraldev/doc2tex/blob/main/doc2tex/utils.py): helpers · Shared helper routines · `doc2tex/utils.py`
- [Errors](https://github.com/rraldev/doc2tex/blob/main/doc2tex/errors.py): domain · Conversion-specific failures · `doc2tex/errors.py`
- [Package init](https://github.com/rraldev/doc2tex/blob/main/doc2tex/__init__.py): package · Package export boundary · `doc2tex/__init__.py`

### Infrastructure

Local runtime, filesystem, and third-party libraries

- **Filesystem**: runtime · Local files, media, and outputs
- **python-docx**: library · DOCX parsing and writing
- **Pillow**: library · Image loading and handling
- **Flask**: library · Local HTTP server framework
- [Basic tests](https://github.com/rraldev/doc2tex/blob/main/tests/test_basic.py): test · Core conversion checks · `tests/test_basic.py`

## Connections

- CLI → Converter: invoke · CLI delegates conversions to the core orchestrator
- Web app → Converter: invoke · Web requests are handed to the core orchestrator
- Web app → Flask: runs on · Web entrypoint is built on Flask
- Web app → Page template: serves · Server renders the main page template
- Web app → Browser script: serves · Server exposes browser-side behavior
- Web app → Stylesheet: serves · Server exposes browser-side styling
- Converter → DOCX→LaTeX: selects · Orchestrator routes DOCX-to-LaTeX work
- Converter → LaTeX→DOCX: selects · Orchestrator routes LaTeX-to-DOCX work
- Converter → Options: uses · Shared configuration flows through conversion
- Converter → Utilities: uses · Shared helpers support orchestration
- Converter → Errors: raises · Domain failures are surfaced from the core layer
- DOCX→LaTeX → python-docx: reads DOCX · DOCX parsing relies on python-docx
- LaTeX→DOCX → python-docx: writes DOCX · DOCX generation relies on python-docx
- DOCX→LaTeX → Pillow: uses images · Image processing depends on Pillow
- LaTeX→DOCX → Pillow: uses images · Image handling depends on Pillow
- DOCX→LaTeX → Filesystem: reads/writes · Conversion uses local files and output assets
- LaTeX→DOCX → Filesystem: reads/writes · Conversion uses local files and output artifacts
- Page template → Filesystem: stored on · Template is a local project asset
- Browser script → Filesystem: stored on · Static asset is served from local files
- Stylesheet → Filesystem: stored on · Static asset is served from local files
- Basic tests → Converter: verifies · Tests exercise the core conversion path
- Package init → Converter: exports · Package boundary exposes core functionality
