library.lua - crossword.koplugin - Unnamed repository; edit this file 'description' to name the repository. (DIR) Log (DIR) Files (DIR) Refs (DIR) Submodules (DIR) README (DIR) LICENSE --- library.lua (3164B) --- 1 local KeyValuePage = require("ui/widget/keyvaluepage") 2 local UIManager = require("ui/uimanager") 3 4 local util = require("util") 5 local lfs = require("libs/libkoreader-lfs") 6 local logger = require("logger") 7 local sort = require("frontend/sort") 8 9 local Puzzle = require("puzzle") 10 11 local function titleToMonth(title) 12 local months = { 13 ["01"] = "January", 14 ["02"] = "February", 15 ["03"] = "March", 16 ["04"] = "April", 17 ["05"] = "May", 18 ["06"] = "June", 19 ["07"] = "July", 20 ["08"] = "August", 21 ["09"] = "September", 22 ["10"] = "October", 23 ["11"] = "November", 24 ["12"] = "December", 25 } 26 return months[title] or title 27 end 28 29 local Library = { 30 puzzle_dir = nil, 31 onSelectPuzzle = function() end 32 } 33 34 function Library:new(o) 35 o = o or {} 36 setmetatable(o, self) 37 self.__index = self 38 return o 39 end 40 41 --[[-- 42 Given a directory, return a table of files located within this directory. Filter 43 the files by type and extension, showing only JSON and IPUZ files. These are assumed to be 44 the crossword puzzles. 45 ]] 46 function Library:getFilesInDirectory(path_to_dir) 47 local items = {} 48 49 local ok, iter, dir_obj = pcall(lfs.dir, path_to_dir) 50 if not ok then 51 return items 52 end 53 54 for f in iter, dir_obj do 55 local attributes = lfs.attributes(("%s/%s"):format(path_to_dir, f)) 56 if attributes.mode == "directory" 57 or attributes.mode == "file" 58 and f ~= "." 59 and f ~= ".." 60 and (util.stringEndsWith(f, ".json") or 61 util.stringEndsWith(f, ".ipuz")) 62 then 63 local title = (attributes.mode == "directory") and 64 f or -- Use the file name as the title 65 Puzzle:initializePuzzleFromFile(("%s/%s"):format(path_to_dir, f)).title -- Use the puzzle's name as the title 66 local item = { 67 title = title, -- The item's name to show the user. 68 filename = f, -- The item's name in the filesystem. 69 mode = attributes.mode, -- The mode of the item (i.e.: file or directory). 70 path_to_dir = path_to_dir -- The path to the item's directory. 71 } 72 -- Maybe change the title into a month 73 item.title = titleToMonth(item.title) 74 table.insert(items, item) 75 end 76 end 77 78 table.sort(items, function(a, b) 79 local fn = sort.natsort_cmp() 80 return fn(a.filename, b.filename) 81 end) 82 83 return items 84 end 85 86 function Library:processDirectoryCallback(item) 87 if item.mode == "directory" then 88 self:showDirectoryView(("%s/%s"):format(item.path_to_dir, item.filename)) 89 else 90 self.onSelectPuzzle(item) 91 end 92 end 93 94 function Library:showDirectoryView(path_to_directory) 95 local directory_items = self:getFilesInDirectory(path_to_directory) 96 local kv_pairs = {} 97 for key, item in ipairs(directory_items) do 98 table.insert(kv_pairs, { 99 item.title, 100 "", 101 callback = function() 102 self:processDirectoryCallback(item) 103 end 104 }) 105 end 106 UIManager:show(KeyValuePage:new{ 107 title = "Puzzles", 108 kv_pairs = kv_pairs 109 }) 110 end 111 112 return Library