/EDIT: He reescrito toda la publicación.
A continuación se muestra una solución bastante completa al problema resaltado VB. Si SO no tiene nada mejor, por favor, úselo. El resaltado de sintaxis VB es definitivamente deseado.
También he añadido un ejemplo de código con algunos literales de códigos complejos que consigue destacado correctamente. Sin embargo, ni siquiera he intentado obtener XLinq correctamente. Sin embargo, aún podría funcionar. El keywords list está tomado de MSDN. Las palabras clave contextuales no están incluidas. ¿Sabía que el operador GetXmlNamespace
?
El algoritmo sabe el tipo caracteres literales. También debería ser capaz de manejar caracteres de tipo identificador pero no los he probado. Tenga en cuenta que el código funciona en HTML. Como consecuencia, se requiere que &, <y> se lean como entidades con nombre (!), No caracteres individuales.
siento por el largo de expresiones regulares.
Código
var highlightVB = function(code) {
var regex = /("(?:""|[^"])+"c?)|('.*$)|#.+?#|(&[HO])?\d+(\.\d*)?(e[+-]?\d+)?U?([SILDFR%@!#]|&)?|\.\d+[FR!#]?|\s+|\w+|&|<|>|([-+*/\\^[email protected]!#%&<>()\[\]{}.,:=]+)/gi;
var lines = code.split("\n");
for (var i = 0; i < lines.length; i++) {
var line = lines[i];
var tokens;
var result = "";
while (tokens = regex.exec(line)) {
var tok = getToken(tokens);
switch (tok.charAt(0)) {
case '"':
if (tok.charAt(tok.length - 1) == "c")
result += span("char", tok);
else
result += span("string", tok);
break;
case "'":
result += span("comment", tok);
break;
case '#':
result += span("date", tok);
break;
default:
var c1 = tok.charAt(0);
if (isDigit(c1) ||
tok.length > 1 && c1 == '.' && isDigit(tok.charAt(1)) ||
tok.length > 5 && (tok.indexOf("&") == 0 &&
tok.charAt(5) == 'H' || tok.charAt(5) == 'O')
)
result += span("number", tok);
else if (isKeyword(tok))
result += span("keyword", tok);
else
result += tok;
break;
}
}
lines[i] = result;
}
return lines.join("\n");
}
var keywords = [
"addhandler", "addressof", "alias", "and", "andalso", "as", "boolean", "byref",
"byte", "byval", "call", "case", "catch", "cbool", "cbyte", "cchar", "cdate",
"cdec", "cdbl", "char", "cint", "class", "clng", "cobj", "const", "continue",
"csbyte", "cshort", "csng", "cstr", "ctype", "cuint", "culng", "cushort", "date",
"decimal", "declare", "default", "delegate", "dim", "directcast", "do", "double",
"each", "else", "elseif", "end", "endif", "enum", "erase", "error", "event",
"exit", "false", "finally", "for", "friend", "function", "get", "gettype",
"getxmlnamespace", "global", "gosub", "goto", "handles", "if", "if",
"implements", "imports", "in", "inherits", "integer", "interface", "is", "isnot",
"let", "lib", "like", "long", "loop", "me", "mod", "module", "mustinherit",
"mustoverride", "mybase", "myclass", "namespace", "narrowing", "new", "next",
"not", "nothing", "notinheritable", "notoverridable", "object", "of", "on",
"operator", "option", "optional", "or", "orelse", "overloads", "overridable",
"overrides", "paramarray", "partial", "private", "property", "protected",
"public", "raiseevent", "readonly", "redim", "rem", "removehandler", "resume",
"return", "sbyte", "select", "set", "shadows", "shared", "short", "single",
"static", "step", "stop", "string", "structure", "sub", "synclock", "then",
"throw", "to", "true", "try", "trycast", "typeof", "variant", "wend", "uinteger",
"ulong", "ushort", "using", "when", "while", "widening", "with", "withevents",
"writeonly", "xor", "#const", "#else", "#elseif", "#end", "#if"
]
var isKeyword = function(token) {
return keywords.indexOf(token.toLowerCase()) != -1;
}
var isDigit = function(c) {
return c >= '0' && c <= '9';
}
var getToken = function(tokens) {
for (var i = 0; i < tokens.length; i++)
if (tokens[i] != undefined)
return tokens[i];
return null;
}
var span = function(class, text) {
return "<span class=\"" + class + "\">" + text + "</span>";
}
para la prueba:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
'set page title
Page.Title = "Something"
Dim r As String = "Say ""Hello"""
Dim i As Integer = 1234
Dim d As Double = 1.23
Dim s As Single = .123F
Dim l As Long = 123L
Dim ul As ULong = 123UL
Dim c As Char = "x"c
Dim h As Integer = &H0
Dim t As Date = #5/31/1993 1:15:30 PM#
Dim f As Single = 1.32e-5F
End Sub
Nota: si usted necesita tener el resaltado de sintaxis es así, utilice '<- idioma: lang-VB ->' – Laurel