mtx-bibtex.lua /size: 5985 b    last modification: 2025-02-21 11:03
1if not modules then modules = { } end modules ['mtx-bibtex'] = {
2    version   = 1.002,
3    comment   = "this script is part of publication support",
4    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5    copyright = "PRAGMA ADE",
6    license   = "see context related readme files"
7}
8
9local sortedhash, sortedkeys = table.sortedhash, table.sortedkeys
10
11local helpinfo = [[
12<?xml version="1.0"?>
13<application>
14 <metadata>
15  <entry name="name">mtx-bibtex</entry>
16  <entry name="detail">bibtex helpers</entry>
17  <entry name="version">1.00</entry>
18 </metadata>
19 <flags>
20  <category name="basic">
21   <subcategory>
22    <flag name="toxml"><short>convert bibtex database(s) to xml</short></flag>
23    <flag name="tolua"><short>convert bibtex database(s) to lua</short></flag>
24    <flag name="totxt"><short>convert bibtex database(s) to text (key = value)</short></flag>
25    <flag name="search"><short>seatch bibtex database(s)</short></flag>
26   </subcategory>
27  </category>
28 </flags>
29 <examples>
30  <category>
31   <title>Example</title>
32   <subcategory>
33    <example><command>mtxrun --script bibtex --tolua bibl-001.bib</command></example>
34    <example><command>mtxrun --script bibtex --tolua --simple bibl-001.bib</command></example>
35    <example><command>mtxrun --script bibtex --totxt bibl-003.bib</command></example>
36    <example><command>mtxrun --script bibtex --toxml bibl-001.bib bibl-002.bib bibl-003.bib biblio.xml</command></example>
37    <example><command>mtxrun --script bibtex --search --list --pattern=match(author:foo) bar.bib</command></example>
38   </subcategory>
39  </category>
40 </examples>
41</application>
42]]
43
44local application = logs.application {
45    name     = "mtx-bibtex",
46    banner   = "bibtex helpers",
47    helpinfo = helpinfo,
48}
49
50local report = application.report
51
52require("util-seq")
53require("publ-dat")
54require("publ-fnd")
55
56scripts        = scripts        or { }
57scripts.bibtex = scripts.bibtex or { }
58
59function scripts.bibtex.toxml(files)
60    local instance = publications.new()
61    local target   = "mtx-bibtex-output.xml"
62    for i=1,#files do
63        local filename = files[i]
64        local filetype = file.suffix(filename)
65        if filetype == "xml" then
66            target = filename
67        elseif filetype == "bib" then
68            publications.load { dataset = instance, filename = filename }
69        else
70            -- not supported
71        end
72    end
73    publications.converttoxml(instance,true)
74    instance.shortcuts = nil
75    instance.luadata   = nil
76    xml.save(instance.xmldata,target)
77end
78
79function scripts.bibtex.tolua(files)
80    local instance = publications.new()
81    local target = "mtx-bibtex-output.lua"
82    for i=1,#files do
83        local filename = files[i]
84        local filetype = file.suffix(filename)
85        if filetype == "lua" then
86            target = filename
87        elseif filetype == "bib" then
88            publications.load { dataset = instance, filename = filename }
89
90        else
91            -- not supported
92        end
93    end
94    instance.shortcuts = nil
95    instance.xmldata   = nil
96    publications.analyze(instance)
97    if environment.arguments.simple then
98        table.save(target,instance)
99    else
100        table.save(target,instance.luadata)
101    end
102end
103
104function scripts.bibtex.totxt(files)
105    local instance = publications.new()
106    local target   = "mtx-bibtex-output.txt"
107    for i=1,#files do
108        local filename = files[i]
109        local filetype = file.suffix(filename)
110        if filetype == "txt" then
111            target = filename
112        elseif filetype == "bib" then
113            publications.load { dataset = instance, filename = filename }
114        else
115            -- not supported
116        end
117    end
118    local luadata = instance.luadata
119    local result  = { }
120    local r       = 0
121    local line    = string.formatters["%s = %s"]
122    if luadata then
123        for k, v in sortedhash(luadata) do
124            for k, v in sortedhash(v) do
125                r = r + 1 ; result[r] = line(k,v)
126            end
127            r = r + 1 ; result[r] = ""
128        end
129    end
130    io.savedata(target,table.concat(result,"\n"))
131end
132
133
134function scripts.bibtex.search(files,pattern,list)
135    if pattern then
136        local dataset = publications.datasets["whatever"]
137        for i=1,#files do
138            local filename = resolvers.findfile(files[i])
139            if filename and filename ~= "" then
140                publications.load { dataset = "whatever", filename = filename }
141            end
142        end
143        local found = publications.search(dataset,pattern)
144        local tags  = sortedkeys(found)
145        if #tags == 0 then
146            report("no match")
147        elseif list then
148            report("%s matches:",#tags)
149            local result  = { }
150            local luadata = dataset.luadata
151            for i=1,#tags do
152                local tag   = tags[i]
153                local entry = luadata[tag]
154                result[i] = {
155                    tag,
156                    entry.year,
157                    entry.author,
158                    entry.title,
159                }
160            end
161            utilities.formatters.formatcolumns(result)
162            logs.newline()
163            for i=1,#result do
164                logs.writer(result[i])
165            end
166            logs.newline()
167        else
168            report("%s matches: % t",#tags,tags)
169        end
170    end
171end
172
173if environment.arguments.search then
174    scripts.bibtex.search(environment.files,environment.arguments.pattern,environment.arguments.list)
175elseif environment.arguments.toxml then
176    scripts.bibtex.toxml(environment.files)
177elseif environment.arguments.tolua then
178    scripts.bibtex.tolua(environment.files)
179elseif environment.arguments.totxt then
180    scripts.bibtex.totxt(environment.files)
181elseif environment.arguments.exporthelp then
182    application.export(environment.arguments.exporthelp,environment.files[1])
183else
184    application.help()
185end
186
187-- scripts.bibtex.toxml { "tugboat.bib" }
188-- scripts.bibtex.tolua { "tugboat.bib" }
189