bibout 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b5ace274726271cc7dad80e58d5f017244081497
4
+ data.tar.gz: 473d2b2666ad65b7ac322e94890cb77cb19661e9
5
+ SHA512:
6
+ metadata.gz: a286408043f4143eea87aafc3cddb435ea6a84679e571fc141e88517a126d8e756aa91c88c5474bad6cb42ba30ad71b33a855191a498dd25364a4229d45a9586
7
+ data.tar.gz: 7cea2697ceefa319cb502941893e75117d2a53454e35445037b212bd50e71ad7a0f4fedc8dd890fd3a423625ddf3284d6af3008df20024f345ba6e31025ffa4b
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.libs << 'test'
5
+ end
6
+
7
+ desc "Run tests"
8
+ task :default => :test
9
+
10
+ require 'rdoc/task'
11
+
12
+ RDoc::Task.new do |rdoc|
13
+ rdoc.main = "README.rdoc"
14
+ rdoc.rdoc_files.include("README.rdoc", "lib/bibout.rb", "lib/bibout/bibtex.rb", "lib/bibout/erb_binding.rb")
15
+ rdoc.generator = 'bootstrap'
16
+ rdoc.rdoc_dir = 'gh-pages/rdoc'
17
+ end
18
+
data/bin/bibout ADDED
@@ -0,0 +1,63 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Author: Charles Sutton <csutton@inf.ed.ac.uk>
4
+ # Created: 2008-03-03.
5
+ # Copyright (c) 2008. All rights reserved.
6
+
7
+ $CURRENT_DIR = "FOOBAR"
8
+
9
+ require 'bibout'
10
+ require 'optparse'
11
+
12
+ options = { :template => nil, :output_file => nil }
13
+
14
+ opts = OptionParser.new
15
+
16
+ opts.banner = "Usage: bibout [options] <bibfile>"
17
+ opts.on("-tMANDATORY", "--template MANDATORY", "Name of template file") do |tmpl|
18
+ options[:template] = tmpl
19
+ end
20
+ opts.on("-oFILENAME", "--output FILE_NAME", "Name of output file (if unspecified, uses stdout)") do |ofile|
21
+ options[:output_file] = ofile
22
+ end
23
+ opts.on("-dDIRECTORY", "--directory DIR", "Base directory for embed() calls in template (default: directory of template)") do |dir|
24
+ options[:dir] = dir
25
+ end
26
+ opts.on("-r", "--require module1,...,moduleN", Array, "Ruby modules to require before running templates") do |r|
27
+ options[:require_list] = r
28
+ end
29
+ opts.on_tail("-h", "--help", "Show this message") do
30
+ puts opts
31
+ exit
32
+ end
33
+ opts.parse!
34
+
35
+ bibfile = ARGV[0]
36
+
37
+ if options[:template].nil?
38
+ puts "Error: Missing template file"
39
+ puts opts
40
+ abort
41
+ end
42
+ if bibfile.nil?
43
+ puts "Error: Missing BibTeX file"
44
+ puts opts
45
+ abort
46
+ end
47
+ if not options[:require_list].nil?
48
+ options[:require_list].each { |x| require x }
49
+ end
50
+
51
+ #########################################################################
52
+
53
+ bib = BibTeX.open(bibfile, :filter => :latex)
54
+ bib.replace # substitute all @string mentions
55
+
56
+ result = BibOut.new(options[:dir]).process_file(bib, options[:template])
57
+
58
+ if options[:output_file].nil?
59
+ print (result)
60
+ else
61
+ File.open(options[:output_file], "w") { |outf| outf << result }
62
+ end
63
+
@@ -0,0 +1,109 @@
1
+ # -*- coding: iso-8859-1 -*-
2
+ # -*- ruby-mode -*-
3
+ # Extensions to 'bibtex-ruby' library that adds some convenience methods.
4
+
5
+ # Author:: Charles Sutton (mailto:csutton@inf.ed.ac.uk)
6
+ # Copyright:: Copyright (c) 2013 Charles Sutton
7
+ # License:: MIT
8
+
9
+
10
+ require 'bibtex'
11
+
12
+ module BibTeX
13
+
14
+ # A list of Symbols naming fields that are commonly used in BibTeX entries.
15
+ $STANDARD_FIELDS = [
16
+ :address,
17
+ :annote,
18
+ :author,
19
+ :booktitle,
20
+ :chapter,
21
+ :crossref,
22
+ :edition,
23
+ :editor,
24
+ :howpublished,
25
+ :institution,
26
+ :journal,
27
+ :month,
28
+ :number,
29
+ :organization,
30
+ :pages,
31
+ :publisher,
32
+ :school,
33
+ :series,
34
+ :title,
35
+ :type,
36
+ :volume,
37
+ :year
38
+ ].freeze
39
+
40
+
41
+ # This class represents a bibliography, usually a single BibTeX file.
42
+ #
43
+ # This is the same class as {BibTeX::Bibliography}[http://rubydoc.info/gems/bibtex-ruby/BibTeX/Bibliography] from bibtex-ruby.
44
+ # See those class docs for more information.
45
+ class Bibliography
46
+
47
+ # Returns a list of all values of the specified field type, across all entries in the bibliography
48
+ #
49
+ # This generalizes the names method to arbitrary field types
50
+ #
51
+ # Ex: all_values(:year)
52
+ # --> list of all years that appear in the bibliography
53
+ def all_values(field)
54
+ return map { |e| e[field] }.flatten.compact.map { |v| v.to_s }.sort.uniq
55
+ end
56
+
57
+ # Returns a list of all of the Entries in this bibliography,
58
+ # sorted by calling the given block. Fixes a problem with the
59
+ # sort_by method in the bibtex-ruby version
60
+ # of this class.
61
+ def sort_by(*arguments, &block)
62
+ data.sort_by(*arguments, &block)
63
+ end
64
+
65
+ # Returns a list of all of the Entries in this bibliography,
66
+ # sorted by in the specified order. Fixes a problem with bibtex-ruby.
67
+ def sort(*arguments, &block)
68
+ data.sort(*arguments, &block)
69
+ end
70
+
71
+ end
72
+
73
+ # This class represents a single entry in a bibliography.
74
+ # This is the same class as {BibTeX::Entry}[http://rubydoc.info/gems/bibtex-ruby/BibTeX/Entry] from bibtex-ruby.
75
+ # See those class docs for more information.
76
+ class Entry
77
+
78
+ # Returns a copy of this entry that has all non-standard BibTeX fields removed.
79
+ def minimize
80
+ result = clone
81
+ fields.each do |k,v|
82
+ if not $STANDARD_FIELDS.include? k
83
+ result.delete k
84
+ end
85
+ end
86
+ result
87
+ end
88
+
89
+ end
90
+
91
+
92
+ # This class represents a list of names.
93
+ # This is the same class as {BibTeX::Names}[http://rubydoc.info/gems/bibtex-ruby/BibTeX/Names] from bibtex-ruby.
94
+ # See those class docs for more information.
95
+ class Names
96
+
97
+ # Returns a string containing the list of names in first-last order,
98
+ # correctly delimited by commas and and
99
+ def pretty(options={})
100
+ names = map { |n| n.display_order(options) }
101
+ return to_s if names.nil? or names.length < 1
102
+ return names[0].to_s if names.length == 1
103
+ names[0..-2].join(", ") + " and " + names[-1]
104
+ end
105
+
106
+ end
107
+
108
+
109
+ end
@@ -0,0 +1,63 @@
1
+ # Contains variables and methods that are useful for bibout templates.
2
+ #
3
+ # When a template is formatted using #BibOut.result or ErbBinding#embed,
4
+ # it is evaluated within the context of an ErbBinding object,
5
+ # so all instance varaibles and methods are available within
6
+ # code blocks of the template.
7
+ class ErbBinding < OpenStruct
8
+
9
+ # Name of the base directory for finding any sub-templates using #embed
10
+ attr_accessor :root_dir
11
+
12
+ # The current bibliography being processed. Of type #BibTeX::Bibliography
13
+ attr_accessor :bib
14
+
15
+ # Creates an Erb binding that can be used to process a template.
16
+ # Params:
17
+ # +bib+:: Bibliography object to run the template on
18
+ # +root_dir+:: Name of the directory to run the template in. This is used
19
+ # as the "current directory" if sub-templates are called using #embed
20
+ # +hash+:: Contains any keyword arguments that should be passed to the template
21
+ def initialize(bib, root_dir, hash=nil)
22
+ super(hash)
23
+ @bib = bib
24
+ @root_dir = root_dir
25
+ end
26
+
27
+ # Processes a given string as a bibout template
28
+ # Params:
29
+ # +tmpl+:: String containing text of template to process
30
+ # +fname+:: Name that should be used to identify template in error messages
31
+ # +hash+:: Contains any keyword arguments that should be passed to the template
32
+ def process_string(tmpl, fname='(bibout)', hash=nil)
33
+ bind = ErbBinding.new(@bib, @root_dir, hash).get_binding()
34
+ erb = ERB.new(tmpl)
35
+ erb.filename = fname
36
+ erb.result(bind)
37
+ end
38
+
39
+ # Processes a file as a bibout template, and returns the result.
40
+ #
41
+ # The file is assumed to reside in the directory #@root_dir
42
+ # Keyword options can be passed to the template.
43
+ # The sub-template will be passed in the same bibliography as this template.
44
+ #
45
+ # Params:
46
+ # +fname+:: Name of file containing template to process
47
+ # +hash+:: Contains any keyword arguments that should be passed to the template
48
+ def embed(fname, hash=nil)
49
+ if not @root_dir.nil?
50
+ fname = File.join(@root_dir, fname)
51
+ end
52
+ File.open(fname) do |f|
53
+ tmpl = f.read()
54
+ process_string(tmpl, fname, hash)
55
+ end
56
+ end
57
+
58
+ # Should not be called by external users
59
+ def get_binding
60
+ return binding()
61
+ end
62
+
63
+ end
data/lib/bibout.rb ADDED
@@ -0,0 +1,40 @@
1
+ # Author: Charles Sutton <csutton@inf.ed.ac.uk>
2
+ # Created: 2008-03-03.
3
+ # Copyright (c) 2008. All rights reserved.
4
+
5
+ require 'erb'
6
+ require 'ostruct'
7
+ require 'bibout/bibtex'
8
+ require 'bibout/erb_binding'
9
+
10
+ # Toplevel class for processing templates.
11
+ # Delegates everything to ErbBinding
12
+ class BibOut
13
+
14
+ # Name of a directory containing template files.
15
+ # This is used as the base directory when one template is embedded within another.
16
+ attr_accessor :root_dir
17
+
18
+ def initialize(root_dir=nil)
19
+ @root_dir = root_dir
20
+ end
21
+
22
+ # Processes a file containing a bibout template.
23
+ # Params:
24
+ # +bib+:: Name of BibTeX file
25
+ # +filename+:: Name of BibOut template file
26
+ def process_file(bib, filename)
27
+ ErbBinding.new(bib, @root_dir).embed(filename)
28
+ end
29
+
30
+ # Processes a string containing a bibout template.
31
+ # Params:
32
+ # +bib+:: Name of BibTeX file
33
+ # +filename+:: String containing text of BibOut template
34
+ def process_string(bib, string)
35
+ ErbBinding.new(bib, @root_dir).process_string(string)
36
+ end
37
+
38
+ end
39
+
40
+
data/test/test_bib.rb ADDED
@@ -0,0 +1,77 @@
1
+ require 'test/unit'
2
+ require 'bibout'
3
+
4
+ # Tests the extensions that this project makes to
5
+ # BibTeX::Bibliography and friends.
6
+ class BibliographyTest < Test::Unit::TestCase
7
+
8
+ def setup
9
+ current_dir = File.expand_path File.dirname(__FILE__)
10
+ @data_dir = File.join(current_dir, "data")
11
+ @bib = BibTeX.open(File.join(@data_dir, "test.bib"))
12
+ @bib_sutton = BibTeX.open(File.join(@data_dir, "sutton.bib"), :format => :latex)
13
+ end
14
+
15
+ def test_years
16
+ years = @bib.all_values(:year).sort
17
+ assert_equal ["1951", "1953", "1979"], years
18
+ end
19
+
20
+ def test_all_values2
21
+ vols = @bib.all_values(:volume).sort
22
+ assert_equal ["21", "22"], vols
23
+ end
24
+
25
+ def test_names_pretty
26
+ e = @bib["robbins-monro"]
27
+ name_str = e.author.pretty
28
+ assert_equal "H. Robbins and S. Monro", name_str
29
+
30
+ e = @bib["metropolis"]
31
+ name_str = e.author.pretty
32
+ assert_equal "N. Metropolis, A. Rosenbluth, M. Rosenbluth, A. Teller and E. Teller", name_str
33
+ end
34
+
35
+ def test_names_single_pretty
36
+ e = @bib_sutton["sutton:thesis"]
37
+ assert_equal "Charles Sutton", e.author.pretty
38
+ end
39
+
40
+ $FUNNY_BIB = <<END
41
+ @book{garey:johnson,
42
+ Address = {New York, NY, USA},
43
+ Author = {Garey, Michael R. and Johnson, David S.},
44
+ Publisher = {W. H. Freeman \& Co.},
45
+ Title = {Computers and Intractability: A Guide to the Theory of NP-Completeness},
46
+ Tags = {Classic},
47
+ InternalCruft = xyzzy,
48
+ Year = {1979}}
49
+ END
50
+
51
+ $CLEAN_BIB = <<END
52
+ @book{garey:johnson,
53
+ address = {New York, NY, USA},
54
+ author = {Garey, Michael R. and Johnson, David S.},
55
+ publisher = {W. H. Freeman & Co.},
56
+ title = {Computers and Intractability: A Guide to the Theory of NP-Completeness},
57
+ year = {1979}
58
+ }
59
+ END
60
+
61
+ def test_minimize
62
+ entry = BibTeX.parse($FUNNY_BIB)[0]
63
+ entry2 = entry.minimize
64
+ assert_equal $CLEAN_BIB, entry2.to_s
65
+ end
66
+
67
+ def test_bib_sort_by
68
+ entries = @bib.sort_by { |e| e.year }
69
+ assert_equal ["robbins-monro", "metropolis", "garey:johnson"], entries.map { |e| e.key }
70
+ end
71
+
72
+ def test_bib_sort
73
+ entries = @bib.sort { |e1,e2| e1.year <=> e2.year }
74
+ assert_equal ["robbins-monro", "metropolis", "garey:johnson"], entries.map { |e| e.key }
75
+ end
76
+
77
+ end
@@ -0,0 +1,109 @@
1
+ require 'test/unit'
2
+ require 'bibout'
3
+
4
+ class BiboutTest < Test::Unit::TestCase
5
+
6
+ def setup
7
+ current_dir = File.expand_path File.dirname(__FILE__)
8
+ @data_dir = File.join(current_dir, "data")
9
+ @bib = BibTeX.open(File.join(@data_dir, "test.bib"))
10
+ @tmpl1 = <<ENDTMPL
11
+ <% bib.each do |entry| %>
12
+ <%= entry.key %>
13
+ <%end%>
14
+ ENDTMPL
15
+ end
16
+
17
+ $RESULT1 = <<END
18
+
19
+ garey:johnson
20
+
21
+ robbins-monro
22
+
23
+ metropolis
24
+
25
+ END
26
+
27
+ def test_tmpl1
28
+ result = BibOut.new.process_string(@bib, @tmpl1)
29
+ assert_equal $RESULT1, result
30
+ end
31
+
32
+ def test_embed
33
+ result = BibOut.new(@data_dir).process_file(@bib, "root.tmpl")
34
+ assert_equal $RESULT1, result
35
+ end
36
+
37
+ $BY_YEAR_RESULT = <<END
38
+
39
+
40
+ 1951
41
+
42
+ Robbins, H. and Monro, S.. A stochastic approximation method. 1951.
43
+
44
+
45
+ 1953
46
+
47
+ Metropolis, N. and Rosenbluth, A. and Rosenbluth, M. and Teller, A. and Teller, E.. Equations of state calculations by fast computing machines. 1953.
48
+
49
+
50
+ 1979
51
+
52
+ Garey, Michael R. and Johnson, David S.. Computers and Intractability: A Guide to the Theory of NP-Completeness. 1979.
53
+
54
+
55
+ END
56
+
57
+ def test_by_year
58
+ result = BibOut.new(@data_dir).process_file(@bib, "by_year.tmpl")
59
+ assert_equal $BY_YEAR_RESULT, result
60
+ end
61
+
62
+ $RESULT2 = <<END
63
+
64
+ garey:johnson
65
+
66
+ robbins-monro
67
+
68
+ metropolis
69
+
70
+
71
+ garey:johnson
72
+
73
+ robbins-monro
74
+
75
+ metropolis
76
+
77
+ END
78
+
79
+ def test_double_embed
80
+ result = BibOut.new(@data_dir).process_file(@bib, "double_embed.tmpl")
81
+ assert_equal $RESULT2, result
82
+ end
83
+
84
+ $EMBED2_RESULT = <<END
85
+ <html>
86
+ <body>
87
+ <div id="text">
88
+
89
+
90
+ <h2>Dissertation</h2>
91
+ <ol>
92
+
93
+ <li>Computers and Intractability: A Guide to the Theory of NP-Completeness
94
+ </li>
95
+
96
+ </ol>
97
+
98
+
99
+ </div>
100
+ </body>
101
+ </html>
102
+ END
103
+
104
+ def test_embed2
105
+ result = BibOut.new(@data_dir).process_file(@bib, "embed2_root.tmpl")
106
+ assert_equal $EMBED2_RESULT, result
107
+ end
108
+
109
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bibout
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Charles Sutton
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bibtex-ruby
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 3.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 3.0.0
27
+ description: Converts BibTeX files into pretty output using arbitrary user-specified
28
+ templates, which can contain arbitrary Ruby code using ERB.
29
+ email: csutton@inf.ed.ac.uk
30
+ executables:
31
+ - bibout
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - Rakefile
36
+ - lib/bibout.rb
37
+ - lib/bibout/bibtex.rb
38
+ - lib/bibout/erb_binding.rb
39
+ - test/test_bibout.rb
40
+ - test/test_bib.rb
41
+ - bin/bibout
42
+ homepage: http://rubygems.org/gems/bibout
43
+ licenses:
44
+ - MIT
45
+ metadata: {}
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubyforge_project:
62
+ rubygems_version: 2.1.11
63
+ signing_key:
64
+ specification_version: 4
65
+ summary: BibTeX formatter
66
+ test_files:
67
+ - test/test_bibout.rb
68
+ - test/test_bib.rb