commit 11d8bd16bd92a764f298c77d306f25d6da0981fc
parent c8ecb8284161673a00021e518c2aef2e8211e7ca
Author: Hugo Soucy <hugo@soucy.cc>
Date: Mon, 6 Dec 2021 20:53:24 -0500
Add the possibility to input CSS by stdin
Diffstat:
M | cssmin | | | 31 | ++++++++++++++++++++++++------- |
1 file changed, 24 insertions(+), 7 deletions(-)
diff --git a/cssmin b/cssmin
@@ -1,12 +1,26 @@
#!/usr/bin/env lua
--- Removes line breaks, comments and spaces around symbols in a CSS file.
--- No dependency, you only need lua (>= 5.1).
+--- With CSS file as argument or CSS block by stdin
-- @name cssmin
-- @param filepath the location of a CSS file
-- @return A 'text/css' block
--- @usage ./cssmin styles.css[ > styles.min.css]
+-- Examples of uses
+--[[
+ $ ./cssmin styles.css > styles.min.css
+ $ ./cssmin < styles.css > styles.min.css
+ $ cat styles.css | ./cssmin
+ $ echo ".red { color: red; } | cssmin"
+]]
do
+ local function minify(css)
+ return css
+ :gsub('\n', '') -- removes line-breaks
+ :gsub('%s*([:;,*>{}~])%s+', '%1') -- removes spaces around :;,*>{}~
+ :gsub('/%*.-%*/', '') -- removes comments
+ end
+
if arg[1] and io.open(arg[1], 'r') then
local file = arg[1]
local extension = file:match("^.+(%..+)$")
@@ -24,15 +38,18 @@ do
end
if extension == '.css' then
- local cssmin = read(file)
- :gsub('\n', '') -- removes line-breaks
- :gsub('%s*([:;,*>{}~])%s+', '%1') -- removes spaces around :;,*>{}~
- :gsub('/%*.-%*/', '') -- removes comments
-
- return print(cssmin)
+ return print(minify(read(file)))
else
print "[Error!] Wrong file type. It should be a 'text/css'."
end
+ elseif not arg[1] and io.input() ~= nil then
+ local css = {}
+
+ for line in (io.lines()) do
+ css[#css+1] = line
+ end
+
+ return print(minify(table.concat(css)))
elseif not arg[1] then
print "An argument is missing. You sould add a CSS file as argument."
else