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 operationsparagraph.go
- Paragraph handling and text operationstable.go
- Table creation and manipulationstyles.go
- Style application and managementpage.go
- Page settings and layoutproperties.go
- Document properties and metadataadvanced.go
- Advanced features (TOC, footnotes, etc.)iterator.go
- Table cell iterator implementationformat.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 constantsmanager.go
- Style management and applicationcustom.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 conversionconverter.go
- Word to Markdown conversionformatter.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
- File Naming: Tests follow
*_test.go
convention - Function Naming: Test functions start with
Test
- Benchmark Naming: Benchmark functions start with
Benchmark
- Test Data: Stored in
fixtures/
directory - Output Isolation: Each test uses unique output files
๐ Examples Structure
Example Categories
The examples are organized by complexity and feature focus:
examples/basic/
)
Basic Examples (- Simple document creation
- Basic text and paragraph operations
- Fundamental style application
examples/advanced_features/
)
Advanced Examples (- Complex document structures
- Advanced features usage
- Multiple feature integration
Specialized Examples
table/
- Table operations and stylingstyle_demo/
- Style system showcaseformatting/
- Text formatting optionstemplate_demo/
- Template-based generationmarkdown_demo/
- Format conversionpage_settings/
- Page configuration
Example Conventions
- Self-contained: Each example is a complete, runnable program
- Output Directory: All examples save to
examples/output/
- Error Handling: Comprehensive error checking and logging
- Documentation: Inline comments explaining key concepts
- Progressive Complexity: Examples build on previous concepts
๐ Development Workflow
Adding New Features
- Package Selection: Determine the appropriate package
- Interface Design: Design public APIs first
- Implementation: Implement core functionality
- Testing: Write comprehensive tests
- Examples: Create usage examples
- Documentation: Update relevant documentation
Code Organization Principles
- Single Responsibility: Each package has a clear, focused purpose
- Minimal Dependencies: Avoid unnecessary external dependencies
- Consistent APIs: Follow established patterns across packages
- Error Handling: Use standard Go error handling patterns
- Documentation: Comprehensive godoc comments
๐ Documentation Structure
Wiki Organization
The documentation is organized in a logical learning progression:
- Getting Started (01-05): Basic concepts and operations
- Core Features (06-08): Main functionality areas
- Advanced Topics (09-12): Complex features and best practices
- 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
- Formatting: Use
gofmt
for consistent formatting - Linting: Follow
golangci-lint
recommendations - Testing: Maintain >80% test coverage
- Documentation: Write comprehensive godoc comments
- Performance: Regular benchmark testing
๐ฏ Contributing Guidelines
Code Contributions
- Fork and Branch: Create feature branches from main
- Tests Required: All new code must include tests
- Documentation: Update relevant documentation
- Examples: Provide usage examples for new features
- Backwards Compatibility: Maintain API compatibility
Documentation Contributions
- Accuracy: Ensure technical accuracy
- Clarity: Write clear, beginner-friendly explanations
- Examples: Include practical code examples
- Completeness: Cover all aspects of features
- 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
orUPPER_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.