With Tonc I pretty much did all the syntax highlighting of code manually.
As you might expect, this experience was – well, the proper description is
something not suitable for anyone under the age of several thousand,
so let's keep it at “somewhat less than pleasant”. So the first
thing I looked when starting this whole blogging gig for was something
that could do that automatically. In my case, that was
which was build on the very awesome
Geshi. There were some
small problems with number formatting and whitespace handling, but
overall it's served me well.
The Geshi that came with it was … 18.104.22.168, I think. In any case, Geshi's
is now at 22.214.171.124, so I figured it was time for an upgrade. Most notable was
that the way numbers were parsed has been greatly modified, with different
types of representations now being parsed separately – and correctly
to boot. Right now, it's almost fully correct, as you can see from the list
// Regular int
123ll // fail
123LL // fail
123u // fail
123U // fail
Only some of the more special integer literals aren't parsed correctly,
specifically the unsigned (
-U) and long long
-LL) suffixes aren't accepted. I don't suppose hex floats will
work either, but that's a GCC extension anyway.
To fix this, you need to modify geshi a little; specifically the
GESHI_NUMBER_INT_CSTYLE regular expression:
… yeah. I'm not sure why it's formulated like that either. I'd have thought
\b' would have worked just as well, but alright. Anyway, notice the single '
l' character in there? That needs to be extended to something
that matches a potential single '
u', possibly followed by one or two
l's. In other words: '
HTML in code
An astute readed may have noted the bold in the previous snippet. Normally,
you can't do that in Geshi.. One of the things that Geshi does is translate
HTML entities like '
<' into things like "
so that it'll turn up right on the resulting page. This, of course, is one of the
things Geshi is expected to do. However, in this case it also makes it impossible
to add HTML parts in the code snippet, which at times can be very useful.
So what do we do now? Well, we can use escaped HTML tags. Much like
\n" doesn't actually mean backslash + '
n' but a
newline character, "
\<" can be used for the actual
<'. And to unescape that, a double backslash can be used,
much like it is in C.
\\<b\\>BOLD\\</b\\> becomes \<b\>BOLD\</b\>
There are several ways to implement this. One would be to modify it in the geshi
code. I haven't tried that route yet because I expect it could get messy. That's
arguably how it should be done, but it's easier to do it after the fact:
when all the conversions have been done. Basically, you need something like this:
// Initialize geshi with the text to convert and language file to use.
// This does the actual work.
// Replace (un)escaped html entities.
// Normal entities
// In-string escapes get crap added, gaddammittohell >_<.
// Unescaped entities
, // Normal entities
, // In-string entities.
, '\\\>' // Unescaped entities
There are three sets of items to search & replace here. The first two are
the basic escaped tag delimiters, so that they'll actually result in HTML tags,
and unescaped delimiters, so that you can print the combination itself. The
third category are for HTML in string literals. Since the backslash has a
specific meaning there as well, Geshi puts some highlighting stuff around it
that would make the standard search fail. So that whole thing would need
to be searched for and
It's ugly, I know, but it seems to work. It'd be nicer if this could be done
in the parser itself, but I have a feeling that'd take changes in multiple
places. Since I don't know the code that well yet, I'm not touching that
one with a ten-foot pole.
Lastly, let's test the ARM asm highlighter:
// Regular int
Still works too. Bitchin'.