Sublime Forum

HTML and Python Prettify

#1

I was looking for a plugin, that will fix your python and html indentation so the code looks as pretty as possible, I know this isn’t super important but when I am pulling between couple of people with different styling formats it would be cool to automatically format code so I can read it without much trouble. I also wouldn’t mind JS/CSS support too.

Any plugins for this feature?

0 Likes

#2

I don’t know about Python, but for HTML you can roll your own formatter using pytidylib and HTML Tidy. It took me a few days of trial and error to make my own XML formatter but it works great and I learned a lot about Sublime and Python in the process. Not sure if HTML Tidy does CSS or JS, though.

0 Likes

#3

Just looking into Sublime Text 2. So there is no way to format code other than manually? How did you get HTML tidy to work with ST2?

0 Likes

#4

I followed the directions here:
countergram.com/open-source/pyti … index.html

But instead of “installing” pytidylib, I downloaded the source and put the “tidylib” directory under Sublime’s Packages/User directory.

Then I had to change line 33 in init.py like so - putting ‘tidy’ first got it running on Windows.

LIB_NAMES = 'tidy', 'libtidy', 'libtidy.so', 'libtidy-0.99.so.0', 'cygtidy-0-99-0', 'tidylib', 'libtidy.dylib']

Then I created a Sublime command:

[code]import sublime, sublime_plugin
from tidylib import tidy_document

class XmlTidyCommand(sublime_plugin.TextCommand):
def run(self, edit):
allRegion = sublime.Region(0, self.view.size())
allText = self.view.substr(allRegion)
document, errors = tidy_document(allText, options={‘input-xml’:1, ‘indent’:1, ‘newline’:‘LF’})
self.view.replace(edit, allRegion, document)
if errors != “”:
sublime.error_message(errors)
else:
sublime.status_message(“Xml Tidy: No errors”)[/code]

0 Likes