Usage
Basic Setup
Add the following to your gem’s Rakefile
:
require 'docco/tasks'
That’s it! Now you can generate documentation with:
bundle exec rake docco:docs
This will:
- Read your
README.md
- Extract metadata from your
.gemspec
- Generate a beautiful HTML website in the
docs/
directory
- Copy the necessary CSS styles
Programmatic Usage
You can also use Docco programmatically in your Ruby code:
require 'docco'
# Basic usage - auto-detects gemspec
builder = Docco::DocsBuilder.new(
readme_path: 'README.md',
output_dir: 'docs'
)
builder.build
# With custom gemspec path
builder = Docco::DocsBuilder.new(
readme_path: 'README.md',
output_dir: 'public/docs',
gemspec_path: 'my_gem.gemspec'
)
builder.build
Available Rake Tasks
Docco provides three rake tasks:
Generate Documentation
# Default: uses README.md and outputs to docs/
bundle exec rake docco:docs
# With custom paths
bundle exec rake docco:docs[path/to/README.md,output/dir,my_gem.gemspec]
Copy Styles
If you want to customize the styles, first copy the default stylesheet:
# Copies to docs/styles.css (default)
bundle exec rake docco:css
# Copy to custom directory
bundle exec rake docco:css[custom/path]
Then you can edit docs/styles.css
to customize the appearance.
Generate GitHub Action
Automatically create a GitHub Action that builds and deploys your documentation to GitHub Pages:
bundle exec rake docco:gh
This creates .github/workflows/deploy-docs.yml
with a pre-configured workflow that:
- Runs on push to main branch
- Builds your documentation
- Deploys to GitHub Pages
GitHub Actions Integration
After running rake docco:gh
, you’ll have a GitHub Action that automatically deploys documentation. To complete the setup:
- Go to your GitHub repository settings
- Navigate to Pages section
- Set source to “GitHub Actions”
Now every push to main will automatically rebuild and deploy your docs!
Example workflow (created by docco:gh
):
name: Deploy Documentation
on:
push:
branches: [ main ]
jobs:
deploy:
runs-on: ubuntu-latest
permissions:
contents: read
pages: write
id-token: write
steps:
- uses: actions/checkout@v3
- uses: ruby/setup-ruby@v1
with:
ruby-version: '3.2'
- run: bundle install
- run: bundle exec rake docco:docs
- uses: actions/upload-pages-artifact@v1
with:
path: docs
- uses: actions/deploy-pages@v1
How It Works
Docco analyzes your README.md structure and creates a documentation website with:
- Navigation Sidebar: Generated from level 2 and 3 headings (
##
and ###
) in your README
- Main Content: Your entire README rendered as HTML
- Page Header: Uses your gem name and summary from the gemspec
- GitHub Link: Automatically extracted from your gemspec’s
source_code_uri
or homepage
README Structure Requirements
For best results, structure your README like this:
# Gem Name
Brief description of your gem.
## Installation
Installation instructions...
## Usage
### Basic Usage
Example code...
### Advanced Usage
More examples...
## Configuration
Configuration options...
## Contributing
Contributing guidelines...
- The first
#
heading becomes the page title
- Level 2 headings (
##
) become main navigation items
- Level 3 headings (
###
) become sub-navigation items
Custom Styles
Copy the default styles and customize them:
bundle exec rake docco:css
Then edit docs/styles.css
to match your branding. The CSS uses CSS custom properties (variables) for easy theming:
:root {
--primary-color: #007bff;
--bg-color: #ffffff;
--text-color: #333333;
/* ... and many more */
}
Docco extracts information from your gemspec. Make sure these fields are set:
Gem::Specification.new do |spec|
spec.name = "my_gem"
spec.summary = "A short description"
spec.description = "A longer description"
spec.homepage = "https://github.com/username/my_gem"
# For the GitHub link, set source_code_uri
spec.metadata["source_code_uri"] = "https://github.com/username/my_gem"
end
Example Output
Check out Docco’s own documentation (built with Docco, of course!):
https://ismasan.github.io/docco
Development
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
.
Testing Your Changes
Generate documentation for Docco itself:
bundle exec rake docco:docs
Then open docs/index.html
in your browser to see the results.
Requirements
- Ruby >= 3.2.0
- A README.md file
- (Optional) A .gemspec file for metadata
Dependencies
kramdown
- Markdown parsing
kramdown-parser-gfm
- GitHub-flavored Markdown support
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/ismasan/docco.
License
The gem is available as open source under the terms of the MIT License.