Skip to content

Commit ff059a9

Browse files
Copilotneongreen
authored andcommitted
lion
1 parent 4ab5823 commit ff059a9

21 files changed

Lines changed: 1953 additions & 0 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ linters/uselesswrapper/uselesswrapper
2727

2828
# Idk
2929
actionlint
30+
bin/

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ This repository contains multiple independent projects.
2222
| [jj-run-py](jj-run-py/) | deprecated | Old version of jj-run written in Python. |
2323
| [tk-vscode](tk-vscode/) | alpha | VS Code extension that lists tk tasks by running `tk ls --json`. |
2424
| [aihook](aihook/) | alpha | Claude Code hook validator that enforces shell scripting best practices. |
25+
| [lion](lion/) | alpha | Documentation extraction tool that generates markdown from special comments in Go code. |
2526

2627
## Libraries
2728

lion/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
lion

lion/AGENTS.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Agent Guidelines for lion
2+
3+
## Building and Testing
4+
5+
```bash
6+
# Build from repository root
7+
go build ./lion
8+
9+
# Run tests
10+
go test ./lion/...
11+
12+
# Run the tool
13+
go run ./lion generate
14+
go run ./lion topics
15+
16+
# Format code
17+
goimports -w lion/
18+
```
19+
20+
## Project Structure
21+
22+
```
23+
lion/
24+
├── main.go # CLI entry point
25+
├── internal/
26+
│ ├── extractor/ # AST parsing and comment extraction
27+
│ │ └── extractor.go
28+
│ └── generator/ # Markdown file generation
29+
│ └── generator.go
30+
├── README.md
31+
├── AGENTS.md
32+
└── go.mod
33+
```
34+
35+
## Comment Format
36+
37+
The tool looks for comments starting with `//lion:topic-name`:
38+
39+
```go
40+
//lion:topic-name Optional documentation content
41+
func SomeFunction() {
42+
// ...
43+
}
44+
```
45+
46+
## Implementation Notes
47+
48+
- Uses Go's `go/ast` package for parsing
49+
- Extracts comments attached to functions, types, constants, and variables
50+
- Skips test files (`*_test.go`)
51+
- Generates one markdown file per topic
52+
- Sorts entries by file and line number for consistency
53+
- Includes source references in generated markdown
54+
55+
## Testing Approach
56+
57+
Tests should cover:
58+
- Comment parsing (various formats and edge cases)
59+
- AST traversal (different Go declarations)
60+
- Markdown generation (formatting and structure)
61+
- File I/O operations
62+
63+
## Future Enhancements
64+
65+
Potential features (not yet implemented):
66+
- Multi-line comment support
67+
- Custom templates for generated markdown
68+
- Cross-references between topics
69+
- Filtering by package or file patterns
70+
- HTML output format

lion/README.md

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
# lion
2+
3+
Documentation extraction tool for Go code that generates markdown files from special comments.
4+
5+
## Overview
6+
7+
lion scans Go source code for special `//lion:topic-name` comments and generates organized markdown documentation. This allows you to build a book-like documentation structure by adding comments throughout your codebase that contribute to different chapters/topics.
8+
9+
## Installation
10+
11+
```bash
12+
go install github.com/neongreen/mono/lion@latest
13+
```
14+
15+
## Usage
16+
17+
### Add Documentation Comments
18+
19+
Add `//lion:topic-name` comments to your Go code:
20+
21+
```go
22+
//lion:architecture This is the main entry point for the application.
23+
//lion:architecture It handles initialization and routing.
24+
func main() {
25+
// ...
26+
}
27+
28+
//lion:api The Config struct holds application configuration.
29+
//lion:api Fields can be loaded from environment variables or config files.
30+
type Config struct {
31+
Port int
32+
Debug bool
33+
}
34+
```
35+
36+
### Generate Documentation
37+
38+
```bash
39+
# Generate docs from current directory to ./docs
40+
lion generate
41+
42+
# Specify input directory and output directory
43+
lion generate ./myproject --output ./documentation
44+
45+
# List all topics found in code
46+
lion topics
47+
```
48+
49+
### Output
50+
51+
The tool generates:
52+
- One markdown file per topic (e.g., `architecture.md`, `api.md`)
53+
- An `index.md` file listing all topics
54+
- Each entry includes source file reference for traceability
55+
56+
## Comment Format
57+
58+
lion supports three comment formats:
59+
60+
### 1. Single-line format (marker first)
61+
62+
```
63+
//lion:topic-name Optional content describing this code element
64+
```
65+
66+
- **topic-name**: Hyphen-separated identifier (becomes filename)
67+
- **Content**: Optional markdown-formatted text
68+
- Multiple consecutive `//lion:topic` lines are combined into one entry
69+
70+
### 2. Block comment format
71+
72+
```go
73+
/*lion:topic-name
74+
Multi-line content can go here
75+
without repeating the topic name.
76+
This makes documentation cleaner.
77+
*/
78+
```
79+
80+
- **topic-name**: Specified once at the beginning
81+
- **Content**: All subsequent lines in the block comment
82+
- Cleaner for longer documentation blocks
83+
84+
All formats can be attached to functions, types, constants, variables, and package declarations.
85+
86+
## Example
87+
88+
Marker-first format (recommended):
89+
90+
```go
91+
//lion:getting-started
92+
//
93+
// lion is a documentation extraction tool.
94+
// Add lion comments to generate markdown docs.
95+
// This is the cleanest syntax for multi-line docs.
96+
package main
97+
98+
//lion:architecture
99+
//
100+
// The main function initializes the app.
101+
// It handles setup and configuration.
102+
func main() {
103+
// ...
104+
}
105+
```
106+
107+
Single-line format:
108+
109+
```go
110+
//lion:getting-started lion is a documentation extraction tool.
111+
//lion:getting-started Add lion comments to generate markdown docs.
112+
package main
113+
114+
//lion:architecture The main function initializes the app.
115+
func main() {
116+
// ...
117+
}
118+
```
119+
120+
Block comment format:
121+
122+
```go
123+
/*lion:getting-started
124+
lion is a documentation extraction tool.
125+
Add lion comments to generate markdown docs.
126+
No need to repeat the topic on each line.
127+
*/
128+
package main
129+
130+
/*lion:architecture
131+
The main function initializes the app.
132+
It handles setup and configuration.
133+
*/
134+
func main() {
135+
// ...
136+
}
137+
```
138+
139+
Running `lion generate` creates:
140+
- `docs/getting-started.md`
141+
- `docs/architecture.md`
142+
- `docs/index.md`
143+
144+
## Status
145+
146+
**Alpha** - Core functionality works. Comment format and CLI may change.
147+
148+
## Self-Documentation
149+
150+
lion uses itself to generate its own documentation! The `docs/` directory contains markdown files extracted from lion comments in the source code.
151+
152+
To regenerate the documentation:
153+
```bash
154+
./regenerate-docs.sh
155+
```
156+
157+
Or manually:
158+
```bash
159+
go run . generate . --output ./docs
160+
```

lion/docs/errors-and-validation.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Errors And Validation
2+
3+
## Error handling
4+
5+
Validation and error handling:
6+
- Invalid or empty topics are ignored silently (no explicit validation yet).
7+
- Conflicting topic/section titles within the same comment group fail extraction with file:line.
8+
- Comments that do not attach to package/func/type/const/var doc groups are skipped.
9+
- CLI exits non-zero only when extraction fails (parse/metadata error) or generation fails (write error).
10+
- Bad markers inside otherwise valid files do not stop extraction; they are just skipped.
11+
12+
*Source: `lion/internal/extractor/extractor.go:292`*
13+

lion/docs/file-title.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# File Title
2+
3+
## Titles and headings
4+
5+
The generated file title comes from the topic display title:
6+
- Default: Title Case of the topic slug (e.g., "getting-started" -> "Getting Started").
7+
- Override: set title="Custom Title" on any lion marker for that topic; conflicts fail generation.
8+
Entry headings:
9+
- Default: the attached entity name (package <name>, function name, first const/var in the block).
10+
- Override per entry: section="Custom Section"; section="" suppresses the heading entirely (will emit a warning).
11+
- Conflicting section titles within the same comment group fail extraction.
12+
13+
*Source: `lion/internal/generator/generator.go:23`*
14+

lion/docs/how-to-run-generation.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# How To Run Generation
2+
3+
## Run generation
4+
5+
To run generation:
6+
7+
1. Navigate to your project directory
8+
9+
2. Run: lion generate
10+
11+
3. Check ./docs for generated markdown files
12+
13+
You can specify a different directory and output location:
14+
15+
lion generate ./myproject --output ./documentation
16+
17+
*Source: `lion/main.go:23`*
18+

lion/docs/implementation.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Implementation
2+
3+
## Extraction pipeline
4+
5+
Extraction pipeline:
6+
7+
- Walks all .go files under the directory, skipping *_test.go.
8+
9+
- Parses with comments and pulls lion markers from package doc, func doc, and type/const/var
10+
11+
doc comments (first name in a const/var block is used as the entity).
12+
13+
- Supports single-line markers and block comment markers (marker at top of the doc block).
14+
15+
- Aggregates snippets per topic across files; generator writes one file per topic.
16+
17+
*Source: `lion/internal/extractor/extractor.go:26`*
18+

lion/docs/index.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Documentation Index
2+
3+
This documentation was generated from lion comments in the source code.
4+
5+
## Topics
6+
7+
- [Errors And Validation](errors-and-validation.md)
8+
- [File Title](file-title.md)
9+
- [How To Run Generation](how-to-run-generation.md)
10+
- [Implementation](implementation.md)
11+
- [Output Behavior](output-behavior.md)
12+
- [Snippet Order](snippet-order.md)
13+
- [Supported Syntax](supported-syntax.md)

0 commit comments

Comments
 (0)