Skip to content

Instantly share code, notes, and snippets.

@zwim
Last active December 19, 2022 19:28
Show Gist options
  • Save zwim/a6c2a170c6e7b22785b259a7ad6b9fe0 to your computer and use it in GitHub Desktop.
Save zwim/a6c2a170c6e7b22785b259a7ad6b9fe0 to your computer and use it in GitHub Desktop.
KOREADER: A quick'n'dirty patch to allow overwriting the provided hyphenation pattern with a pattern file located in `/mnt/onboard/German.pattern`
-- A patch for updating a German.pattern file, if this is newer than the provide one.
-- quick and dirty; for my personal usage, will most likely not fit any other users needs
print("Update hyphenation patterns")
local language = "German" -- is someone wants to use this patch with another language
local xxx = "" -- add your location here
local lfs = require("libs/libkoreader-lfs")
local DataStorage = require("datastorage")
local ffiutil = require("ffi/util")
local data_dir = DataStorage:getDataDir()
local provided_pattern_path = data_dir .. "/data/hyph/" .. language .. ".pattern"
local pattern_path = {
"/mnt/onboard/" .. language .. ".pattern", -- on a Kobo, for other devices feel free to add other paths
"xxx/work/hyphenation/" .. language .. ".pattern", -- for tests in the emulator
}
local function get_version_of_pattern_file(path)
local date = ""
if lfs.attributes(path, "mode") ~= "file" then
-- most probably the file has been deleted or does not exist
return date
end
for line in io.lines(path) do
if line:find("<pattern>", 1, true) then
break -- end of preamble
end
local _, version_pos = line:find("version. *") -- the version line looks like: "% version: 2022-12-24"
if version_pos then
local version = line:sub(version_pos + 1)
local version_start, version_end = version:find("%d+-%d+-%d+")
if version_end then
version = version:sub(version_start, version_end)
date = version
break
end
end
end
return date
end
local provided_date
for pos = 1, #pattern_path do
if lfs.attributes(pattern_path[pos], "mode") == "file" then
print("Potential hyphenation pattern to update found at:", pattern_path[pos])
provided_date = provided_date or get_version_of_pattern_file(provided_pattern_path) -- calc this late = here
local candiate_date = get_version_of_pattern_file(pattern_path[pos])
if not provided_date or provided_date < candiate_date then
print("Copy pattern to", provided_pattern_path)
ffiutil.copyFile(pattern_path[pos], provided_pattern_path)
end
break
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment