Berlioz Doc Parser is a PHP library to parse documentation and allow to integrate this on website for example.
You can install Berlioz Doc Parser with Composer, it's the recommended installation.
$ composer require berlioz/doc-parser
- PHP ^8.0
- PHP libraries:
- mbstring
- Packages:
- berlioz/html-selector
- berlioz/http-message
- league/flysystem
To parse files, you need additional package:
- For MarkDown files:
league/commonmark
- For reStructuredText files:
gregwar/rst
Library use league/flysystem
library to manipulate files.
This library permit to use some adapter like Local files, GitLab, Google... that's very useful for documentation source.
You need to define the parser of you documentation, in the example: Berlioz\DocParser\Parser\Markdown
.
use Berlioz\DocParser\DocGenerator;
use Berlioz\DocParser\Parser\Markdown;
use League\Flysystem\Filesystem;
use League\Flysystem\Local\LocalFilesystemAdapter;
$version = '1.0';
$docGenerator = new DocGenerator();
$docGenerator->addParser(new Markdown());
$documentation =
$docGenerator->handle(
$version,
new Filesystem(new LocalFilesystemAdapter('/path-of-project/doc'))
);
You can access to documentation files easily:
/** @var \Berlioz\DocParser\Doc\Documentation $documentation */
$files = $documentation->getFiles();
And filter the files by type:
use Berlioz\DocParser\Doc\File\FileInterface;
use Berlioz\DocParser\Doc\File\Page;
/** @var \Berlioz\DocParser\Doc\Documentation $documentation */
$pages = $documentation->getFiles(fn(FileInterface $file) => $file instanceof Page);
Two file type exists:
Berlioz\DocParser\Doc\File\Page
: page with parsed contentBerlioz\DocParser\Doc\File\RawFile
: raw file like images
When your documentation is generate, you can use Documentation::handle()
method to get pages and files.
use Berlioz\DocParser\Doc\Documentation;
use Berlioz\DocParser\Doc\File\Page;
/** @var Documentation $documentation */
$file = $documentation->handle('path/of/my/page');
if (null === $file) {
// Show not found error
}
// Raw file?
if (!$file instanceof Page) {
// Return \Psr\Http\Message\ResponseInterface object
return $file->response();
}
// Show HTML content of my page
echo $file->getContents();
A concept of treatment is implement to manipulate files after generation.
Some treatments are activate by default:
Berlioz\DocParser\Treatment\DocSummaryTreatment
: generate the summary of documentationBerlioz\DocParser\Treatment\PageSummaryTreatment
: generate the pages summariesBerlioz\DocParser\Treatment\PathTreatment
: resolve all links to transform in relative linksBerlioz\DocParser\Treatment\TitleTreatment
: extract title of page and set toPage
object
Optionals treatments are available:
Berlioz\DocParser\Treatment\BootstrapTreatment
: add CSS classes to HTML elements to beautify them for Bootstrap framework
You can also create your treatment class and add this to generator:
use Berlioz\DocParser\DocGenerator;
use Berlioz\DocParser\Treatment\BootstrapTreatment;
/** @var DocGenerator $docGenerator */
$docGenerator->addTreatment(new BootstrapTreatment($docGenerator));
A specific tag is for indexation of documentation.
```index
title: Title of page
slug: Slug name (replace only the filename, not path)
breadcrumb: Category; Sub category; My page
summary-order: 1
summary-visible: true
```
A specific directive is for indexation of documentation.
...index:
:title: Title of page
:slug: Slug name (replace only the filename, not path)
:breadcrumb: Category; Sub category; My page
:summary-order: 1
:summary-visible: true
title
: replace the title of page, the title treatment does not replace this titleslug
: replace the filename of page, not the complete path, it's an url encoded representation of valuebreadcrumb
: the breadcrumb of page into the documentation summary, if not present, the page will not in the summarysummary-order
: the order in summary sectionsummary-visible
: default to true, but you can define the page to not visible in summary
You can define your own options, for your treatments for example, options are available with Page::getMeta(...)
method.
Doc summary references all pages with a breadcrumb.
It's a hierarchy of Berlioz\DocParser\Doc\Summary\Entry
objects.
You can get path of entry with method Entry::getPath()
, it's the absolute path from root directory of documentation.
Page summary references all headings in the page content.
It's a hierarchy of Berlioz\DocParser\Doc\Summary\Entry
objects.
No path given to the entry, but an id Entry::getId()
linked to the corresponding heading.
Two parser are available by default:
Berlioz\DocParser\Parser\Markdown
: to parse MarkDown files, useleague/commonmark
packageBerlioz\DocParser\Parser\reStructuredText
: to parse reStructuredText files, usegregwar/rst
package
You can also create your own parser, you need only to implement Berlioz\DocParser\Parser\ParserInterface
interface.
If you need to add an extension to a specific parser, a getter method is available to access to the original parser. Example for MarkDown parser: \Berlioz\DocParser\Parser\Markdown::getCommonMarkConverter()
.
You can also pass to the Markdown
and reStructuredText
constructors the same parameters that corresponding libraries.
Generation of documentation take time, and it's not acceptable to generate documentation at every request.
Berlioz\DocParser\DocCacheGenerator
class generate a cache of generated and treated documentation to reuse it quickly.
The cache use league/flysystem
package to store cache files.
Example of usage:
use Berlioz\DocParser\DocCacheGenerator;
use Berlioz\DocParser\DocGenerator;
use Berlioz\DocParser\Parser\Markdown;
use League\Flysystem\Filesystem;
use League\Flysystem\Local\LocalFilesystemAdapter;
$version = '1.0';
$cacheFilesystem = new Filesystem(new LocalFilesystemAdapter('/path-of-project/cache'));
$docCacheGenerator = new DocCacheGenerator($cacheFilesystem);
if (null === ($documentation = $docCacheGenerator->get($version))) {
$docGenerator = new DocGenerator();
$docGenerator->addParser(new Markdown());
$documentation =
$docGenerator->handle(
$version,
new Filesystem(new LocalFilesystemAdapter('/path-of-project/doc'))
);
$docCacheGenerator->save($documentation);
}
$documentation->handle('path/of/my/page');