-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'aleksanderwlodarczyk-class-loader-refactor'
- Loading branch information
Showing
6 changed files
with
68 additions
and
47 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,13 @@ | ||
local function load_classes (themes) | ||
for _, theme in pairs(themes) do | ||
log.trace("Loading classes in theme " .. theme.name) | ||
|
||
for class_filename, classes in pairs(theme.files) do | ||
--local pattern = "^(.+/)_class/(.+%.%w+)%-(.+)%.txt$" | ||
--local dir, basename, elem = class_filename:match(pattern) | ||
|
||
local pattern = [[^((?:.+/)?)_class/(.+\.\w+)/(.+)\.txt$]] | ||
local regex_object = regex.new(pattern) | ||
local matches = regex_object:capture(class_filename) | ||
|
||
-- If current file is a class file | ||
if matches then | ||
log.trace("Processing class file " .. class_filename) | ||
|
||
local dir, basename, elem = matches:get(1), matches:get(2), matches:get(3) | ||
|
||
local filename = basename | ||
if dir ~= "" then | ||
filename = dir .. basename | ||
end | ||
classes_loader = { } | ||
|
||
local content = theme.files[filename] | ||
require("loaders.classes.mod") | ||
|
||
-- Execute the sustitution | ||
if content then | ||
|
||
-- Either an id or just a tag name | ||
if elem:sub(1,1) == "#" then | ||
local id = elem:sub(2, -1) | ||
content = string.gsub(content, | ||
"<([^>]*%sid=\"" .. id .. "\"[^>]*)>", | ||
"<%1 class=\"\n" .. classes .. "\">" | ||
) | ||
else | ||
-- This *maaay* break if a tag name contains another tag name at the beggining | ||
content = string.gsub(content, | ||
"<(" .. elem .. "[^>]*)>", | ||
"<%1 class=\"\n" .. classes .. "\">" | ||
) | ||
end | ||
|
||
theme.files[filename] = content | ||
else | ||
log.error("File " .. filename .. " not found") | ||
end | ||
end | ||
end | ||
function load_classes (themes) | ||
for _, theme in pairs(themes) do | ||
log.trace("Loading classes in theme " .. theme.name) | ||
classes_loader.process_files(theme) | ||
end | ||
end | ||
|
||
|
||
themes_loader.add_preprocessor(load_classes) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
function classes_loader.get_filename(dir, basename) | ||
local filename = basename | ||
if dir ~= "" then | ||
filename = dir .. basename | ||
end | ||
|
||
return filename | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
function classes_loader.get_matches_table(class_filename) | ||
local pattern = [[^((?:.+/)?)_class/(.+\.\w+)/(.+)\.txt$]] | ||
local regex_object = regex.new(pattern) | ||
local matches = regex_object:capture(class_filename) | ||
|
||
return matches | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
require("loaders.classes.process_files") | ||
require("loaders.classes.process_content") | ||
require("loaders.classes.get_matches_table") | ||
require("loaders.classes.get_filename") |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
function classes_loader.process_content(theme, filename, elem, classes) | ||
local content = theme.files[filename] | ||
|
||
-- Execute the sustitution | ||
if content then | ||
|
||
-- Either an id or just a tag name | ||
if elem:sub(1,1) == "#" then | ||
local id = elem:sub(2, -1) | ||
content = string.gsub(content, | ||
"<([^>]*%sid=\"" .. id .. "\"[^>]*)>", | ||
"<%1 class=\"\n" .. classes .. "\">" | ||
) | ||
else | ||
-- This *maaay* break if a tag name contains another tag name at the beggining | ||
content = string.gsub(content, | ||
"<(" .. elem .. "[^>]*)>", | ||
"<%1 class=\"\n" .. classes .. "\">" | ||
) | ||
end | ||
|
||
theme.files[filename] = content | ||
else | ||
log.error("File " .. filename .. " not found") | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
function classes_loader.process_files(theme) | ||
for class_filename, classes in pairs(theme.files) do | ||
|
||
local matches = classes_loader.get_matches_table(class_filename) | ||
|
||
-- If current file is a class file | ||
if matches then | ||
log.trace("Processing class file " .. class_filename) | ||
|
||
local dir, basename, elem = matches:get(1), matches:get(2), matches:get(3) | ||
local filename = classes_loader.get_filename(dir, basename) | ||
classes_loader.process_content(theme, filename, elem, classes) | ||
|
||
end | ||
end | ||
end |