Pass code to the [Pygments](http://pygments.pocoo.org/) highliter
    with optional line numbers. The output should then be styled with CSS
    to  your liking. No styles are applied by default - only styling hooks
    (i.e.: <span class="k">).

def highlight_syntax(src, lang, linenums=False):
    """Pass code to the [Pygments](http://pygments.pocoo.org/) highliter
    with optional line numbers. The output should then be styled with CSS
    to  your liking. No styles are applied by default - only styling hooks
    (i.e.: <span class="k">).
    """
    src = src.strip('\n')
    if not lang:
        lexer = TextLexer()
    else:
        try:
            lexer = get_lexer_by_name(lang, stripall=True)
        except ValueError:
            lexer = TextLexer()
    formatter = HtmlFormatter(linenos=linenums, tab_length=TAB_LENGTH)
    html = highlight(src, lexer, formatter)

    if lang:
        open_code = OPEN_CODE % (LANG_TAG % (lang, ), )
    else:
        open_code = OPEN_CODE % u''
    html = html.replace('<div class="highlight"><pre>', open_code, 1)
    html = html.replace('</pre></div>', CLOSE_CODE)
    return html