joelpt/sublimetext-print-to-html — pull request #12 by DanielOaks (2014-03-06 22:30:46+00:00)
Title: Python3, and Sublime Text scoping

This pull request is for Python 3 support, building on top of Matt's Pull Request #11. However, I wouldn't really like this to be pulled directly into Master. It would probably be best if we created a new Python3 branch on this repo, (and make that the default), so that we could make Sublime Text 3 point to the new version, and Sublime Text 2 point to this current version of Print to HTML.

I had to make a few little changes to how it loaded files (particularly the wordwrap.js file), so that it didn't fail on startup and give a huge delay before retrying.

The colour and scoping change is fairly large, and written to accomplish the following goal:

* Use ST2's internal scope and color data from a buffer to produce an exact replica of a file from ST2 in HTML form. The current approach using Pygments has limitations regarding formats unknown by Pygments (e.g. Markdown) and documents with mixed syntaxes (e.g. PHP embedded in HTML).

Basically, instead of passing the text to Pygments' lexer, it goes through a custom-written lexer that maps ST2 scopes to Pygments tokens. The formatting and output are the same, it's just how it interfaces with the lexer that changes.

Currently, here's a comparison between the outputs of the current Pygments (top) and the SublimeLexer in this PR (bottom):

![pyg](https://f.cloud.github.com/assets/251281/53188/610456a0-5a51-11e2-8a62-a0eaf3d7423a.png)

I believe that language support is at an alright state, with most of the languages I've tried working fairly well. There's also a document for how to add your own scoping information there, just in case someone finds a case we don't handle too well.

The good part about doing the lexing like this is that even if Pygments doesn't explicitly support the language you're trying to print, it will still work (even if it may miss one or two language-specific scopes). And with some languages, such as Python, Sublime's scoping and colouring information is even more extensive than Pygments' support, so we get improved colours there.

**NOTE**
Once again, I think it would be best if we **DON'T** pull this into master, but instead create a new Python3 branch and pull something like this into it, so we can dual-support Sublime Text 2 and 3 (even if 2 is in mostly a just-keep-it-running mode)