en Project Structure - ZeroHawkeye/wordZero GitHub Wiki

Project Structure

This document explains the organization and architecture of the WordZero codebase, helping developers understand the project layout and contribute effectively.

๐Ÿ“ Directory Structure

wordZero/
โ”œโ”€โ”€ pkg/                     # Core library packages
โ”‚   โ”œโ”€โ”€ document/           # Document manipulation core
โ”‚   โ”œโ”€โ”€ style/              # Style system
โ”‚   โ””โ”€โ”€ markdown/           # Markdown conversion utilities
โ”œโ”€โ”€ examples/               # Example applications and demos
โ”‚   โ”œโ”€โ”€ basic/             # Basic usage examples
โ”‚   โ”œโ”€โ”€ advanced_features/ # Advanced feature demonstrations
โ”‚   โ”œโ”€โ”€ table/             # Table manipulation examples
โ”‚   โ”œโ”€โ”€ style_demo/        # Style system showcase
โ”‚   โ”œโ”€โ”€ formatting/        # Text formatting examples
โ”‚   โ”œโ”€โ”€ template_demo/     # Template system usage
โ”‚   โ”œโ”€โ”€ markdown_demo/     # Markdown conversion examples
โ”‚   โ””โ”€โ”€ page_settings/     # Page configuration examples
โ”œโ”€โ”€ test/                   # Test files and test data
โ”‚   โ”œโ”€โ”€ output/            # Test output files
โ”‚   โ””โ”€โ”€ fixtures/          # Test fixtures and sample data
โ”œโ”€โ”€ benchmark/              # Performance benchmarks
โ”‚   โ”œโ”€โ”€ golang/            # Go benchmarks
โ”‚   โ”œโ”€โ”€ javascript/        # JavaScript comparison benchmarks
โ”‚   โ”œโ”€โ”€ python/            # Python comparison benchmarks
โ”‚   โ””โ”€โ”€ results/           # Benchmark results and charts
โ”œโ”€โ”€ docs/                   # Additional documentation
โ”œโ”€โ”€ wordZero.wiki/          # Wiki documentation (this documentation)
โ”œโ”€โ”€ go.mod                  # Go module definition
โ”œโ”€โ”€ go.sum                  # Go module checksums
โ”œโ”€โ”€ README.md               # Project overview
โ”œโ”€โ”€ README_zh.md           # Chinese README
โ”œโ”€โ”€ CHANGELOG.md           # Version history
โ”œโ”€โ”€ LICENSE                # Project license
โ””โ”€โ”€ debug.go               # Debug utilities

๐Ÿ“ฆ Core Packages

pkg/document

The document package is the core of WordZero, providing the main document manipulation functionality.

Key Files:

  • document.go - Main document structure and operations
  • paragraph.go - Paragraph handling and text operations
  • table.go - Table creation and manipulation
  • styles.go - Style application and management
  • page.go - Page settings and layout
  • properties.go - Document properties and metadata
  • advanced.go - Advanced features (TOC, footnotes, etc.)
  • iterator.go - Table cell iterator implementation
  • format.go - Text formatting structures and methods

Responsibilities:

  • Document creation, loading, and saving
  • Paragraph and text manipulation
  • Table operations and cell management
  • Style application and formatting
  • Page layout and settings
  • Advanced features implementation

pkg/style

The style package defines the style system and predefined styles.

Key Files:

  • styles.go - Style definitions and constants
  • manager.go - Style management and application
  • custom.go - Custom style creation and modification

Responsibilities:

  • Predefined style definitions (18 built-in styles)
  • Style inheritance and management
  • Custom style creation
  • Style validation and application

pkg/markdown

The markdown package provides bidirectional conversion between Markdown and Word formats.

Key Files:

  • parser.go - Markdown parsing and conversion
  • converter.go - Word to Markdown conversion
  • formatter.go - Format preservation and mapping

Responsibilities:

  • Markdown to Word document conversion
  • Word document to Markdown conversion
  • Format mapping and preservation
  • Content structure translation

๐Ÿ—๏ธ Architecture Patterns

1. Builder Pattern

WordZero uses the builder pattern for document construction:

// Document builder pattern
doc := document.New()
    .SetTitle("My Document")
    .SetPageSize(document.PageSizeA4)
    .AddParagraph("Content")
    .Save("document.docx")

2. Fluent Interface

Many operations support method chaining for better readability:

para := doc.AddParagraph("Text")
    .SetStyle(style.StyleHeading1)
    .SetAlignment(document.AlignmentCenter)

3. Iterator Pattern

Table operations use iterators for efficient cell traversal:

iterator := table.GetCellIterator()
for iterator.HasNext() {
    cell := iterator.Next()
    // Process cell
}

4. Factory Pattern

Document and component creation uses factory methods:

doc := document.New()              // Document factory
table := doc.AddTable(3, 3)       // Table factory
para := doc.AddParagraph("text")   // Paragraph factory

๐Ÿงช Testing Structure

Test Organization

test/
โ”œโ”€โ”€ unit/                   # Unit tests
โ”‚   โ”œโ”€โ”€ document_test.go   # Document operations tests
โ”‚   โ”œโ”€โ”€ table_test.go      # Table functionality tests
โ”‚   โ”œโ”€โ”€ style_test.go      # Style system tests
โ”‚   โ””โ”€โ”€ format_test.go     # Formatting tests
โ”œโ”€โ”€ integration/           # Integration tests
โ”‚   โ”œโ”€โ”€ complete_test.go   # End-to-end workflows
โ”‚   โ””โ”€โ”€ performance_test.go # Performance tests
โ”œโ”€โ”€ fixtures/              # Test data and samples
โ”‚   โ”œโ”€โ”€ sample.docx       # Sample Word documents
โ”‚   โ”œโ”€โ”€ test_data.json    # Test data sets
โ”‚   โ””โ”€โ”€ expected/         # Expected output files
โ””โ”€โ”€ output/               # Test output directory

Testing Conventions

  1. File Naming: Tests follow *_test.go convention
  2. Function Naming: Test functions start with Test
  3. Benchmark Naming: Benchmark functions start with Benchmark
  4. Test Data: Stored in fixtures/ directory
  5. Output Isolation: Each test uses unique output files

๐Ÿ“Š Examples Structure

Example Categories

The examples are organized by complexity and feature focus:

Basic Examples (examples/basic/)

  • Simple document creation
  • Basic text and paragraph operations
  • Fundamental style application

Advanced Examples (examples/advanced_features/)

  • Complex document structures
  • Advanced features usage
  • Multiple feature integration

Specialized Examples

  • table/ - Table operations and styling
  • style_demo/ - Style system showcase
  • formatting/ - Text formatting options
  • template_demo/ - Template-based generation
  • markdown_demo/ - Format conversion
  • page_settings/ - Page configuration

Example Conventions

  1. Self-contained: Each example is a complete, runnable program
  2. Output Directory: All examples save to examples/output/
  3. Error Handling: Comprehensive error checking and logging
  4. Documentation: Inline comments explaining key concepts
  5. Progressive Complexity: Examples build on previous concepts

๐Ÿš€ Development Workflow

Adding New Features

  1. Package Selection: Determine the appropriate package
  2. Interface Design: Design public APIs first
  3. Implementation: Implement core functionality
  4. Testing: Write comprehensive tests
  5. Examples: Create usage examples
  6. Documentation: Update relevant documentation

Code Organization Principles

  1. Single Responsibility: Each package has a clear, focused purpose
  2. Minimal Dependencies: Avoid unnecessary external dependencies
  3. Consistent APIs: Follow established patterns across packages
  4. Error Handling: Use standard Go error handling patterns
  5. Documentation: Comprehensive godoc comments

๐Ÿ“ Documentation Structure

Wiki Organization

The documentation is organized in a logical learning progression:

  1. Getting Started (01-05): Basic concepts and operations
  2. Core Features (06-08): Main functionality areas
  3. Advanced Topics (09-12): Complex features and best practices
  4. Reference (13-16): Detailed specifications and analysis

Documentation Types

  • Tutorials: Step-by-step learning guides
  • How-to Guides: Problem-solving focused
  • Reference: Comprehensive API documentation
  • Explanation: Conceptual background and design decisions

๐Ÿ”ง Build and Development

Prerequisites

  • Go 1.19 or higher
  • Git for version control
  • Make (optional, for build automation)

Development Commands

# Run tests
go test ./...

# Run specific package tests
go test ./pkg/document

# Run benchmarks
go test -bench=. ./...

# Generate documentation
go doc -all ./pkg/document

# Build examples
go build ./examples/basic

# Run linting (if configured)
golangci-lint run

Code Quality

  1. Formatting: Use gofmt for consistent formatting
  2. Linting: Follow golangci-lint recommendations
  3. Testing: Maintain >80% test coverage
  4. Documentation: Write comprehensive godoc comments
  5. Performance: Regular benchmark testing

๐ŸŽฏ Contributing Guidelines

Code Contributions

  1. Fork and Branch: Create feature branches from main
  2. Tests Required: All new code must include tests
  3. Documentation: Update relevant documentation
  4. Examples: Provide usage examples for new features
  5. Backwards Compatibility: Maintain API compatibility

Documentation Contributions

  1. Accuracy: Ensure technical accuracy
  2. Clarity: Write clear, beginner-friendly explanations
  3. Examples: Include practical code examples
  4. Completeness: Cover all aspects of features
  5. Maintenance: Keep documentation current with code changes

๐Ÿ“‹ Module Dependencies

Direct Dependencies

  • Standard Library Only: WordZero uses only Go standard library
  • No External Dependencies: Minimizes dependency management issues
  • Self-contained: Complete functionality without external packages

Internal Dependencies

pkg/document โ† pkg/style     # Document uses style definitions
pkg/document โ† pkg/markdown  # Markdown conversion uses document
examples/* โ† pkg/*           # Examples use all packages

๐Ÿ” File Conventions

Naming Conventions

  • Files: snake_case.go
  • Packages: lowercase
  • Types: PascalCase
  • Functions: PascalCase (exported), camelCase (unexported)
  • Constants: PascalCase or UPPER_CASE
  • Variables: camelCase

File Organization

  • Main functionality: package_name.go
  • Types and structures: types.go
  • Constants and enums: constants.go
  • Utilities: utils.go
  • Tests: *_test.go

This structure provides a solid foundation for understanding, using, and contributing to the WordZero project. The modular design ensures maintainability while the comprehensive examples and documentation support both learning and production use.