GithubHelp home page GithubHelp logo

cardinal's Introduction

cardinal Build Status

become a patron

NPM

car·di·nal (kärdn-l, kärdnl) - crested thick-billed North American finch having bright red plumage in the male.

screenshot

Features

  • highlights JavaScript code with ANSI colors to improve terminal output
  • theming support, see custom color themes
  • optionally print line numbers
  • API and command line interface (cdl)
  • .cardinalrc config to customize settings
  • supports UNIX pipes

Table of Contents generated with DocToc

Installation

As library

npm install cardinal

As Commandline Tool

[sudo] npm install -g cardinal

Note:

When installed globally, cardinal exposes itself as the cdl command.

Commandline

Highlight a file

cdl <file.js> [options]

options:

  • --nonum: turns off line number printing (relevant if it is turned on inside ~/.cardinalrc

As part of a UNIX pipe

cat file.js | grep console | cdl

Note:

Not all code lines may be parsable JavaScript. In these cases the line is printed to the terminal without highlighting it.

Theme

The default theme will be used for highlighting.

To use a different theme, include a .cardinalrc file in your HOME directory.

This is a JSON file of the following form:

{
  "theme": "hide-semicolons",
  "linenos": true|false
}
  • theme can be the name of any of the built-in themes or the full path to a custom theme anywhere on your computer.
  • linenos toggles line number printing

API

highlight(code[, opts])

  • returns the highlighted version of the passed code ({String}) or throws an error if it was not able to parse it
  • opts (see below)

highlightFileSync(fullPath[, opts])

  • returns the highlighted version of the file whose fullPath ({String}) was passed or throws an error if it was not able to parse it
  • opts (see below)

highlightFile(fullPath[, opts], callback)

  • calls back with the highlighted version of the file whose fullPath ({String}) was passed or with an error if it was not able to parse it
  • opts (see below)
  • callback ({Function}) has the following signature: function (err, highlighted) { .. }

opts

opts is an {Object} with the following properties:

  • theme {Object} is used to optionally override the theme used to highlight
  • linenos {Boolean} if true line numbers are included in the highlighted code
  • firstline {Integer} sets line number of the first line when line numbers are printed
  • jsx {Boolean} if true JSX syntax is supported, otherwise cardinal will raise an error when encountering JSX (default: false)

Note The json option is obsoleted and not necessary anymore as cardinal properly understands both JSON and JavaScript.

Examples (browse)

cardinal's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cardinal's Issues

Problem targeting a custom theme

I'm trying to customize it, but when I simply add the theme option, it stops highlighting!

For example:

// works with default theme
cardinal.highlight(codeString,  {
    linenos: true
});

But:

// do not work!
// nothing gets lighlighted
cardinal.highlight(codeString,  {
    linenos: true,
    theme: 'default'
});

Am I doing something wrong?

My idea is to use it like this:

cardinal.highlight(codeString,  {
    linenos: true,
    theme: __dirname + '/sh-theme.js'
});

Should that work?
Also, it would be interesting if we could send the theme object itself, like this:

cardinal.highlight(codeString,  {
    linenos: true,
    theme: require('./sh-theme.js')
});

Docked

Hi, just pointing out that since I found cardinal pretty handy for prettifying JSON in some of my scripts but I didn't want to try to package my scripts with the dependency on node and cardinal, I threw this together:

https://github.com/giftig/docker-cardinal

If having a docker quickstart for cardinal is something that's of interest to you, I'd like to contribute it. It's pretty rough but does the job. I stuck an image in https://hub.docker.com/r/giftig/cardinal/

Can't colorize JavaScript with syntax errors.

I was trying to super colorize a console error stack trace code part of the message and run into cardinal which seemed great for colorizing valid JS, but fails if:

 Unable to perform highlight. The code contained syntax errors:

Kind of ironic. Too bad.

Related

Link Typo

Thanks for this repo man, just wanted to show ya a lil missing .

image

Erroneous parse error

I'm getting a syntax error when I don't think I should.

$ node
> var cardinal = require('cardinal')

> cardinal.highlight("{name: 'lodash'}")
'\u001b[33m{\u001b[39m\u001b[37mname\u001b[39m\u001b[93m:\u001b[39m \u001b[92m\'lodash\'\u001b[39m\u001b[33m}\u001b[39m'

> cardinal.highlight("{name: 'lodash', version: '1.3.1'}")
        Error: Unable to perform highlight. The code contained syntax errors: Line 1: Unexpected token :
            at throwError (./node_modules/cardinal/node_modules/redeyed/node_modules/esprima/esprima.js:1156:21)
        at throwUnexpected (./node_modules/cardinal/node_modules/redeyed/node_modules/esprima/esprima.js:1213:9)
        at consumeSemicolon (./node_modules/cardinal/node_modules/redeyed/node_modules/esprima/esprima.js:1295:13)
        at parseStatement (./node_modules/cardinal/node_modules/redeyed/node_modules/esprima/esprima.js:2804:9)
        at ./node_modules/cardinal/node_modules/redeyed/node_modules/esprima/esprima.js:3609:38
        at parseStatement (./node_modules/cardinal/node_modules/redeyed/node_modules/esprima/esprima.js:2794:27)
        at ./node_modules/cardinal/node_modules/redeyed/node_modules/esprima/esprima.js:3609:38
        at parseSourceElement (./node_modules/cardinal/node_modules/redeyed/node_modules/esprima/esprima.js:3062:20)
        at parseStatementList (./node_modules/cardinal/node_modules/redeyed/node_modules/esprima/esprima.js:2051:25)
        at parseBlock (./node_modules/cardinal/node_modules/redeyed/node_modules/esprima/esprima.js:2066:17)

Am I doing something wrong?

Suppor ES6 syntax

Unfortunately cardinal does not seem to be able to recognise most of the ES6 syntax.

screen shot 2015-09-03 at 20 52 36

Support marking lines and positions

Supporting line markers and character positions would be greatly useful. We can use such functionality in tools to show script errors etc.

Highlight a selected line

It would be neat if this library supported highlighting a specific line only, and fading others (like empty theme?) so that you could make one of the lines prominent.

Removes the shebang line

Hi,

Great project, works pretty robustly. I am using to output highlighted javascript or json file in the center of the terminal in center-code. I noticed that shebang line if it is the first one is handled, but it is removed from the output. Thus something like this

#!/usr/bin/env node

var foo = 42;

will be highlighted but simply as


var foo = 42;

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.