Modifying the generated HTML

CodeHighlighter for ASP.NET Forum

Posted 16 years ago by Matt Adamson
Avatar
Guys,

Firstly I really like the control, it's perfect to show source code files in an ASP.NET application.

I'd like to extend the control to allow certain lines to be highlighted in specific colours and was wondering how I could extend it. What would be the best approach?

I can certainly create a new web custom control in another assembly and derive from the CodeHighlighter class. Then override the Render method however although provides a way to add tags to the start and end I can't see how I can modify the generated HTML.

Even if I call the base Render method first, how could I modify the HTML in the middle e.g. for each line output?

Note I am aware of the GenerateHtmlInline method which exists however I would like to create a control so can simply drop it on the ASP.NET page. In any case even if I used this method would I have to parse the string text to obtain the HTML or could I use an ASP.NET control to parse into HTML controls?

Thoughts much appreciated.

Cheers

Matt

[Modified at 10/03/2007 02:31 PM]

Comments (9)

Posted 16 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Matt,

It sounds like it would be helpful for you if we eventually added indicator support to CodeHighlighter. By that I mean you could set up somehow set up line or span indicators whose highlighting styles would be merged into the output. We can add it to the TODO list for future possible implementation.

Right now, after looking at the code, I don't think you can do anything by inheriting CodeHighlighter. I believe you would need to call the engine's GenerateHtmlInline method directly and then split the result by \n characters and insert your spans as appropriate. However you may run into issues since a span that colors the foreground may be open from the previous line if the token on the current line uses the same style. This could lead to overlapping spans, which is bad HTML.


Actipro Software Support

Posted 16 years ago by Matt Adamson
Avatar
Thanks for the reponse

I'm not too hot on HTML so what specific tags would you recommend inserting before the /n character then to colour a single line

What exactly do you mean by

Quote:
However you may run into issues since a span that colors the foreground may be open from the previous line if the token on the current line uses the same style
Posted 16 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Matt,

What I'm saying is this... say you have this C# code that is being highlighted:
public 
class Test {}
The first two lines might be in a single HTML 'span' tag that colors the keywords 'public' and 'class'. So if you insert a 'span' around the entire second line then you end up with overlapping HTML tags and/or incorrect results, which is bad. Like this:

<span class="keyword">public<br/>
<span style="yourstyle">class</span><span class="default"> Test {}</span></span>
Basically, there are some issues here that will probably not let you do this correctly because of the above scenario.


Actipro Software Support

Posted 16 years ago by Svyatoslav Danyliv
Avatar
Hi All,

Is tehere some news for this feature?
I need to highlight some lines of code with different background and i see that i will need additional HTML parsing. It's possible but i'm looking for more accurate solution.

I suggest the following. I can add to original source file a special tags. For examle:
public 
[CodeHighliter, Background=yellow, VirtualSpace=1, Bookmark=#BookmarkName, HTML="maybe some open html tags"]class Test {}[/CodeHighliter, HTML="maybe some close html tags"]
  void [CodeHighliter, Bacground=green]SomeFunction[/CodeHighliter](int someParam)
  {
  }
Background tells that background color should be changed to yellow,
VirtualSpace tells that line should be colored fully including virtual space.
Bookmark - it will be nice if CodeHighlighter will put bookmarks for needful lines of code for navigation
HTML - maybe for placing some marks to correct html after generating.

I assume that this needs some preprocessing and may be out of scope of SyntaxtEdit engine. But who know maybe it's not too hard to implement. It will be very appresiated!

I'm trying to finish our internal tool for analysing Crash Call Stack information, and will be good to have engine that can highlight problematic lines in code.
Also preprocessing can be replaced by additional API before generation. IOW acceptable is all, that can tell to CodeHighlighter how highlighting should be changed :)

BTW, Thanks for great product!

P.S. What about limiting code lines? I found post with this suggestion but i think this still in TODO also?

TIA,
Svyatoslav

[Modified at 08/10/2008 12:26 PM]
Posted 16 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Svyatoslav,

Thanks for the suggestions. No updates yet, however right now we are working on the next major version of our document/parsing engine as we develop our WPF version of SyntaxEditor. We are making a separate assembly that only does document/parsing functionality. So the next major version of CodeHighlighter will be using this updated assembly and we hope to add support for things like span indicators at that time.

What did you mean about limiting code lines, like just showing a range of text int the rendered output? Couldn't you just pre-filter the text you pass to the control to accomplish that?


Actipro Software Support

Posted 16 years ago by Svyatoslav Danyliv
Avatar
Quote:
So the next major version of CodeHighlighter will be using this updated assembly and we hope to add support for things like span indicators at that time.


I'll be waiting :) Approximately when?

Quote:
What did you mean about limiting code lines, like just showing a range of text int the rendered output? Couldn't you just pre-filter the text you pass to the control to accomplish that?


Yes, I can limit code passed to Highlighter but there will be problem with opened comments
/* comment comment
   comment comment
   comment comment
   comment comment
*/
public class SomeClass
{
  public int SomeMethod();
}
Drop first line (limit range) and we have
   comment comment
   comment comment
   comment comment
*/
public class SomeClass
{
  public int SomeMethod();
}
So range of text can be catched correctly only by CodeHighlighter.
Posted 16 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Not sure yet but our SyntaxEditor for WPF product is under heavy development now and that means that work is progressing on the improved document/parsing library that will eventually be used for CodeHighlighter. Still it may be several months before a new CodeHighlighter version for the improved framework is ready.

Ahh I see. Thanks for the suggestion, I'll mark it down.


Actipro Software Support

Posted 16 years ago by Svyatoslav Danyliv
Avatar
Hi Again

After deeper investigation i found that it will be more useful to have client side functions for highlighting lines or pieces of code.
    1. Select piece of code
    2. Select line(s) it's a part of 1
    3. Navigate to line
It is possible?

Thanks,
Svyatoslav
Posted 16 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Sorry but not right now since all CodeHighlighter outputs is raw HTML code.


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.