highlight_string in C# - c#

Is there an equivalent of PHP's highlight_string function in C#?
It is not necessary to be a built-in function.
Edit: If not is there a good library for that?
Edit2: I really need a server side solution for that like PHP does.

There is nothing in the framework that will give you the coloured output. You will have to use a library to do this.
C# Code Format was one of the first .Net ones to come up in Google, although it only supports highlighting of C#, VB, HTML, XML, T-SQL or Monad. It is capable of running server side though as it is written in C#. If you need it to format PHP, then it is probably quite simple to extend it to do so - just make a custom PHPFormat class.

You can build yourself a basic syntax highlighter easy using RegExp.
Here's an example:
Syntax Highlight in C#
Or if you want a html C# highlighter written in C#
C# Syntax Highlighter 2.0
Or a JS highlighter:
highlight.js

Nope, certainly not in the Framework itself. You'd have to look for libraries that support this.

If you want it for webpages (which I assume, since the PHP highlight function formats it as HTML) you can use Googles Code prettify. It's however a javascript, which on the other hand makes it code-behind language independent.
I'm using it at my site with great success :-)
You can find it here: http://code.google.com/p/google-code-prettify/

I recently wrote a SQL formatting library (Poor Man's T-SQL Formatter library) in C# (.Net 2.0) which, among the available options, supports just coloring the output (with html span tags).
It's available online for testing at http://poorsql.com, and here's a link that already sets the options to "just colorize" so you can test/play:
http://poorsql.com/?reFormat=false

Related

MarkdownSharp & GitHub syntax for C# code

Is there a way to get MarkdownSharp (I'm using the NuGet package) to handle 'GitHub flavored Markdown (GFM)' and especially syntax highlighting of c# code, which (in GFM) is written like this:
```c#
//my code.....
```
So, if I pass Markdown formatted content to MarkDownSharp, containg a C# code block (as above) I want it to generate syntax highlighted html for that c# code. Any ideas? I know I can use the supported 4 spaces to indicate a code block, but again, I'm seeking a solution for getting it to support GitHub flavored Markdown.
I have made some light modifications to MarkdownSharp that will transform github flavored fenced code blocks
https://github.com/KyleGobel/MarkdownSharp-GithubCodeBlocks
```cs
Console.WriteLine("Fenced code blocks ftw!");
```
Would become
<pre><code class='language-cs'>
Console.WriteLine("Fenced code blocks ftw!");
</code></pre>
It handles the cases I needed to use, there are probably lots of edge cases though, feel free to fork/change/modify/pull request. Markdown sharp has plenty of comments and is all only one file, so it's not too bad to modify.
Here's the result: https://github.com/danielwertheim/kiwi/wiki/Use-with-Asp.Net-MVC
//D
As one can read in this post, GitHub relies on RedCarpet to render Markdown syntax.
However, Vicent Marti (Sundown (ex-Upskirt) and RedCarpet maintainer) states that the syntax highlighting is specifically handled by Pygments, a python library.
Back to your concern, I can think of several options to benefit from syntax highlighting from C#:
Try and build a compiled managed version of Pygments source code thanks to IronPython ("IronPython’s Hosting APIs can be used to compile Python scripts into DLLs, console executables, or Windows executables.")
Port Pygment to C#
Use a different syntax highlighting product (for instance, ColorCode which is used by Codeplex...)
Then either:
Fork MarkDownSharp to make it accept plug-ins
Similarly to what GitHub does, use the managed syntax highlighting product and post process the Html generated by MarkDownSharp
By the way, as a MarkDown alternative, you might want to consider Moonshine, a managed wrapper on top of Sundown which is said to be "at least 20x faster than MarkdownSharp when run against MarkdownSharp's own benchmark app."

Convert VB.NET To C# [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Convert VB.NET code to C#
Hey,
I'm looking for a powerfull tool who can convert & C# code to VB.NET or vice-versa.
I've tried some websites but they are not very good.
Any ideas ?
Thanks
I have used .Net Reflector. Just load the DLL and select the language C#, VB etc.
See http://reflector.red-gate.com/download.aspx
The two most popular ones are developerFusions and teleriks:
http://www.developerfusion.com/tools/convert/vb-to-csharp/
http://converter.telerik.com/
Sometimes you will have to hand convert certain parts of the converted code that the converters have trouble understanding but on the whole they do a large part of the heavy lifting for you.
One example is that vb.net uses array indexes as () whereas c# uses []. For some reason I have seen the developerFusion get confused and leave these as () in the c# code which confuses the compiler.
I mostly use this for quick translations of code when I am answering forum questions and the op has specifically requested a vb.net answer. I just find it easier to code it in c# and then convert it.
If you have a very large project then you might find it tedious to convert each of the individual pages. In this case you might (I haven't actually tested this) find a way to convert the code more quickly by using the #develop IDE. I only say this as the developerFusion online converter is actually powered by code from this tool.
http://www.icsharpcode.net/OpenSource/SD/
Another thing to remember, as has been mentioned elsewhere in this thread, is that its entirely possible to mix and match languages within a single project. The only restriction to my knowledge is that you can only have one language per folder. This is to do with the way that the .net code is compiled. By default each folder is compiled into its own assembly.
If you really need to mix and match on the same page I think you could make a usercontrol to contain the code for one language and put it onto a different languages page.
You've both online- and offline solutions at your disposal.
A very popular online converter: Developer Fusion's Converter
Reflector has for long been the most popular offline source inspector. It allows you to view any assembly in the language of your choice. However, recently they changed and it is now no longer free.

Translate vbscript to C# using ANTLR

I need to write a translator for vbscript to c#. What would be the basic steps invloved to translate using ANTLR? I am not very clear about whether to use grammar (lexer/parser? file or stringtemplate or AST or all.
Suggestions?
Thanks in advance.
Is this really possible?
I'm "translating" (read: rewriting) a MS Access/VBA application since two years to C# and found out that even the online available converters (like this one which is more VB.NET, but anyway) fails at most basic conversions.
So my assumption until now is that there are way too much kind of constructs that are simply not translatable from VBScript to C#.
See What kinds of patterns could I enforce on the code to make it easier to translate to another programming language?
The easiest way to translate VBScript to C# would be using VB.NET as an intermediate step. VBScript is very similar to VB.NET (there are some differences though), so all you need to do is copy-paste your code in a new solution in Visual Studio. After you make a few changes, the code will be able to compile.
You can leave it at that (you can call the resulting DLL from your C# code), but even if you don't want to do that, converting VB.NET to C# is trivial. You can even decompile your resulting DLL/EXE (although this will lead to some loss of information) or use any online VB.NET to C# converter.

Format C# Source Code with Hyperlinks to Reference Library Documentation

I'm wondering if anyone has done this already.
I want to format C# source code in HTML. But with a twist! I want to turn the names of all types and methods that appear in the code into hyperlinks to the MSDN Library documentation of the types and methods.
To do a good job, the data types of variables and expressions needs to be known, just like how the C# compiler does it. So it's a tall order. If something like this is not available, please point me to any free libraries that can generate a parsed tree of the C# source code in sufficient detail to do this task. (In fact, I'd like to know about such a standalone parser library even if the full solution I am asking for already exists.)
This kind of utility might benefit blogs and forums -- maybe even Stack Overflow!
Have you checked out Docu? It's an open source library that converts .net documentation into HTML documents.
I'd suggest using the Visual Studio SDK.

Lex/Yacc for C#?

Actually, maybe not full-blown Lex/Yacc. I'm implementing a command-interpreter front-end to administer a webapp. I'm looking for something that'll take a grammar definition and turn it into a parser that directly invokes methods on my object. Similar to how ASP.NET MVC can figure out which controller method to invoke, and how to pony up the arguments.
So, if the user types "create foo" at my command-prompt, it should transparently call a method:
private void Create(string id) { /* ... */ }
Oh, and if it could generate help text from (e.g.) attributes on those controller methods, that'd be awesome, too.
I've done a couple of small projects with GPLEX/GPPG, which are pretty straightforward reimplementations of LEX/YACC in C#. I've not used any of the other tools above, so I can't really compare them, but these worked fine.
GPPG can be found here and GPLEX here.
That being said, I agree, a full LEX/YACC solution probably is overkill for your problem. I would suggest generating a set of bindings using IronPython: it interfaces easily with .NET code, non-programmers seem to find the basic syntax fairly usable, and it gives you a lot of flexibility/power if you choose to use it.
I'm not sure Lex/Yacc will be of any help. You'll just need a basic tokenizer and an interpreter which are faster to write by hand. If you're still into parsing route see Irony.
As a sidenote: have you considered PowerShell and its commandlets?
Also look at Antlr, which has C# support.
Still early CTP so can't be used in production apps but you may be interested in Oslo/MGrammar:
http://msdn.microsoft.com/en-us/oslo/
Jison is getting a lot of traction recently. It is a Bison port to javascript. Because of it's extremely simple nature, I've ported the jison parsing/lexing template to php, and now to C#. It is still very new, but if you get a chance, take a look at it here: https://github.com/robertleeplummerjr/jison/tree/master/ports/csharp/Jison
If you don't fear alpha software and want an alternative to Lex / Yacc for creating your own languages, you might look into Oslo. I would recommend you to sit through session recordings of sessions TL27 and TL31 from last years PDC. TL31 directly addresses the creation of Domain Specific Languages using Oslo.
Coco/R is a compiler generator with a .NET implementation. You could try that out, but I'm not sure if getting such a library to work would be faster than writing your own tokenizer.
http://www.ssw.uni-linz.ac.at/Research/Projects/Coco/
I would suggest csflex - C# port of flex - most famous unix scanner generator.
I believe that lex/yacc are in one of the SDKs already (i.e. RTM). Either Windows or .NET Framework SDK.
Gardens Point Parser Generator here provides Yacc/Bison functionality for C#. It can be donwloaded here. A usefull example using GPPG is provided here
As Anton said, PowerShell is probably the way to go. If you do want a lex/ yacc implementation then Malcolm Crowe has a good set.
Edit: Direct Link to the Compiler Tools
Just for the record, implementation of lexer and LALR parser in C# for C#:
http://code.google.com/p/naive-language-tools/
It should be similar in use to Lex/Yacc, however those tools (NLT) are not generators! Thus, forget about speed.

Categories

Resources