Skip to content

Commit

Permalink
put query in to separate module
Browse files Browse the repository at this point in the history
  • Loading branch information
leafo committed Jul 3, 2015
1 parent 433d063 commit bbcf662
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 103 deletions.
2 changes: 2 additions & 0 deletions sitegen-dev-1.rockspec
Expand Up @@ -46,8 +46,10 @@ build = {
["sitegen.plugins.feed"] = "sitegen/plugins/feed.lua",
["sitegen.plugins.indexer"] = "sitegen/plugins/indexer.lua",
["sitegen.plugins.pygments"] = "sitegen/plugins/pygments.lua",
["sitegen.query"] = "sitegen/query.lua",
["sitegen.renderer"] = "sitegen/renderer.lua",
["sitegen.renderers.html"] = "sitegen/renderers/html.lua",
["sitegen.renderers.lapis"] = "sitegen/renderers/lapis.lua",
["sitegen.renderers.markdown"] = "sitegen/renderers/markdown.lua",
["sitegen.renderers.moon"] = "sitegen/renderers/moon.lua",
["sitegen.site"] = "sitegen/site.lua",
Expand Down
75 changes: 75 additions & 0 deletions sitegen/query.lua
@@ -0,0 +1,75 @@
local array_includes
array_includes = function(array, val)
for _index_0 = 1, #array do
local array_val = array[_index_0]
if array_val == val then
return true
end
end
return false
end
local query_page_match
query_page_match = function(page, query)
if not query or not next(query) then
return true
end
for k, query_val in pairs(query) do
local _continue_0 = false
repeat
local page_val = page.meta[k]
if type(page_val) == "table" then
if array_includes(page_val, query_val) then
_continue_0 = true
break
end
end
if page_val ~= query_val then
return false
end
_continue_0 = true
until true
if not _continue_0 then
break
end
end
return true
end
local query_pages
query_pages = function(pages, query, opts)
if query == nil then
query = { }
end
if opts == nil then
opts = { }
end
local out
do
local _accum_0 = { }
local _len_0 = 1
for _index_0 = 1, #pages do
local _continue_0 = false
repeat
local page = pages[_index_0]
if not (query_page_match(page, query)) then
_continue_0 = true
break
end
local _value_0 = page
_accum_0[_len_0] = _value_0
_len_0 = _len_0 + 1
_continue_0 = true
until true
if not _continue_0 then
break
end
end
out = _accum_0
end
if opts.sort then
local _ = nil
end
return out
end
return {
query_pages = query_pages
}
34 changes: 34 additions & 0 deletions sitegen/query.moon
@@ -0,0 +1,34 @@

array_includes = (array, val) ->
for array_val in *array
return true if array_val == val
false

query_page_match = (page, query) ->
-- empty query matches all
return true if not query or not next query

for k,query_val in pairs query
page_val = page.meta[k]

if type(page_val) == "table"
if array_includes page_val, query_val
continue

if page_val != query_val
return false

true

query_pages = (pages, query={}, opts={}) ->
out = for page in *pages
continue unless query_page_match page, query
page

-- sort..
if opts.sort
nil

out

{ :query_pages }
74 changes: 4 additions & 70 deletions sitegen/site.lua
Expand Up @@ -19,16 +19,6 @@ local Templates
Templates = require("sitegen.templates").Templates
local Page
Page = require("sitegen.page").Page
local array_includes
array_includes = function(array, val)
for _index_0 = 1, #array do
local array_val = array[_index_0]
if array_val == val then
return true
end
end
return false
end
local Site
do
local _base_0 = {
Expand Down Expand Up @@ -176,67 +166,11 @@ do
end)()
return self.pages
end,
query_page_match = function(self, page, query)
if not query or not next(query) then
return true
end
for k, query_val in pairs(query) do
local _continue_0 = false
repeat
local page_val = page.meta[k]
if type(page_val) == "table" then
if array_includes(page_val, query_val) then
_continue_0 = true
break
end
end
if page_val ~= query_val then
return false
end
_continue_0 = true
until true
if not _continue_0 then
break
end
end
return true
end,
query_pages = function(self, query, opts)
if query == nil then
query = { }
end
if opts == nil then
opts = { }
end
query_pages = function(self, ...)
self:load_pages()
local out
do
local _accum_0 = { }
local _len_0 = 1
local _list_0 = self.pages
for _index_0 = 1, #_list_0 do
local _continue_0 = false
repeat
local page = _list_0[_index_0]
if not (self:query_page_match(page, query)) then
_continue_0 = true
break
end
local _value_0 = page
_accum_0[_len_0] = _value_0
_len_0 = _len_0 + 1
_continue_0 = true
until true
if not _continue_0 then
break
end
end
out = _accum_0
end
if opts.sort then
local _ = nil
end
return out
local query_pages
query_pages = require("sitegen.query").query_pages
return query_pages(self.pages, ...)
end,
write = function(self, filter_files)
if filter_files == nil then
Expand Down
36 changes: 3 additions & 33 deletions sitegen/site.moon
@@ -1,4 +1,3 @@

import concat from table

import
Expand All @@ -23,11 +22,6 @@ import SiteScope from require "sitegen.site_scope"
import Templates from require "sitegen.templates"
import Page from require "sitegen.page"

array_includes = (array, val) ->
for array_val in *array
return true if array_val == val
false

-- a webpage
class Site
@default_renderers: {
Expand Down Expand Up @@ -161,34 +155,10 @@ class Site
@pages or= [@Page path for path in @scope.files\each!]
@pages

query_page_match: (page, query) =>
-- empty query matches all
return true if not query or not next query

for k,query_val in pairs query
page_val = page.meta[k]

if type(page_val) == "table"
if array_includes page_val, query_val
continue

if page_val != query_val
return false

true

query_pages: (query={}, opts={}) =>
query_pages: (...) =>
@load_pages!

out = for page in *@pages
continue unless @query_page_match page, query
page

-- sort..
if opts.sort
nil

return out
import query_pages from require "sitegen.query"
query_pages @pages, ...

-- write the entire website
write: (filter_files=false) =>
Expand Down

0 comments on commit bbcf662

Please sign in to comment.