PygmentSharp icon indicating copy to clipboard operation
PygmentSharp copied to clipboard

C# Port of Pygments syntax highlighter

PygmentSharp

Build Status

A port of python's Pygments syntax highlighter.

Aims to follow the overall approach (for now) while still being mostly idiomatic c#.

Roadmap:

I need the following features from Pygment for my static blog generator. Other features can be ported by the community as needed.

  • [x] HTML Output
  • [x] C# Highlighting
  • [X] SQL Highlighting
  • [X] HTML Highlighting
    • [X] Javascript Highlighting
    • [X] CSS Highlighting
  • [X] XML Highlighting
  • [X] Shell script highlighting
  • [ ] PHP Highlighting
  • [ ] Ruby Highlighting

Installation

Get it from Nuget: PygmentSharp.Core

Usage


// basic case, specify lexer and formatter
var highlighted = Pygmentize.Content("class Foo {}")
    .WithLexer(new CSharpLexer())
    .WithFormatter(new HtmlFormatter())
    .AsString();

// Common overloads get their own fluent command

var highlighted = Pygmentize.Content("class Foo { }")
    .WithLexer(new CSharpLexer())
    .ToHtml()
    .AsString();

// It will support inference for input and output types

Pygmentize.File("test.cs")  // infers language from extension
    .ToFile("output.html"); // infers output formatter from extension