cssmin

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit ce7f15d8dbad54dcdadc78d8d5c3283152f97cb3
Author: Hugo Soucy <hugo@soucy.cc>
Date:   Fri, 19 Nov 2021 17:16:44 -0500

Do the first commit.

Diffstat:
A.editorconfig | 12++++++++++++
Acssmin | 41+++++++++++++++++++++++++++++++++++++++++
2 files changed, 53 insertions(+), 0 deletions(-)

diff --git a/.editorconfig b/.editorconfig @@ -0,0 +1,12 @@ +# EditorConfig <http://EditorConfig.org> + +root = true + +# Unix-style newlines with a newline ending every file +[*] +charset = utf-8 +end_of_line = lf +insert_final_newline = true +indent_style = space +indent_size = 2 +trim_trailing_whitespace = true diff --git a/cssmin b/cssmin @@ -0,0 +1,41 @@ +#!/usr/bin/env lua +--- Removes line breaks and spaces around symbols in a CSS file. +--- No dependency, you only need lua (>= 5.1). +-- @name cssmin +-- @param filepath the location of a CSS file +-- @return A 'text/css' block +-- @usage ./cssmin styles.css[ > styles.min.css] + +do + if arg[1] and io.open(arg[1], 'r') then + local file = arg[1] + local extension = file:match("^.+(%..+)$") + local function read(filepath) + if not filepath then + print "[Error!] A file is missing as argument." + end + + local buffer = assert(io.open(filepath, 'r')) + local content = buffer:read '*a' + + buffer:close() + + return content + 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) + else + print "[Error!] Wrong type of file. It should be a 'text/css'." + end + elseif not arg[1] then + print "An argument is missing. You sould add a CSS file as argument." + else + print("[Error!] The '" .. arg[1] .. "' file does not exist.") + end +end