The following script removes all comments (single line or multi line) from 5 types of files that I use mostly: JavaScript, HTML, XML, CSS and Lua.
formats={
["js"]={
name="JavaScript",
single="//.-\n",
multi="/%*.-%*/"
},
["xml"]={
name="XML",
single=nil,
multi="<!--.--->"
},
["html"]={
name="HTML",
single=nil,
multi="<!--.--->"
},
["css"]={
name="CSS",
single=nil,
multi="/%*.-%*/"
},
["lua"]={
name="Lua",
single="--.-\n",
multi="/%[%[.-%]%]/"
},
}
---Gets a valid compilable javascript code and removes its comments
-- @note the javascript code should be ready to compile that is: there are no syntax
-- errors whatsoever. Otherwise the behavior of this function is unpredictable.
-- @param txt the compilable javascript code
-- @param patt the pattern table from format table. See how it's called from removeCommentsFile()
-- @return a text string the semantically behaves the same as the txt that was passed
-- to the function but doesn't have comments
function removeComments(txt,patt)
--remove all multi-line comments
if patt.multi then
txt=txt:gsub(patt.multi,"\n")
end
--remove all single-line comments
if patt.single then
txt=txt:gsub(patt.single,"\n")
end
txt=txt:gsub("\n[%s]*","\n") --remove all soaces at the start of lines
txt=txt:gsub("[%s\n]*\n","\n")--remove all empty lines and all spaces at the end of lines
txt=txt:gsub("^\n","") --remove first empty new line
txt=txt:gsub("\n$","") --remove last empty new line
return txt
end
---Opens a file and detects its extension before passing the contents to removeComments()
function removeCommentsFile(fileName)
local f=io.open(fileName,"r")
local txt=f:read("*a")
local extension=fileName:match("%.(.-)$")
if not extension then
return nil,"Could not find extension for file '"..fileName.."'"
end
extension=extension:lower()
if formats[extension] then
print("Removing comments from '"..fileName.."' (a "..formats[extension].name.." file)")
return removeComments(txt,formats[extension])
else
return nil,"This file is not recognized: '"..fileName.."'"
end
end
for k,v in ipairs(arg) do
print(removeCommentsFile(v))
end
Comment remover script for JavaScript, HTML, XML, CSS and Lua
Nov 28 2011 Published by Alex Hanif under Tips&Tricks
The following script removes all comments (single line or multi line) from 5 types of files that I use mostly: JavaScript, HTML, XML, CSS and Lua.
formats={ ["js"]={ name="JavaScript", single="//.-\n", multi="/%*.-%*/" }, ["xml"]={ name="XML", single=nil, multi="<!--.--->" }, ["html"]={ name="HTML", single=nil, multi="<!--.--->" }, ["css"]={ name="CSS", single=nil, multi="/%*.-%*/" }, ["lua"]={ name="Lua", single="--.-\n", multi="/%[%[.-%]%]/" }, } ---Gets a valid compilable javascript code and removes its comments -- @note the javascript code should be ready to compile that is: there are no syntax -- errors whatsoever. Otherwise the behavior of this function is unpredictable. -- @param txt the compilable javascript code -- @param patt the pattern table from format table. See how it's called from removeCommentsFile() -- @return a text string the semantically behaves the same as the txt that was passed -- to the function but doesn't have comments function removeComments(txt,patt) --remove all multi-line comments if patt.multi then txt=txt:gsub(patt.multi,"\n") end --remove all single-line comments if patt.single then txt=txt:gsub(patt.single,"\n") end txt=txt:gsub("\n[%s]*","\n") --remove all soaces at the start of lines txt=txt:gsub("[%s\n]*\n","\n")--remove all empty lines and all spaces at the end of lines txt=txt:gsub("^\n","") --remove first empty new line txt=txt:gsub("\n$","") --remove last empty new line return txt end ---Opens a file and detects its extension before passing the contents to removeComments() function removeCommentsFile(fileName) local f=io.open(fileName,"r") local txt=f:read("*a") local extension=fileName:match("%.(.-)$") if not extension then return nil,"Could not find extension for file '"..fileName.."'" end extension=extension:lower() if formats[extension] then print("Removing comments from '"..fileName.."' (a "..formats[extension].name.." file)") return removeComments(txt,formats[extension]) else return nil,"This file is not recognized: '"..fileName.."'" end end for k,v in ipairs(arg) do print(removeCommentsFile(v)) endComments are off for this post