How to format "single line html" as a beautiful document in a sublime?

I have an html source file containing only one line, such as:

<html><head>test</head><body>wow</body></html>

and I want to format it as follows:

<html>
    <head>
        test
    </head>
    <body>
        wow
    </body>
</html>

I used the command: Edit-> Line-> Reindent, but it does not work.

+5
source share
3 answers

In Sublime Text, try the following:

  • Highlight the last character in the first tag: " >"
  • Use Find> Quick Add Next( Command+ Don Mac or Ctrl+ Don PC)

Now you will see all the occurrences in several selections, move the cursor to the end of the tag and press "return" to insert all the new lines that you need.

Good luck.

+7

- , (tools → record macro), , . , .

0

, Sublime . , , . , XML/HTML, . Sublime , , .

Sublime Plugins

, tidy , , , , . . , , , , . , , , , .

(Ctrl+n) " ..." "" "Hello World!". ( Python), sublime_plugin.TextCommand. TextCommand / . WindowCommand ApplicationCommand, run.

API , , Sublime, Packages/Default Sublime, - . .

, View, . , TextCommand, , , , , , .

: , <start-tag>, <empty-tag/>, </close-tag> text-node. , , . . , , , . , , .

Python, . , , , . , , , . .

Another thing to take care of is to bind the keys to the plugin command and contribute to the menu items. Probably, perhaps somehow , but the files .sublime-menuand .sublime-commandsby default at least give an idea. Anyway, here is some kind of code. It should be saved as Packages/User/whatever.py, and may be invoked from the Python Console Sublime ( ) as follows: .Ctrl + `view.run_command('guess_indentation')

The code

import sublime
import sublime_plugin
import re

class GuessIndentationCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        view = self.view
        #view.begin_edit()
        # patterns
        start_tag = '<\w+(?:\s+[^>\/]+)*\s*>'       # tag_start
        node_patterns = [start_tag, 
                        start_tag[:-1]+'\/\s*>',    # tag_empty
                        '<\/\s?\w+\s?>',            # tag_close
                        '[^>\s][^<>]*[^<\s]']       # text_node
        patterns = '(?:{0})'.format('|'.join(node_patterns))
        indentors = re.compile('[ \t]*({0})'.format('|'.join(node_patterns[:1])))
        unindentors=re.compile('[ \t]*({0})'.format(node_patterns[2]))
        # process selected text
        for region in view.sel():
            # if selection contains text:
            if not region.empty():
                selection = view.substr(region)
                expanded = []
                # divide selected lines into XML elements, if it contains more than one
                for line in selection.split('\n'):
                    elements = re.findall(patterns, line)
                    if len(elements)>0:
                        expanded += elements
                    else:
                        expanded.append(line)
                # indent output
                indent=0
                indented = []
                for line in expanded:
                    match = unindentors.match(line)
                    if match:
                        indent = max(0, indent-1)
                    # append line to output, unindented if closing tag
                    indented.append('\t'*indent+line)
                    if match:
                        continue
                    # test for possible indentation candidate
                    # indentation applies to the NEXT line
                    match = indentors.match(line)
                    if match:
                        indent+=1
                # replace selection with aligned output
                view.replace(edit, region, '\n'.join(indented))
0
source

All Articles