publ-jrn.lua /size: 5868 b    last modification: 2020-07-01 14:35
1if not modules then modules = { } end modules ['publ-jrn'] = {
2    version   = 1.001,
3    comment   = "this module part of publication support",
4    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5    copyright = "PRAGMA ADE / ConTeXt Development Team",
6    license   = "see context related readme files"
7}
8
9-- http://jabref.sourceforge.net/journals/journal_abbreviations_general.txt
10--
11-- <full name> = <abbreviation> [;shortest unique abbreviation[;frequency]].
12
13-- require("char-utf")
14
15-- Abhandlungen aus dem Westfälischen Museum für Naturkunde = Abh. Westfäl. Mus. Nat.kd.
16-- Abhandlungen der Naturforschenden Gesellschaft in Zürich = Abh. Nat.forsch. Ges. Zür.
17-- Abhandlungen des Naturwissenschaftlichen Vereins zu Bremen = Abh. Nat.wiss. Ver. Bremen
18
19local context  = context
20local commands = commands
21
22local type = type
23local find = string.find
24local P, C, S, Cs, lpegmatch, lpegpatterns = lpeg.P, lpeg.C, lpeg.S, lpeg.Cs, lpeg.match, lpeg.patterns
25
26local report_journals  = logs.reporter("publications","journals")
27
28local publications     = publications
29local journals         = { }
30publications.journals  = journals
31
32local lowercase        = characters.lower
33
34local expansions       = { }
35local abbreviations    = { }
36local nofexpansions    = 0
37local nofabbreviations = 0
38
39local valid   = 1 - S([[ ."':;,-]])
40local pattern = Cs((valid^1 + P(1)/"")^1)
41
42local function simplify(name)
43    -- we have utf but it doesn't matter much if we lower the bytes
44    return name and lowercase(lpegmatch(pattern,name)) or name
45end
46
47local function add(expansion,abbreviation)
48    if expansion and abbreviation then
49        local se = simplify(expansion)
50        local sa = simplify(abbreviation)
51        if not expansions[sa] then
52            expansions[sa] = expansion
53            nofexpansions = nofexpansions + 1
54        end
55        if not abbreviations[se] then
56            abbreviations[se] = abbreviation
57            nofabbreviations = nofabbreviations + 1
58        end
59    end
60end
61
62-- [#%-] comment
63-- meaning = abbreviations [;.....]
64
65local whitespace = lpegpatterns.whitespace^0
66local assignment = whitespace * P("=") * whitespace
67local separator  = P(";")
68local newline    = lpegpatterns.newline
69local endofline  = lpegpatterns.space^0 * (newline + P(-1) + separator)
70local restofline = (1-newline)^0
71local splitter   = whitespace * C((1-assignment)^1) * assignment * C((1-endofline)^1) * restofline
72local comment    = S("#-%") * restofline
73local pattern    = (comment + splitter / add)^0
74
75function journals.load(filename)
76    if not filename then
77        return
78    end-- error
79    if file.suffix(filename,"txt") then
80        local data = io.loaddata(filename)
81        if type(data) ~= "string" then
82            return
83        elseif find(data,"=") then
84            -- expansion = abbreviation
85            lpegmatch(pattern,data)
86        end
87    elseif file.suffix(filename,"lua") then
88        local data = table.load(filename)
89        if type(data) ~= "table" then
90            return
91        else
92            local de = data.expansions
93            local da = data.abbreviations
94            if de and da then
95                -- { expansions = { a = e }, abbreviations = { e = a } }
96                if next(expansions) then
97                    table.merge(expansions,de)
98                else
99                    expansions = de
100                end
101                if next(abbreviations) then
102                    table.merge(abbreviations,da)
103                else
104                    abbreviations = da
105                end
106            elseif #data > 0 then
107                -- { expansion, abbreviation }, ... }
108                for i=1,#data do
109                    local d = d[i]
110                    add(d[1],d[2])
111                end
112            else
113                -- { expansion = abbreviation, ... }
114                for expansion, abbreviation in data do
115                    add(expansion,abbreviation)
116                end
117            end
118        end
119    end
120    report_journals("file %a loaded, %s expansions, %s abbreviations",filename,nofexpansions,nofabbreviations)
121end
122
123function journals.save(filename)
124    table.save(filename,{ expansions = expansions, abbreviations = abbreviations })
125end
126
127function journals.add(expansion,abbreviation)
128    add(expansion,abbreviation)
129end
130
131function journals.expanded(name)
132    local s = simplify(name)
133    return expansions[s] or expansions[simplify(abbreviations[s])] or name
134end
135
136function journals.abbreviated(name)
137    local s = simplify(name)
138    return abbreviations[s] or abbreviations[simplify(expansions[s])] or name
139end
140
141local implement = interfaces and interfaces.implement
142
143if implement then
144
145    implement {
146        name      = "btxloadjournallist",
147        arguments = "string",
148        actions   = journals.load
149    }
150
151    implement {
152        name      = "btxsavejournallist",
153        arguments = "string",
154        actions   = journals.save
155    }
156
157    implement {
158        name      = "btxaddjournal",
159        arguments = "2 strings",
160        actions   = { journals.add, context }
161    }
162
163    implement {
164        name      = "btxexpandedjournal",
165        arguments = "string",
166        actions   = { journals.expanded, context },
167    }
168
169    implement {
170        name      = "btxabbreviatedjournal",
171        arguments = "string",
172        actions   = { journals.abbreviated, context },
173    }
174
175end
176
177-- journals.load("e:/tmp/journals.txt")
178-- journals.save("e:/tmp/journals.lua")
179
180-- inspect(journals.expanded   ("Z. Ökol. Nat.schutz"))
181-- inspect(journals.abbreviated("Z.       Ökol. Nat. schutz"))
182
183if typesetters then
184    typesetters.manipulators.methods.expandedjournal    = journals.expanded
185    typesetters.manipulators.methods.abbreviatedjournal = journals.abbreviated
186end
187
188-- journals.load("t:/manuals/publications-mkiv/journals.txt")
189-- journals.save("t:/manuals/publications-mkiv/journals.lua")
190