Skip to content

Instantly share code, notes, and snippets.

@ranisalt
Created July 4, 2014 02:42
Show Gist options
  • Save ranisalt/294a511955d376624412 to your computer and use it in GitHub Desktop.
Save ranisalt/294a511955d376624412 to your computer and use it in GitHub Desktop.
function cleanPreviousIndentation(line)
return line:gsub("^%s+", ""):gsub("%s+$", "")
end
function escapeString(str)
return str:gsub("[()]", "%%%1")
end
function string:split(delimiter)
local array = {}
local first, last = string.find(self, '[^'..delimiter..']*'..delimiter)
while first do
array[#array + 1] = string.sub(self, first, last - 1)
self = string.sub(self, last + 1)
first, last = string.find(self, '[^'..delimiter..']*'..delimiter)
end
array[#array + 1] = self
return array
end
function indent(script)
local indented = script:split('\n')
local level = 0
for index, line in ipairs(script) do
if line:find("^end") or line:find("^}") then
level = level - 1
end
indented[index] = string.rep("\t", level)..line
if line:find("^if") or line:find("^while") or line:find("^= {") or line:find("^(local )?function)") then
level = level + 1
end
end
return table.concat(indented, "\n")
end
function indent_file(filename)
io.open(filename)
local code = io.read("*all")
io.close()
io.open("fixed_"..filename)
io.write(indent(escapeString(cleanPreviousIndentation(code))))
io.close()
end
indent_file("luck.lua")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment