mtx-bibtex.lua /size: 4744 b    last modification: 2020-07-01 14:35
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 helpinfo = [[
10<?xml version="1.0"?>
11<application>
12 <metadata>
13  <entry name="name">mtx-bibtex</entry>
14  <entry name="detail">bibtex helpers</entry>
15  <entry name="version">1.00</entry>
16 </metadata>
17 <flags>
18  <category name="basic">
19   <subcategory>
20    <flag name="toxml"><short>convert bibtex database(s) to xml</short></flag>
21    <flag name="tolua"><short>convert bibtex database(s) to lua</short></flag>
22    <flag name="search"><short>seatch bibtex database(s)</short></flag>
23   </subcategory>
24  </category>
25 </flags>
26 <examples>
27  <category>
28   <title>Example</title>
29   <subcategory>
30    <example><command>mtxrun --script bibtex --tolua bibl-001.bib</command></example>
31    <example><command>mtxrun --script bibtex --tolua --simple bibl-001.bib</command></example>
32    <example><command>mtxrun --script bibtex --toxml bibl-001.bib bibl-002.bib bibl-003.bib biblio.xml</command></example>
33    <example><command>mtxrun --script bibtex --search --list --pattern=match(author:foo) bar.bib</command></example>
34   </subcategory>
35  </category>
36 </examples>
37</application>
38]]
39
40local application = logs.application {
41    name     = "mtx-bibtex",
42    banner   = "bibtex helpers",
43    helpinfo = helpinfo,
44}
45
46local report = application.report
47
48require("util-seq")
49require("publ-dat")
50require("publ-fnd")
51
52scripts        = scripts        or { }
53scripts.bibtex = scripts.bibtex or { }
54
55function scripts.bibtex.toxml(files)
56    local instance = bibtex.new()
57    local target   = "mtx-bibtex-output.xml"
58    for i=1,#files do
59        local filename = files[i]
60        local filetype = file.suffix(filename)
61        if filetype == "xml" then
62            target = filename
63        elseif filetype == "bib" then
64            bibtex.load { dataset = instance, filename = filename }
65        else
66            -- not supported
67        end
68    end
69    bibtex.converttoxml(instance,true)
70    instance.shortcuts = nil
71    instance.luadata   = nil
72    xml.save(instance.xmldata,target)
73end
74
75function scripts.bibtex.tolua(files)
76    local instance = bibtex.new()
77    local target = "mtx-bibtex-output.lua"
78    for i=1,#files do
79        local filename = files[i]
80        local filetype = file.suffix(filename)
81        if filetype == "lua" then
82            target = filename
83        elseif filetype == "bib" then
84            bibtex.load { dataset = instance, filename = filename }
85
86        else
87            -- not supported
88        end
89    end
90    instance.shortcuts = nil
91    instance.xmldata   = nil
92    bibtex.analyze(instance)
93    if environment.arguments.simple then
94        table.save(target,instance)
95    else
96        table.save(target,instance.luadata)
97    end
98end
99
100function scripts.bibtex.search(files,pattern,list)
101    if pattern then
102        local dataset = publications.datasets["whatever"]
103        for i=1,#files do
104            local filename = resolvers.findfile(files[i])
105            if filename and filename ~= "" then
106                publications.load { dataset = "whatever", filename = filename }
107            end
108        end
109        local found = publications.search(dataset,pattern)
110        local tags  = table.sortedkeys(found)
111        if #tags == 0 then
112            report("no match")
113        elseif list then
114            report("%s matches:",#tags)
115            local result  = { }
116            local luadata = dataset.luadata
117            for i=1,#tags do
118                local tag   = tags[i]
119                local entry = luadata[tag]
120                result[i] = {
121                    tag,
122                    entry.year,
123                    entry.author,
124                    entry.title,
125                }
126            end
127            utilities.formatters.formatcolumns(result)
128            logs.newline()
129            for i=1,#result do
130                logs.writer(result[i])
131            end
132            logs.newline()
133        else
134            report("%s matches: % t",#tags,tags)
135        end
136    end
137end
138
139if environment.arguments.search then
140    scripts.bibtex.search(environment.files,environment.arguments.pattern,environment.arguments.list)
141elseif environment.arguments.toxml then
142    scripts.bibtex.toxml(environment.files)
143elseif environment.arguments.tolua then
144    scripts.bibtex.tolua(environment.files)
145elseif environment.arguments.exporthelp then
146    application.export(environment.arguments.exporthelp,environment.files[1])
147else
148    application.help()
149end
150
151-- scripts.bibtex.toxml { "tugboat.bib" }
152-- scripts.bibtex.tolua { "tugboat.bib" }
153