VarspoolPygmentsBundle
Provides (X)HTML rendering of Markdown similar to GFM (Github flavored markdown),
including syntax-highted fenced code blocks. Code coloring is provided by
the Python Pygments library (interfaces with pygmentize using proc_open for now)., (*1)
VarspoolPygmentsBundle doesn't reinvent the wheel: it uses the Sundown support
in KwattroMarkdownBundle to do
the initial Markdown rendering., (*2)
Installation
Install the Sundown PHP extension
The Sundown extension is available via
PECL in beta state, so installing it should be something like:, (*3)
sudo pecl install sundown-beta
You'll be able to tell you're successful when the Sundown extension is shown in
the output of php\_info()
:, (*4)
php -i | grep 'Sundown Support'
Install pygments
This is usually available via your package manager, as the python-pygments
package or similar., (*5)
``` sh
sudo apt-get install python-pygments, (*6)
The key thing, however, is just that you have the pygmentize script available
to execute. It's usually at `/usr/bin/pygmentize`, but if not, you can
configure its location (see app/config.yml below).
### Composer
Add `varspool/pygments-bundle` to your requires field. Then install/update your
dependencies.
### app/AppKernel.php
Register the `KwattroMarkdownBundle` and `VarspoolPygmentsBundle`:
```php
# app/AppKernel.php
public function registerBundles()
{
$bundles = array(
//...
new Kwattro\MarkdownBundle\KwattroMarkdownBundle(),
new Varspool\PygmentsBundle\VarspoolPygmentsBundle(),
);
}
app/config.yml
Next, configure the default Markdown renderer for the kwattro_markdown
service,
so that it'll stop complaining., (*7)
kwattro_markdown:
renderer: xhtml
You can optionally configure where to find the pygmentize
script. The default
is /usr/bin/pygmentize
:, (*8)
varspool_pygments:
bin: /usr/local/bin/pygmentize
You can also specify lexer arguments, that'll be passed to Pygmentize. See
the documentation for a list:, (*9)
varspool_pygments:
lexer_arguments:
linenos: table
Despite its name, this option can also contain formatter arguments,
such as linenos., (*10)
Usage
Services
kwattro_markdown
KwattroMarkdownBundle usually provides the kwattro_markdown
service. This
won't change when you set up VarspoolPygmentsBundle: the service will continue
to provide a Markdown rendering without syntax highlighting. This service is
usually a Kwattro\MarkdownBundle\Markdown\KwattroMarkdown
object., (*11)
$xhtml = $this->get('kwattro_markdown')->render($markdown_source);
varspool_markdown
Once you've installed VarspoolPygmentsBundle, you'll have a second service
available: vaspool_markdown
. This service will extend
Kwattro\MarkdownBundle\Markdown\KwattroMarkdown
, so you should just be able
to swap it in as a replacement quite easily. It'll colorize fenced code blocks
in the markdown. This service is usually a
Varspool\PygmentsBundle\Markdown\KwattroMarkdown
object., (*12)
$colorized_xhtml = $this->get('varspool_markdown')->render($markdown_source);
varspool_pygments
This service is the Sundown renderer instance responsible for coloring the
output. It's usually an instance of Varspool\PygmentsBundle\Sundown\Render\ColorXHTML
., (*13)
Stylesheets
The Pygments renderer marks up parts of the output with div
tags and classes.
You'll then need to assign stlying to these tags., (*14)
SCSS/Compass
If you're already using Compass or SASS, there's an example Pygments stylesheet
in Resources/public/css/_pygments.scss. The default implementation uses the
Solarized color scheme. You should be
able to @import this stylesheet from one of your own., (*15)
Dynamic Styles
Pygments can provide one of several stylesheets to automatically color the
output. A controller is provided that will output styles by calling
pygmentize -S <style>
. To use the controller, reference it from your routing:, (*16)
# app/config/routing.yml
varspool_pygments:
resource: '@VarspoolPygmentsBundle/Controller/PygmentsController.php'
type: annotation
Then include a CSS file in your page via the URL /pygments/<pygments_formatter>/<pygments_style>.css
.
(e.g. /pygments/html/friendly.css)., (*17)
Alternatively, you can get the styles as a string from the varspool_pygments service:, (*18)
$pygments_formatter = $this->container->get('varspool_pygments');
$styles = $pygments_formatter->getStyles('friendly');