This repository provides a Perl-based syntax highlighter that leverages nanorc configuration files to provide ANSI-coloured output in the terminal, or HTML and SVG output for post-processing.
The primary tool in this repository is nano-colour. It was designed to bring nano-style syntax highlighting to the command line, allowing you to view highlighted source code directly in your terminal or pipe it to other tools.
- Syntax Auto-discovery: Automatically selects the correct highlighting rules based on the file extension.
- Support for
nanorcSyntax: Parses standardnanorcfiles, supporting:- Single-line regex rules (
color,icolor). - Multi-line rules (
start="..." end="..."). - Foreground and background color combinations.
brightcolor modifiers.
- Single-line regex rules (
- Multiple Output Formats:
- ANSI terminal colouring by default.
- HTML with CSS classes for later restyling.
- SVG with classed text spans and background rectangles.
- Flexible Input:
- Highlights files by name.
- Supports reading from
STDIN(when syntax is specified).
- Customizable Syntax Path: Uses the
NANOCOLOUR_DIRenvironment variable to locate highlighting definitions.
The nano-colour script is a standalone Perl script. Ensure you have Perl installed on your system.
# Add the script to your path or run it directly
chmod +x nano-colourTo highlight a file, provide the filename and the directory containing .nanorc files:
./nano-colour myfile.pl examples/You can set NANOCOLOUR_DIR to avoid passing the directory every time:
export NANOCOLOUR_DIR=$(pwd)/examples
./nano-colour myfile.plUse the -s or --syntax option to force a specific highlighting scheme:
./nano-colour -s perl myfile.txtWhen a syntax is forced, nano-colour can read from standard input:
cat myfile.pl | ./nano-colour -s perlUse --html to produce a wrapper <div> and a style block. The generated
output uses classes such as nanocolour-fg-red and nanocolour-bg-blue, so
you can restyle the output later:
./nano-colour --html myfile.pl examples/ > myfile.htmlUse --svg to produce an SVG document with the same colour classes:
./nano-colour --svg myfile.pl examples/ > myfile.svgRun the repository checks with:
./tests/run.shGitHub Actions runs the same command for pushes and pull requests.
The examples/ directory contains a variety of nanorc definitions for popular formats, including:
- Bash, C, HTML, JavaScript, JSON, Lua, Makefile, Markdown, Perl, Python, XML, YAML, and even
nanorcitself.
This project is licensed under the MIT License.