Created
August 27, 2015 10:30
-
-
Save phoenixenero/4935813ee68b8b5b5bea to your computer and use it in GitHub Desktop.
Lazy _s (underscores) theme generation with Lua. Tested in Windows only. Needs the `_s` dist folder in the same directory as build.lua
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
-- Written by Phoenix Enero. MIT License. Sorry for bad english | |
-- Global Config | |
local os = 'windows' -- 'unix' | |
local settings = { | |
-- note: must be contained in single quotes | |
text_domain = "'megatherium'", | |
function_names = 'megatherium_', | |
text_domain_style = 'Text Domain: megatherium', | |
docblocks = ' Megatherium', | |
handles = 'megatherium-' | |
} | |
-- Requires | |
local gsub = string.gsub | |
-- Other | |
local _log = '' | |
------------- | |
-- HELPERS -- | |
------------- | |
-- Log files | |
function log(str, doprint) | |
_log = _log .. str .. '\n' | |
if doprint then | |
print(str) | |
end | |
end | |
-- Lua implementation of PHP scandir function | |
function scandir(directory) | |
local i, t, popen, cmd = 0, {}, io.popen, '' | |
if os == 'windows' then | |
cmd = 'dir "' .. directory .. '" /s /b' | |
else | |
cmd = 'ls -aR "' .. directory .. '"' | |
end | |
-- Loop through files and populate table | |
for filename in popen( cmd ):lines() do | |
i = i + 1 | |
t[i] = filename | |
log(filename) | |
end | |
-- Return table of filenames | |
return t | |
end | |
-- Literalize strings for string.gsub | |
-- https://stackoverflow.com/questions/1745448/lua-plain-string-gsub | |
function literalize(str) | |
return str:gsub("[%(%)%.%%%+%-%*%?%[%]%^%$]", function(c) return "%" .. c end) | |
end | |
------------- | |
--/HELPERS -- | |
------------- | |
print('\n\n##############################################') | |
print('\nCurrently building an Underscores distribution\n') | |
print('##############################################\n\n') | |
-- Literalize settings | |
log('Escaping problematic characters\n', true) | |
for _, str in pairs( settings ) do | |
str = literalize(str) | |
log('escaped: ' .. str) | |
end | |
-- Find relevant files | |
local filenames = {} | |
log('\nBuilding file list\n', true) | |
local all_files = scandir('_s') | |
log('\nFiltering files\n', true) | |
for i, file in pairs(all_files) do | |
if string.find( file, '%.php' ) or | |
string.find( file, '%.css' ) then | |
filenames[i] = file | |
log(file) | |
end | |
end | |
-- Automagically find and replace all relevant files | |
print('\nFinding and replacing all patterns for _s') | |
for _, fn in pairs( filenames ) do | |
fp = io.open( fn, 'r' ) | |
str = fp:read( '*all' ) | |
print('Reading ' .. fn) | |
-- Capture text domain | |
str = gsub( str, "'_s'", settings.text_domain ) | |
-- Capture function names | |
str = gsub( str, '_s_', settings.function_names ) | |
-- Capture style.css text domain | |
str = gsub( str, 'Text Domain: _s', settings.text_domain_style ) | |
-- Capture DocBlocks | |
str = gsub( str, ' _s', settings.docblocks ) | |
-- Capture prefixed handles | |
str = gsub( str, '_s%-', settings.handles ) | |
fp:close() | |
fp = io.open( fn, 'w+' ) | |
print('Writing to ' .. fn) | |
fp:write( str ) | |
fp:close() | |
end | |
print('\n\n##############################################') | |
print('\nDone!\n') | |
print('##############################################\n\n') | |
-- Finish logging | |
debug_log = io.open( 'debug.log', 'w+' ) | |
print('\nLogging info to debug.log') | |
debug_log:write( _log ) | |
debug_log:close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment