Skip to content
This repository has been archived by the owner on Apr 5, 2018. It is now read-only.

Added possibility to specify End of Line character in output #9

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Language/lexer, formatter, and their options are currently supported. Filters ar
* `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`
* `eol`: desired End of Line marker in output, defaults to yours OS End od Line char - `String`
* `options`: lexer and formatter options, each key/value pair is passed through to `pygmentize` with `-P` - `Object`

## Examples
Expand Down
31 changes: 25 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const spawn = require('child_process').spawn
, exec = require('child_process').exec
, path = require('path')
, fs = require('fs')
, os = require('os')
, PassThrough = require('readable-stream/passthrough')
, mkdirp = require('mkdirp')
, bl = require('bl')
Expand All @@ -10,18 +11,29 @@ const spawn = require('child_process').spawn
, defaultFormat = 'html'
, defaultLang = 'js'
, defaultEncoding = 'utf8'
// This will create a RegExp for OS specific EOL character(s)
, osEolRegExp = new RegExp( os.EOL.toString(), 'g' )

var pythonVersions = {}

function fromString (child, code, callback) {
function fromString (child, code, options, callback) {
var stdout = bl()
, stderr = bl()
, ec = 0
, exitClose = function () {
if (++ec < 2)
return

callback(null, stdout.slice())

var ret = stdout.slice()
// If particular EOL are expected, we need to replace all of
// the OS specific EOL and change them to the desired ones
if (options.eol && options.eol !== os.EOL) {
ret = ret.toString().replace(osEolRegExp, options.eol)
// Cast back to the Buffer type
ret = new Buffer(ret, options.encoding || defaultEncoding)
}

return callback(null, ret)
}

child.stdout.pipe(stdout)
Expand All @@ -40,9 +52,16 @@ function fromString (child, code, callback) {
child.stdin.end()
}

function fromStream (retStream, intStream, child) {
function fromStream (retStream, intStream, options, child) {
var stderr = bl()
, outStream = through2(function (chunk, enc, callback) {
// If particular EOL are expected, we need to replace all of
// the OS specific EOL and change them to the desired ones
if (options.eol && options.eol !== os.EOL) {
chunk = chunk.toString().replace(osEolRegExp, options.eol)
chunk = new Buffer(chunk, options.encoding || defaultEncoding)
}

retStream.__write(chunk, enc, callback)
})

Expand Down Expand Up @@ -79,8 +98,8 @@ function pygmentize (options, code, callback) {
if (err)
return callback(err)
if (toString)
return fromString(child, code, callback)
fromStream(retStream, intStream, child)
return fromString(child, code, options, callback)
fromStream(retStream, intStream, options, child)
})

if (retStream) {
Expand Down
10 changes: 9 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ function simpleStringConversionTest (python) {
, output: '<div class="highlight"><pre><span class="k">var</span> <span class="nx">a</span> '
+ '<span class="o">=</span> <span class="k">true</span><span class="p">;</span></pre></div>'
}
, {
lang: 'python'
, format: 'html'
, input: '#a\n'
, output: '<div class="highlight"><pre><span class="c">#a</span>\r</pre></div>\r'
, eol: '\r'
}
]

t.plan(cases.length * 3)
Expand All @@ -46,6 +53,7 @@ function simpleStringConversionTest (python) {
, format : c.format
, options : c.options || {}
, python : python
, eol : c.eol || '\n'
}
, c.input
, function (err, result) {
Expand All @@ -69,7 +77,7 @@ function fileConversionTest (python) {
, { flags: 'w+', encoding: null, mode: 0666 }
)

fileIn.pipe(pygments({ lang: 'rb', format: 'html', python: python })).pipe(fileOut)
fileIn.pipe(pygments({ lang: 'rb', format: 'html', python: python, eol: '\n' })).pipe(fileOut)

fileOut.on('close', function() {
var expectedResult = fs.createReadStream(path.join(__dirname, '/test-fixtures/active_model.html'))
Expand Down