source-highlight
v1.0.0
Published
Bindings for the GNU source-highlight library
Maintainers
Readme
node-source-highlight
GNU source-highlight is a library to highlight source code in several different languages and output formats. This package provides Node.js bindings to the C++ library; currently, it requires you to have the library preinstalled.
Installation :package:
npm install --save source-highlightExample
// Load the source code into a UTF-8 buffer
const input = Buffer.from(`
console.log('Hello world!');
`);
// Create the SourceHighlight instance. "esc.outlang" will output colored characters to the console. You can
// use "html.outlang" too.
const sourceHighlight = new SourceHighlight('esc.outlang');
// Highlight the source code.
const output = sourceHighlight.highlight(input, 'javascript.lang', '').toString();
console.log(output);API Documentation - SourceHighlight
import { SourceHighlight } from 'source-highlight';[email protected] exports just the SourceHighlight class. The API documentation for the GNU source-highlight
can be found here. The methods of SourceHighlight provided
by this package are enlisted below.
Constructor
constructor(outLangDef: string)Create a SourceHighlight instance that will output into the format specified, e.g. html.outlang.
highlight
highlight(input: string, output: string, inputLang: string): voidHighlight the input file and write into the output file. inputLang is the name of the language
definition file you want to use. (This cannot be a path, e.g. html.lang is valid not /path/to/html.lang)
highlight(input: Buffer, inputLang: string, inputFileName: string): BufferHighlight the source code in the UTF-8 buffer using the language definition file inputLang (again only a
filename in the data directory, not a path). You can provide inputFileName optionally OR pass a blank string.
checkLangDef
checkLangDef(langFile: string): voidCheck if the language definition file (in the data directory) is valid. It will throw an error if it is invalid.
checkOutLangDef
checkOutLangDef(outLangFile: string): voidCheck if the output format specification file (in the data directory) is valid. It will throw an error if it is invalid.
setDataDir
setDataDir(dataDir: string): voidSet the data directory. By default, this will be a directory with all the language definition files and output formats that ship with GNU source-highlight.
setStyleFile
setStyleFile(styleFile: string): voidSet the style file.
setStyleCssFile
setStyleCssFile(cssFile: string): voidUse a CSS style file.
setStyleDefaultFile
setStyleDefaultFile(styleFile: string): voidSet the default style file.
setTitle
setTitle(title: string): voidsetInputLang
setInputLang(inputLang: string): voidSet the input language to use when it can't be inferred.
setCss
setCss(css: string): voidsetHeaderFileName
setHeaderFileName(headerFileName: string): voidSet the header file to be prepended to the output document.
setFooterFileName
setFooterFileName(footerFileName: string): voidSet the footer file to be appended to the output document.
setOutputDir
setOutputDir(outputDir: string): void