summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/publ-usr.lua
blob: 6bb93ebeef5de3a9fcfdcc70b518aa12195e3e75 (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
if not modules then modules = { } end modules ['publ-usr'] = {
    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"
}

-- local chardata = characters.data

-- local str = [[
--     \startpublication[k=Berdnikov:TB21-2-129,t=article,a={{Berdnikov},{}},y=2000,n=2257,s=BHHJ00]
--     \artauthor[]{Alexander}[A.]{}{Berdnikov}
--     \artauthor[]{Hans}[H.]{}{Hagen}
--     \artauthor[]{Taco}[T.]{}{Hoekwater}
--     \artauthor[]{Bogus{\l}aw}[B.]{}{Jackowski}
--     \pubyear{2000}
--     \arttitle{{Even more MetaFun with \MP: A request for permission}}
--     \journal{TUGboat}
--     \issn{0896-3207}
--     \volume{21}
--     \issue{2}
--     \pages{129--130}
--     \month{6}
--     \stoppublication
-- ]]

local remapped = {
    artauthor = "author",
    arttitle  = "title",
}

local P, Cs, R, Cc, Carg = lpeg.P, lpeg.Cs, lpeg.R, lpeg.Cc, lpeg.Carg

local function register(target,key,a,b,c,d,e)
    key = remapped[key] or key
    if b and d and e then
        local s = nil
        if b ~= "" and b then
            s = s and s .. " " .. b or b
        end
        if d ~= "" and d then
            s = s and s .. " " .. d or d
        end
        if e ~= "" and e then
            s = s and s .. " " .. e or e
        end
        if a ~= "" and a then
            s = s and s .. " " .. a or a
        end
        local value = target[key]
        if s then
            if value then
                target[key] = value .. " and " .. s
            else
                target[key] = s
            end
        else
            if not value then
                target[key] = s
            end
        end
    else
        target[key] = b
    end
end

local leftbrace    = P("{")
local rightbrace   = P("}")
local leftbracket  = P("[")
local rightbracket = P("]")

local key          = P("\\") * Cs(R("az","AZ")^1) * lpeg.patterns.space^0
local mandate      = leftbrace * Cs(lpeg.patterns.balanced) * rightbrace + Cc(false)
local optional     = leftbracket * Cs((1-rightbracket)^0) * rightbracket + Cc(false)
local value        = optional^-1 * mandate^-1 * optional^-1 * mandate^-2

local pattern      = ((Carg(1) * key * value) / register + P(1))^0

function publications.addtexentry(dataset,settings,content)
    settings = utilities.parsers.settings_to_hash(settings)
    local data = {
        tag      = settings.tag      or settings.k or "no tag",
        category = settings.category or settings.t or "article",
    }
    lpeg.match(pattern,content,1,data) -- can set tag too
    dataset.userdata[data.tag] = data
    dataset.luadata[data.tag] = data
    publications.markasupdated(dataset)
    return data
end