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