GitXplorerGitXplorer
l

csharp2colorized

public
2 stars
2 forks
0 issues

Commits

List of commits on branch master.
Unverified
fb5e86dd93f3ffc45d032417a71a0cedfe7c035b

Merge branch 'master' of https://github.com/ljw1004/csharp2colorized.git

lljw1004 committed 9 years ago
Unverified
4ab462c85d19fd98c8fa29e3aa44edcc6f7b67a3

Updated readme to reflect the new command-line techniques, and provide a link to the NuGet package.

lljw1004 committed 9 years ago
Unverified
5b108d5e7d4048aedc40e62f82d448d5f31155a9

Fixed off-by-one bug in string splitter.

lljw1004 committed 9 years ago
Unverified
885de20ff09431bb91cdbcee3a11574e9f1638c6

Got rid of non-ascii characters in the tests. (dotnetcli and .NETFramework seemed to be treating them differently, and I didn't want this noise.)

lljw1004 committed 9 years ago
Unverified
2ddc0cb2cdd0bc60787a1c691a61768a593a31e8

Improved type+namespace names. Cleared up the command-line arguments. Made it much more aggressive in echoing to stdout as soon as it can (only applies to -md case).

lljw1004 committed 9 years ago
Unverified
4c2d0a410936deea806e7c31ce5bb1dd6f44e357

Updated nuspec version numbers to hopefully make it work with dotnetcli

lljw1004 committed 9 years ago

README

The README file for this repository.

CSharp2Colorized

This is a command-line utility and a nuget package for colorizing C#. It's goal is to give good colorization even for incomplete code snippets. Here's how it stacks up:

comparison

How to use the command-line utility

You can run the command-line utility in a few ways...

echo "Console.WriteLine();" | csharp2colorized -cs
csharp2colorized file1.cs
csharp2colorized *.cs *.vb
csharp2colorized tests.md > results.html

In the final example it's given a markdown file, and will replace all fenced codeblocks in vb/csharp with html <pre> tags.

I haven't packaged up the command-line utility for download. You have to build it yourself. As well as working on .NET Framework, it also works on "dotnet-cli" for Windows/OSX/linux.

How to use the library API programmatically

Add a NuGet reference to csharp2colorized. Programmatically you can call the library API like this:

using CSharp2Colorized;
var lines = Colorize.CSharp(code);  // or Colorize.VB(code);
var pre = Colorize.Lines2Html(lines);
Console.WriteLine($"<html><body>{pre}</body></html>");

How it works

It uses Microsoft's Roslyn to parse the code. It parses it as a "script" (.csx/.vbx). Script files are allowed to include expressions, statements and methods at the top level, as well as namespaces and types. That lets it parse fragments pretty well.

Like Visual Studio, it attempts to resolve each identifier to see if it's a type (colorized azure) or a field/property/method/variable (colorized black). Where it differs from VS is that if symbol resolution fails then it falls back on a load of heuristics. These embody expert knowledge of the language syntax. For instance, if you write typeof(C) then we can safely colorize C as a type even if it hasn't been defined.

Development notes

If you're contributing to this project, the regression tests are in a file called "tests.md". You should run

csharp2colorized tests.md > results.html

and then verify that your results.html is an exact match for the checked-in baseline.html. These tests comprise every code snippet found in the VB and C# language specifications.