Giter Club home page Giter Club logo

regex-highlighter's Introduction

regex-highlighter

A javascript tool to highlight regex pattern matches in a HTML document via CSS. Whilst this tool is primarily aimed at programming languages syntax highlighting, it can be used with any regular expressions. Highlighted matches from a given regex file will be wrapped in a span tag and then given a class which can be used to style that particular match. There are several supported languages which can be used, or you can create your own custom rules.

How to Use

Online Converter

If you don't need to dynamically convert text to its highlighted format, you can use the online converter and copy the output into the HTML file. Then all that is needed is to either include the CSS file, or to style the span tags yourself. The online converter allows for all currently supported languages, custom JSON files and single regex lines.

Adding to a website

Adding the highlighter to a project is very simple! All you have to do is to include the javascript file inside your HTML and run a function from the RegexHighlighter object. To see which functions will best suit your use case, check out the documentation. Simply put, you can just follow these few steps:

  • Download the zip file from github, then move the files to relevant folders somewhere on the server (Note: the script has to run on a server as it uses AJAX)
  • Add the highlight.min.js script to the html file that you want it to run on using <script type="text/javascript" src="scripts/highlight.min.js"></script>.
  • Add the syntax.css stylesheet to highlight the matched text using <link rel="stylesheet" href="styles/syntax.css">
  • Now you can run regex-highlighter with the loadSyntaxHighlightingByClass() function. This function is inside the RegexHighlighter class, so first create an instance then call the function, e.g. new RegexHighlighter().loadSyntaxHighlightingByClass(). The loadSyntaxHighlightingByClass() function takes 2 optional parameters in order to determine which elements should have syntax highlighting added, as well as the folder for where the default language support is. The default options for these are regex-color and ./languages/. This means that only elements which have a class of regex-color will be coloured and only the languages/ folder in the same directory as the script will be searched for the regex support. These can be changed by supplying arguments to the function.
  • To tell the regex-highlighter what language it should use, add a class to the same element. The class name should be the same as the language specification file used to define the regex rules.

Example

<script  type="text/javascript" src="build/highlight.min.js"></script>
<link rel="stylesheet" href="build/syntax.css" charset="utf-8">

    ...    

<pre><code class="syntax-color haskell">import Text.Printf

criticalProbability :: (Integral a, Floating b) => a -> a -> b
criticalProbability d h
    | h > d     = 1.0 / fromIntegral d * criticalProbability d (h - d)
    | otherwise = 1.0 - fromIntegral (h - 1) / fromIntegral d

main = let
    ds = [4,4,4,4,1,100,8]
    hs = [1,4,5,6,10,200,20]
    in sequence . map (putStrLn . printf "%f") $
        zipWith (\d h < criticalProbability d h :: Double) ds hs</code></pre>

    ...

<script type="text/javascript">new RegexHighlighter().loadSyntaxHighlightingByClass("syntax-color");</script>

Documentation

There are a few other public functions which allow for finer control of highlighting text, the documentation for these can be found here.

Screenshots

Haskell

Haskell Syntax

Java

Java Syntax

Currently supported languages

  • Dockerfile
  • Java
  • Javascript
  • Go
  • Haskell
  • Erlang
  • C++
  • JSON
  • PHP
  • Python

Creating custom JSON files

When creating the custom JSON files, make sure that they are in the following formats:

[
    {
        "type": "class-name",
        "regexes": [
            "regexString1",
            {
                "regexString": "regexString2",
                "captureGroup": 1
            }
        ],
        "precedence": 1
    },
    {
        ...
    }
]

Where class-name is class that will attached to the span when the script is run and regex is any regex in a string format. Unfortunately due to JSON not supporting regex notation or raw string, any backslashes in the regex have to be escape e.g \\bhello\\b.

You can use the testing folder for trying out any new builds and generally editing the source files. This folder also has a way of testing and previewing the regex highlighting that is currently supported, via the index webpage.

Building

To build the src files into a build directory and minimised files you can use the commands from the following table.

Make Command Description
make or make build Build all files into a build dir. This directory contains the built js files, css files and minimised JSON files for the currently supported language syntax.
make docs Builds the documentation locally.
make test Builds the src files and copy them into the testing directory. Then the test code can be run on a simple http file server such as an apache server.
make test-fileserver Uses the python http server to help run the tests. Stops the needs of having to setup apache and copy over the files.
make clean Removes the build dir.
make deps-clean Removes the node_modules folder so that no deps are installed locally.
make docs-clean Removes the docs file produced by JSDoc.
make clean-all Removes everything that has been built. This will give a completely clean directory only containing src files and testing code.

To Do

  • Add more tests for each of the different regexes
    • java
    • json
    • PHP
    • python
  • Write DOCS.md to fully document this project
  • Write documentation for each of the different languages that are currently supported.

Change Log:

  • Add a build script to generate the files in the build directory.
  • Added a list of languages supported in the readme.
  • Added better testing
    • c++
    • haskell
    • javascript
  • Change the json to use an array instead of keys for order preservation
  • Add name property to type
  • Moved to prototype class structure
  • Fix the highlighting within the imports and variables
  • Document new regex object
  • Add some way to express which capture group is needed in the language data
  • Added more robust commenting to the javascript file (came with JSDoc)
  • Added some more error checking to the javascript file, mostly for optional arguments
  • Added in depth documentation to this project, using JSDoc
  • Added a way to add options to the regexes, for example to set the precedent. This was so I can get regex matches with the same precedent, so the longer of the 2 matches will be chosen.
  • Updated the duplicate removal for new precedent matching.
  • Added a more complex regex duplication detection system
  • Fixed java character matching
  • Changed to last group pattern matching
  • Fixed the haskell function errors
  • Created a guide on how to add more languages
  • Added Languages:
    • Python
    • PHP
    • basic C++
    • JSON
    • Javascript
    • Java
    • Haskell
  • Added main javascript file
  • Added support for multiple languages

regex-highlighter's People

Contributors

imitablerabbit avatar

Stargazers

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

Watchers

 avatar  avatar

regex-highlighter's Issues

Add some sort of building tool

Stop the need to have to convert each file by using some external website. Makefile or even just a simple script would work fine.

Ability to include other highlighting files

Perhaps add an include key to the JSON files so that common features like number highlighting, commenting and strings can be loaded in. Hierarchy should be maintained when loading the files.

{
    "includes": [
        "strings.json",
        "comments.json"
    ]
}
Pros Cons
Can split common functionality into different files. This will save some time when creating new syntax files Will take longer to load highlighting as more files have to be read and parsed.
Its clearer what is being loaded into the system Can get messy if different cascading order is needed

Fix number regex for all languages

Replace the number formatting with something which can match the same across all languages.

\d*\.?\d+ matches everything including the decimal points.

Build node graph rather than text in highlighter

Currently the text is being wrapped in span tags, this is fine until you have text which includes < and >. Then when .innerHtml is used to automatically parse the html tags from the text, it is also matching the < and > signs. An example of this is in the c++ include tests.

#include <vector>

Is turned into...

#include 

and a new html node <vector></vector> is created

Clean up the documentation

The documentation for this project needs some cleaning up. Separate different concerns into different files. Instructions for contributions can be in a file called contributions.md, etc.

Better instructions for creating custom language files could also help.

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.