# mcs391/word-bid-beautifier architecture

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

- Interactive diagram: https://gitdiagram.com/mcs391/word-bid-beautifier
- Repository: https://github.com/mcs391/word-bid-beautifier

## Overview

1. **Repository purpose**
   - This is a Python utility repo for **post-processing Word `.docx` bid documents** used in HIK/华数-style tender templates.
   - The architecture is centered on **direct OOXML manipulation**, not on Word automation or a GUI.

2. **Main executable subsystem**
   - `scripts/bid_doc_optimizer.py` is the primary entry point.
   - It exposes a command-line workflow that takes an input `.docx`, applies selected phases, and writes a new `.docx`.
   - This is the script that should be treated as the system’s “orchestrator” in an architecture diagram.

3. **Secondary utility subsystem**
   - `scripts/word_bid_beautify.py` is a supporting script focused on **style beautification**.
   - The README positions it as a narrower tool compared with the main optimizer, so it should be drawn as a helper/legacy/specialized path rather than the core pipeline.

4. **Pipeline structure**
   - The main tool is organized as a **three-phase pipeline**:
     - **Phase 0**: fix numbering inheritance and list linkage at the OOXML level.
     - **Phase 1**: repair broken chapter/heading numbering sequences and remove stray textual prefixes.
     - **Phase 2**: apply style cleanup and layout polishing.
   - In a diagram, these phases form a linear processing chain, though each phase can also be run independently via CLI flags.

5. **Phase 0 architectural boundary**
   - Phase 0 is the most structural step: it modifies the internal Word model so headings inherit numbering correctly.
   - It touches three OOXML areas:
     - `word/numbering.xml` for list definitions,
     - `word/styles.xml` for style-to-list binding consistency,
     - `word/document.xml` for paragraph-level numbering references.
   - This phase exists to fix Word’s behavior when users manually apply H2/H3/H4 styles.

6. **Phase 1 and Phase 2 responsibilities**
   - **Phase 1** is logic-level normalization of heading numbers and hardcoded prefixes in text.
   - **Phase 2** is presentation-level refinement: heading styles, indentation, font family normalization, spacing adjustments, and repeated numbering cleanup.
   - The distinction matters: Phase 1 corrects content structure, while Phase 2 corrects appearance.

7. **Document processing data flow**
   - The repository’s data flow is:
     - read `.docx` archive,
     - extract relevant OOXML parts,
     - parse and transform XML/text content,
     - repackage the archive as a new `.docx`.
   - This means the system operates on the file as a structured package, not as a rendered document.

8. **Core file-level dependencies**
   - The tool’s architectural dependency set is intentionally small:
     - Python 3.8+,
     - standard library only,
     - Word OOXML package structure.
   - There are no external APIs, databases, or runtime services involved.

9. **Template and style model**
   - The repository is specialized around a fixed HIK style mapping:
     - styles `12–16` correspond to chapter through fifth-level headings,
     - style `17` is body text.
   - A diagram should show this as a template-specific domain model, because the tool is not generic for arbitrary Word documents.

10. **Input/output boundary**
    - The main architectural boundary is the `.docx` file itself.
    - Input is a bid document with existing OOXML definitions; output is a new, modified `.docx`.
    - The README explicitly notes that the tool assumes the source document already contains the expected multi-level list structure in many cases.

11. **Configuration and operational controls**
    - The CLI flags act as the system’s configuration layer:
      - phase selection,
      - chapter range bounds,
      - indentation values,
      - dry-run mode,
      - skip-prefix-stripping option.
    - These controls change how the transformer behaves without changing the underlying architecture.

12. **Reference and documentation subsystem**
    - `references/ooxml-quickref.md` serves as the internal OOXML reference for developers.
    - `README.md` documents usage and architecture; `SKILL.md` suggests the repo is also packaged for a workflow/assistant environment.
    - These are supporting artifacts, not runtime components.

13. **Operational concerns**
    - The tool includes validation such as paragraph-count consistency checks after processing.
    - It is designed for safe batch document cleanup, with the README emphasizing backup before use.
    - In a diagram, this can be represented as a post-transform validation step before output.

14. **What is not in scope**
    - There is no web frontend, server process, or background job system in this repository.
    - There is also no persistent state: each run is a stateless file transformation job.

15. **Suggested architecture diagram framing**
    - Draw one main pipeline box labeled `bid_doc_optimizer.py`.
    - Feed it a `.docx` input package, pass it through three sequential transformation phases over OOXML parts, then emit a new `.docx`.
    - Show `word/document.xml`, `word/styles.xml`, and `word/numbering.xml` as the main internal data surfaces, with `word_bid_beautify.py` as a supporting utility and `references/ooxml-quickref.md` as developer documentation.

Main entry points: [`scripts/bid_doc_optimizer.py`](https://github.com/mcs391/word-bid-beautifier/blob/main/scripts/bid_doc_optimizer.py) (Optimizer), [`scripts/word_bid_beautify.py`](https://github.com/mcs391/word-bid-beautifier/blob/main/scripts/word_bid_beautify.py) (Beautifier), [`README.md`](https://github.com/mcs391/word-bid-beautifier/blob/main/README.md) (README), [`SKILL.md`](https://github.com/mcs391/word-bid-beautifier/blob/main/SKILL.md) (SKILL)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_grp_runtime["Runtime pipeline"]
  node_n_input["DOCX input"]
  node_n_optimizer["Optimizer<br/>cli orchestrator"]
  node_n_flags{{"CLI flags<br/>config"}}
  node_n_phase0["Phase 0<br/>ooxml fixup"]
  node_n_phase1["Phase 1<br/>heading normalize"]
  node_n_phase2["Phase 2<br/>style polish"]
  node_n_validate{{"Validation<br/>consistency check"}}
  node_n_output["DOCX output"]
  node_n_beautify["Beautifier<br/>style utility"]
end

subgraph group_grp_ooxml["OOXML surfaces"]
  node_n_package[("OOXML package<br/>archive structure")]
  node_n_docxml["document.xml<br/>paragraph tree"]
  node_n_stylesxml["styles.xml<br/>style map"]
  node_n_numberingxml["numbering.xml<br/>list defs"]
  node_n_hikmap{{"HIK styles<br/>template model"}}
end

subgraph group_grp_support["Support files"]
  node_n_readme["README<br/>docs<br/>[README.md]"]
  node_n_skill["SKILL<br/>workflow metadata<br/>[SKILL.md]"]
  node_n_ref["OOXML ref<br/>reference notes<br/>[ooxml-quickref.md]"]
end

node_n_input -->|"open"| node_n_optimizer
node_n_optimizer -->|"reads"| node_n_flags
node_n_optimizer -->|"runs"| node_n_phase0
node_n_phase0 -->|"updates"| node_n_docxml
node_n_phase0 -->|"updates"| node_n_stylesxml
node_n_phase0 -->|"updates"| node_n_numberingxml
node_n_phase0 -->|"feeds"| node_n_phase1
node_n_optimizer -->|"runs"| node_n_phase1
node_n_phase1 -->|"reads/writes"| node_n_docxml
node_n_phase1 -->|"feeds"| node_n_phase2
node_n_optimizer -->|"runs"| node_n_phase2
node_n_phase2 -->|"updates"| node_n_stylesxml
node_n_phase2 -->|"updates"| node_n_docxml
node_n_optimizer -->|"checks"| node_n_validate
node_n_validate -->|"gate"| node_n_output
node_n_optimizer -->|"write"| node_n_output
node_n_package -->|"contains"| node_n_docxml
node_n_package -->|"contains"| node_n_stylesxml
node_n_package -->|"contains"| node_n_numberingxml
node_n_optimizer -->|"extracts/rebuilds"| node_n_package
node_n_beautify -->|"focuses on"| node_n_stylesxml
node_n_beautify -->|"polishes"| node_n_docxml
node_n_hikmap -->|"defines"| node_n_stylesxml
node_n_hikmap -->|"constrains"| node_n_phase1
node_n_readme -->|"documents"| node_n_optimizer
node_n_ref -->|"supports"| node_n_optimizer
node_n_skill -->|"packages"| node_n_readme

click node_n_optimizer "https://github.com/mcs391/word-bid-beautifier/blob/main/scripts/bid_doc_optimizer.py"
click node_n_flags "https://github.com/mcs391/word-bid-beautifier/blob/main/scripts/bid_doc_optimizer.py"
click node_n_phase0 "https://github.com/mcs391/word-bid-beautifier/blob/main/scripts/bid_doc_optimizer.py"
click node_n_phase1 "https://github.com/mcs391/word-bid-beautifier/blob/main/scripts/bid_doc_optimizer.py"
click node_n_phase2 "https://github.com/mcs391/word-bid-beautifier/blob/main/scripts/bid_doc_optimizer.py"
click node_n_validate "https://github.com/mcs391/word-bid-beautifier/blob/main/scripts/bid_doc_optimizer.py"
click node_n_beautify "https://github.com/mcs391/word-bid-beautifier/blob/main/scripts/word_bid_beautify.py"
click node_n_readme "https://github.com/mcs391/word-bid-beautifier/blob/main/README.md"
click node_n_skill "https://github.com/mcs391/word-bid-beautifier/blob/main/SKILL.md"
click node_n_ref "https://github.com/mcs391/word-bid-beautifier/blob/main/references/ooxml-quickref.md"

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_n_input,node_n_optimizer,node_n_flags,node_n_phase0,node_n_phase1,node_n_phase2,node_n_validate,node_n_output,node_n_beautify toneBlue
class node_n_package,node_n_docxml,node_n_stylesxml,node_n_numberingxml,node_n_hikmap toneAmber
class node_n_readme,node_n_skill,node_n_ref toneMint
```

## Components

### Runtime pipeline

Command-line document transformation workflow and its specialized helper path.

- **DOCX input**: package · Source bid document archive provided to the tool.
- [Optimizer](https://github.com/mcs391/word-bid-beautifier/blob/main/scripts/bid_doc_optimizer.py): cli orchestrator · Primary command-line entry point that coordinates the document pipeline. · `scripts/bid_doc_optimizer.py`
- [CLI flags](https://github.com/mcs391/word-bid-beautifier/blob/main/scripts/bid_doc_optimizer.py): config · Phase selection, ranges, indentation, dry-run, and prefix control. · `scripts/bid_doc_optimizer.py`
- [Phase 0](https://github.com/mcs391/word-bid-beautifier/blob/main/scripts/bid_doc_optimizer.py): ooxml fixup · Repairs numbering inheritance and style-list linkage at the package level. · `scripts/bid_doc_optimizer.py`
- [Phase 1](https://github.com/mcs391/word-bid-beautifier/blob/main/scripts/bid_doc_optimizer.py): heading normalize · Normalizes chapter numbering and removes stray textual prefixes. · `scripts/bid_doc_optimizer.py`
- [Phase 2](https://github.com/mcs391/word-bid-beautifier/blob/main/scripts/bid_doc_optimizer.py): style polish · Applies style cleanup, indentation, spacing, and repeated-numbering cleanup. · `scripts/bid_doc_optimizer.py`
- [Validation](https://github.com/mcs391/word-bid-beautifier/blob/main/scripts/bid_doc_optimizer.py): consistency check · Post-transform checks such as paragraph-count consistency before writeout. · `scripts/bid_doc_optimizer.py`
- **DOCX output**: package · Repackaged modified document emitted by the optimizer.
- [Beautifier](https://github.com/mcs391/word-bid-beautifier/blob/main/scripts/word_bid_beautify.py): style utility · Specialized helper script for style-focused document cleanup. · `scripts/word_bid_beautify.py`

### OOXML surfaces

Word package parts that are read and rewritten during processing.

- **OOXML package**: archive structure · The zipped Word document container from which relevant parts are extracted and rebuilt.
- **document.xml**: paragraph tree · Paragraph content and numbering references within the Word document body.
- **styles.xml**: style map · Style definitions and style-to-list bindings used by HIK templates.
- **numbering.xml**: list defs · Numbering and multilevel list definitions that drive heading inheritance.
- **HIK styles**: template model · Fixed style mapping where 12–16 map to chapter headings and 17 to body text.

### Support files

Documentation and reference artifacts used by developers and operators.

- [README](https://github.com/mcs391/word-bid-beautifier/blob/main/README.md): docs · Usage and architecture overview for the repository. · `README.md`
- [SKILL](https://github.com/mcs391/word-bid-beautifier/blob/main/SKILL.md): workflow metadata · Assistant/workflow packaging metadata for the repository. · `SKILL.md`
- [OOXML ref](https://github.com/mcs391/word-bid-beautifier/blob/main/references/ooxml-quickref.md): reference notes · Quick reference for OOXML details used during development. · `references/ooxml-quickref.md`

## Connections

- DOCX input → Optimizer: open · The CLI consumes the source .docx package.
- Optimizer → CLI flags: reads · Operational flags control which phases and bounds run.
- Optimizer → Phase 0: runs · Phase 0 can execute as part of the main pipeline.
- Phase 0 → document.xml: updates · Paragraph-level numbering references are repaired.
- Phase 0 → styles.xml: updates · Style-to-list binding consistency is corrected.
- Phase 0 → numbering.xml: updates · List definitions and inheritance behavior are fixed.
- Phase 0 → Phase 1: feeds · Structural fixes enable later numbering normalization.
- Optimizer → Phase 1: runs · Phase 1 normalizes heading numbering and prefixes.
- Phase 1 → document.xml: reads/writes · Heading text and numbering sequences are normalized.
- Phase 1 → Phase 2: feeds · Content cleanup flows into presentation polishing.
- Optimizer → Phase 2: runs · Phase 2 performs style and layout cleanup.
- Phase 2 → styles.xml: updates · Heading styles, fonts, indentation, and spacing are refined.
- Phase 2 → document.xml: updates · Paragraph layout and repeated numbering cleanup are applied.
- Optimizer → Validation: checks · Validation runs after transformation before output is written.
- Validation → DOCX output: gate · Only validated output is repackaged.
- Optimizer → DOCX output: write · A new modified .docx is emitted.
- OOXML package → document.xml: contains · The archive is unpacked into document XML parts.
- OOXML package → styles.xml: contains · The archive carries style definitions.
- OOXML package → numbering.xml: contains · The archive carries numbering definitions.
- Optimizer → OOXML package: extracts/rebuilds · Processing operates on the OOXML package boundary.
- Beautifier → styles.xml: focuses on · The helper tool concentrates on style beautification.
- Beautifier → document.xml: polishes · It applies narrower text and layout cleanup.
- HIK styles → styles.xml: defines · Template-specific style semantics are expressed through styles.xml.
- HIK styles → Phase 1: constrains · Heading normalization follows the HIK numbering model.
- README → Optimizer: documents · README describes the main pipeline and usage.
- OOXML ref → Optimizer: supports · OOXML reference guides implementation details.
- SKILL → README: packages · Workflow metadata accompanies the repository docs.
