summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/scripts/texdoc/texdoclib-util.tlu
blob: d14a212a1237dd58d54118c100a0aaaec6b31544 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
-- texdoclib-util.tlu: utility functions for texdoc
--
-- The TeX Live Team, GPLv3, see texdoclib.tlu for details

-- dependencies
local texdoc = {
    const = require('texdoclib-const'),
    config = require('texdoclib-config'),
}

-- shortcuts
local M = {}
local C = texdoc.const

-- change '/' to '\' on windows
-- Note: Internal representation of files always use forward slashes.
--       This function should be called only before displaying a path.
if os.type == 'windows' then
    function M.w32_path(path)
        return (string.gsub(path, '/', '\\'))
    end
else
    function M.w32_path(path)
        return path
    end
end

-- remove the last directory component of a path
if os.type == 'windows' then
    function M.path_parent(path)
        return string.match(path, '^(.*)[\\/]')
    end
else
    function M.path_parent(path)
        return string.match(path, '^(.*)/')
    end
end

-- generic log display function
local function log(label, msg, ...)
    io.stderr:write('texdoc ' .. label .. ': ' .. msg:format(...) .. '\n')
end

-- generic error display function (see the err_priority constant)
function M.err_print(lvl, msg, ...)
    -- be careful: maybe config item "verbosity_level" has not set yet
    local verbosity_level = texdoc.config.get_value('verbosity_level')
        or tonumber(C.def_verbosity)

    -- print if the priority is higher than current verbosity level
    if C.err_priority[lvl] <= verbosity_level then
        log(lvl, msg, ...)
    end
end
local err_print = M.err_print

do -- begin scope of active_debugs
local active_debugs

-- set active_debugs
local function set_active_debugs()
    local debug_list = texdoc.config.get_value('debug_list')

    if not debug_list then return end
    active_debugs = {}

    -- all debug options imply version info
    if debug_list[1] then
        active_debugs.version = true
    else
        return
    end

    -- if 'all' is the first keyword, just activate all categories
    if debug_list[1] == 'all' then
        for dbg in pairs(C.known_debugs) do
            active_debugs[dbg] = true end
        return
    end

    -- activate options from the list
    for _, dbg in ipairs(debug_list) do
        local deps = C.known_debugs[dbg]
        if deps then
            active_debugs[dbg] = true
            for _, d in ipairs(deps) do active_debugs[d] = true end
        else
            err_print('warning', 'Unknown debug category "' .. dbg .. '".')
        end
    end
end

-- generic debug function
function M.dbg_print(cat, msg, ...)
    -- make sure active_debugs is set
    if not active_debugs then set_active_debugs() end

    -- print message it belongs to an active category
    if active_debugs and active_debugs[cat] or cat == 'XXX' then
        log('debug-' .. cat, msg, ...)
    end
end

end -- end scope of active_debugs

-- if file is base .. '.' .. zip with zip in zipext_list, return: base, zip
-- otherwise, return: file, nil
function M.parse_zip(file)
    local zip
    for _, zip in ipairs(texdoc.config.get_value('zipext_list')) do
        local l = #zip + 1
        if string.sub(file, -l, -1) == '.' .. zip then
            return string.sub(file, 1, -l - 1), zip
        end
    end

    return file, nil
end

-- print a usage message
function M.print_usage()
    print(C.usage_msg)
end

return M

-- vim: ft=lua: