Sublime Forum

How to normaliser/beautifier HTML

#1

thanks,

0 Likes

#2

You can make a plugin for it. The next code will only work on xml/html with no error.

import sublime, sublime_plugin
import re
from xml.dom.minidom import parse, parseString
class PrettyPrintXmlCommand(sublime_plugin.TextCommand):
    def run(self, edit):
      view = self.view
      for region in view.sel():
         str_xml = "".join(re.split("\n \t]*", view.substr(region)))
         result = parseString(str_xml).toprettyxml()

         self.view.replace(edit, region, str(result))

0 Likes

#3

thanks a lot, but still no effect.

when I test this command in console panel:

test:

<html>
<head>
	<title></title>
</head>
<body>

</body>
</html>
Traceback (most recent call last):
  File "./sublime_plugin.py", line 282, in run_
  File "./beauty_html.py", line 9, in run
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/xml/dom/minidom.py", line 1928, in parseString
    return expatbuilder.parseString(string)
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/xml/dom/expatbuilder.py", line 940, in parseString
    return builder.parseString(string)
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/xml/dom/expatbuilder.py", line 223, in parseString
    parser.Parse(string, True)
xml.parsers.expat.ExpatError: no element found: line 1, column 0
0 Likes

#4

leecade: you need to select the part you want to beautify.
If I select the code you pasted the beautifier works.
If I don’t I get the error you posted.

0 Likes

#5

“Tag” plugin provides an indent function to allow prettify portions of invalid HTML/XML
github.com/SublimeText/Tag

0 Likes