summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/publ-jrn.lua
blob: 0cc41862f5301b2470113fe5e072d9f2cef3ae6f (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
if not modules then modules = { } end modules ['publ-jrn'] = {
    version   = 1.001,
    comment   = "this module part of publication support",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

-- http://jabref.sourceforge.net/journals/journal_abbreviations_general.txt
--
-- <full name> = <abbreviation> [;shortest unique abbreviation[;frequency]].

-- require("char-utf")

-- Abhandlungen aus dem Westfälischen Museum für Naturkunde = Abh. Westfäl. Mus. Nat.kd.
-- Abhandlungen der Naturforschenden Gesellschaft in Zürich = Abh. Nat.forsch. Ges. Zür.
-- Abhandlungen des Naturwissenschaftlichen Vereins zu Bremen = Abh. Nat.wiss. Ver. Bremen

local context  = context
local commands = commands

local type = type
local find = string.find
local P, C, S, Cs, lpegmatch, lpegpatterns = lpeg.P, lpeg.C, lpeg.S, lpeg.Cs, lpeg.match, lpeg.patterns

local report_journals  = logs.reporter("publications","journals")

local publications     = publications
local journals         = { }
publications.journals  = journals

local lowercase        = characters.lower

local expansions       = { }
local abbreviations    = { }
local nofexpansions    = 0
local nofabbreviations = 0

local valid   = 1 - S([[ ."':;,-]])
local pattern = Cs((valid^1 + P(1)/"")^1)

local function simplify(name)
    -- we have utf but it doesn't matter much if we lower the bytes
    return name and lowercase(lpegmatch(pattern,name)) or name
end

local function add(expansion,abbreviation)
    if expansion and abbreviation then
        local se = simplify(expansion)
        local sa = simplify(abbreviation)
        if not expansions[sa] then
            expansions[sa] = expansion
            nofexpansions = nofexpansions + 1
        end
        if not abbreviations[se] then
            abbreviations[se] = abbreviation
            nofabbreviations = nofabbreviations + 1
        end
    end
end

-- [#%-] comment
-- meaning = abbreviations [;.....]

local whitespace = lpegpatterns.whitespace^0
local assignment = whitespace * P("=") * whitespace
local separator  = P(";")
local newline    = lpegpatterns.newline
local endofline  = lpegpatterns.space^0 * (newline + P(-1) + separator)
local restofline = (1-newline)^0
local splitter   = whitespace * C((1-assignment)^1) * assignment * C((1-endofline)^1) * restofline
local comment    = S("#-%") * restofline
local pattern    = (comment + splitter / add)^0

function journals.load(filename)
    if not filename then
        return
    end-- error
    if file.suffix(filename,"txt") then
        local data = io.loaddata(filename)
        if type(data) ~= "string" then
            return
        elseif find(data,"=") then
            -- expansion = abbreviation
            lpegmatch(pattern,data)
        end
    elseif file.suffix(filename,"lua") then
        local data = table.load(filename)
        if type(data) ~= "table" then
            return
        else
            local de = data.expansions
            local da = data.abbreviations
            if de and da then
                -- { expansions = { a = e }, abbreviations = { e = a } }
                if next(expansions) then
                    table.merge(expansions,de)
                else
                    expansions = de
                end
                if next(abbreviations) then
                    table.merge(abbreviations,da)
                else
                    abbreviations = da
                end
            elseif #data > 0 then
                -- { expansion, abbreviation }, ... }
                for i=1,#data do
                    local d = d[i]
                    add(d[1],d[2])
                end
            else
                -- { expansion = abbreviation, ... }
                for expansion, abbreviation in data do
                    add(expansion,abbreviation)
                end
            end
        end
    end
    report_journals("file %a loaded, %s expansions, %s abbreviations",filename,nofexpansions,nofabbreviations)
end

function journals.save(filename)
    table.save(filename,{ expansions = expansions, abbreviations = abbreviations })
end

function journals.add(expansion,abbreviation)
    add(expansion,abbreviation)
end

function journals.expanded(name)
    local s = simplify(name)
    return expansions[s] or expansions[simplify(abbreviations[s])] or name
end

function journals.abbreviated(name)
    local s = simplify(name)
    return abbreviations[s] or abbreviations[simplify(expansions[s])] or name
end

local implement = interfaces and interfaces.implement

if implement then

    implement {
        name      = "btxloadjournallist",
        arguments = "string",
        actions   = journals.load
    }

    implement {
        name      = "btxsavejournallist",
        arguments = "string",
        actions   = journals.save
    }

    implement {
        name      = "btxaddjournal",
        arguments = { "string", "string" },
        actions   = { journals.add, context }
    }

    implement {
        name      = "btxexpandedjournal",
        arguments = "string",
        actions   = { journals.expanded, context },
    }

    implement {
        name      = "btxabbreviatedjournal",
        arguments = "string",
        actions   = { journals.abbreviated, context },
    }

end

-- journals.load("e:/tmp/journals.txt")
-- journals.save("e:/tmp/journals.lua")

-- inspect(journals.expanded   ("Z. Ökol. Nat.schutz"))
-- inspect(journals.abbreviated("Z.       Ökol. Nat. schutz"))

if typesetters then
    typesetters.manipulators.methods.expandedjournal    = journals.expanded
    typesetters.manipulators.methods.abbreviatedjournal = journals.abbreviated
end

-- journals.load("t:/manuals/publications-mkiv/journals.txt")
-- journals.save("t:/manuals/publications-mkiv/journals.lua")