archived-node-pygmentize-bundled icon indicating copy to clipboard operation
archived-node-pygmentize-bundled copied to clipboard

Added possibility to specify End of Line character in output

Open mlewand opened this issue 10 years ago • 0 comments

What's the issue?

Currently node-pygmentize-bundled adds OS-specific line endings to output, even if input source code has diffrent line endings. I've faced this issue while working with grunt plugin which is using this project.

Since Windows by default uses \r\n as its EOL, it started to result with files having both \n and \r\n mixed in.

I've started to dig it a little bit, and tested Pygments itself, but it was managing EOLs as expected.

Because of this issue node-pygmentize-bundled tests were failing too at Win.

Design concept:

I didn't change the defalut behaviour, lines will be changes only when the options.eol property will be set - this way it will keep backward compatibility.

I've added a note to options list in README.md.

What I've done here?

index.js modifications:

Changes went to both fromString and fromStream functions.

  • Added a osEolRegExp variable, which will store a RegExp for OS EOL.
  • I've added extra parameter, options. Options allows us to check desired EOL markers.
  • Logic, which will handle a case, when options.eol was explicitly specified:
    • if expected EOL is the same as current OS, we don't need to do any extra steps.
    • if OS EOL differs from expected, we need to replace all the OS specific markers.

test.js modifications:

  • I've added options.eol = "\n" in each TC, since this is the case that we want to work with.
  • I've created extra TC to make sure that fix is working.

Extra thoughts

I didn't test this solution on Mac, so it might be a good idea to check it there.

mlewand avatar May 17 '14 12:05 mlewand