asciidoc-bib 2.4.5

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
+ SHA256:
3
+ metadata.gz: 28a9a41d86ee2820dd4eae638d5b30eb367a01b6fba84db47f860725a7fecb8c
4
+ data.tar.gz: 9e59a842758e91db5c751bdffd4a25d60d079de9f791fdc20f9ea2c0aa3b5028
5
+ SHA512:
6
+ metadata.gz: cfa817faad29f0fa5264cdbc8a750594a10c00ed76365aa266be98c8e38bbd3aa4d69f4f745a2eab524f5a14e233b921717d9c991cd77d425a575571fa1e47e6
7
+ data.tar.gz: 69a5837ccd61d00e2414b227484a57098c45de36c1eeba54f289fb1dbefae6c6555f7b39652072a0ad46482ef63553ce3ab47364008c46832ba044c2d32e5053
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019-20, Peter Lane
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
data/README.rdoc ADDED
@@ -0,0 +1,102 @@
1
+ = asciidoc-bib
2
+
3
+ == Description
4
+
5
+ Adds references from a bibtex file to an asciidoc[http://asciidoc.org] file.
6
+
7
+ Features:
8
+
9
+ - simple syntax for adding a citation within text and placing bibliography
10
+ - formatting of references and reference list according to range of styles supported by citeproc-ruby
11
+ - supports some styling of citation text (page numbers, bracket placement, and pretext)
12
+ - multiple references within a single citation
13
+ - groups consecutive numeric references, e.g. [1,2,3] -> [1-3]
14
+ - transforms source text to include references and full reference list
15
+
16
+ (Note: for asciidoctor[https://asciidoctor.org], prefer
17
+ asciidoctor-bibtex[https://github.com/asciidoctor/asciidoctor-bibtex/].)
18
+
19
+
20
+ == Use
21
+
22
+ 'asciidoc-bib' works by transforming an asciidoc document containing syntax
23
+ to include citations and a bibliography. The transformed document will
24
+ contain a complete reference and bibliography list where indicated, and
25
+ can then be processed further by asciidoc's toolchain to produce a completed
26
+ document.
27
+
28
+ Styles must be one of those supported by CSL[https://github.com/citation-style-language/styles].
29
+
30
+ === Include a citation
31
+
32
+ Syntax for inserting a citation is [cite|citenp:(pretext:)ref(,pages)]
33
+ where 'pretext' and 'pages' are optional. The ref and optional pages may
34
+ be repeated multiple times, separated by ';'. A citation _must_ be
35
+ complete on a single line of text.
36
+
37
+ Examples of +chicago-author-date+ style:
38
+
39
+ [[cite:Lane12]] becomes "(Lane 2012)"
40
+
41
+ [[citenp:Lane12]] becomes "Lane (2012)"
42
+
43
+ [[cite:Lane12,59]] becomes "(Lane 2012, 59)"
44
+
45
+ [[cite:See:Lane12,59]] becomes "(See Lane 2012, 59)"
46
+
47
+ [[cite:See:Lane12,59;Lane11]] becomes "(See Lane 2012, 59; Lane 2011)"
48
+
49
+ For +apa+ (Harvard-like) style:
50
+
51
+ [[cite:Lane12]] becomes "(Lane, 2012)"
52
+
53
+ [[citenp:Lane12]] becomes "Lane (2012)"
54
+
55
+ [[cite:Lane12,59]] becomes "(Lane, 2012, p.59)"
56
+
57
+ For +ieee+, a numeric style:
58
+
59
+ [[cite:Lane12;Lane11]] becomes "[1, 2]"
60
+
61
+ [[cite:See:Lane12,59;Lane11]] becomes "See [1 p.59, 2]"
62
+
63
+ === Place bibliography in text
64
+
65
+ [[bibliography]] on a line by itself.
66
+
67
+ === Processing text
68
+
69
+ > asciidoc-bib filename.txt
70
+
71
+ Looks for a bib file in current folder and in ~/Documents.
72
+
73
+ Outputs a new file: filename-ref.txt
74
+ which includes your references.
75
+
76
+ Check the new file, and process in the usual way with asciidoc.
77
+
78
+ Command-line options:
79
+
80
+ > asciidoc-bib -h
81
+ Usage: asciidoc-bib filename
82
+ -h, --help help message
83
+ -b, --bibfile FILE location of bib file
84
+ -n, --no-links do not add internal links
85
+ --numeric-alphabetic-order
86
+ sort numeric styles in alphabetical order (DEFAULT)
87
+ --numeric-appearance-order
88
+ sort numeric styles in order of appearance
89
+ -s, --style STYLE reference style
90
+ -v, --version show version
91
+
92
+ All styles available through CSL are supported.
93
+ The default style is 'apa'.
94
+
95
+
96
+ == Notes
97
+
98
+ If you make a Bibliography/Reference heading, a2x interprets this specially,
99
+ and will fail to make a pdf. To prevent a2x treating a heading specially, place
100
+ a section template name, [sect1], before it.
101
+
102
+
data/bin/asciidoc-bib ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # Executable for asciidoc-bib
4
+ #
5
+ # Provides some command line options for adding reference material
6
+ # to asciidoc files, generating revised files for processing by
7
+ # asciidoc.
8
+ #
9
+
10
+ unless $:.include?(libdir = File.expand_path('../lib', File.dirname(__FILE__)))
11
+ $:.unshift libdir
12
+ end
13
+ require 'asciidoc-bib'
14
+
15
+ options = AsciidocBib::Options.new
16
+ AsciidocBib::Processor.run options
data/changes.txt ADDED
@@ -0,0 +1,39 @@
1
+ = Changes to asciidoc-bib
2
+
3
+ == version 2.4.4
4
+
5
+ - confirm works with ruby 3.0-preview
6
+
7
+ == version 2.4.3
8
+
9
+ - update tests to support minitest 5.13
10
+ - confirm works with ruby 2.7
11
+ - updated documentation to most classes
12
+
13
+ == version 2.4.2
14
+
15
+ - updated documentation
16
+ - removed all OWL license references
17
+ - removed all asciidoctor references
18
+
19
+ == version 2.4.1
20
+
21
+ - changed license to MIT
22
+
23
+ == version 2.4.0
24
+
25
+ - removed asciidoctor references
26
+
27
+ == github version
28
+
29
+ - accepted dash inside citation keys
30
+
31
+ == version 2.3.1
32
+
33
+ - now works with asciidoctor 1.5 api
34
+
35
+ == version 2.3.0
36
+
37
+ - added switch for order of numeric citations in bibliography, with choice of
38
+ order of appearance or alphabetical order
39
+
@@ -0,0 +1,25 @@
1
+ module AsciidocBib
2
+
3
+ # Holds information about a single citation:
4
+ # its reference and any page numbers
5
+ class Citation
6
+ # Reference for a citation
7
+ attr_reader :ref
8
+ # Page numbers
9
+ attr_reader :pages
10
+
11
+ # Construct instance of citation, from a reference and definition of page numbers.
12
+ def initialize ref, pages
13
+ @ref = ref
14
+ @pages = pages
15
+ # clean up pages
16
+ @pages = '' unless @pages
17
+ @pages.gsub!("--","-")
18
+ end
19
+
20
+ def to_s
21
+ "#{@ref}:#{@pages}"
22
+ end
23
+ end
24
+ end
25
+
@@ -0,0 +1,33 @@
1
+ module AsciidocBib
2
+
3
+ # Holds information about a citation in text:
4
+ # the text forming the citation, its type, pretext, and enclosed cites.
5
+ #
6
+ # For example, the citation: "[cite:See:Lane12,59;Lane11]"
7
+ # * original: "[cite:See:Lane12,59;Lane11]"
8
+ # * type: "cite"
9
+ # * pretext: "See"
10
+ # * cites: [Citation{ref="Lane12",pages="59"}, Citation{ref="Lane11",pages=""} ]
11
+ #
12
+ class CitationData
13
+ # Original text defining this set of citations.
14
+ attr_reader :original
15
+ # The type of citation (e.g. 'cite', 'citenp').
16
+ attr_reader :type
17
+ # Any pretext given for the citation (e.g. 'See').
18
+ attr_reader :pretext
19
+ # List of Citation instances, defining each cite.
20
+ attr_reader :cites
21
+
22
+ def initialize original, type, pretext, cites
23
+ @original = original
24
+ @type = type
25
+ @pretext = if pretext.nil?
26
+ ''
27
+ else
28
+ pretext
29
+ end
30
+ @cites = cites
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,43 @@
1
+ module AsciidocBib
2
+
3
+ # Stores list of citations used in document.
4
+ #
5
+ class Citations
6
+ include CitationUtils
7
+
8
+ # List of citations.
9
+ attr_reader :cites_used
10
+
11
+ def initialize
12
+ @cites_used = []
13
+ end
14
+
15
+ # Given a line of text, extract any citations and
16
+ # include new citation references in current list.
17
+ def add_from_line line
18
+ retrieve_citations(line).each do |citation|
19
+ @cites_used += citation.cites.collect {|cite| cite.ref}
20
+ end
21
+ @cites_used.uniq! {|item| item.to_s} # only keep each reference once
22
+ end
23
+
24
+ # Return a list of citation references in document, sorted into order.
25
+ def sorted_cites biblio
26
+ @cites_used.sort_by do |ref|
27
+ bibitem = biblio[ref]
28
+
29
+ unless bibitem.nil?
30
+ # extract the reference, and uppercase.
31
+ # Remove { } from grouped names for sorting.
32
+ author = bibitem.author
33
+ if author.nil?
34
+ author = bibitem.editor
35
+ end
36
+ author_chicago(author).collect {|s| s.upcase.gsub("{","").gsub("}","")} + [bibitem.year]
37
+ else
38
+ [ref]
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,54 @@
1
+ module AsciidocBib
2
+
3
+ # Some utility functions used in Citations class
4
+ module CitationUtils
5
+
6
+ # Given a line, return a list of CitationData instances
7
+ # containing information on each set of citation information.
8
+ def retrieve_citations line
9
+ result = []
10
+ md = CITATION_FULL.match line
11
+ while md
12
+ data = CitationData.new md[0], md[1], md[3], []
13
+ cm = CITATION.match md[4]
14
+ while cm
15
+ data.cites << Citation.new(cm[1], cm[3])
16
+ # look for next ref within citation
17
+ cm = CITATION.match cm.post_match
18
+ end
19
+ result << data
20
+ # look for next citation on line
21
+ md = CITATION_FULL.match md.post_match
22
+ end
23
+
24
+ return result
25
+ end
26
+
27
+ # Arrange author string, flag for order of surname/initials.
28
+ def arrange_authors authors, surname_first
29
+ return [] if authors.nil?
30
+ authors.split(/\band\b/).collect do |name|
31
+ if name.include?(", ")
32
+ parts = name.strip.rpartition(", ")
33
+ if surname_first
34
+ "#{parts.first}, #{parts.third}"
35
+ else
36
+ "#{parts.third} #{parts.first}"
37
+ end
38
+ else
39
+ name
40
+ end
41
+ end
42
+ end
43
+
44
+ # Arrange given author string into Chicago format.
45
+ def author_chicago authors
46
+ arrange_authors authors, true
47
+ end
48
+
49
+ # matches a single ref with optional pages
50
+ CITATION = /([\w\-]+)(,([\w\.\- ]+))?/
51
+ # matches complete citation with multiple references
52
+ CITATION_FULL = /\[(cite|citenp):(([\w\-\;\!\? ]+):)?(#{CITATION}(;#{CITATION})*)\]/
53
+ end
54
+ end
@@ -0,0 +1,51 @@
1
+ # Two methods are added to extend the Array class.
2
+ #
3
+ class Array
4
+
5
+ # Retrieve the third item of an array
6
+ # Note: no checks for validity
7
+ def third
8
+ self[2]
9
+ end
10
+
11
+ # Join items in array using commas and 'and' on last item
12
+ def comma_and_join
13
+ if size < 2
14
+ return self.join("")
15
+ end
16
+ result = ""
17
+ self.each_with_index do |item, index|
18
+ if index.zero?
19
+ result << item
20
+ elsif index == size-1
21
+ result << " and #{item}"
22
+ else
23
+ result << ", #{item}"
24
+ end
25
+ end
26
+
27
+ return result
28
+ end
29
+ end
30
+
31
+
32
+ # Two methods are added to extend the String class.
33
+ class String
34
+
35
+ # Converts html output produced by citeproc to asciidoc markup.
36
+ def html_to_asciidoc
37
+ r = self.gsub(/<\/?i>/, '_')
38
+ r = r.gsub(/<\/?b>/, '*')
39
+ r = r.gsub(/<\/?span.*?>/, '')
40
+ r = r.gsub(/\{|\}/, '')
41
+ r
42
+ end
43
+
44
+ # Provides a check that a string is in integer.
45
+ # Taken from:
46
+ # http://stackoverflow.com/questions/1235863/test-if-a-string-is-basically-an-integer-in-quotes-using-ruby
47
+ def is_i?
48
+ !!(self =~ /^[-+]?[0-9]+$/)
49
+ end
50
+ end
51
+
@@ -0,0 +1,33 @@
1
+ module AsciidocBib
2
+
3
+ #
4
+ # Utility methods for working with the files.
5
+ #
6
+ module FileHandlers
7
+
8
+ # Locate a bibliography file (*.bib) to read in given _directory_.
9
+ # Returns "" if any error or no file found.
10
+ def FileHandlers.find_bibliography dir
11
+ begin
12
+ candidates = Dir.glob("#{dir}/*.bib")
13
+ if candidates.empty?
14
+ return ""
15
+ else
16
+ return candidates.first
17
+ end
18
+ rescue # catch all errors, and return empty string
19
+ return ""
20
+ end
21
+ end
22
+
23
+ # Add '-ref' before the extension of a given _filename_,
24
+ # returning full path of new filename.
25
+ def FileHandlers.add_ref filename
26
+ file_dir = File.dirname(File.expand_path(filename))
27
+ file_base = File.basename(filename, ".*")
28
+ file_ext = File.extname(filename)
29
+ return "#{file_dir}#{File::SEPARATOR}#{file_base}-ref#{file_ext}"
30
+ end
31
+ end
32
+ end
33
+
@@ -0,0 +1,105 @@
1
+ require 'optparse'
2
+
3
+ module AsciidocBib
4
+
5
+ # Class to read in asciidoc-bib options from command-line, and
6
+ # store results in an accessible form.
7
+ #
8
+ class Options
9
+ # Location of bib file to read references from.
10
+ attr_reader :bibfile
11
+ # Location of file to process.
12
+ attr_reader :filename
13
+ # Flag to indicate if links should be included.
14
+ attr_reader :links
15
+ # Name of style to use for displaying citations and references.
16
+ attr_reader :style
17
+
18
+ # Sets up options for the program, including:
19
+ # * location of bib file
20
+ # * whether internal links should be added
21
+ # * sorting of references
22
+ # * style of references
23
+ # * name of file to process
24
+ #
25
+ def initialize(program_name = 'asciidoc-bib')
26
+ @bibfile = ''
27
+ @links = true
28
+ @numeric_order = :alphabetical
29
+ @style = AsciidocBib::Styles.default_style
30
+
31
+ options = OptionParser.new do |opts|
32
+ opts.banner = "Usage: #{program_name} filename"
33
+ opts.on("-h", "--help", "help message") do |v|
34
+ puts "#{program_name} #{AsciidocBib::VERSION}"
35
+ puts
36
+ puts options
37
+ puts
38
+ puts "All styles available through CSL are supported."
39
+ puts "The default style is 'apa'."
40
+ exit!
41
+ end
42
+ opts.on("-b", "--bibfile FILE", "location of bib file") do |v|
43
+ @bibfile = v
44
+ end
45
+ opts.on("-n", "--no-links", "do not add internal links") do |v|
46
+ @links = false
47
+ end
48
+ opts.on('', '--numeric-alphabetic-order', 'sort numeric styles in alphabetical order (DEFAULT)') do |v|
49
+ @numeric_order = :alphabetical
50
+ end
51
+ opts.on('', '--numeric-appearance-order', 'sort numeric styles in order of appearance') do |v|
52
+ @numeric_order = :appearance
53
+ end
54
+ opts.on("-s", "--style STYLE", "reference style") do |v|
55
+ @style = v
56
+ end
57
+ opts.on("-v", "--version", "show version") do |v|
58
+ puts "#{program_name} version #{AsciidocBib::VERSION}"
59
+ exit!
60
+ end
61
+ end
62
+
63
+ begin
64
+ options.parse!
65
+ rescue
66
+ puts options
67
+ exit!
68
+ end
69
+
70
+ # unless specified by caller, try to find the bibliography
71
+ if @bibfile.empty?
72
+ @bibfile = AsciidocBib::FileHandlers.find_bibliography "."
73
+ if @bibfile.empty?
74
+ @bibfile = AsciidocBib::FileHandlers.find_bibliography "#{ENV['HOME']}/Documents"
75
+ end
76
+ end
77
+ if @bibfile.empty?
78
+ puts "Error: could not find a bibliography file"
79
+ exit
80
+ end
81
+ unless AsciidocBib::Styles.valid? @style
82
+ puts "Error: style #{@style} was not one of the available styles"
83
+ exit
84
+ end
85
+
86
+ if ARGV.length == 1
87
+ @filename = ARGV[0]
88
+ else
89
+ puts "Error: a single file to convert must be given"
90
+ exit
91
+ end
92
+
93
+ puts "Reading biblio: #{@bibfile}"
94
+ puts "Reference style: #{@style}"
95
+ end
96
+
97
+ # Convenience method to check @numeric_order.
98
+ # This is used in place of making the @numeric_order field visible
99
+ # to external classes.
100
+ def numeric_in_appearance_order?
101
+ @numeric_order == :appearance
102
+ end
103
+ end
104
+ end
105
+
@@ -0,0 +1,279 @@
1
+ module AsciidocBib
2
+
3
+ # Class used through utility method to hold data about citations for
4
+ # current document, and run the different steps to add the citations
5
+ # and bibliography.
6
+ #
7
+ class Processor
8
+ include ProcessorUtils
9
+
10
+ # Top-level method to include citations in given asciidoc file.
11
+ def Processor.run options
12
+ processor = Processor.new BibTeX.open(options.bibfile), options.links, options.style, options.numeric_in_appearance_order?
13
+ processor.read_filenames options.filename
14
+ processor.read_citations
15
+ processor.add_citations
16
+ end
17
+
18
+ # The BibTeX instance based on the provided .bib file.
19
+ attr_reader :biblio
20
+ # Flag to indicate if links should be included, as set in options.
21
+ attr_reader :links
22
+ # Citation style, as set in options.
23
+ attr_reader :style
24
+ # List of citations as read in from the file.
25
+ attr_reader :citations
26
+
27
+ def initialize biblio, links, style, numeric_in_appearance_order = false
28
+ @biblio = biblio
29
+ @links = links
30
+ @numeric_in_appearance_order = numeric_in_appearance_order
31
+ @style = style
32
+ @citations = Citations.new
33
+ @filenames = Set.new
34
+
35
+ @citeproc = CiteProc::Processor.new style: @style, format: :html
36
+ @citeproc.import @biblio.to_citeproc
37
+ end
38
+
39
+ # Given an asciidoc filename, reads in all dependent files based on 'include::' statements
40
+ # Leaving a list of files in @filenames
41
+ def read_filenames filename
42
+ puts "Reading file: #{filename}"
43
+ files_to_process = [filename]
44
+
45
+ begin
46
+ @filenames.add files_to_process.first
47
+ File.new(files_to_process.shift).each_line do |line|
48
+ if line.include?("include::")
49
+ line.split("include::").drop(1).each do |filetxt|
50
+ file = File.expand_path(filetxt.partition(/\s|\[/).first)
51
+ files_to_process << file unless @filenames.include?(file)
52
+ end
53
+ end
54
+ end
55
+ end until files_to_process.empty?
56
+ end
57
+
58
+ # Scans each filename and extracts citations
59
+ def read_citations
60
+ @filenames.each do |file|
61
+ IO.foreach(file) do |line|
62
+ @citations.add_from_line line
63
+ end
64
+ end
65
+ end
66
+
67
+ # Read given text to add cites and biblio to a new file
68
+ # Order is always decided by author surname first with year.
69
+ # If no author present, then use editor field.
70
+ # Links indicates if internal links to be added.
71
+ # Assumes @filenames has been set to list of filenames to process.
72
+ def add_citations
73
+ @filenames.each do |curr_file|
74
+ ref_filename = FileHandlers.add_ref(curr_file)
75
+ puts "Writing file: #{ref_filename}"
76
+ output = File.new(ref_filename, "w")
77
+
78
+ IO.foreach(curr_file) do |line|
79
+ begin # catch any errors, and ensure the lines of text are written
80
+ case
81
+ when line.include?('include::')
82
+ output_include_line output, line
83
+ when line.include?('[bibliography]')
84
+ output_bibliography output
85
+ else
86
+ output_cite_completed_line output, line
87
+ end
88
+ rescue # Any errors, just output the line
89
+ output.puts line
90
+ end
91
+ end
92
+
93
+ output.close
94
+ end
95
+ end
96
+
97
+ # Output bibliography to given output stream.
98
+ def output_bibliography output
99
+ cites = if Styles.is_numeric?(@style) and @numeric_in_appearance_order
100
+ @citations.cites_used
101
+ else
102
+ sorted_cites
103
+ end
104
+ cites.each do |ref|
105
+ output.puts get_reference(ref)
106
+ output.puts
107
+ end
108
+ end
109
+
110
+ # Handles a line of text which includes another file.
111
+ # The included file must be redirected to reference the -ref version,
112
+ # the one which includes any expanded citations.
113
+ def output_include_line output, line
114
+ line.split("include::").drop(1).each do |filetxt|
115
+ ifile = filetxt.partition(/\s|\[/).first
116
+ file = File.expand_path ifile
117
+ # make sure included file points to the -ref version
118
+ line.gsub!("include::#{ifile}", "include::#{FileHandlers.add_ref(file)}")
119
+ end
120
+ output.puts line
121
+ end
122
+
123
+ # For each citation in given line, expand into complete citation text
124
+ # before outputting the line
125
+ def output_cite_completed_line output, line
126
+ @citations.retrieve_citations(line).each do |citation|
127
+ line.gsub!(citation.original, complete_citation(citation))
128
+ end
129
+ output.puts line
130
+ end
131
+
132
+ # Return the complete citation text for given cite_data.
133
+ def complete_citation cite_data
134
+ result = ''
135
+ ob, cb = '(', ')'
136
+
137
+ cite_data.cites.each_with_index do |cite, index|
138
+ # before all items apart from the first, insert appropriate separator
139
+ result << "#{separator} " unless index.zero?
140
+
141
+ # @links requires adding hyperlink to reference
142
+ result << "<<#{cite.ref}," if @links
143
+
144
+ # if found, insert reference information
145
+ unless biblio[cite.ref].nil?
146
+ item = biblio[cite.ref].clone
147
+ cite_text, ob, cb = make_citation item, cite.ref, cite_data, cite
148
+ else
149
+ puts "Unknown reference: #{cite.ref}"
150
+ cite_text = "#{cite.ref}"
151
+ end
152
+
153
+ result << cite_text.html_to_asciidoc
154
+ # @links requires finish hyperlink
155
+ result << ">>" if @links
156
+ end
157
+
158
+ unless @links
159
+ # combine numeric ranges
160
+ if Styles.is_numeric? @style
161
+ result = combine_consecutive_numbers result
162
+ end
163
+ end
164
+
165
+ include_pretext result, cite_data, ob, cb
166
+ end
167
+
168
+ # Retrieve text for reference in given style.
169
+ # ref:: reference for item to give reference for
170
+ def get_reference ref
171
+ result = ""
172
+ result << ". " if Styles.is_numeric? @style
173
+
174
+ begin
175
+ cptext = @citeproc.render :bibliography, id: ref
176
+ rescue Exception => e
177
+ puts "Failed to render #{id}: #{e}"
178
+ end
179
+ result << "[[#{ref}]]" if @links
180
+ if cptext.nil?
181
+ return result+ref
182
+ else
183
+ result << cptext.first
184
+ end
185
+
186
+ return result.html_to_asciidoc
187
+ end
188
+
189
+ # Return the appropriate separator, depending on the current style.
190
+ def separator
191
+ if Styles.is_numeric? @style
192
+ ','
193
+ else
194
+ ';'
195
+ end
196
+ end
197
+
198
+ # Format pages with pp/p as appropriate.
199
+ def with_pp pages
200
+ return '' if pages.empty?
201
+
202
+ if @style.include? "chicago"
203
+ pages
204
+ elsif pages.include? '-'
205
+ "pp.&#160;#{pages}"
206
+ else
207
+ "p.&#160;#{pages}"
208
+ end
209
+ end
210
+
211
+ # Return page string for given _cite_.
212
+ def page_str cite
213
+ result = ''
214
+ unless cite.pages.empty?
215
+ result << "," unless Styles.is_numeric? @style
216
+ result << " #{with_pp(cite.pages)}"
217
+ end
218
+
219
+ return result
220
+ end
221
+
222
+ # The pretext is the message such as "See" given in the original citation.
223
+ # This method includes the pretext as appropriate, depending on the
224
+ # style and citation type.
225
+ def include_pretext result, cite_data, ob, cb
226
+ pretext = cite_data.pretext
227
+ pretext += ' ' unless pretext.empty? # add space after any content
228
+
229
+ if Styles.is_numeric? @style
230
+ "#{pretext}#{ob}#{result}#{cb}"
231
+ elsif cite_data.type == "cite"
232
+ "#{ob}#{pretext}#{result}#{cb}"
233
+ else
234
+ "#{pretext}#{result}"
235
+ end
236
+ end
237
+
238
+ # Numeric citations are handled by computing the position of the reference
239
+ # in the list of used citations.
240
+ # Other citations are formatted by citeproc.
241
+ def make_citation item, ref, cite_data, cite
242
+ if Styles.is_numeric? @style
243
+ cite_text = if @numeric_in_appearance_order
244
+ "#{@citations.cites_used.index(cite.ref) + 1}"
245
+ else
246
+ "#{sorted_cites.index(cite.ref) + 1}"
247
+ end
248
+ fc = '['
249
+ lc = ']'
250
+ else
251
+ cite_text = @citeproc.process id: ref, mode: :citation
252
+
253
+ fc = cite_text[0,1]
254
+ lc = cite_text[-1,1]
255
+ cite_text = cite_text[1..-2]
256
+ end
257
+
258
+ if Styles.is_numeric? @style
259
+ cite_text << "#{page_str(cite)}"
260
+ elsif cite_data.type == "citenp"
261
+ cite_text.gsub!(item.year, "#{fc}#{item.year}#{page_str(cite)}#{lc}")
262
+ cite_text.gsub!(", #{fc}", " #{fc}")
263
+ else
264
+ cite_text << page_str(cite)
265
+ end
266
+
267
+ cite_text.gsub!(",", "&#44;") if @links # replace comma
268
+
269
+ return cite_text, fc, lc
270
+ end
271
+
272
+ # Return the list of sorted citations.
273
+ def sorted_cites
274
+ @citations.sorted_cites @biblio
275
+ end
276
+ end
277
+ end
278
+
279
+
@@ -0,0 +1,39 @@
1
+ module AsciidocBib
2
+
3
+ module ProcessorUtils
4
+
5
+ # Used with numeric styles to combine consecutive numbers into ranges
6
+ #
7
+ # combine_consecutive_numbers("1,2,3") # -> "1-3"
8
+ # combine_consecutive_numbers("1,2,3,6,7,8,9,12") # -> "1-3,6-9,12"
9
+ #
10
+ # Leaves references with page numbers alone.
11
+ #
12
+ def combine_consecutive_numbers str
13
+ nums = str.split(",").collect(&:strip)
14
+ res = ""
15
+ # Loop through ranges
16
+ start_range = 0
17
+ while start_range < nums.length do
18
+ end_range = start_range
19
+ while (end_range < nums.length-1 and
20
+ nums[end_range].is_i? and
21
+ nums[end_range+1].is_i? and
22
+ nums[end_range+1].to_i == nums[end_range].to_i + 1) do
23
+ end_range += 1
24
+ end
25
+ if end_range - start_range >= 2
26
+ res += "#{nums[start_range]}-#{nums[end_range]}, "
27
+ else
28
+ start_range.upto(end_range) do |i|
29
+ res += "#{nums[i]}, "
30
+ end
31
+ end
32
+ start_range = end_range + 1
33
+ end
34
+ # finish by removing last comma
35
+ res.gsub(/, $/, '')
36
+ end
37
+ end
38
+ end
39
+
@@ -0,0 +1,30 @@
1
+ module AsciidocBib
2
+
3
+ #
4
+ # Simple checks on available styles through CSL
5
+ #
6
+ module Styles
7
+
8
+ # Returns a list of available styles.
9
+ def Styles.available
10
+ CSL::Style.ls
11
+ end
12
+
13
+ # Retrieves a default style, here set to 'apa'.
14
+ def Styles.default_style
15
+ 'apa'
16
+ end
17
+
18
+ # Checks if a given style (as input by user) is available in CSL.
19
+ def Styles.valid? style
20
+ Styles.available.include? style
21
+ end
22
+
23
+ # Checks if given style is a numeric style,
24
+ # i.e. one that uses numeric references.
25
+ def Styles.is_numeric? style
26
+ CSL::Style.load(style).citation_format == :numeric
27
+ end
28
+ end
29
+ end
30
+
@@ -0,0 +1,4 @@
1
+ module AsciidocBib
2
+ # Version number of project.
3
+ VERSION = '2.4.5'
4
+ end
@@ -0,0 +1,19 @@
1
+ # asciidoc-bib.rb
2
+ #
3
+
4
+ require 'bibtex'
5
+ require 'citeproc'
6
+ require 'csl/styles'
7
+ require 'set'
8
+
9
+ require 'asciidoc-bib/citation'
10
+ require 'asciidoc-bib/citationdata'
11
+ require 'asciidoc-bib/citationutils'
12
+ require 'asciidoc-bib/citations'
13
+ require 'asciidoc-bib/extensions'
14
+ require 'asciidoc-bib/filehandlers'
15
+ require 'asciidoc-bib/options'
16
+ require 'asciidoc-bib/processorutils'
17
+ require 'asciidoc-bib/processor'
18
+ require 'asciidoc-bib/styles'
19
+ require 'asciidoc-bib/version'
@@ -0,0 +1,31 @@
1
+ @book{Lane12a,
2
+ author = {P. Lane},
3
+ title = {Book title},
4
+ publisher = {Publisher},
5
+ year = {2000}
6
+ }
7
+
8
+ @book{Lane12b,
9
+ author = {P. Lane and D. Smith},
10
+ title = {Book title},
11
+ publisher = {Publisher},
12
+ year = {2000}
13
+ }
14
+
15
+ @book{Anderson98,
16
+ editor = {J. R. Anderson and C. Lebiere},
17
+ title = {The Atomic Components of Thought},
18
+ publisher = {Lawrence Erlbaum},
19
+ address = {Mahwah, NJ},
20
+ year = {1998}
21
+ }
22
+
23
+ @article{Anderson04,
24
+ author = {J. R. Anderson and D. Bothell and M. D. Byrne and S. Douglass and C. Lebiere and Y. L. Qin},
25
+ title = {An integrated theory of the mind},
26
+ journal = {Psychological Review},
27
+ volume = {111},
28
+ number = {4},
29
+ pages = {1036--1060},
30
+ year = {2004}
31
+ }
@@ -0,0 +1,23 @@
1
+ = Sample of using asciidoc-bib =
2
+
3
+ The bibliography is searched in the folder of the document, and then
4
+ in ~/Documents.
5
+
6
+ Author-year references can use different styles such as: [cite:Lane12a] or
7
+ [citenp:Lane12a].
8
+
9
+ Page numbers can be added: [cite:Lane12a,89] or [citenp:Lane12a,89-93].
10
+
11
+ A bit of pretext can be included too: [cite:See:Lane12a,89]
12
+
13
+ We can include other files, which are also processed:
14
+
15
+ include::sample-2.txt[]
16
+
17
+ To include the reference list, use the section template before
18
+ title, to prevent problems with a2x.
19
+
20
+ [sect2]
21
+ == Bibliography ==
22
+
23
+ [bibliography]
@@ -0,0 +1,18 @@
1
+ == Sample file 2: multiple authors ==
2
+
3
+ Author-year references can use different styles such as: [cite:Lane12b] or
4
+ [citenp:Lane12b].
5
+
6
+ Page numbers can be added: [cite:Lane12b,89] or [citenp:Lane12b,89-93].
7
+
8
+ A bit of pretext can be included too: [cite:See:Lane12b,89]
9
+
10
+ === multiple refs ===
11
+
12
+ One citation can include several references, such as [cite:Lane12a;Lane12b].
13
+
14
+ These can include pages and be in-text: [citenp:Lane12a,78;Lane12b,89-93].
15
+
16
+ And include pretext: [cite:See:Lane12a;Lane12b,89-93]
17
+
18
+ Note: citations cannot be split over lines; each citation must be on one line.
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: asciidoc-bib
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.4.5
5
+ platform: ruby
6
+ authors:
7
+ - Peter Lane
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-04-25 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: '5.0'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 5.1.4
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '5.0'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 5.1.4
33
+ - !ruby/object:Gem::Dependency
34
+ name: citeproc-ruby
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.0'
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 1.0.5
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '1.0'
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 1.0.5
53
+ - !ruby/object:Gem::Dependency
54
+ name: csl-styles
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: '1.0'
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: 1.0.1.6
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '1.0'
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: 1.0.1.6
73
+ description: "asciidoc-bib generates in-text references and a reference list from
74
+ an asciidoc\nfile, using a bibtex file as a source of citation information. The
75
+ syntax for\nan in-text reference is simply [cite:bibref], and a line containing\n[bibliography]
76
+ inserts a complete reference list. See the README for more\nexamples and further
77
+ options. The references are formatted using styles provided \nby CSL.\n"
78
+ email: peterlane@gmx.com
79
+ executables:
80
+ - asciidoc-bib
81
+ extensions: []
82
+ extra_rdoc_files:
83
+ - README.rdoc
84
+ - LICENSE.txt
85
+ files:
86
+ - LICENSE.txt
87
+ - README.rdoc
88
+ - bin/asciidoc-bib
89
+ - changes.txt
90
+ - lib/asciidoc-bib.rb
91
+ - lib/asciidoc-bib/citation.rb
92
+ - lib/asciidoc-bib/citationdata.rb
93
+ - lib/asciidoc-bib/citations.rb
94
+ - lib/asciidoc-bib/citationutils.rb
95
+ - lib/asciidoc-bib/extensions.rb
96
+ - lib/asciidoc-bib/filehandlers.rb
97
+ - lib/asciidoc-bib/options.rb
98
+ - lib/asciidoc-bib/processor.rb
99
+ - lib/asciidoc-bib/processorutils.rb
100
+ - lib/asciidoc-bib/styles.rb
101
+ - lib/asciidoc-bib/version.rb
102
+ - samples/biblio.bib
103
+ - samples/sample-1.txt
104
+ - samples/sample-2.txt
105
+ homepage:
106
+ licenses:
107
+ - MIT
108
+ metadata: {}
109
+ post_install_message:
110
+ rdoc_options:
111
+ - "-m"
112
+ - README.rdoc
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '2.0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubygems_version: 3.3.7
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: asciidoc-bib adds references from a bibtex file to an asciidoc file.
130
+ test_files: []