Created
February 19, 2017 12:23
-
-
Save exerro/a38006f3b8aa6c5bf4f4689e9fe6e37e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local function microminify( line, state ) | |
local segments = {} | |
local i, n = 1, 1 | |
local res = {} | |
local is_word = false | |
if state.in_string then | |
local pos = find_non_escaped( line, state.string_closer, 1 ) | |
if pos then | |
segments[1] = line:sub( 1, pos ) | |
i = 2 | |
line = line:sub( pos + 1 ) | |
state.in_string = false | |
else | |
segments[1] = line | |
line = "" | |
end | |
end | |
local s, f = line:find "%S" | |
while s do | |
local ch = line:sub( s, s ) | |
if ch == "\"" or ch == "'" then | |
local close_pos = find_non_escaped( line, ch, s + 1 ) | |
if close_pos then | |
segments[i] = line:sub( s, close_pos ) | |
s, f = line:find( "%S", close_pos + 1 ) | |
i = i + 1 | |
else | |
state.in_string = true | |
state.string_closer = ch | |
segments[i] = line:sub( s ) | |
break | |
end | |
elseif line:find( "^%[=*%[", s ) then -- multiline string | |
error "multiline strings are not yet supported" | |
else | |
segments[i] = line:sub( s, f ) | |
i = i + 1 | |
s, f = line:find( "%S", f + 1 ) | |
end | |
end | |
for i = 1, #segments do | |
local is_this_word = segments[i]:find "^[%w_]" or segments[i]:find "[%w_]$" | |
if is_word and is_this_word then | |
res[n] = " " | |
n = n + 1 | |
end | |
res[n] = segments[i] | |
n = n + 1 | |
end | |
return table.concat( res ) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment