thomasjo / highlight Goto Github PK
View Code? Open in Web Editor NEWA relatively simple and extensible syntax highlighter written in C#
License: MIT License
A relatively simple and extensible syntax highlighter written in C#
License: MIT License
Same example as #3.
static void Main(string[] args)
{
var highlighter = new Highlighter(new HtmlEngine());
var s = @" static void Main(string[] args)
{
var highlighter = new Highlighter(new HtmlEngine());
var s = @"";
var highlightedCode = highlighter.Highlight(""C#"", s);
Console.Write(highlightedCode);
Console.ReadKey();
}";
var highlightedCode = "<pre>" + highlighter.Highlight("C#", s) + "</pre>";
Console.Write(highlightedCode);
Console.ReadKey();
}
output:
<pre><span style="color: Black;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"> <span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;">static</span> <span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;">void</span> Main(<span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;">string</span>[] args)
{
var highlighter <span style="color: Red;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;">=</span> <span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;">new</span> Highlighter(<span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;">new</span> HtmlEngine());
var s <span style="color: Red;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;">=</span> <span style="color: #666666;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;">@";
var highlightedCode = highlighter.Highlight("</span>C#", s);
Console.Write(highlightedCode);
Console.ReadKey();
}</span></pre>
Hi, would you be interested in a PR for a .NET standard 2.0 version of your code?
I just worked on it for a Blazor project and it seems to be working.
Of course, I don't know all the ins and outs of the project, so more testing may be required.
I have just used the HtmlEngine to Highlight some HTML and C# strings.
Let me know if you are interested.
Thanks for the project - it has been a help to me - I appreciate it.
I get this error when calling highlighter.Highlight("JavaScript", myString)
:
System.IO.FileNotFoundException: 'Could not load file or assembly 'System.Drawing.Common, Version=0.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.'
I installed this library via Nuget.
I'm using .NET Core 2.1.302. Is this library compatible with .NET Core?
Code:
static void Main(string[] args)
{
var highlighter = new Highlighter(new HtmlEngine());
var s = @" static void Main(string[] args)
{
var highlighter = new Highlighter(new HtmlEngine());
var s = @"";
var highlightedCode = highlighter.Highlight(""C#"", s);
Console.Write(highlightedCode);
Console.ReadKey();
}";
var highlightedCode = highlighter.Highlight("C#", s);
Console.Write(highlightedCode);
Console.ReadKey();
}
output:
<span style="color: Black;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"> <span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;">static</span> <span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;">void</span> Main(<span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;">string</span>[] args)
{
var highlighter <span style="color: Red;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;">=</span> <span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;">new</span> Highlighter(<span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;">new</span> HtmlEngine());
var s <span style="color: Red;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;">=</span> <span style="color: #666666;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;">@";
var highlightedCode = highlighter.Highlight("</span>C#", s);
Console.Write(highlightedCode);
Console.ReadKey();
}</span>
`Private Highlighter As New Highlight.Highlighter(New Highlight.Engines.RtfEngine)
Private Sub RichTextBox1_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox1.TextChanged
RichTextBox1.Rtf = Highlighter.Highlight("Python", RichTextBox1.Text)
End Sub`
Throws a stackoverflow exception when highlighter.highlight is called for the first time. Am I doing something wrong?
Hi there,
I was looking at your project to try and figure a few things out (I was curious to see if the inline styles work better for pasting code in to a document...) and I spent a little while playing with it. While I was in there, I spotted a small bug in the Engine...
if (definition.CaseSensitive) {
return DefaultRegexOptions | RegexOptions.IgnoreCase;
}
The check is the wrong way around, it should be: if (definition.CaseSensitive == false).
I've changed it in my cloned repo, and added a test for it; happy to send over a PR if you care. Wanted to post this here just on the off-chance somebody else hits the same thing and wonders why :)
Many thanks!
Adrian
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.