summaryrefslogtreecommitdiff
path: root/Master/texmf/scripts/texdoc/score.tlu
blob: 734f4b5b7d89ea0d02bf42f5a03db759978afed5 (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
-- scoring functions for texdoc
--[[ 
Copyright 2008, 2009 Manuel Pégourié-Gonnard
Distributed under the terms of the GNU GPL version 3 or later.
See texdoc.tlu for details.
--]]

local L = {}
load_env(L, {
    'export_symbols',
    'string', 'table',
    'ipairs',
    'config', 'parse_zip',
})

-- sort docfiles
function sort_docfiles(df)
    table.sort(df, docfile_order)
end

-- compare docfiles: (see search.tlu for structure)
-- 1. exact is better than non-exact,
-- 2. then extensions are ordered as in ext_list,
-- 3. then trees,
-- 4. then filenames lexicographically.
-- return true is a is better than b
function docfile_order (a, b)
    if a.exact and not b.exact then
        return true
    elseif b.exact and not a.exact then
        return false
    elseif a.tree < b.tree then
        return true
    elseif b.tree < a.tree then
        return false
    else
        a.ext_pos = a.ext_pos or ext_pos(a.name)
        b.ext_pos = b.ext_pos or ext_pos(b.name)
        if a.ext_pos < b.ext_pos then
            return true
        elseif a.ext_pos > b.ext_pos then
            return false
        else
            return (a.name < b.name)
        end
    end
end

-- returns the index of the most specific extension of file in ext_list,
-- or config.ext_list_max + 1
function ext_pos(file)
    -- remove zipext if applicable
    file = parse_zip(file)
    -- now find the extension
    local p, e, pos, ext
    for p, e in ipairs(config.ext_list) do
        if (e == '*') and (ext == nil) then
            pos, ext = p, e
        elseif (e == '') and not string.find(file, '.', 1, true) then
            pos, ext = p, e
        elseif string.sub(file, -string.len(e)-1) == '.'..e then
            if (ext == nil) or (ext == '*')
                or (string.len(e) > string.len(ext)) then
                pos, ext = p, e
            end
        end
    end
    return pos or (config.ext_list_max + 1)
end

-- export a few symbols
export_symbols(L, {
    'sort_docfiles',
})