Skip to content

Instantly share code, notes, and snippets.

@rrreese
Created July 17, 2012 12:21
Show Gist options
  • Save rrreese/3129130 to your computer and use it in GitHub Desktop.
Save rrreese/3129130 to your computer and use it in GitHub Desktop.
Reformat XML - Lua Plugin for Scite
function reformatXML()
editor:BeginUndoAction()
--Format
for m in editor:match("\r") do --remove carriage returns
m:replace("")
end
for m in editor:match("\t") do --remove tabs
m:replace("")
end
for m in editor:match("\n") do --remove newlines
m:replace("")
end
for m in editor:match("<") do --make each tag use 1 line - text only lines included
m:replace("\n<")
end
for m in editor:match(">") do --make each tag use 1 line
m:replace(">\n")
end
for m in editor:match("\n\n") do --make each tag use 1 line
m:replace("\n")
end
--tabify
local tabLevel = 1
local tempCount = 1
local itt = 1
local length = 1
--count number of lines
for m in editor:match("\n") do --each newline increment length
length = length + 1
end
body = ""--have to do this
line = ""--to avoid comparing to empty strings
while itt < length do --length do
line = editor:GetLine(itt)
offSet = 0
if line then --line not null (i think)
tempCount = tabLevel
tabify = "" --number of tabs
while tempCount > 1 do --generate line of tabs
tabify = tabify.."\t"
tempCount = tempCount - 1
end
tabifyE = ""--number of tabs for </
tempCount = tabLevel + offSet
while tempCount > 2 do --generate line of tabs
tabifyE = tabifyE.."\t"
tempCount = tempCount - 1
end
if string.find(line,"/>") then --<tag/>
line = tabify .. line
elseif string.find(line,"?>") then --<?xml> Wierdness
line = tabify .. line
elseif string.find(line,"</") then --</tag>
tabLevel=tabLevel - 1
line = tabifyE .. line
elseif string.find(line,"<!") then --<!-- Or <![[CDATA
line = tabify .. line
elseif string.find(line,"<") then --<tag>
tabLevel=tabLevel + 1
line = tabify .. line
else --Plain Text
line = tabify .. line
end
body = body .. line --append line to body
end
itt = itt + 1 --increment iterator
end
editor:ClearAll() --Clear old text
editor:AppendText(body) --Add new text
editor:EndUndoAction()
end
@m34nbunny
Copy link

I just discovered Scite over notepad++ and I am definitely going to try this plugin out

@Zac610
Copy link

Zac610 commented Jul 24, 2023

How do I add the lua script to scite?
I tried

command.name.1.=Format XML
command.subsystem.1.
=3
command.1.*=dofile $(SciteDefaultHome)/reformatXML.lua

but selecting the new menu item "Format XML" in the Tools section does nothing (the xml does not get formatted)

@rrreese
Copy link
Author

rrreese commented Jul 24, 2023

Sorry @Zac610 but I haven't used Scite in over a decade. Its possible this script is no longer compatible with current versions, and its possible that you haven't linked it in correctly.

@Zac610
Copy link

Zac610 commented Jul 24, 2023

Yes, @rrreese, both of them are possible, thank you anyway :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment