Using CodeHighlighterEngine outside of a web control

CodeHighlighter for ASP.NET Forum

Posted 18 years ago by Mike Mason
Avatar
Hi there,

I'm trying to use the code highlighting engine without using the web control, and looking at the API and other posts around here it seems I should use CodeHighlighterEngine directly. Sadly I haven't been able to get it to work -- it seems to throw a null reference exception somewhere internally.

I think I've probably got the set up wrong and it's not finding the language definition correctly or something, but it's unclear to me what setup steps I need to do. Here's the code that doesn't work right now:

            string content = "// C Sharp source code here";
            SyntaxLanguageConfiguration slConfig = new SyntaxLanguageConfiguration();
            slConfig.DefinitionPath = @"C:\temp\CodeHighlighter\Languages";
            SyntaxLanguage sLanguage = new SyntaxLanguage("CSharp", false);
            CodeHighlighterEngine chEngine = new CodeHighlighterEngine();
            string rendered = chEngine.GenerateHtmlInline("Foo", "CSharp", sLanguage);
The final line throws a null reference inside the GenerateHtmlInline method. I'm also think "Foo" is probably not the unique ID you were looking for...

Any help appreciated,

Mike.

Comments (3)

Posted 18 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
You don't need any of the SyntaxLanguageConfiguration related lines in there. I think the problem is that you used an empty SyntaxLanguage. Try loading a SyntaxLanguage using SyntaxLanguage.LoadFromXml and load one of our sample ones. Then try the same code but with that language.


Actipro Software Support

Posted 18 years ago by Mike Mason
Avatar
Thanks for the help - this works great loading the syntax from a file. I am successfully generating colorized HTML!

In terms of performance, which objects can I safely store and reuse? For example if I load a syntax definition for each file type, can I reuse the definitions for multiple HTML sources? Is the syntax definition thread-safe so I can share it between multiple threads? How about the CodeHighlighterEngine itself? Is that a cheap object that I can create for each request, or should I hold on to it for better performance?

Thanks,
Mike.
Posted 18 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Great... it is recommended that you store and reuse the SyntaxLanguage instances since that will speed things up and conserve on resources. We haven't done anything to make the languages thread-safe however since you are only reading the languages, they probably are ok to use across threads. The engine is pretty lightweight so it's up to you as to whether you want to reuse it or not.


Actipro Software Support

The latest build of this product (v4.0.59) was released 13 years ago, which was after the last post in this thread.