publ-usr.lua /size: 4291 b    last modification: 2020-07-01 14:35
1if not modules then modules = { } end modules ['publ-usr'] = {
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, Cs, R, Cc, C, Carg = lpeg.P, lpeg.Cs, lpeg.R, lpeg.Cc, lpeg.C, lpeg.Carg
10local lpegmatch = lpeg.match
11local settings_to_hash = utilities.parsers.settings_to_hash
12
13local publications = publications
14local datasets     = publications.datasets
15
16local report       = logs.reporter("publications")
17local trace        = false  trackers.register("publications",function(v) trace = v end)
18
19-- local str = [[
20--     \startpublication[k=Berdnikov:TB21-2-129,t=article,a={{Berdnikov},{}},y=2000,n=2257,s=BHHJ00]
21--     \artauthor[]{Alexander}[A.]{}{Berdnikov}
22--     \artauthor[]{Hans}[H.]{}{Hagen}
23--     \artauthor[]{Taco}[T.]{}{Hoekwater}
24--     \artauthor[]{Bogus{\l}aw}[B.]{}{Jackowski}
25--     \pubyear{2000}
26--     \arttitle{{Even more MetaFun with \MP: A request for permission}}
27--     \journal{TUGboat}
28--     \issn{0896-3207}
29--     \volume{21}
30--     \issue{2}
31--     \pages{129--130}
32--     \month{6}
33--     \stoppublication
34-- ]]
35
36local lists = {
37    author    = true,
38    editor    = true,
39 -- artauthor = true,
40 -- arteditor = true,
41}
42
43local function registervalue(target,key,value)
44    target[key] = value
45end
46
47-- Instead of being generic we just hardcode the old stuff:
48
49local function registerauthor(target,key,juniors,firstnames,initials,vons,surnames)
50    local value = target[key]
51    target[key]= ((value and value .. " and {") or "{") ..
52        vons       .. "},{" ..
53        surnames   .. "},{" ..
54        juniors    .. "},{" ..
55        firstnames .. "},{" ..
56        initials   .. "}"
57end
58
59local leftbrace    = P("{")
60local rightbrace   = P("}")
61local leftbracket  = P("[")
62local rightbracket = P("]")
63local backslash    = P("\\")
64local letter       = R("az","AZ")
65
66local skipspaces   = lpeg.patterns.whitespace^0
67local key          = Cs(letter^1)
68local value        = leftbrace   * Cs(lpeg.patterns.balanced) * rightbrace
69local optional     = leftbracket * Cs((1-rightbracket)^0)     * rightbracket
70
71local authorkey    = (P("artauthor") + P("author")) / "author"
72                   + (P("arteditor") + P("editor")) / "editor"
73local authorvalue  = (optional + Cc("{}")) * skipspaces -- [juniors]
74                   * (value    + Cc("{}")) * skipspaces -- {firstnames}
75                   * (optional + Cc("{}")) * skipspaces -- [initials]
76                   * (value    + Cc("{}")) * skipspaces -- {vons}
77                   * (value    + Cc("{}")) * skipspaces -- {surnames}
78
79local keyvalue     = Carg(1) * authorkey * skipspaces * authorvalue / registerauthor
80                   + Carg(1) * key       * skipspaces * value       / registervalue
81
82local pattern      = (backslash * keyvalue + P(1))^0
83
84local function addtexentry(dataset,settings,content)
85    local current  = datasets[dataset]
86    local settings = settings_to_hash(settings)
87    local data = {
88        tag      = settings.tag      or settings.k or "no tag",
89        category = settings.category or settings.t or "article",
90    }
91    lpegmatch(pattern,content,1,data) -- can set tag too
92    local tag   = data.tag
93    local index = publications.getindex(dataset,current.luadata,tag)
94    current.ordered[index] = data
95    current.luadata[tag]   = data
96    current.userdata[tag]  = data
97    current.details[tag]   = nil
98    return data
99end
100
101local pattern = ( Carg(1)
102      * P("\\startpublication")
103      * skipspaces
104      * optional
105      * C((1 - P("\\stoppublication"))^1)
106      * P("\\stoppublication") / addtexentry
107      + P("%") * (1-lpeg.patterns.newline)^0
108      + P(1)
109)^0
110
111function publications.loaders.bbl(dataset,filename)
112    local dataset, fullname = publications.resolvedname(dataset,filename)
113    if not fullname then
114        return
115    end
116    local data = io.loaddata(filename) or ""
117    if data == "" then
118        report("empty file %a, nothing loaded",fullname)
119        return
120    end
121    if trace then
122        report("loading file %a",fullname)
123    end
124    lpegmatch(pattern,data,1,dataset)
125end
126
127publications.addtexentry = addtexentry
128commands.addbtxentry     = addtexentry
129