I’m using WP-GeSHi in WordPress, and largely I’m very happy with it. There are, however, a few minor scenarios where the color highlighting is too aggressive when a keyword is:
- a variable name (denoted by a leading
@
) - part of another word (e.g.
IN
inINSERTED
) - the combination (part of a variable name, e.g.
JOIN
andIN
in@JOINBING
) - inside square brackets (e.g.
[status]
)
Certain keywords are case sensitive, and others are not. The below screenshot sums up the various cases where this goes wrong:
Now, the code in GeSHi.php
is pretty verbose, and I am by no means a PHP expert. I’m not afraid to get my hands a little dirty here, but I’m hoping someone else has made corrections to this code and can provide some pointers. I already implemented a workaround to prevent @@ROWCOUNT
from being highlighted incorrectly, but this was easy, since @@ROWCOUNT
is defined – I just shuffled the arrays around so that it was found before ROWCOUNT
.
What I’d like is for GeSHi to completely ignore keywords that aren’t whole words (whether they are prefixed by @
or immediately surrounded by other letters/numbers). JOIN
should be grey, but @JOIN
and JOINS
should not. I’d also like it to ignore keywords that are inside square brackets (after all, this is how we tell Management Studio to not color highlight it, and it’s also how we tell the SQL engine to ignore reserved words, keywords, and invalid identifiers).
You can do this by adding a
PARSER_CONTROL
control to the end of the array:Edit
I’ve modified your gist to move some of the keywords you added to
SYMBOLS
back toKEYWORDS
(though in their own group and with your custom style), and I updated thePARSER_CONTROL
array to match the new keyword array indexes and also to include the defaultregex
that geshi generates. Here is the link:https://gist.github.com/jamend/07e60bf0b9acdfdeee7a
According to me, what you are doing would take a lot of time. So, I suggest that you install a different plugin:
It has better features and supports more languages and in a better way. So, it would remove all these problems.
EDIT:
Hey, I tried out the same code with latest version and got following result-
EDIT:
So, if you don’t want to use another plugin, then I’ll tell you about the coding:
First open wp-contentpluginswp-geshi-highlightgeshigeshitsql.php in your text editor.
Then, locate the array
'KEYWORDS'
or search for it.Add
6
to the last of it (after5
) and add your custom keywords in it. For example:Note: I have just shown array element 5 (already present) and array element 6 (which I have made).
Then, to make it case-sensitive add below code to the last of
'CASE_SENSITIVE'
array:The
'CASE_SENSITIVE'
array should look like this:Now, you will have to add styling to the custom keywords. This can be achieved by adding below line to the
'KEYWORDS'
element of'STYLES'
array. The starting of'STYLES'
array should look like this:You can solve your problems by above guidelines, but for the part in which the plugin highlights incomplete words, I have found only one solution, that you update your plugin to latest version, because it solves this problem.