"Xml Beautifier" by AllenDang

Rating: (0 votes)

Report this Script

Name: Xml Beautifier
Uploaded By: AllenDang
Date Created: May 7, 2009 1:53 a.m.
Date Modified: May 7, 2009 1:53 a.m.
Description: This script will beautify xml content in current active tab.
Num Views: 556
Num Downloads: 207

Contents

Download this script

import pn
import scintilla
import re, string
from pypn.decorators import script

@script("Beautify", "Xml")
def Beautify():
    editor = scintilla.Scintilla(pn.CurrentDoc())
    data = editor.GetText(editor.Length)

    fields = re.split('(<.*?>)',data)
    content = ''
    level = 0
    for f in fields:
        if string.strip(f)=='': continue
        if f[0]=='<' and f[1] != '/' and f[-2] != '/':
            content += ' '*(level*4) + f + '\n'
            level = level + 1
        elif f[0] == '<' and f[1] != '/' and f[-2] == '/':
            content += ' '*(level*4) + f + '\n'
        elif f[:2]=='</':
            level = level - 1
            content += ' '*(level*4) + f + '\n'
        else:
            content += ' '*(level*4) + f + '\n'
            
    editor.BeginUndoAction()
    editor.ClearAll()
    editor.AddText(len(content), content)
    editor.EndUndoAction()

Comments: