jQuery Chili with Python Syntax Highlighting

Contents

    If you like the jQuery Chili Plugin for syntaxhighlighting and like to publish python scripts, you will see that there is not a default highlighting file for this available. If you need one read further.. After short googling the web I came across this: python.js from Ben Godfrey. The only problem I noticed with that is the missing support for """ syntax. So I cam across this regex:

    /(?:"""([^A]|A)+?""")/gi
    

    /gi says it is a multiline match ([\^A]|A)+? is a non-greedy search for everything. You might say that everything is normally ".+" but this wasn't possible here because "." doesn't include newlines. The "[\^A]" says basically everything but "A". And this includes newlines so I have to join the "|A" to this.

    So the final script looks like:

    ChiliBook.recipes[ "python.js" ] =
    {
              _name: 'python'
            , _case: true
            , _main: {
                      sl_comment: {
                              _match: /#.*/
                            , _style: 'color: green;'
                    }
                    , string: {
                              _match: /(?:"""([^A]|A)+?""")|(?:'[^'\n]*(?:\.[^'\n]*)*')|(?:"[^"\n]*(?:\.[^"\n]*)*")/gi
                            , _style: 'color: teal;'
                    }
                    , num: {
                              _match: /b[+-]?(?:d*.?d+|d+.?d*)(?:[eE][+-]?d+)?b/
                            , _style: 'color: red;'
                    }
                    , statement: {
                              _match: /b(and|as|assert|break|class|continue|def|del|elif|else|except|exec|finally|for|from|global|if|import|in|is|lambda|not|or|pass|print|raise|return|try|while|with|yield)b/
                            , _style: 'color: navy; font-weight: bold;'
                    }
                    , property: {
                              _match: /b(None|True|False)b/
                            , _style: 'color: Purple; font-weight: bold;'
                    }
            }
    }
    

    Commentaires: