pygmentize-bundled

A simple wrapper around Python's Pygments code formatter, with Pygments bundled

no longer maintained
Last updated 10 years ago by rvagg .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install pygmentize-bundled 
SYNC missed versions from official npm registry.

Pygmentize (Bundled)

Python's Pygments code formatter, for Node.js, distributed with Pygments

NPM NPM

Can be used as either a String-in, Buffer-out, or as a Duplex stream.

Compatible with both Python v2 and v3.

Note: this library makes use of a child process which calls Python to invoke Pygments. This can cause performance problems where a large number of code blocks are being separately formatted. Consider using pygmentize-bundled-cached, an API-compatible wrapper for this library that keeps an on-disk cache of formatted code samples which will result in significantly faster formats when repeatedly formatting the same blocks of code.

API

pygmentize(options, code, callback)

Pygmentize a given code string and return it as a Buffer to the callback Function.

  • options contains options to be passed to Pygments (see Options).
  • code is a String to be formatted.
  • callback is a Function, called when complete. The first argument will be an error object/string if there was a problem and the second argument will be a Buffer containing your formatted code.

pygmentize(options)

When you only supply the options argument, it will return a Duplex stream that you can pipe to and from to format your code.

  • options contains options to be passed to Pygments (see Options).

Options

Language/lexer, formatter, and their options are currently supported. Filters are not supported yet.

  • lang: source language/lexer name - String
  • format: output formatter name - String
  • python: the full path to the python command on the current system, defaults to 'python' - String
  • options: lexer and formatter options, each key/value pair is passed through to pygmentize with -P - Object

Examples

The string interface is very simple:

var pygmentize = require('pygmentize-bundled')

pygmentize({ lang: 'js', format: 'html' }, 'var a = "b";', function (err, result) {
  console.log(result.toString())
})

Results in:

<div class="highlight"><pre>
  <span class="kd">var</span>
  <span class="nx">a</span>
  <span class="o">=</span>
  <span class="s2">&quot;b&quot;</span>
  <span class="p">;</span>
</pre></div>

Example with extra options:

var pygmentize = require('pygmentize-bundled')

pygmentize({ lang: 'php', format: 'html', options: { startinline: 1 } }, 'var a = true;', function (err, result) {
  console.log(result.toString())
})

A duplex streaming API is also available. Simply omit the code and callback arguments:

var pygmentize = require('pygmentize-bundled')

process.stdin
  .pipe(pygmentize({ lang: 'js', format: 'html' }))
  .pipe(process.stdout);

Refer to the Pygments documentation. For supported languages, see the list of lexers, for supported formatted, see the list of formatters.

Contributors

Licence & copyright

Pygments (Bundled) is Copyright (c) 2012 Rod Vagg <@rvagg> and licenced under the MIT licence. All rights not explicitly granted in the MIT license are reserved. See the included LICENSE file for more details.

Pygments is licenced under the BSD licence.

Current Tags

  • 2.3.0                                ...           latest (10 years ago)

11 Versions

  • 2.3.0 [deprecated]           ...           10 years ago
  • 2.3.0-pre1                                ...           10 years ago
  • 2.2.0 [deprecated]           ...           10 years ago
  • 2.1.1 [deprecated]           ...           11 years ago
  • 2.1.0 [deprecated]           ...           11 years ago
  • 2.0.0 [deprecated]           ...           11 years ago
  • 1.2.0 [deprecated]           ...           11 years ago
  • 1.1.0 [deprecated]           ...           11 years ago
  • 1.0.1 [deprecated]           ...           12 years ago
  • 1.0.0 [deprecated]           ...           12 years ago
  • 0.0.0 [deprecated]           ...           12 years ago
Maintainers (1)
Downloads
Total 1
Today 0
This Week 0
This Month 1
Last Day 0
Last Week 1
Last Month 0
Dependencies (2)
Dev Dependencies (2)
Dependents (0)
None

© 2010 - cnpmjs.org x YWFE | Home | YWFE