asciidoctor-bibliography 0.1 → 0.2.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.
Files changed (57) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -0
  3. data/.rubocop.yml +17 -0
  4. data/Gemfile +1 -1
  5. data/Rakefile +3 -3
  6. data/asciidoctor-bibliography.gemspec +28 -26
  7. data/lib/asciidoctor-bibliography.rb +4 -1
  8. data/lib/asciidoctor-bibliography/asciidoctor.rb +3 -9
  9. data/lib/asciidoctor-bibliography/asciidoctor/bibliographer_preprocessor.rb +43 -15
  10. data/lib/asciidoctor-bibliography/asciidoctor/document_ext.rb +11 -0
  11. data/lib/asciidoctor-bibliography/bibliographer.rb +20 -9
  12. data/lib/asciidoctor-bibliography/citation.rb +74 -51
  13. data/lib/asciidoctor-bibliography/citation_item.rb +27 -0
  14. data/lib/asciidoctor-bibliography/database.rb +14 -7
  15. data/lib/asciidoctor-bibliography/databases/bibtex.rb +5 -4
  16. data/lib/asciidoctor-bibliography/exceptions.rb +5 -0
  17. data/lib/asciidoctor-bibliography/formatters/csl.rb +5 -0
  18. data/lib/asciidoctor-bibliography/formatters/tex.rb +20 -22
  19. data/lib/asciidoctor-bibliography/helpers.rb +3 -3
  20. data/lib/asciidoctor-bibliography/index.rb +21 -25
  21. data/lib/asciidoctor-bibliography/version.rb +1 -1
  22. data/samples/{biblio.bib → standard/biblio.bib} +0 -0
  23. data/samples/standard/sample-default.adoc +22 -0
  24. data/samples/standard/sample-default.html +476 -0
  25. data/samples/standard/sample-din.adoc +22 -0
  26. data/samples/standard/sample-din.html +476 -0
  27. data/samples/standard/sample-ieee.adoc +22 -0
  28. data/samples/standard/sample-ieee.html +476 -0
  29. data/samples/tex/biblio.bib +31 -0
  30. data/samples/{sample-authoryear.adoc → tex/sample-authoryear.adoc} +2 -3
  31. data/samples/{sample-authoryear.html → tex/sample-authoryear.html} +9 -6
  32. data/samples/tex/sample-din.adoc +74 -0
  33. data/samples/tex/sample-din.html +556 -0
  34. data/samples/{sample-numbers.adoc → tex/sample-numbers.adoc} +4 -0
  35. data/samples/{sample-numbers.html → tex/sample-numbers.html} +14 -8
  36. data/samples/tex/sample-ordering.adoc +20 -0
  37. data/samples/tex/sample-ordering.html +467 -0
  38. data/spec/citation_item_spec.rb +52 -0
  39. data/spec/database_spec.rb +39 -0
  40. data/spec/fixtures/database.bib +31 -0
  41. data/spec/fixtures/database.bibtex +6 -0
  42. data/spec/fixtures/database.unk +0 -0
  43. data/spec/throwaway_spec.rb +6 -0
  44. metadata +61 -25
  45. data/deprecated/asciidoctor-bibliography/asciidoctor/bibliographer_postprocessor.rb +0 -23
  46. data/deprecated/asciidoctor-bibliography/asciidoctor/bibliography_block_macro.rb +0 -77
  47. data/deprecated/asciidoctor-bibliography/asciidoctor/citation_processor.rb +0 -144
  48. data/deprecated/asciidoctor-bibliography/asciidoctor/cite_inline_macro.rb +0 -30
  49. data/deprecated/asciidoctor-bibliography/citationdata.rb +0 -23
  50. data/deprecated/asciidoctor-bibliography/citations.rb +0 -45
  51. data/deprecated/asciidoctor-bibliography/citationutils.rb +0 -67
  52. data/deprecated/asciidoctor-bibliography/extensions.rb +0 -64
  53. data/deprecated/asciidoctor-bibliography/filehandlers.rb +0 -32
  54. data/deprecated/asciidoctor-bibliography/index.rb +0 -31
  55. data/deprecated/asciidoctor-bibliography/processor.rb +0 -208
  56. data/deprecated/asciidoctor-bibliography/processorutils.rb +0 -34
  57. data/deprecated/asciidoctor-bibliography/styles.rb +0 -27
@@ -1,30 +0,0 @@
1
- require 'asciidoctor'
2
- require 'asciidoctor/extensions'
3
-
4
- require 'securerandom'
5
-
6
- module AsciidoctorBibliography
7
- module Asciidoctor
8
-
9
- class CiteInlineMacro < ::Asciidoctor::Extensions::InlineMacroProcessor
10
- use_dsl
11
-
12
- named :cite
13
- # name_positional_attributes 'volnum'
14
-
15
- def process parent, target, attrs
16
- puts self
17
-
18
- citation = AsciidoctorBibliography::Citation.new parent, target, attrs, SecureRandom.uuid
19
- parent.document.bibliographer.citations << citation
20
-
21
- # text = target # TODO: typeset
22
- # target = "#bibliography-#{target}"
23
- # parent.document.register :links, target
24
- # (create_anchor parent, text, type: :link, target: target).render
25
-
26
- citation.placeholder
27
- end
28
- end
29
- end
30
- end
@@ -1,23 +0,0 @@
1
- # citationdata class
2
- #
3
- # Copyright (c) Peter Lane, 2013.
4
- # Released under Open Works License, 0.9.2
5
-
6
- module AsciidoctorBibliography
7
- # Class to hold information about a citation in text:
8
- # the text forming the citation, its type, pretext, and enclosed cites
9
- class CitationData
10
- attr_reader :original, :type, :pretext, :cites
11
-
12
- def initialize original, type, pretext, cites
13
- @original = original
14
- @type = type
15
- @pretext = if pretext.nil?
16
- ''
17
- else
18
- pretext
19
- end
20
- @cites = cites
21
- end
22
- end
23
- end
@@ -1,45 +0,0 @@
1
- #
2
- # Class to hold and manage citations
3
- #
4
- # Copyright (c) Peter Lane, 2013.
5
- # Released under Open Works License, 0.9.2
6
-
7
- module AsciidoctorBibliography
8
- # Class to store list of citations used in document
9
- class Citations
10
- include CitationUtils
11
-
12
- attr_reader :cites_used
13
-
14
- def initialize
15
- @cites_used = []
16
- end
17
-
18
- # Given a line of text, extract any citations and include new citation references in current list
19
- def add_from_line line
20
- retrieve_citations(line).each do |citation|
21
- @cites_used += citation.cites.collect {|cite| cite.ref}
22
- end
23
- @cites_used.uniq! {|item| item.to_s} # only keep each reference once
24
- end
25
-
26
- # Return a list of citation references in document, sorted into order
27
- def sorted_cites biblio
28
- @cites_used.sort_by do |ref|
29
- bibitem = biblio[ref]
30
-
31
- unless bibitem.nil?
32
- # extract the reference, and uppercase.
33
- # Remove { } from grouped names for sorting.
34
- author = bibitem.author
35
- if author.nil?
36
- author = bibitem.editor
37
- end
38
- author_chicago(author).collect {|s| s.upcase.gsub("{","").gsub("}","")} + [bibitem.year]
39
- else
40
- [ref]
41
- end
42
- end
43
- end
44
- end
45
- end
@@ -1,67 +0,0 @@
1
- # Utility functions for citations class
2
- #
3
- # Copyright (c) Peter Lane, 2013.
4
- # Released under Open Works License, 0.9.2
5
-
6
- module AsciidoctorBibliography
7
- # Some utility functions used in Citations class
8
- module CitationUtils
9
-
10
- # Given a line, return a list of CitationData instances
11
- # containing information on each set of citation information
12
- def retrieve_citations line
13
- result = []
14
- md = CITATION_FULL.match line
15
- while md
16
- data = CitationData.new md[0], md[1], nil, []
17
- cm = CITATION_KEY.match md[2]
18
- while cm
19
- pages = nil
20
- if cm[2]
21
- pages = cm[2][1...-1]
22
- end
23
- data.cites << Citation.new(cm[1], pages)
24
- # look for next ref within citation
25
- cm = CITATION_KEY.match cm.post_match
26
- end
27
- result << data
28
- # look for next citation on line
29
- md = CITATION_FULL.match md.post_match
30
- end
31
-
32
- return result
33
- end
34
-
35
- # arrange author string, flag for order of surname/initials
36
- def arrange_authors authors, surname_first
37
- return [] if authors.nil?
38
- authors.split(/\band\b/).collect do |name|
39
- if name.include?(", ")
40
- parts = name.strip.rpartition(", ")
41
- if surname_first
42
- "#{parts.first}, #{parts.third}"
43
- else
44
- "#{parts.third} #{parts.first}"
45
- end
46
- else
47
- name
48
- end
49
- end
50
- end
51
-
52
- # Arrange given author string into Chicago format
53
- def author_chicago authors
54
- arrange_authors authors, true
55
- end
56
-
57
- # matches a citation key, such as 'Dan2012(99-100)'
58
- CITATION_KEY = /([^\s,()\[\]]+)(\(\d+(-\d+)*\))?/
59
- # matches a citation type
60
- CITATION_TYPE = /cite|citenp|fullcite/
61
- # matches a citation list
62
- CITATION_LIST_TAIL = /(\s*,\s*#{CITATION_KEY})*/
63
- CITATION_LIST = /(?:#{CITATION_KEY}#{CITATION_LIST_TAIL})/
64
- # matches the whole citation
65
- CITATION_FULL = /(#{CITATION_TYPE}):\[(#{CITATION_LIST})\]/
66
- end
67
- end
@@ -1,64 +0,0 @@
1
- # Some extension and helper methods.
2
- #
3
- # Copyright (c) Peter Lane, 2012-13.
4
- # Released under Open Works License, 0.9.2
5
-
6
- module AsciidoctorBibliographyArrayExtensions
7
-
8
- # Retrieve the third item of an array
9
- # Note: no checks for validity
10
- def third
11
- self[2]
12
- end
13
-
14
- # Join items in array using commas and 'and' on last item
15
- def comma_and_join
16
- if size < 2
17
- return self.join("")
18
- end
19
- result = ""
20
- self.each_with_index do |item, index|
21
- if index.zero?
22
- result << item
23
- elsif index == size-1
24
- result << " and #{item}"
25
- else
26
- result << ", #{item}"
27
- end
28
- end
29
-
30
- return result
31
- end
32
- end
33
-
34
- # monkey patch the extension methods to Array
35
- class Array
36
- include AsciidoctorBibliographyArrayExtensions
37
- end
38
-
39
- # Converts html output produced by citeproc to asciidoc markup
40
- module StringHtmlToAsciiDoc
41
- def html_to_asciidoc
42
- r = self.gsub(/<\/?i>/, '_')
43
- r = r.gsub(/<\/?b>/, '*')
44
- r = r.gsub(/<\/?span.*?>/, '')
45
- r = r.gsub(/\{|\}/, '')
46
- r
47
- end
48
- end
49
-
50
- # Provides a check that a string is in integer
51
- # Taken from:
52
- # http://stackoverflow.com/questions/1235863/test-if-a-string-is-basically-an-integer-in-quotes-using-ruby
53
- module IntegerCheck
54
- def is_i?
55
- !!(self =~ /^[-+]?[0-9]+$/)
56
- end
57
- end
58
-
59
- # monkey patch the extension methods into String
60
- class String
61
- include StringHtmlToAsciiDoc
62
- include IntegerCheck
63
- end
64
-
@@ -1,32 +0,0 @@
1
- #
2
- # filehandlers.rb
3
- # Contains top-level file utility methods
4
- #
5
-
6
- module AsciidoctorBibliography
7
-
8
- module FileHandlers
9
- # Locate a bibliography file to read in given dir
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 filename
24
- def FileHandlers.add_ref filename
25
- file_dir = File.dirname(File.expand_path(filename))
26
- file_base = File.basename(filename, ".*")
27
- file_ext = File.extname(filename)
28
- return "#{file_dir}#{File::SEPARATOR}#{file_base}-ref#{file_ext}"
29
- end
30
- end
31
- end
32
-
@@ -1,31 +0,0 @@
1
- module AsciidoctorBibliography
2
- class Index
3
- attr_reader :parent, :target, :attrs, :uuid
4
-
5
- def initialize(parent, target, attrs, uuid)
6
- @parent = parent
7
- @target = target
8
- @attrs = attrs
9
- @uuid = uuid
10
- end
11
-
12
- def placeholder
13
- "{#{uuid}}"
14
- end
15
-
16
- # attr_reader :ref, :pages
17
-
18
- # def initialize ref, pages
19
- # @ref = ref
20
- # @pages = pages
21
- # # clean up pages
22
- # @pages = '' unless @pages
23
- # @pages.gsub!("--","-")
24
- # end
25
-
26
- # def to_s
27
- # "#{@ref}:#{@pages}"
28
- # end
29
- end
30
- end
31
-
@@ -1,208 +0,0 @@
1
- #
2
- # Manage the current set of citations, the document settings,
3
- # and main operations.
4
- #
5
-
6
- module AsciidoctorBibliography
7
-
8
- # Class used through utility method to hold data about citations for
9
- # current document, and run the different steps to add the citations
10
- # and bibliography
11
- class Processor
12
- include ProcessorUtils
13
-
14
- attr_reader :biblio, :links, :style, :citations
15
-
16
- def initialize biblio, links, style, numeric_in_appearance_order = false, output = :asciidoc, bibfile = ""
17
- @biblio = biblio
18
- @links = links
19
- @numeric_in_appearance_order = numeric_in_appearance_order
20
- @style = style
21
- @citations = Citations.new
22
- @filenames = Set.new
23
- @output = output
24
- @bibfile = bibfile
25
-
26
- if output != :latex
27
- @citeproc = CiteProc::Processor.new style: @style, format: :html
28
- @citeproc.import @biblio.to_citeproc
29
- end
30
- end
31
-
32
- # Return the complete citation text for given cite_data
33
- def complete_citation cite_data
34
-
35
- if @output == :latex
36
- result = '+++'
37
- cite_data.cites.each do |cite|
38
- # NOTE: xelatex does not support "\citenp", so we output all
39
- # references as "cite" here.
40
- # result << "\\" << cite_data.type
41
- result << "\\" << 'cite'
42
- if cite.pages != ''
43
- result << "[p. " << cite.pages << "]"
44
- end
45
- result << "{" << "#{cite.ref}" << "},"
46
- end
47
- if result[-1] == ','
48
- result = result[0..-2]
49
- end
50
- result << "+++"
51
- return result
52
- else
53
- result = ''
54
- ob, cb = '(', ')'
55
-
56
- cite_data.cites.each_with_index do |cite, index|
57
- # before all items apart from the first, insert appropriate separator
58
- result << "#{separator} " unless index.zero?
59
-
60
- # @links requires adding hyperlink to reference
61
- result << "<<#{cite.ref}," if @links and (cite_data.type != 'fullcite')
62
-
63
- # if found, insert reference information
64
- unless biblio[cite.ref].nil?
65
- item = biblio[cite.ref].clone
66
- cite_text, ob, cb = make_citation item, cite.ref, cite_data, cite
67
- else
68
- puts "Unknown reference: #{cite.ref}"
69
- cite_text = "#{cite.ref}"
70
- end
71
-
72
- result << cite_text.html_to_asciidoc
73
- # @links requires finish hyperlink
74
- result << ">>" if @links and (cite_data.type != 'fullcite')
75
- end
76
-
77
- unless @links
78
- # combine numeric ranges
79
- if Styles.is_numeric? @style
80
- result = combine_consecutive_numbers result
81
- end
82
- end
83
-
84
- return include_pretext result, cite_data, ob, cb
85
- end
86
- end
87
-
88
- # Retrieve text for reference in given style
89
- # - ref is reference for item to give reference for
90
- def get_reference ref
91
- result = ""
92
- result << ". " if Styles.is_numeric? @style
93
-
94
- begin
95
- cptext = @citeproc.render :bibliography, id: ref
96
- rescue Exception => e
97
- puts "Failed to render #{ref}: #{e}"
98
- end
99
- result << "[[#{ref}]]" if @links
100
- if cptext.nil?
101
- return result+ref
102
- else
103
- result << cptext.first
104
- end
105
-
106
- return result.html_to_asciidoc
107
- end
108
-
109
- def separator
110
- if Styles.is_numeric? @style
111
- ','
112
- else
113
- ';'
114
- end
115
- end
116
-
117
- # Format pages with pp/p as appropriate
118
- def with_pp pages
119
- return '' if pages.empty?
120
-
121
- if @style.include? "chicago"
122
- pages
123
- elsif pages.include? '-'
124
- "pp.&#160;#{pages}"
125
- else
126
- "p.&#160;#{pages}"
127
- end
128
- end
129
-
130
- # Return page string for given cite
131
- def page_str cite
132
- result = ''
133
- unless cite.pages.empty?
134
- result << "," unless Styles.is_numeric? @style
135
- result << " #{with_pp(cite.pages)}"
136
- end
137
-
138
- return result
139
- end
140
-
141
- def include_pretext result, cite_data, ob, cb
142
- pretext = cite_data.pretext
143
- pretext += ' ' unless pretext.empty? # add space after any content
144
-
145
- if Styles.is_numeric? @style
146
- "#{pretext}#{ob}#{result}#{cb}"
147
- elsif cite_data.type == "cite"
148
- "#{ob}#{pretext}#{result}#{cb}"
149
- else
150
- "#{pretext}#{result}"
151
- end
152
- end
153
-
154
- # Numeric citations are handled by computing the position of the reference
155
- # in the list of used citations.
156
- # Other citations are formatted by citeproc.
157
- def make_citation item, ref, cite_data, cite
158
- if cite_data.type == "fullcite"
159
- cite_text = @citeproc.render(:bibliography, id: ref).join
160
-
161
- fc = ''
162
- lc = ''
163
- elsif Styles.is_numeric? @style
164
- cite_text = if @numeric_in_appearance_order
165
- "#{@citations.cites_used.index(cite.ref) + 1}"
166
- else
167
- "#{sorted_cites.index(cite.ref) + 1}"
168
- end
169
- fc = '['
170
- lc = ']'
171
- else
172
- cite_text = @citeproc.process id: ref, mode: :citation
173
-
174
- fc = cite_text[0,1]
175
- lc = cite_text[-1,1]
176
- cite_text = cite_text[1..-2]
177
- end
178
-
179
- if cite_data.type == "fullcite"
180
- cite_text = cite_text[0...-1] + page_str(cite) + cite_text[-1]
181
- elsif Styles.is_numeric? @style
182
- cite_text << "#{page_str(cite)}"
183
- elsif cite_data.type == "citenp"
184
- cite_text.gsub!(item.year, "#{fc}#{item.year}#{page_str(cite)}#{lc}")
185
- cite_text.gsub!(", #{fc}", " #{fc}")
186
- else
187
- cite_text << page_str(cite)
188
- end
189
-
190
- cite_text.gsub!(",", "&#44;") if @links # replace comma
191
-
192
- return cite_text, fc, lc
193
- end
194
-
195
- def sorted_cites
196
- @citations.sorted_cites @biblio
197
- end
198
-
199
- def cites
200
- if Styles.is_numeric?(@style) and @numeric_in_appearance_order
201
- @citations.cites_used
202
- else
203
- sorted_cites
204
- end
205
- end
206
-
207
- end
208
- end