来自Google的prettify.js有lang-vb或lang-basic选项吗?

Zac*_*son 10 javascript vb.net prettify

使用来自Google的prettify.js无法正确呈现Visual Basic代码.

在Stack Overflow:

Partial Public Class WebForm1
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'set page title
        Page.Title = "Something"
    End Sub

End Class
Run Code Online (Sandbox Code Playgroud)

在Visual Studio中......

Visual Studio中的Visual Basic

我在README文档中找到了这个:

如何指定代码所在的语言?

您不需要指定语言,因为prettyprint()会猜测.您可以通过指定语言扩展名和prettyprint类来指定语言,如下所示:

<pre class="prettyprint lang-html">
  The lang-* class specifies the language file extensions.
  Supported file extensions include
    "c", "cc", "cpp", "cs", "cyc", "java", "bsh", "csh", "sh",
    "cv", "py", "perl", "pl", "pm", "rb", "js",
    "html", "html", "xhtml", "xml", "xsl".
</pre>
Run Code Online (Sandbox Code Playgroud)

我看不到lang-vblang-basic选项.有谁知道是否存在加载项?


注意:这与Stack Overflow 的VB.NET代码块建议有关.

Kon*_*lph 8

/编辑:我改写了整篇文章.

以下是VB突出显示问题的完整解决方案.如果SO没有更好的,使用它.VB语法高亮是绝对需要的.

我还添加了一个代码示例,其中包含一些可以正确突出显示的复杂代码文字.但是,我甚至没有尝试让XLinq正确.但是仍然可以工作.该关键字列表从MSDN拍摄.不包括上下文关键字.你认识GetXmlNamespace运营商吗?

该算法知道文字类型字符.它也应该能够处理标识符类型字符,但我还没有测试过这些.请注意,代码适用于HTML.因此,&,<和>需要被读作命名(!)实体,而不是单个字符.

对不起长正则表达式.

var highlightVB = function(code) {
    var regex = /("(?:""|[^"])+"c?)|('.*$)|#.+?#|(&amp;[HO])?\d+(\.\d*)?(e[+-]?\d+)?U?([SILDFR%@!#]|&amp;)?|\.\d+[FR!#]?|\s+|\w+|&amp;|&lt;|&gt;|([-+*/\\^$@!#%&<>()\[\]{}.,:=]+)/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("&amp;") == 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>";
}
Run Code Online (Sandbox Code Playgroud)

测试代码:

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 = &amp;H0
    Dim t As Date = #5/31/1993 1:15:30 PM#
    Dim f As Single = 1.32e-5F
End Sub
Run Code Online (Sandbox Code Playgroud)