Git Product home page Git Product logo

highlighter's Introduction

Highlighter

Build Status Build status Coverage Status License Version

Highlighter is a regular expression based syntax highlighter library written in Scala. Highlighter is inspired by Pygments, and it is not difficult to port Pygments lexers to Highlighter. Highlighter has a convenient definition language to make it easier to develop new highlighters.

Examples

Highlighter Definition

Here's an example of a typical Highlighter definition and the result of applying it to a small HTML document. This definition is a port of the Pygments HTML Lexer which includes CSS highlighting for style elements.

Definition

highlighter
  name: HTML
options
  regex: dotall ignorecase
templates
  default: {{ <span class="\class">\escape\text</span> }}
states
  root:
    &\S*?;                        => Name.Entity
    \<\!\[CDATA\[.*?\]\]\>        => Comment.Preproc
    <!--                          => Comment >comment
    <\?.*?\?>                     => Comment.Preproc
    <![^>]*>                      => Comment.Preproc
    (<)\s*(script)\s*             => (Punctuation Name.Tag) >script-content >tag
    (<)\s*(style)\s*              => (Punctuation Name.Tag) >style-content >tag
    (<)\s*([\w:.-]+)              => (Punctuation Name.Tag) >tag
    (<)\s*(/)\s*([\w:.-]+)\s*(>)  => (Punctuation Punctuation Name.Tag Punctuation)
  comment:
    [^-]+  => Comment
    -->    => Comment ^
    -      => Comment
  tag:
    ([\w:-]+)\s*(=)\s*  => (Name.Attribute Operator) >attr
    [\w:-]+             => Name.Attribute
    (/?)\s*(>)          => (Punctuation Punctuation) ^
  script-content:
    (<)\s*(/)\s*(script)\s*(>)  => (Punctuation Punctuation Name.Tag Punctuation) ^
    .+?(?=<\s*/\s*script\s*>)   => [javascript]
  style-content:
    (<)\s*(/)\s*(style)\s*(>)  => (Punctuation Punctuation Name.Tag Punctuation) ^
    .+?(?=<\s*/\s*style\s*>)   => [css]
  attr:
    ".*?"    => String ^
    '.*?'    => String ^
    [^\s>]+  => String ^

Input

<!DOCTYPE html>
<html>
  <head>
    <style>
      body {background-color: powderblue;}
      p    {color: red;}
    </style>
  </head>
  <body>
    <!-- comment -->
    <p align="right">&lt;paragraph&gt;</p>
  </body>
</html>

Output

The styling that was used for the highlighting was Pygments "friendly" theme.

example

Usage

Library

Use the following definition to use Highlighter in your Maven project:

<repository>
  <id>hyperreal</id>
  <url>https://dl.bintray.com/edadma/maven</url>
</repository>

<dependency>
  <groupId>io.github.edadma</groupId>
  <artifactId>highlighter</artifactId>
  <version>0.1.7</version>
</dependency>

Add the following to your build.sbt file to use Highlighter in your SBT project:

resolvers += "Hyperreal Repository" at "https://dl.bintray.com/edadma/maven"

libraryDependencies += "io.github.edadma" %% "highlighter" % "0.1.0"

Executable

An executable can be downloaded from here. You do not need the Scala library for it to work because the JAR already contains all dependencies. You just need Java 8+ installed.

Run it as a normal Java executable JAR with the command java -jar highlighter-0.1.7.jar <definition>... [<input>] in the folder where you downloaded the file, where definition is the name of the highlighter definition file, and is the file containing the code to be highlighted. If multiple definition files are given, the first one is used for highlighting and the others are passed to it as dependencies.

Building

Requirements

  • Java 8
  • SBT 1.1.5+
  • Scala 2.12.6+

Clone and Assemble Executable

git clone git://github.com/edadma/highlighter.git
cd highlighter
sbt assembly

The command sbt assembly also runs all the unit tests.

License

ISC © 2018 Edward A. Maxedon, Sr.

highlighter's People

Contributors

edadma avatar

Watchers

James Cloos avatar  avatar

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.