* Initial plan * Add Copilot instructions for repository Co-authored-by: ZeroHawkeye <161401688+ZeroHawkeye@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: ZeroHawkeye <161401688+ZeroHawkeye@users.noreply.github.com>
8.1 KiB
Copilot Instructions for WordZero
Project Overview
WordZero is a Golang-based Word document (.docx) manipulation library that follows the Office Open XML (OOXML) specifications. The library provides comprehensive functionality for creating, reading, and modifying Word documents with a focus on performance and ease of use.
Core Philosophy
- Zero Dependencies: Pure Go implementation with minimal external dependencies (only goldmark for Markdown support)
- Performance First: Optimized for speed - average 2.62ms processing time
- Clean API: Fluent interface design for intuitive usage
- Office Open XML Compliance: Strict adherence to OOXML specifications
Code Conventions
General Guidelines
-
Language:
- Code comments should be in Chinese (中文) as per project standard
- Function/method documentation should include Chinese descriptions
- Variable names and code should use English
-
Naming Conventions:
- Use PascalCase for exported types and functions
- Use camelCase for unexported types and functions
- Constants should use PascalCase or SCREAMING_SNAKE_CASE where appropriate
-
Code Organization:
- Keep related functionality in the same file
- Main types:
Document,Paragraph,Table,Run, etc. - Package structure:
pkg/document/- Core document manipulationpkg/style/- Style managementpkg/markdown/- Markdown conversion
-
Error Handling:
- Use custom error types from
errors.go - Wrap errors with context using
WrapErrororWrapErrorWithContext - Return meaningful error messages
- Use custom error types from
Go-Specific Guidelines
-
Interfaces: Define interfaces where abstraction is needed (e.g.,
BodyElement) -
Struct Tags: Use XML struct tags for OOXML serialization:
type Paragraph struct { XMLName xml.Name `xml:"w:p"` Properties *ParagraphProperties `xml:"w:pPr,omitempty"` Runs []Run `xml:"w:r"` } -
Method Receivers: Use pointer receivers for methods that modify the receiver
-
XML Marshaling: Implement custom
MarshalXMLmethods when element ordering matters (seeBody.MarshalXML)
Project Structure
wordZero/
├── pkg/
│ ├── document/ # Core document manipulation API
│ ├── style/ # Style management system
│ └── markdown/ # Markdown ↔ Word conversion
├── examples/ # Usage examples and demos
├── benchmark/ # Performance benchmarks
├── test/ # Integration tests
└── docs/ # Documentation
Building and Testing
Build Commands
# Build all packages
go build ./...
# Build specific package
go build ./pkg/document
Test Commands
# Run all tests
go test ./...
# Run tests for specific packages
go test ./pkg/document ./pkg/style ./test
# Run tests with coverage
go test -cover ./...
# Run short tests (skip long-running tests)
go test -short ./...
# Verbose test output
go test -v ./pkg/...
Code Quality
# Format code
go fmt ./...
# Vet code for issues
go vet ./...
# Run linters (if golangci-lint is installed)
golangci-lint run
Key Features to Understand
1. Document Operations
- Create, open, and save Word documents
- Add paragraphs, headings, and formatted text
- Support for document properties and metadata
2. Style System
- 18 predefined styles
- Custom style creation and inheritance
- Style manager for centralized style handling
3. Table Operations
- Complete table creation and manipulation
- Cell merging (horizontal, vertical, range)
- Cell formatting and styling
- Table iterators for traversal
4. Page Settings
- Page size (A4, Letter, Legal, etc.)
- Page orientation (portrait/landscape)
- Margins, headers, footers
- Section properties
5. Advanced Features
- Table of contents (TOC) generation
- Footnotes and endnotes
- List numbering (ordered/unordered)
- Template engine with inheritance
- Image embedding and manipulation
- Markdown ↔ Word bidirectional conversion
6. Template Engine
- Variable substitution:
{{variable}} - Conditionals:
{{#if condition}}...{{/if}} - Loops:
{{#each list}}...{{/each}} - Template inheritance:
{{extends "base"}}with{{#block}}overrides
Testing Standards
- Test File Naming: Use
_test.gosuffix - Test Function Naming: Use
Testprefix followed by function name - Test Organization:
- Unit tests in
pkg/*/directories - Integration tests in
test/directory
- Unit tests in
- Test Output: Write test output files to
test_output/directory - Cleanup: Always defer cleanup of test files using
os.RemoveAll
Example:
func TestCreateDocument(t *testing.T) {
doc := document.New()
doc.AddParagraph("Test content")
err := doc.Save("test_output/test.docx")
if err != nil {
t.Fatalf("Failed to save: %v", err)
}
defer os.RemoveAll("test_output")
}
API Design Patterns
Fluent Interface
Methods should return the receiver to allow chaining:
para := doc.AddParagraph("Text")
para.SetAlignment(document.AlignCenter).
SetSpacing(&document.SpacingConfig{LineSpacing: 1.5}).
SetStyle("Heading1")
Configuration Structs
Use config structs for complex operations:
type TableConfig struct {
Rows int
Cols int
Width int
Style string
}
Index-Based Access
All array/slice indexing starts at 0
XML and OOXML Standards
-
Namespace Prefixes: Use standard OOXML prefixes:
w:for WordprocessingML main namespacer:for relationshipsa:for DrawingML
-
Element Ordering: OOXML requires specific element ordering. Use custom marshaling when needed.
-
Units:
- Measurements in twentieths of a point (twips): 1 pt = 20 twips
- Some APIs accept millimeters and convert internally
Common Pitfalls to Avoid
- Don't modify the
Elementsslice inBodydirectly; use provided methods - Don't assume table cells exist before checking bounds
- Don't forget to handle errors from Save/Open operations
- Do use the style manager for style operations
- Do clean up test files in test functions
- Do preserve XML element ordering when implementing custom marshaling
Documentation Standards
- Package Documentation: Include package-level doc.go files
- Exported Functions: Must have godoc comments starting with the function name
- Complex Functions: Include usage examples in comments
- README Updates: Update relevant README files when adding features
Example:
// AddParagraph 添加简单段落到文档
// 参数:
// text - 段落文本内容
// 返回:
// *Paragraph - 新创建的段落对象
func (d *Document) AddParagraph(text string) *Paragraph {
// implementation
}
Performance Considerations
- Avoid Unnecessary Allocations: Reuse slices and buffers where possible
- Lazy Loading: Load document parts only when needed
- Streaming: For large documents, consider streaming approaches
- Benchmarking: Add benchmarks for performance-critical code
Contributing Guidelines
-
Before Adding Features:
- Check if similar functionality exists
- Consider backward compatibility
- Add tests for new functionality
-
Code Changes:
- Run
go fmtbefore committing - Ensure all tests pass
- Add examples for new public APIs
- Run
-
Documentation:
- Update pkg/document/README.md for API changes
- Add examples in examples/ directory
- Update CHANGELOG.md
Dependencies
The project maintains minimal dependencies:
github.com/yuin/goldmark- Markdown parsing (used only in markdown package)
When adding dependencies:
- Justify the need for the dependency
- Prefer standard library solutions
- Consider performance and maintenance implications
Special Notes
- Character Encoding: All text is UTF-8; ensure proper encoding handling
- File Paths: Use absolute paths in tests to avoid working directory issues
- Compatibility: Target Go 1.22+ (as specified in go.mod)
- Examples: Each major feature should have a working example in examples/