<commit_before>--[[--
This module provides a way to display book information (filename and book metadata)
]]

local BD = require("ui/bidi")
local DocSettings = require("docsettings")
local DocumentRegistry = require("document/documentregistry")
local ImageViewer = require("ui/widget/imageviewer")
local InfoMessage = require("ui/widget/infomessage")
local InputContainer = require("ui/widget/container/inputcontainer")
local KeyValuePage = require("ui/widget/keyvaluepage")
local UIManager = require("ui/uimanager")
local filemanagerutil = require("apps/filemanager/filemanagerutil")
local lfs = require("libs/libkoreader-lfs")
local util = require("util")
local _ = require("gettext")

local BookInfo = InputContainer:extend{
    bookinfo_menu_title = _("Book information"),
}

function BookInfo:init()
    if self.ui then -- only for Reader menu
        self.ui.menu:registerToMainMenu(self)
    end
end

function BookInfo:addToMainMenu(menu_items)
    menu_items.book_info = {
        text = self.bookinfo_menu_title,
        callback = function()
            self:onShowBookInfo()
        end,
    }
end

function BookInfo:isSupported(file)
    return lfs.attributes(file, "mode") == "file"
end

function BookInfo:show(file, book_props)
    local kv_pairs = {}

    local directory, filename = util.splitFilePathName(file)
    local filename_without_suffix, filetype = util.splitFileNameSuffix(filename) -- luacheck: no unused
    local file_size = lfs.attributes(file, "size") or 0
    local size_f = util.getFriendlySize(file_size)
    local size_b = util.getFormattedSize(file_size)
    local size = string.format("%s (%s bytes)", size_f, size_b)
    table.insert(kv_pairs, { _("Filename:"), BD.filename(filename) })
    table.insert(kv_pairs, { _("Format:"), filetype:upper() })
    table.insert(kv_pairs, { _("Size:"), size })
    table.insert(kv_pairs, { _("Directory:"), BD.dirpath(filemanagerutil.abbreviate(directory)) })
    table.insert(kv_pairs, "----")

    -- book_props may be provided if caller already has them available
    -- but it may lack 'pages', that we may get from sidecar file
    if not book_props or not book_props.pages then<commit_msg>BookInfo: add File date, handle zip sub-suffix (.fb2.zip)<commit_after>--[[--
This module provides a way to display book information (filename and book metadata)
]]

local BD = require("ui/bidi")
local DocSettings = require("docsettings")
local DocumentRegistry = require("document/documentregistry")
local ImageViewer = require("ui/widget/imageviewer")
local InfoMessage = require("ui/widget/infomessage")
local InputContainer = require("ui/widget/container/inputcontainer")
local KeyValuePage = require("ui/widget/keyvaluepage")
local UIManager = require("ui/uimanager")
local filemanagerutil = require("apps/filemanager/filemanagerutil")
local lfs = require("libs/libkoreader-lfs")
local util = require("util")
local _ = require("gettext")

local BookInfo = InputContainer:extend{
    bookinfo_menu_title = _("Book information"),
}

function BookInfo:init()
    if self.ui then -- only for Reader menu
        self.ui.menu:registerToMainMenu(self)
    end
end

function BookInfo:addToMainMenu(menu_items)
    menu_items.book_info = {
        text = self.bookinfo_menu_title,
        callback = function()
            self:onShowBookInfo()
        end,
    }
end

function BookInfo:isSupported(file)
    return lfs.attributes(file, "mode") == "file"
end

function BookInfo:show(file, book_props)
    local kv_pairs = {}

    local directory, filename = util.splitFilePathName(file)
    local filename_without_suffix, filetype = util.splitFileNameSuffix(filename) -- luacheck: no unused
    if filetype:lower() == "zip" then
        local filename_without_sub_suffix, sub_filetype = util.splitFileNameSuffix(filename_without_suffix) -- luacheck: no unused
        sub_filetype = sub_filetype:lower()
        local supported_sub_filetypes = { "fb2", "htm", "html", "log", "md", "txt" }

        for __, t in ipairs(supported_sub_filetypes) do
            if sub_filetype == t then
                filetype = sub_filetype .. "." .. filetype
                break
            end
        end
    end
    local file_size = lfs.attributes(file, "size") or 0
    local file_modification = lfs.attributes(file, "modification") or 0
    local size_f = util.getFriendlySize(file_size)
    local size_b = util.getFormattedSize(file_size)
    local size = string.format("%s (%s bytes)", size_f, size_b)
    table.insert(kv_pairs, { _("Filename:"), BD.filename(filename) })
    table.insert(kv_pairs, { _("Format:"), filetype:upper() })
    table.insert(kv_pairs, { _("Size:"), size })
    table.insert(kv_pairs, { _("File date:"), os.date("%Y-%m-%d %H:%M:%S", file_modification) })
    table.insert(kv_pairs, { _("Directory:"), BD.dirpath(filemanagerutil.abbreviate(directory)) })
    table.insert(kv_pairs, "----")

    -- book_props may be provided if caller already has them available
    -- but it may lack 'pages', that we may get from sidecar file
    if not book_props or not book_props.pages then
{'fraction_non_alphanumeric': 0.12319842053307009, 'fraction_numerical': 0.0009871668311944718, 'mean_word_length': 4.127530364372469, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4662894', 'n_tokens_mistral': 1565, 'n_tokens_neox': 1537, 'n_words': 377}