# moon-like-gray-cat/wordformatter architecture

> Architecture diagram of the GitHub repository moon-like-gray-cat/wordformatter, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/moon-like-gray-cat/wordformatter
- Repository: https://github.com/moon-like-gray-cat/wordformatter

## Overview

#### Purpose and Entry Points
WordFormatter is a Windows-oriented desktop utility that normalizes `.docx` formatting: it detects headings, applies configured typography and Word heading styles, standardizes paragraph indentation/alignment, normalizes parentheses, and centers figure/table captions.

Primary launch path is `run.py`, which starts the Tkinter application in `src/wordtool/app/main.py`. The package is organized as a GUI shell over a document-formatting core.

#### Desktop Application Layer
The UI is built with Tkinter and presents document selection plus user-controlled heading patterns, fonts, sizes, boldness, and processing options. Layout and widgets reside in `src/wordtool/app/ui_components.py`; application startup/window coordination is in `src/wordtool/app/main.py`.

UI defaults or persisted interface settings are represented by `src/wordtool/resources/ui_config.json`, with shared application configuration in `src/wordtool/config.py`.

#### Event-to-Processing Boundary
User actions are handled in `src/wordtool/app/event_handlers.py`. This is the key boundary between GUI state and document processing: it gathers selected input/options, invokes the formatter, and reports completion or failure back to the desktop UI.

The formatter operates on `.docx`; `.doc` is explicitly outside the supported input boundary. Processing failures may result in an output file being announced but not successfully written, so handler-level error reporting is material.

#### Document Formatting Core
`src/wordtool/core/formatter.py` contains the primary business pipeline. It interprets paragraph content and applies Word document mutations rather than exposing a separate service/API layer.

Core behavior includes:
- Detecting configured textual numbering patterns such as Chinese numerals, hierarchical Arabic numbering, letters, Roman numerals, and circled numbers.
- Mapping recognized levels to Word’s built-in heading styles so generated documents can support tables of contents.
- Applying per-level font, size, and bold settings to headings and body text.
- Normalizing paragraph layout: headings left aligned; paragraphs flush-left with a two-character first-line indent.
- Replacing ASCII `(` with Chinese full-width `（` and centering table/figure captions.

#### Heading Recognition and Numbering Dependencies
Heading classification is prefix-based and paragraph-oriented: numbering must be at the beginning of a hard-return-delimited paragraph. This makes paragraph boundaries an important input contract and means ordinary body text beginning with a configured marker can be misclassified.

The tool can also process Word automatic numbering when the user enables that option. That capability depends on Microsoft Office being installed; WPS alone is insufficient for automatic-list-number recognition. Text-number recognition and the remaining formatting features do not share that Office dependency.

#### Output and Delivery
For an input such as `xxx.docx`, processing writes a formatted Word document in the chosen directory, conventionally named `格式化_xxx.docx`. The tool is intended for Windows 7+ distribution as an executable rather than as a hosted application.

Packaging is supported by `scripts/build_exe.bat`; project metadata/dependencies are declared in `pyproject.toml` and `requirements.txt`. There is no server runtime, database, or network service in the repository’s described architecture.

Main entry points: [`src/wordtool/core/formatter.py`](https://github.com/moon-like-gray-cat/wordformatter/blob/main/src/wordtool/core/formatter.py) (Document formatter), [`src/wordtool/app/event_handlers.py`](https://github.com/moon-like-gray-cat/wordformatter/blob/main/src/wordtool/app/event_handlers.py) (Processing handlers), [`src/wordtool/app/ui_components.py`](https://github.com/moon-like-gray-cat/wordformatter/blob/main/src/wordtool/app/ui_components.py) (Document options UI), [`src/wordtool/app/main.py`](https://github.com/moon-like-gray-cat/wordformatter/blob/main/src/wordtool/app/main.py) (Window coordinator)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_desktop["Desktop application"]
  node_launcher["Application launcher<br/>Python entry point<br/>[run.py]"]
  node_app_main["Window coordinator<br/>Tkinter application<br/>[main.py]"]
  node_ui["Document options UI<br/>Tkinter widgets<br/>[ui_components.py]"]
  node_ui_settings["UI defaults<br/>JSON settings<br/>[ui_config.json]"]
  node_app_config["Shared configuration<br/>Python configuration<br/>[config.py]"]
  node_handlers{{"Processing handlers<br/>GUI-to-core boundary<br/>[event_handlers.py]"}}
end

subgraph group_core["Formatting core"]
  node_formatter["Document formatter<br/>Formatting pipeline<br/>[formatter.py]"]
  node_paragraph_contract["Paragraph boundaries<br/>Input contract"]
  node_heading_detection["Heading recognition<br/>Prefix classifier<br/>[formatter.py]"]
  node_auto_numbering["Automatic numbering<br/>Optional Office integration<br/>[formatter.py]"]
  node_style_mapping["Heading style mapping<br/>Word style application<br/>[formatter.py]"]
  node_paragraph_normalization["Paragraph normalization<br/>Document mutation<br/>[formatter.py]"]
end

subgraph group_delivery["Build and delivery"]
  node_build_script["Executable build<br/>Windows build script<br/>[build_exe.bat]"]
  node_project_metadata["Dependencies and metadata<br/>Project manifest<br/>[pyproject.toml]"]
  node_requirements["Runtime dependencies<br/>Dependency manifest<br/>[requirements.txt]"]
  node_windows_exe["Windows executable<br/>Desktop distribution"]
end

node_input_docx["Input .docx<br/>Word document"]
node_output_docx["Formatted .docx<br/>Word document output"]

node_launcher -->|"starts"| node_app_main
node_app_main -->|"renders"| node_ui
node_ui_settings -->|"supplies defaults"| node_ui
node_app_config -->|"configures"| node_app_main
node_ui -->|"user actions"| node_handlers
node_handlers -->|"reads selected file"| node_input_docx
node_handlers -->|"passes options and input"| node_formatter
node_formatter -.->|"relies on"| node_paragraph_contract
node_formatter -->|"classifies paragraphs"| node_heading_detection
node_auto_numbering -.->|"optional numbering source"| node_heading_detection
node_heading_detection -->|"recognized level"| node_style_mapping
node_style_mapping -->|"styles headings"| node_paragraph_normalization
node_formatter -->|"applies formatting"| node_paragraph_normalization
node_paragraph_normalization -->|"writes"| node_output_docx
node_formatter -->|"processing result"| node_output_docx
node_handlers -->|"completion or failure"| node_ui
node_project_metadata -->|"build inputs"| node_build_script
node_requirements -->|"dependencies"| node_build_script
node_build_script -->|"packages"| node_windows_exe
node_windows_exe -.->|"distributes"| node_launcher

click node_launcher "https://github.com/moon-like-gray-cat/wordformatter/blob/main/run.py"
click node_app_main "https://github.com/moon-like-gray-cat/wordformatter/blob/main/src/wordtool/app/main.py"
click node_ui "https://github.com/moon-like-gray-cat/wordformatter/blob/main/src/wordtool/app/ui_components.py"
click node_ui_settings "https://github.com/moon-like-gray-cat/wordformatter/blob/main/src/wordtool/resources/ui_config.json"
click node_app_config "https://github.com/moon-like-gray-cat/wordformatter/blob/main/src/wordtool/config.py"
click node_handlers "https://github.com/moon-like-gray-cat/wordformatter/blob/main/src/wordtool/app/event_handlers.py"
click node_formatter "https://github.com/moon-like-gray-cat/wordformatter/blob/main/src/wordtool/core/formatter.py"
click node_heading_detection "https://github.com/moon-like-gray-cat/wordformatter/blob/main/src/wordtool/core/formatter.py"
click node_auto_numbering "https://github.com/moon-like-gray-cat/wordformatter/blob/main/src/wordtool/core/formatter.py"
click node_style_mapping "https://github.com/moon-like-gray-cat/wordformatter/blob/main/src/wordtool/core/formatter.py"
click node_paragraph_normalization "https://github.com/moon-like-gray-cat/wordformatter/blob/main/src/wordtool/core/formatter.py"
click node_build_script "https://github.com/moon-like-gray-cat/wordformatter/blob/main/scripts/build_exe.bat"
click node_project_metadata "https://github.com/moon-like-gray-cat/wordformatter/blob/main/pyproject.toml"
click node_requirements "https://github.com/moon-like-gray-cat/wordformatter/blob/main/requirements.txt"

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_launcher,node_app_main,node_ui,node_ui_settings,node_app_config,node_handlers toneBlue
class node_formatter,node_paragraph_contract,node_heading_detection,node_auto_numbering,node_style_mapping,node_paragraph_normalization toneAmber
class node_build_script,node_project_metadata,node_requirements,node_windows_exe toneMint
class node_input_docx,node_output_docx toneNeutral
```

## Components

### Desktop application

- [Application launcher](https://github.com/moon-like-gray-cat/wordformatter/blob/main/run.py): Python entry point · Starts the Tkinter application. · `run.py`
- [Window coordinator](https://github.com/moon-like-gray-cat/wordformatter/blob/main/src/wordtool/app/main.py): Tkinter application · `src/wordtool/app/main.py`
- [Document options UI](https://github.com/moon-like-gray-cat/wordformatter/blob/main/src/wordtool/app/ui_components.py): Tkinter widgets · Collects files, typography, heading patterns, and processing options. · `src/wordtool/app/ui_components.py`
- [UI defaults](https://github.com/moon-like-gray-cat/wordformatter/blob/main/src/wordtool/resources/ui_config.json): JSON settings · `src/wordtool/resources/ui_config.json`
- [Shared configuration](https://github.com/moon-like-gray-cat/wordformatter/blob/main/src/wordtool/config.py): Python configuration · `src/wordtool/config.py`
- [Processing handlers](https://github.com/moon-like-gray-cat/wordformatter/blob/main/src/wordtool/app/event_handlers.py): GUI-to-core boundary · Invokes formatting and reports write failures to the UI. · `src/wordtool/app/event_handlers.py`

### Formatting core

- [Document formatter](https://github.com/moon-like-gray-cat/wordformatter/blob/main/src/wordtool/core/formatter.py): Formatting pipeline · Mutates Word paragraphs and styles using selected options. · `src/wordtool/core/formatter.py`
- **Paragraph boundaries**: Input contract · Headings must begin a hard-return-delimited paragraph.
- [Heading recognition](https://github.com/moon-like-gray-cat/wordformatter/blob/main/src/wordtool/core/formatter.py): Prefix classifier · Recognizes configured Chinese, Arabic, letter, Roman, and circled numbering. · `src/wordtool/core/formatter.py`
- [Automatic numbering](https://github.com/moon-like-gray-cat/wordformatter/blob/main/src/wordtool/core/formatter.py): Optional Office integration · Recognition requires Microsoft Office; WPS alone is insufficient. · `src/wordtool/core/formatter.py`
- [Heading style mapping](https://github.com/moon-like-gray-cat/wordformatter/blob/main/src/wordtool/core/formatter.py): Word style application · Maps detected levels to built-in Word heading styles. · `src/wordtool/core/formatter.py`
- [Paragraph normalization](https://github.com/moon-like-gray-cat/wordformatter/blob/main/src/wordtool/core/formatter.py): Document mutation · Applies typography, alignment, indentation, parentheses, and caption rules. · `src/wordtool/core/formatter.py`

### Build and delivery

- [Executable build](https://github.com/moon-like-gray-cat/wordformatter/blob/main/scripts/build_exe.bat): Windows build script · `scripts/build_exe.bat`
- [Dependencies and metadata](https://github.com/moon-like-gray-cat/wordformatter/blob/main/pyproject.toml): Project manifest · `pyproject.toml`
- [Runtime dependencies](https://github.com/moon-like-gray-cat/wordformatter/blob/main/requirements.txt): Dependency manifest · `requirements.txt`
- **Windows executable**: Desktop distribution · Targeted at Windows 7+.

### Other components

- **Input .docx**: Word document · Only .docx is supported; .doc is outside the input boundary.
- **Formatted .docx**: Word document output · Conventionally written as 格式化_xxx.docx in the chosen directory.

## Connections

- Application launcher → Window coordinator: starts
- Window coordinator → Document options UI: renders
- UI defaults → Document options UI: supplies defaults
- Shared configuration → Window coordinator: configures
- Document options UI → Processing handlers: user actions
- Processing handlers → Input .docx: reads selected file
- Processing handlers → Document formatter: passes options and input
- Document formatter → Paragraph boundaries: relies on
- Document formatter → Heading recognition: classifies paragraphs
- Automatic numbering → Heading recognition: optional numbering source
- Heading recognition → Heading style mapping: recognized level
- Heading style mapping → Paragraph normalization: styles headings
- Document formatter → Paragraph normalization: applies formatting
- Paragraph normalization → Formatted .docx: writes
- Document formatter → Formatted .docx: processing result
- Processing handlers → Document options UI: completion or failure
- Dependencies and metadata → Executable build: build inputs
- Runtime dependencies → Executable build: dependencies
- Executable build → Windows executable: packages
- Windows executable → Application launcher: distributes
