Home Download Buy Blog Forum Support

How to normaliser/beautifier HTML

How to normaliser/beautifier HTML

Postby leecade on Tue Nov 15, 2011 9:33 am

thanks,
leecade
 
Posts: 14
Joined: Thu Nov 10, 2011 10:26 pm

Re: How to normaliser/beautifier HTML

Postby pcmind on Tue Nov 15, 2011 7:35 pm

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

Code: Select all
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))


pcmind
 
Posts: 2
Joined: Tue Jun 14, 2011 11:40 pm

Re: How to normaliser/beautifier HTML

Postby leecade on Wed Nov 16, 2011 5:44 pm

thanks a lot, but still no effect.

when I test this command in console panel:

test:
Code: Select all
<html>
<head>
   <title></title>
</head>
<body>

</body>
</html>


Code: Select all
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
leecade
 
Posts: 14
Joined: Thu Nov 10, 2011 10:26 pm

Re: How to normaliser/beautifier HTML

Postby emelie on Wed Nov 16, 2011 8:12 pm

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.
emelie
 
Posts: 3
Joined: Wed Nov 16, 2011 8:10 pm

Re: How to normaliser/beautifier HTML

Postby tito on Thu Nov 17, 2011 6:14 pm

"Tag" plugin provides an indent function to allow prettify portions of invalid HTML/XML
https://github.com/SublimeText/Tag
Give APIs, let the community build the rest!
https://github.com/titoBouzout
tito
 
Posts: 866
Joined: Thu Sep 29, 2011 2:27 pm
Location: Montevideo, Uruguay


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 20 guests