summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/scripts/context/lua/mtx-bibtex.lua
blob: d30cc2f6acf35e8659188c2abcb0afa163353b6b (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
if not modules then modules = { } end modules ['mtx-bibtex'] = {
    version   = 1.002,
    comment   = "this script is part of publication support",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE",
    license   = "see context related readme files"
}

local helpinfo = [[
<?xml version="1.0"?>
<application>
 <metadata>
  <entry name="name">mtx-bibtex</entry>
  <entry name="detail">bibtex helpers</entry>
  <entry name="version">1.00</entry>
 </metadata>
 <flags>
  <category name="basic">
   <subcategory>
    <flag name="toxml"><short>convert bibtex database(s) to xml</short></flag>
    <flag name="tolua"><short>convert bibtex database(s) to lua</short></flag>
    <flag name="search"><short>seatch bibtex database(s)</short></flag>
   </subcategory>
  </category>
 </flags>
 <examples>
  <category>
   <title>Example</title>
   <subcategory>
    <example><command>mtxrun --script bibtex --tolua bibl-001.bib</command></example>
    <example><command>mtxrun --script bibtex --tolua --simple bibl-001.bib</command></example>
    <example><command>mtxrun --script bibtex --toxml bibl-001.bib bibl-002.bib bibl-003.bib biblio.xml</command></example>
    <example><command>mtxrun --script bibtex --search --list --pattern=match(author:foo) bar.bib</command></example>
   </subcategory>
  </category>
 </examples>
</application>
]]

local application = logs.application {
    name     = "mtx-bibtex",
    banner   = "bibtex helpers",
    helpinfo = helpinfo,
}

local report = application.report

require("util-seq")
require("publ-dat")
require("publ-fnd")

scripts        = scripts        or { }
scripts.bibtex = scripts.bibtex or { }

function scripts.bibtex.toxml(files)
    local instance = bibtex.new()
    local target   = "mtx-bibtex-output.xml"
    for i=1,#files do
        local filename = files[i]
        local filetype = file.suffix(filename)
        if filetype == "xml" then
            target = filename
        elseif filetype == "bib" then
            bibtex.load { dataset = instance, filename = filename }
        else
            -- not supported
        end
    end
    bibtex.converttoxml(instance,true)
    instance.shortcuts = nil
    instance.luadata   = nil
    xml.save(instance.xmldata,target)
end

function scripts.bibtex.tolua(files)
    local instance = bibtex.new()
    local target = "mtx-bibtex-output.lua"
    for i=1,#files do
        local filename = files[i]
        local filetype = file.suffix(filename)
        if filetype == "lua" then
            target = filename
        elseif filetype == "bib" then
            bibtex.load { dataset = instance, filename = filename }

        else
            -- not supported
        end
    end
    instance.shortcuts = nil
    instance.xmldata   = nil
    bibtex.analyze(instance)
    if environment.arguments.simple then
        table.save(target,instance)
    else
        table.save(target,instance.luadata)
    end
end

function scripts.bibtex.search(files,pattern,list)
    if pattern then
        local dataset = publications.datasets["whatever"]
        for i=1,#files do
            local filename = resolvers.findfile(files[i])
            if filename and filename ~= "" then
                publications.load { dataset = "whatever", filename = filename }
            end
        end
        local found = publications.search(dataset,pattern)
        local tags  = table.sortedkeys(found)
        if #tags == 0 then
            report("no match")
        elseif list then
            report("%s matches:",#tags)
            local result  = { }
            local luadata = dataset.luadata
            for i=1,#tags do
                local tag   = tags[i]
                local entry = luadata[tag]
                result[i] = {
                    tag,
                    entry.year,
                    entry.author,
                    entry.title,
                }
            end
            utilities.formatters.formatcolumns(result)
            logs.newline()
            for i=1,#result do
                logs.writer(result[i])
            end
            logs.newline()
        else
            report("%s matches: % t",#tags,tags)
        end
    end
end

if environment.arguments.search then
    scripts.bibtex.search(environment.files,environment.arguments.pattern,environment.arguments.list)
elseif environment.arguments.toxml then
    scripts.bibtex.toxml(environment.files)
elseif environment.arguments.tolua then
    scripts.bibtex.tolua(environment.files)
elseif environment.arguments.exporthelp then
    application.export(environment.arguments.exporthelp,environment.files[1])
else
    application.help()
end

-- scripts.bibtex.toxml { "tugboat.bib" }
-- scripts.bibtex.tolua { "tugboat.bib" }