bibtex-to-sqlite 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/bin/bibtex-to-sqlite +114 -0
  3. metadata +48 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 322b583bfda84a493e4033518ab3a4c9ac1048bcec3267c3363a6050dcaf92aa
4
+ data.tar.gz: d055e51277e1a200cd70d862e8535e8699d6a25d2ea75ff11867946e8b14b317
5
+ SHA512:
6
+ metadata.gz: d06f7a7d31421c7aca7fd209c2e701fefd0f0e4b2a0e02d373a9f0de28be189711b94533e7fdb2083716638934c88a180902570837ca29309e443bbf4d2c5a88
7
+ data.tar.gz: 1463afe768f49ebbbf3290ef03744ddfbdad1440bf390178323d40c9474fc3a1cf3c993997b118e13a2182c9231a9e4436d62e8a45888d640af2d7d3a4abe6cc
@@ -0,0 +1,114 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bibtex'
4
+ require 'citeproc'
5
+ require 'csl/styles'
6
+ require 'optparse'
7
+ require 'set'
8
+ require 'sqlite3'
9
+
10
+ usage = """Usage: bibtex-to-sqlite [--style csl-style] file.bib [sql-query]"""
11
+ options={:style => 'gost-r-7-0-5-2008', :separator => '|'}
12
+ OptionParser.new do |opts|
13
+ opts.banner = usage
14
+ opts.on("-sSTYLE", "--style=STYLE", "Set bibliography format style") do |s|
15
+ options[:style] = s
16
+ end
17
+ opts.on("-SSEPARATOR", "--separator=SEPARATOR", "Query output separator") do |s|
18
+ options[:separator] = s
19
+ end
20
+ opts.on("-nNAME", "--database-name=NAME", "SQLITE3 database name") do |s|
21
+ options[:name] = s
22
+ end
23
+ end.parse!
24
+
25
+ inputs=ARGV
26
+ if ARGV.length != 1 && ARGV.length != 2
27
+ puts usage
28
+ exit 1
29
+ end
30
+
31
+ bib_file = ARGV[0]
32
+ if not options[:name]
33
+ options[:name] = File.basename(bib_file, '.bib') + '.sqlite3'
34
+ end
35
+ if ARGV.length == 2
36
+ query = ARGV[1]
37
+ else
38
+ query = ''
39
+ end
40
+ bibliography = BibTeX.open(bib_file).convert(:latex)
41
+ bibliography.replace_strings
42
+ cp = CiteProc::Processor.new style: options[:style], format: 'text'
43
+ cp.import bibliography.to_citeproc
44
+
45
+ fields = Set.new
46
+ bibliography.each do |entry|
47
+ fields.merge(entry.field_names.map{ |f| f.to_s.downcase })
48
+ end
49
+ fields.add('author')
50
+ fields.add('title')
51
+ fields.add('booktitle')
52
+ fields.add('journal')
53
+ fields.add('doi')
54
+
55
+ def escape(s)
56
+ s.gsub(/'/, "''")
57
+ end
58
+
59
+ sql = []
60
+ sql.push("""
61
+ CREATE TABLE IF NOT EXISTS bibtex (
62
+ key TEXT,
63
+ type TEXT,
64
+ gost TEXT,
65
+ %s
66
+ );""" % fields.map{ |f| "#{f} TEXT" }.sort.join(",\n"))
67
+
68
+ bibliography.each do |entry|
69
+ names = entry.field_names
70
+ tmp = entry[:author].map do |name|
71
+ name.display_order
72
+ end
73
+ entry.delete(:author)
74
+ entry.add('author', tmp.join(', '))
75
+ sql.push("""
76
+ INSERT INTO bibtex (
77
+ key,
78
+ type,
79
+ gost,
80
+ %s
81
+ )
82
+ VALUES (
83
+ '%s',
84
+ '%s',
85
+ '%s',
86
+ %s
87
+ );
88
+ """ % [names.map{ |f| f.to_s }.join(",\n"),
89
+ escape(entry.key),
90
+ escape(entry.type.to_s.downcase),
91
+ escape((cp.render :bibliography, id: entry.id)[0]),
92
+ names.map{ |i| "'" + escape(entry[i].strip) + "'" }.join(",\n")])
93
+ end
94
+
95
+ if query.strip == ''
96
+ puts sql.join('')
97
+ else
98
+ db = SQLite3::Database.new(options[:name])
99
+ for q in sql
100
+ db.execute(q)
101
+ end
102
+ db.execute(query) do |row|
103
+ puts row.map{|value|
104
+ if value
105
+ value
106
+ else
107
+ ''
108
+ end
109
+ }.join(options[:separator])
110
+ end
111
+ db.close
112
+ end
113
+
114
+ # vim:filetype=ruby
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bibtex-to-sqlite
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Ivan Gankevich
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-10-08 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: |-
14
+ Convert BibTeX files to SQLite databases,
15
+ run queries on them and produce results in CSV.
16
+ Especially useful for reports that require your publications
17
+ in table format.
18
+ email: i.gankevich@spbu.ru
19
+ executables:
20
+ - bibtex-to-sqlite
21
+ extensions: []
22
+ extra_rdoc_files: []
23
+ files:
24
+ - bin/bibtex-to-sqlite
25
+ homepage: https://rubygems.org/gems/bibtex-to-sqlite
26
+ licenses:
27
+ - GPL-3.0
28
+ metadata: {}
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubygems_version: 3.0.3
45
+ signing_key:
46
+ specification_version: 4
47
+ summary: Convert BibTeX files to SQLite databases
48
+ test_files: []