# mikedolg/raw2md architecture

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

- Interactive diagram: https://gitdiagram.com/mikedolg/raw2md
- Repository: https://github.com/mikedolg/raw2md

## Overview

raw2md converts PDF, DjVu, DOCX, or existing Markdown into cleaned Markdown for RAG/LLM workflows. The principal flow is batch orchestration, format-specific conversion, deterministic cleaning, optional source-based inspection and source-free post-processing, then quality evaluation and result writing. The graph preserves the documented conversion engines and repair stages, plus shared Markdown, source-witness, and provider layers. Relationship evidence comes from the README and SOURCE INDEX references; unsampled implementation details and some runtime wiring are not asserted.

Main entry points: [`src/raw2md/pipeline.py`](https://github.com/mikedolg/raw2md/blob/main/src/raw2md/pipeline.py) (File pipeline), [`src/raw2md/engines/__init__.py`](https://github.com/mikedolg/raw2md/blob/main/src/raw2md/engines/__init__.py) (Engine selection), [`src/raw2md/cleaner.py`](https://github.com/mikedolg/raw2md/blob/main/src/raw2md/cleaner.py) (Deterministic cleaning), [`src/raw2md/cleaning/headings.py`](https://github.com/mikedolg/raw2md/blob/main/src/raw2md/cleaning/headings.py) (Heading rules)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_workflow["Conversion workflow"]
  node_orchestrator["Batch orchestration<br/>[orchestrator.py]"]
  node_pipeline["File pipeline<br/>[pipeline.py]"]
  node_engine_select["Engine selection<br/>[__init__.py]"]
  node_marker["Marker conversion<br/>[marker.py]"]
  node_pandoc["Pandoc conversion<br/>[pandoc.py]"]
  node_djvu["DjVu conversion<br/>[djvu.py]"]
  node_gemini_ocr["Gemini OCR<br/>[ocr.py]"]
end

subgraph group_repair["Markdown repair"]
  node_cleaner["Deterministic cleaning<br/>[cleaner.py]"]
  node_heading_rules["Heading rules<br/>[headings.py]"]
  node_math_rules["Formula rules<br/>[math.py]"]
  node_table_rules["Table rules<br/>[tables.py]"]
end

subgraph group_llm["LLM repair"]
  node_inspection["Source inspection<br/>[coordinator.py]"]
  node_post["Markup post-processing<br/>[coordinator.py]"]
  node_providers["LLM providers<br/>[base.py]"]
  node_chunking["Page chunking<br/>[chunking.py]"]
end

subgraph group_shared["Shared document layers"]
  node_source_text["Source text witness<br/>[source_text.py]"]
  node_source_outline["Source outline<br/>[source_outline.py]"]
  node_md_zones["Markdown zones<br/>[zones.py]"]
  node_md_math["Math spans<br/>[math_spans.py]"]
end

subgraph group_delivery["Evaluation and output"]
  node_quality["Quality evaluation<br/>[evaluator.py]"]
  node_header["Result header<br/>[header.py]"]
  node_output["Result writing<br/>[output.py]"]
end

node_user(("CLI user"))
node_model_service{{"Gemini or Claude"}}

node_user -->|"starts batch"| node_orchestrator
node_orchestrator -->|"dispatches files"| node_pipeline
node_pipeline -->|"selects engine"| node_engine_select
node_engine_select -->|"routes PDF"| node_marker
node_engine_select -->|"routes DOCX"| node_pandoc
node_engine_select -->|"routes DjVu"| node_djvu
node_engine_select -.->|"optional OCR"| node_gemini_ocr
node_pipeline -->|"cleans Markdown"| node_cleaner
node_cleaner -->|"applies rules"| node_heading_rules
node_cleaner -->|"applies rules"| node_math_rules
node_cleaner -->|"applies rules"| node_table_rules
node_pipeline -->|"optionally inspects"| node_inspection
node_pipeline -->|"optionally repairs"| node_post
node_inspection -->|"plans chunks"| node_chunking
node_inspection -->|"requests model"| node_providers
node_post -.->|"requests model"| node_providers
node_providers -.->|"calls model"| node_model_service
node_pipeline -->|"loads witness"| node_source_text
node_heading_rules -->|"uses outline"| node_source_outline
node_heading_rules -->|"reads zones"| node_md_zones
node_math_rules -->|"reads spans"| node_md_math
node_pipeline -->|"evaluates result"| node_quality
node_quality -->|"sets verdict"| node_header
node_pipeline -.->|"writes result"| node_output

click node_orchestrator "https://github.com/mikedolg/raw2md/blob/main/src/raw2md/orchestrator.py"
click node_pipeline "https://github.com/mikedolg/raw2md/blob/main/src/raw2md/pipeline.py"
click node_engine_select "https://github.com/mikedolg/raw2md/blob/main/src/raw2md/engines/__init__.py"
click node_marker "https://github.com/mikedolg/raw2md/blob/main/src/raw2md/engines/marker.py"
click node_pandoc "https://github.com/mikedolg/raw2md/blob/main/src/raw2md/engines/pandoc.py"
click node_djvu "https://github.com/mikedolg/raw2md/blob/main/src/raw2md/engines/djvu.py"
click node_gemini_ocr "https://github.com/mikedolg/raw2md/blob/main/src/raw2md/llm/ocr.py"
click node_cleaner "https://github.com/mikedolg/raw2md/blob/main/src/raw2md/cleaner.py"
click node_heading_rules "https://github.com/mikedolg/raw2md/blob/main/src/raw2md/cleaning/headings.py"
click node_math_rules "https://github.com/mikedolg/raw2md/blob/main/src/raw2md/cleaning/math.py"
click node_table_rules "https://github.com/mikedolg/raw2md/blob/main/src/raw2md/cleaning/tables.py"
click node_inspection "https://github.com/mikedolg/raw2md/blob/main/src/raw2md/llm/inspection/coordinator.py"
click node_post "https://github.com/mikedolg/raw2md/blob/main/src/raw2md/llm/post/coordinator.py"
click node_providers "https://github.com/mikedolg/raw2md/blob/main/src/raw2md/llm/base.py"
click node_chunking "https://github.com/mikedolg/raw2md/blob/main/src/raw2md/llm/chunking.py"
click node_source_text "https://github.com/mikedolg/raw2md/blob/main/src/raw2md/source_text.py"
click node_source_outline "https://github.com/mikedolg/raw2md/blob/main/src/raw2md/source_outline.py"
click node_md_zones "https://github.com/mikedolg/raw2md/blob/main/src/raw2md/mdtext/zones.py"
click node_md_math "https://github.com/mikedolg/raw2md/blob/main/src/raw2md/mdtext/math_spans.py"
click node_quality "https://github.com/mikedolg/raw2md/blob/main/src/raw2md/quality/evaluator.py"
click node_header "https://github.com/mikedolg/raw2md/blob/main/src/raw2md/header.py"
click node_output "https://github.com/mikedolg/raw2md/blob/main/src/raw2md/output.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_orchestrator,node_pipeline,node_engine_select,node_marker,node_pandoc,node_djvu,node_gemini_ocr,node_user toneBlue
class node_cleaner,node_heading_rules,node_math_rules,node_table_rules toneAmber
class node_inspection,node_post,node_providers,node_chunking toneMint
class node_source_text,node_source_outline,node_md_zones,node_md_math toneRose
class node_quality,node_header,node_output,node_model_service toneIndigo
```

## Components

### Conversion workflow

- [Batch orchestration](https://github.com/mikedolg/raw2md/blob/main/src/raw2md/orchestrator.py): `src/raw2md/orchestrator.py`
- [File pipeline](https://github.com/mikedolg/raw2md/blob/main/src/raw2md/pipeline.py): `src/raw2md/pipeline.py`
- [Engine selection](https://github.com/mikedolg/raw2md/blob/main/src/raw2md/engines/__init__.py): `src/raw2md/engines/__init__.py`
- [Marker conversion](https://github.com/mikedolg/raw2md/blob/main/src/raw2md/engines/marker.py): `src/raw2md/engines/marker.py`
- [Pandoc conversion](https://github.com/mikedolg/raw2md/blob/main/src/raw2md/engines/pandoc.py): `src/raw2md/engines/pandoc.py`
- [DjVu conversion](https://github.com/mikedolg/raw2md/blob/main/src/raw2md/engines/djvu.py): `src/raw2md/engines/djvu.py`
- [Gemini OCR](https://github.com/mikedolg/raw2md/blob/main/src/raw2md/llm/ocr.py): `src/raw2md/llm/ocr.py`

### Markdown repair

- [Deterministic cleaning](https://github.com/mikedolg/raw2md/blob/main/src/raw2md/cleaner.py): `src/raw2md/cleaner.py`
- [Heading rules](https://github.com/mikedolg/raw2md/blob/main/src/raw2md/cleaning/headings.py): `src/raw2md/cleaning/headings.py`
- [Formula rules](https://github.com/mikedolg/raw2md/blob/main/src/raw2md/cleaning/math.py): `src/raw2md/cleaning/math.py`
- [Table rules](https://github.com/mikedolg/raw2md/blob/main/src/raw2md/cleaning/tables.py): `src/raw2md/cleaning/tables.py`

### LLM repair

- [Source inspection](https://github.com/mikedolg/raw2md/blob/main/src/raw2md/llm/inspection/coordinator.py): `src/raw2md/llm/inspection/coordinator.py`
- [Markup post-processing](https://github.com/mikedolg/raw2md/blob/main/src/raw2md/llm/post/coordinator.py): `src/raw2md/llm/post/coordinator.py`
- [LLM providers](https://github.com/mikedolg/raw2md/blob/main/src/raw2md/llm/base.py): `src/raw2md/llm/base.py`
- [Page chunking](https://github.com/mikedolg/raw2md/blob/main/src/raw2md/llm/chunking.py): `src/raw2md/llm/chunking.py`

### Shared document layers

- [Source text witness](https://github.com/mikedolg/raw2md/blob/main/src/raw2md/source_text.py): `src/raw2md/source_text.py`
- [Source outline](https://github.com/mikedolg/raw2md/blob/main/src/raw2md/source_outline.py): `src/raw2md/source_outline.py`
- [Markdown zones](https://github.com/mikedolg/raw2md/blob/main/src/raw2md/mdtext/zones.py): `src/raw2md/mdtext/zones.py`
- [Math spans](https://github.com/mikedolg/raw2md/blob/main/src/raw2md/mdtext/math_spans.py): `src/raw2md/mdtext/math_spans.py`

### Evaluation and output

- [Quality evaluation](https://github.com/mikedolg/raw2md/blob/main/src/raw2md/quality/evaluator.py): `src/raw2md/quality/evaluator.py`
- [Result header](https://github.com/mikedolg/raw2md/blob/main/src/raw2md/header.py): `src/raw2md/header.py`
- [Result writing](https://github.com/mikedolg/raw2md/blob/main/src/raw2md/output.py): `src/raw2md/output.py`

### Other components

- **CLI user**
- **Gemini or Claude**

## Connections

- CLI user → Batch orchestration: starts batch
- Batch orchestration → File pipeline: dispatches files (evidence: `src/raw2md/orchestrator.py`)
- File pipeline → Engine selection: selects engine (evidence: `src/raw2md/pipeline.py`)
- Engine selection → Marker conversion: routes PDF
- Engine selection → Pandoc conversion: routes DOCX
- Engine selection → DjVu conversion: routes DjVu
- Engine selection → Gemini OCR: optional OCR
- File pipeline → Deterministic cleaning: cleans Markdown (evidence: `src/raw2md/pipeline.py`)
- Deterministic cleaning → Heading rules: applies rules
- Deterministic cleaning → Formula rules: applies rules
- Deterministic cleaning → Table rules: applies rules
- File pipeline → Source inspection: optionally inspects (evidence: `src/raw2md/pipeline.py`)
- File pipeline → Markup post-processing: optionally repairs (evidence: `src/raw2md/pipeline.py`)
- Source inspection → Page chunking: plans chunks (evidence: `src/raw2md/llm/inspection/coordinator.py`)
- Source inspection → LLM providers: requests model (evidence: `src/raw2md/llm/inspection/coordinator.py`)
- Markup post-processing → LLM providers: requests model
- LLM providers → Gemini or Claude: calls model (evidence: `src/raw2md/llm/base.py`)
- File pipeline → Source text witness: loads witness (evidence: `src/raw2md/pipeline.py`)
- Heading rules → Source outline: uses outline (evidence: `src/raw2md/cleaning/headings.py`)
- Heading rules → Markdown zones: reads zones (evidence: `src/raw2md/cleaning/headings.py`)
- Formula rules → Math spans: reads spans (evidence: `src/raw2md/cleaning/math.py`)
- File pipeline → Quality evaluation: evaluates result (evidence: `src/raw2md/pipeline.py`)
- Quality evaluation → Result header: sets verdict (evidence: `src/raw2md/quality/evaluator.py`)
- File pipeline → Result writing: writes result (evidence: `src/raw2md/pipeline.py`)
