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."
Related
.NET Framework proposes "///" for documentation in code. However, when I see the .NET library, I see they are using normal comment starting with "//". Also, I see that due to that the APIs look clean. See below screenshot:
Notice that collapsed box shows only ellipses("...") and are placed in front of method declaration. But when I try to follow the same, I do not get the desired result. See below screenshot:
Notice that I get "//" characters along with ellipses("..."). Plus, I cannot get the field/method and comment to be placed on the same line.
How do I achieve the same result? Is there any trick that I am missing here?
However, when I see the .NET library
You're not looking at the actual source code. That's just a representation of the metadata, generated for you by Visual Studio. It looks like some documentation is being pulled in from somewhere (that doesn't happen on my box in the way I've just tried it, but that's a different matter). You can tell this isn't real source code because there are constructors declared with no body.
If you look at genuine source code, e.g. ReferenceSource for TcpClient you'll see triple-slash comments. Interestingly, the .NET Core code doesn't have any XML comments either, but I suspect that's because they're defined elsewhere, as part of .NET Standard. (There's an XML file in the dotnet-api-docs repo, but it's not immediately clear what that's generated from...)
When it comes to your own code, I'd strongly recommend just using the /// syntax. /** ... */ should work as well, but I don't remember ever seeing that in C# code. // is not a valid way of writing an XML comment in C#.
I'm writing something that will examine a function and rewrite that function in another language so basically if inside my function F1, i have this line of code var x=a.b(1) how do i break up the function body into symbols or "tokens"?
I've searched around and thought that stuff in System.Reflection.MethodInfo.GetMethodBody would do the trick however that class doesn't seem to be able to have the capabilities to do what i want..
what other solutions do we have?
Edit:
Is there anyway we can get the "method body" of a method using reflection? (like as a string or something)
Edit 2:
basically what I'm trying to do is to write a program in c#/vb and when i hit F5 a serializer function will (use reflection and) take the entire program (all the classes in that program) and serialize it into a single javascript file. of course javascript doesn't have the .net library so basically the C#/VB program will limit its use of classes to the .js library (which is a library written in c#/vb emulating the framework of javascript objects).
The advantage is that i have type safety while coding my javascript classes and many other benefits like using overloading and having classes/etc. since javascript doesn't have classes/overloading features natively, it rely on hacks to get it done. so basically the serializer function will write the javascript based on the C#/VB program input for me (along with all the hacks and possible optimizations).
I'm trying to code this serializer function
It sounds like you want a parse tree, which Reflection won't give you. Have a look at NRefactory, which is a VB and C# parser.
If you want to do this, the best way would be to parse the C#/VB code with a parser/lexer, such as the Gardens Point Parser Generator, flex/bison or ANTLR. then at the token level, reassemble it with proper javascript grammar. There are a few out there for C# and Java.
See this answer on analyzing and transforming source code
and this one on translating between programming languages.
These assume that you use conventional compiler methods for breaking your text into tokens ("lexing") and grouping related tokens into program structures ("parsing"). If you analysis is anything other than trivial, you'll need all the machinery, or it won't be reliable.
Reflection can only give you what the language designers decided to give you. They invariably don't give you detail inside functions.
If you want to go from IL to other language it may be easier than parsing source language first. If you want to go this route consider reading on Microsoft's "Volta" project (IL->JavaScript), while project is no longer available there are still old blogs discussing issues around it.
Note that reflection alone is not enough - reflection gives you byte array for the body of any particular method (MethodInfo.GetMethodBody.GetILAsByteArray - http://msdn.microsoft.com/en-us/library/system.reflection.methodbody.aspx) and you have to read it. There are several publically available "IL reader" libraries.
Does anyone know of a library which helps with generating C# code? For example, if I need to generate a *.cs file containing the definition of a class, I'd like to be able to specify the class and method bodies using an object tree (similar to expression trees) and then tell the library to give me well the formatted C# code as a string.
Thanks.
Have you looked at Microsoft.CSharp.CSharpCodeProvider?
Provides access to instances of the C# code generator and code compiler.
There are many tools for code generation and they are listed below:
Code Smith
codesmithtools dot com
codegeneratorpro dot com
etc
Code generators may sound like saving a lot of time, in reality it depends on the kind of project your working on.
Look into System.CodeDom and CSharpCodeProvider.
I'm rather fond of Terance Parr's StringTemplate. It's at the core of the compiler building tool ANTLR — StringTemplate is responsible for for code generation, allowing ANTLR to target just about any language for its compilers.
You can download the latest C#/.Net port from http://www.stringtemplate.org/download.html
You can read about string template in these papers by Dr. Parr:
[DRAFT] A Functional Language For Generating Structured Text
Enforcing Strict Model-View Separation in Template Engines
More at http://www.stringtemplate.org/article/list
CodeProject has an article on code generation with StringTemplate as well: http://www.codeproject.com/KB/codegen/DotNetCodeGeneration.aspx
I need to parse a simple statement (essentially a chain of function calls on some object) represented as a string variable into a CodeDom object (probably a subclass of CodeStatement). I would also like to provide some default imports of namespaces to be able to use less verbose statements.
I have looked around SO and the Internet to find some suggestions but I'm quite confused about what is and isn't possible and what is the simplest way to do it. For example this question seems to be almost what I want, unfortunately I can't use the solution as the CodeSnippetStatement seems not to be supported by the execution engine that I use (the WF rules engine).
Any suggestions that could help me / point me into the right direction ?
There is no library or function to parse C# code into CodeDOM objects as part of the standard .NET libraries. The CodeDOM libraries have some methods that seem to be designed for this, but none of them are actually implemented. As far as I know, there is some implementation available in Visual Studio (used e.g. by designers), but that is only internal.
CodeSnippetStatement is a CodeDOM node that allows you to place any string into the generated code. If you want to create CodeDOM tree just to generate C# source code, than this is usually fine (the source code generator just prints the string to the output). If the WF engine needs to understand the code in your string (and not just generate source code and compile it), than CodeSnippetStatement won't work.
However, there are 3rd party tools that can be used for parsing C# source code. In one project I worked on, we used NRefactory library (which is used in SharpDevelop) and it worked quite well. It gives you some tree (AST) representing the parsed code and I'm afraid you'll need to convert this to the corresponding CodeDOM tree yourself.
I have found a library implementation here that seems to cover pretty much everything I need for my purposes. I don't know if it's robust enough to be used in business scenarios, but for my unit tests it's pretty much all I can ask for.
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.