publ-oth.lmt /size: 4341 b    last modification: 2024-01-16 10:22
1if not modules then modules = { } end modules ['publ-oth'] = {
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
9local P, S, C, Ct, Cmt, Carg = lpeg.P, lpeg.S, lpeg.C, lpeg.Ct, lpeg.Cmt, lpeg.Carg
10----- Cf, Cg = lpeg.Cf, lpeg.Cg
11local lpegmatch = lpeg.match
12
13local p_endofline = lpeg.patterns.newline
14
15local publications = publications or { loaders = { } }
16
17local loaders      = publications.loaders
18local getindex     = publications.getindex
19
20local function addfield(t,k,v,fields)
21    k = fields[k]
22    if k then
23        local tk = t[k]
24        if tk then
25            t[k] = tk .. " and " .. v
26        else
27            t[k] = v
28        end
29    end
30    return t
31end
32
33local function checkfield(_,_,t,categories,all)
34    local tag = t.tag
35    if tag then
36        local category = t.category
37        t.tag = nil
38        t.category = categories[category] or category
39        all[tag] = t
40    end
41    return true
42end
43
44-- endnotes --
45
46local fields = {
47    ["@"] = "tag",
48    ["0"] = "category",
49    ["A"] = "author",
50    ["E"] = "editor",
51    ["T"] = "title",
52    ["D"] = "year",
53    ["I"] = "publisher",
54}
55
56local categories = {
57    ["Journal Article"] = "article",
58}
59
60----- entry   = P("%") * Cg(C(1) * (S(" \t")^1) * C((1-p_endofline)^0) * Carg(1)) * p_endofline
61----- record  = Cf(Ct("") * (entry^1), addfield)
62local entry   = P("%") * (C(1) * (S(" \t")^1) * C((1-p_endofline)^0) * Carg(1)) * p_endofline
63local record  = Ct("") * (entry % addfield)^1
64local records = (Cmt(record * Carg(2) * Carg(3), checkfield) * P(1))^1
65
66function publications.endnotes_to_btx(data)
67    local all = { }
68    lpegmatch(records,data,1,fields,categories,all)
69    return all
70end
71
72function loaders.endnote(dataset,filename)
73    -- we could combine the next into checkfield but let's not create too messy code
74    local dataset, fullname = publications.resolvedname(dataset,filename)
75    if fullname then
76        loaders.lua(dataset,publications.endnotes_to_btx(io.loaddata(fullname) or ""))
77    end
78end
79
80-- refman --
81
82----- entry   = Cg(C((1-lpeg.S(" \t")-p_endofline)^1) * (S(" \t-")^1) * C((1-p_endofline)^0) * Carg(1)) * p_endofline
83----- record  = Cf(Ct("") * (entry^1), addfield)
84local entry   = (C((1-lpeg.S(" \t")-p_endofline)^1) * (S(" \t-")^1) * C((1-p_endofline)^0) * Carg(1)) * p_endofline
85local record  = Ct("") * (entry % addfield)^1
86local records = (Cmt(record * Carg(2) * Carg(3), checkfield) * P(1))^1
87
88local fields = {
89    ["SN"] = "tag",
90    ["TY"] = "category",
91    ["A1"] = "author",
92    ["E1"] = "editor",
93    ["T1"] = "title",
94    ["Y1"] = "year",
95    ["PB"] = "publisher",
96}
97
98local categories = {
99    ["JOUR"] = "article",
100}
101
102function publications.refman_to_btx(data)
103    local all = { }
104    lpegmatch(records,data,1,fields,categories,all)
105    return all
106end
107
108function loaders.refman(dataset,filename)
109    -- we could combine the next into checkfield but let's not create too messy code
110    local dataset, fullname = publications.resolvedname(dataset,filename)
111    if fullname then
112        loaders.lua(dataset,publications.refman_to_btx(io.loaddata(fullname) or ""))
113    end
114end
115
116-- test --
117
118-- local endnote = [[
119-- %0 Journal Article
120-- %T Scientific Visualization, Overviews, Methodologies, and Techniques
121-- %A Nielson, Gregory M
122-- %A Hagen, Hans
123-- %A Müller, Heinrich
124-- %@ 0818677776
125-- %D 1994
126-- %I IEEE Computer Society
127
128-- %0 Journal Article
129-- %T Scientific Visualization, Overviews, Methodologies, and Techniques
130-- %A Nielson, Gregory M
131-- %A Hagen, Hans
132-- %A Müller, Heinrich
133-- %@ 0818677775
134-- %D 1994
135-- %I IEEE Computer Society
136-- ]]
137
138-- local refman = [[
139-- TY  - JOUR
140-- T1  - Scientific Visualization, Overviews, Methodologies, and Techniques
141-- A1  - Nielson, Gregory M
142-- A1  - Hagen, Hans
143-- A1  - Müller, Heinrich
144-- SN  - 0818677776
145-- Y1  - 1994
146-- PB  - IEEE Computer Society
147
148-- TY  - JOUR
149-- T1  - Scientific Visualization, Overviews, Methodologies, and Techniques
150-- A1  - Nielson, Gregory M
151-- A1  - Hagen, Hans
152-- A1  - Müller, Heinrich
153-- SN  - 0818677775
154-- Y1  - 1994
155-- PB  - IEEE Computer Society
156-- ]]
157
158-- inspect(publications.endnotes_to_btx(endnote))
159-- inspect(publications.refman_to_btx(refman))
160