asciidoctor-bibtex 0.6.0 → 0.7.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bccdd37cd3c92456c39f42cb27ea3b0605a03d72d465252993997e97d5c22550
4
- data.tar.gz: 172ad52204430a2672cbb19d0c4206bd12127ab8aae7eb52a1110b7aa245734e
3
+ metadata.gz: e39be76ce5e649e7075b470a56950f64c91ab7f084b902676356b6672012a9d1
4
+ data.tar.gz: 467a9b62f3f9c51ec8778d41dd8a29bc3d2f06b7b5b88ff55f41b4fded73b66a
5
5
  SHA512:
6
- metadata.gz: 43c2d4492d39072ec712873f0414f5ba988f864a7fc7e9883decefba673357f650667a37d410506335d66c3d7524ebcb2d6686c2d4b3e24a8e2eced503432e61
7
- data.tar.gz: 8a14439358c611b4535942882978581f9957faf43519616c760a4f9a2b3307e923a3d59fa8550d7ecde37c0598dd51d658d444dee8b340506ab939c47e13dfc5
6
+ metadata.gz: 983ff27cc0f7899411b3bba18f0afa0e54aa9aa4fa8ca82c48e4f61de3cbe4ea4a6e4c0dfc78539b54c8c2ae5ba3b7c5816bac88dc1550905f1356d1e163e69a
7
+ data.tar.gz: a6bbc812e04a8f59a6f8499cf750bfc938fc73b5675d6b72d6f749f6c29e056dfc5bff83f0002180f9a076225c94972ef8b8f733d762f9c98595595bc67417e3
@@ -9,6 +9,7 @@ ifdef::env-github,env-browser[]
9
9
  endif::[]
10
10
  ifdef::env-github[]
11
11
  :!toc-title:
12
+ :status:
12
13
  endif::[]
13
14
  // URLs:
14
15
  :url-asciidoctor: http://asciidoctor.org
@@ -16,12 +17,16 @@ endif::[]
16
17
  :url-asciidoctor-pdf: https://github.com/asciidoctor/asciidoctor-pdf
17
18
  :url-asciidoctor-latex: https://github.com/asciidoctor/asciidoctor-latex
18
19
  :url-asciidoc-bib: https://github.com/petercrlane/asciidoc-bib
20
+ :url-gem: https://rubygems.org/gems/asciidoctor-bibtex
19
21
 
20
- asciidoctor-bibtex adds bibtex integration to AsciiDoc documents by introducing two new macros: `cite:[KEY]` and `bibliography::[]`.
21
- Citations are parsed and replaced with formatted inline texts, and reference lists are automatically generated and inserted into where `bibliography::[]` is placed.
22
+ ifdef::status[]
23
+ image:https://img.shields.io/travis/asciidoctor/asciidoctor-bibtex/master.svg[Build Status (Travis CI),link=https://travis-ci.org/asciidoctor/asciidoctor-bibtex]
24
+ image:https://img.shields.io/gem/v/asciidoctor-bibtex.svg[Latest Release, link=https://rubygems.org/gems/asciidoctor-bibtex]
25
+ endif::[]
26
+
27
+ asciidoctor-bibtex adds bibtex integration to AsciiDoc documents by introducing three new macros: `cite:[KEY]`, `bibitem:[KEY]` and `bibliography::[]`. Citations are parsed and replaced with formatted inline texts, and reference lists are automatically generated and inserted into where `bibliography::[]` is placed. `bibitem:[KEY]` will insert a rendered bibliography item directly into the text.
22
28
 
23
- asciidoctor-bibtex is designed to be used as an extension to {url-asciidoctor}[Asciidoctor].
24
- Thus this extension can be used together with other asciidoctor extensions such as {url-asciidoctor-mathematical}[asciidoctor-mathematical] and {url-asciidoctor-pdf}[asciidoctor-pdf] to enrich your AsciiDoc experience.
29
+ asciidoctor-bibtex is designed to be used as an extension to {url-asciidoctor}[Asciidoctor]. Thus this extension can be used together with other asciidoctor extensions such as {url-asciidoctor-mathematical}[asciidoctor-mathematical] and {url-asciidoctor-pdf}[asciidoctor-pdf] to enrich your AsciiDoc experience.
25
30
 
26
31
  NOTE: asciidoctor-bibtex no longer supports AsciiDoc-to-AsciiDoc conversion.
27
32
 
@@ -45,7 +50,7 @@ asciidoctor-bibtex depends on https://github.com/inukshuk/bibtex-ruby[bibtex-rub
45
50
  Ensure the `ruby-dev` and `libxslt1-dev` packages are installed on your machine so the dependencies will compile properly.
46
51
 
47
52
  {url-asciidoctor}[Asciidoctor] must also be installed for 'asciidoctor-bibtex' to work.
48
- Asciidoctor version 1.5.2 or higher is required.
53
+ Asciidoctor version 2.0.0 or higher is required.
49
54
 
50
55
  == Usage
51
56
 
@@ -78,6 +83,21 @@ For *ieee*, a numeric style:
78
83
 
79
84
  To add a list of formatted references, place `bibliography::[]` on a line by itself.
80
85
 
86
+ One can use `bibitem:[Lane12]` to insert a rendered bibliography item inline, maybe to generate a cv. For example:
87
+
88
+ [source, asciidoc]
89
+ ----
90
+ = My CV
91
+
92
+ == Publications
93
+
94
+ === 2019
95
+
96
+ - bibitem:[Me2019a]
97
+ - bibitem:[Me2019b]
98
+ - bibitem:[Me2019c]
99
+ ----
100
+
81
101
  === Configuration
82
102
 
83
103
  Configuration is applied in the form of AsciiDoc document attributes, which must be defined in the document header.
@@ -109,6 +129,11 @@ Configuration is applied in the form of AsciiDoc document attributes, which must
109
129
  | Throw an error on unknown references
110
130
  | `true` or `false`
111
131
  | `false`
132
+
133
+ | bibtex-citation-template
134
+ | Custom citation template for numeric style
135
+ | Any string matching `/(.+?)\$id(.+)/`
136
+ | `[$id]`
112
137
  |===
113
138
 
114
139
  === Commandline
@@ -0,0 +1,53 @@
1
+ #
2
+ # BibitemMacro.rb
3
+ #
4
+ # Copyright (c) Zhang Yang, 2020.
5
+ #
6
+ # Released under Open Works License, 0.9.2
7
+ #
8
+
9
+ module AsciidoctorBibtex
10
+ # BibitemMacro
11
+ #
12
+ # Class to hold information about a bibitem macro. A bibtem macro has
13
+ # only text and key
14
+ #
15
+ # This class also provides a class method to extract macros from a line of
16
+ # text.
17
+ #
18
+ class BibitemMacro
19
+ #
20
+ # Grammar for the bibitem macro: bibitem:[key]
21
+ #
22
+
23
+ # matches a bibitem key
24
+ BIBITEM_KEY = /[^\s\]]+/.freeze
25
+ # matches the full macro
26
+ BIBITEM_MACRO = /bibitem:\[(#{BIBITEM_KEY})\]/.freeze
27
+
28
+ # Given a line, return a list of BibitemMacro instances
29
+ def self.extract_macros(line)
30
+ result = []
31
+ full = BIBITEM_MACRO.match line
32
+ while full
33
+ text = full[0]
34
+ key = full[1]
35
+ result << BibitemMacro.new(text, key)
36
+ # look for next citation on line
37
+ full = BIBITEM_MACRO.match full.post_match
38
+ end
39
+ result
40
+ end
41
+
42
+ attr_reader :text, :key
43
+
44
+ # Create a BibitemMacro object
45
+ #
46
+ # text: the full macro text matched by BIBITEM_MACRO
47
+ # key: bibitem key
48
+ def initialize(text, key)
49
+ @text = text
50
+ @key = key
51
+ end
52
+ end
53
+ end
@@ -54,7 +54,7 @@ module AsciidoctorBibtex
54
54
 
55
55
  # Given a line, return a list of CitationData instances
56
56
  # containing information on each set of citation information
57
- def self.extract_citations(line)
57
+ def self.extract_macros(line)
58
58
  result = []
59
59
  full = CITATION_MACRO.match line
60
60
  while full
@@ -67,6 +67,7 @@ module AsciidoctorBibtex
67
67
  bibtex_order = ((document.attr 'bibtex-order') || 'appearance').to_sym
68
68
  bibtex_format = ((document.attr 'bibtex-format') || 'asciidoc').to_sym
69
69
  bibtex_throw = ((document.attr 'bibtex-throw') || 'false').to_s.downcase
70
+ bibtex_citation_template = ((document.attr 'bibtex-citation-template') || '[$id]').to_s
70
71
 
71
72
  # Fild bibtex file automatically if not supplied.
72
73
  if bibtex_file.empty?
@@ -84,23 +85,29 @@ module AsciidoctorBibtex
84
85
  prose_blocks = document.find_by do |b|
85
86
  (b.content_model == :simple) ||
86
87
  (b.context == :list_item) ||
87
- (b.context == :table_cell)
88
+ (b.context == :table_cell) ||
89
+ (b.title?)
88
90
  end
89
91
  return nil if prose_blocks.nil?
90
92
 
91
93
  processor = Processor.new bibtex_file, true, bibtex_style, bibtex_locale,
92
94
  bibtex_order == :appearance, bibtex_format,
93
- bibtex_throw == 'true'
95
+ bibtex_throw == 'true', custom_citation_template: bibtex_citation_template
94
96
 
95
97
  # First pass: extract all citations.
96
98
  prose_blocks.each do |block|
97
99
  if block.context == :list_item || block.context == :table_cell
98
100
  line = block.text
99
- processor.process_citation_macros line
100
- else
101
+ unless line.nil? || line.empty?
102
+ processor.process_citation_macros line
103
+ end
104
+ elsif block.content_model == :simple
101
105
  block.lines.each do |line|
102
106
  processor.process_citation_macros line
103
107
  end
108
+ else
109
+ line = block.title
110
+ processor.process_citation_macros line
104
111
  end
105
112
  end
106
113
  # Make processor finalize macro processing as required.
@@ -111,15 +118,23 @@ module AsciidoctorBibtex
111
118
  if block.context == :list_item || block.context == :table_cell
112
119
  # NOTE: we access the instance variable @text for raw text.
113
120
  line = block.instance_variable_get(:@text)
114
- if !line.empty?
121
+ unless line.nil? or line.empty?
115
122
  line = processor.replace_citation_macros(line)
123
+ line = processor.replace_bibitem_macros(line)
116
124
  block.text = line
117
125
  end
118
- else
126
+ elsif block.content_model == :simple
119
127
  block.lines.each_with_index do |line, index|
120
128
  line = processor.replace_citation_macros(line)
129
+ line = processor.replace_bibitem_macros(line)
121
130
  block.lines[index] = line
122
131
  end
132
+ else
133
+ # NOTE: we access the instance variable @text for raw text.
134
+ line = block.instance_variable_get(:@title)
135
+ line = processor.replace_citation_macros(line)
136
+ # line = processor.replace_bibitem_macros(line)
137
+ block.title = line
123
138
  end
124
139
  end
125
140
 
@@ -18,6 +18,7 @@ require 'set'
18
18
 
19
19
  require_relative 'citation_macro'
20
20
  require_relative 'citation_utils'
21
+ require_relative 'bibitem_macro'
21
22
  require_relative 'string_utils'
22
23
  require_relative 'style_utils'
23
24
 
@@ -49,7 +50,7 @@ module AsciidoctorBibtex
49
50
  class Processor
50
51
  def initialize(bibfile, links = false, style = 'ieee', locale = 'en-US',
51
52
  numeric_in_appearance_order = false, output = :asciidoc,
52
- throw_on_unknown = false)
53
+ throw_on_unknown = false, custom_citation_template: '[$id]')
53
54
  raise "File '#{bibfile}' is not found" unless FileTest.file? bibfile
54
55
 
55
56
  bibtex = BibTeX.open bibfile, filter: [LatexFilter]
@@ -62,6 +63,13 @@ module AsciidoctorBibtex
62
63
  @filenames = Set.new
63
64
  @output = output
64
65
  @throw_on_unknown = throw_on_unknown
66
+ @bibtex_ob = '['
67
+ @bibtex_cb = ']'
68
+ match = custom_citation_template.match(/^(.+?)\$id(.+)$/)
69
+ unless match.nil?
70
+ @bibtex_ob = match[1]
71
+ @bibtex_cb = match[2]
72
+ end
65
73
 
66
74
  if (output != :latex) && (output != :bibtex) && (output != :biblatex)
67
75
  @citeproc = CiteProc::Processor.new style: @style, format: :html, locale: @locale
@@ -75,7 +83,7 @@ module AsciidoctorBibtex
75
83
  # processor will build a list of all citation keys in the same order as they
76
84
  # appear in the original document.
77
85
  def process_citation_macros(line)
78
- CitationMacro.extract_citations(line).each do |citation|
86
+ CitationMacro.extract_macros(line).each do |citation|
79
87
  @citations += citation.items.collect(&:key)
80
88
  end
81
89
  end
@@ -110,19 +118,29 @@ module AsciidoctorBibtex
110
118
  #
111
119
  # Return new text with all macros replaced.
112
120
  def replace_citation_macros(line)
113
- CitationMacro.extract_citations(line).each do |citation|
121
+ CitationMacro.extract_macros(line).each do |citation|
114
122
  line = line.gsub(citation.text, build_citation_text(citation))
115
123
  end
116
124
  line
117
125
  end
118
126
 
127
+ # Replace bibitem macros with rendered bibitem.
128
+ #
129
+ # Return new text with all macros replaced.
130
+ def replace_bibitem_macros(line)
131
+ BibitemMacro.extract_macros(line).each do |item|
132
+ line = line.gsub(item.text, build_bibitem_text(item.key))
133
+ end
134
+ line
135
+ end
136
+
119
137
  # Build the bibliography list just as bibtex.
120
138
  #
121
139
  # Return an array of texts representing an asciidoc list.
122
140
  def build_bibliography_list
123
141
  result = []
124
- @citations.each do |ref|
125
- result << build_bibliography_item(ref)
142
+ @citations.each_with_index do |ref, index|
143
+ result << build_bibliography_item(ref, index)
126
144
  result << ''
127
145
  end
128
146
  result
@@ -132,17 +150,42 @@ module AsciidoctorBibtex
132
150
  # Internal functions
133
151
  #
134
152
 
153
+ # Build the asciidoc text for a single bibliography item
154
+ def build_bibitem_text(key)
155
+ begin
156
+ if @biblio[key].nil?
157
+ puts "Unknown reference: #{key}"
158
+ cptext = key
159
+ else
160
+ cptext = @citeproc.render :bibliography, id: key
161
+ cptext = cptext.first
162
+ end
163
+ rescue Exception => e
164
+ puts "Failed to render #{key}: #{e}"
165
+ cptext = key
166
+ end
167
+ StringUtils.html_to_asciidoc(cptext)
168
+ end
169
+
135
170
  # Build bibliography text for a given reference
136
- def build_bibliography_item(key)
171
+ def build_bibliography_item(key, index = 0)
172
+ index += 1
137
173
  result = ''
138
- result << '. ' if StyleUtils.is_numeric? @style
139
174
 
140
175
  begin
141
- cptext = @citeproc.render :bibliography, id: key
176
+ cptext = if @biblio[key].nil?
177
+ nil
178
+ else
179
+ @citeproc.render :bibliography, id: key
180
+ end
142
181
  rescue Exception => e
143
182
  puts "Failed to render #{key}: #{e}"
144
183
  end
184
+
145
185
  result << "[[#{key}]]" if @links
186
+ if StyleUtils.is_numeric? @style
187
+ result << "#{@bibtex_ob}#{index}#{@bibtex_cb} "
188
+ end
146
189
  if cptext.nil?
147
190
  return result + key
148
191
  else
@@ -177,8 +220,8 @@ module AsciidoctorBibtex
177
220
  else
178
221
  result = ''
179
222
  if StyleUtils.is_numeric? @style
180
- ob = '+[+'
181
- cb = '+]+'
223
+ ob = "+#{@bibtex_ob}+"
224
+ cb = "+#{@bibtex_cb}+"
182
225
  separator = ','
183
226
  elsif macro.type == 'cite'
184
227
  ob = '('
@@ -200,14 +243,14 @@ module AsciidoctorBibtex
200
243
  # if found, insert reference information
201
244
  if @biblio[cite.key].nil?
202
245
  if @throw_on_unknown
203
- raise "Unknown reference: #{cite.ref}"
246
+ raise "Unknown reference: #{cite.key}"
204
247
  else
205
- puts "Unknown reference: #{cite.ref}"
206
- cite_text = cite.ref.to_s
248
+ puts "Unknown reference: #{cite.key}"
249
+ cite_text = cite.key.to_s
207
250
  end
208
251
  else
209
252
  cite_text = citation_text(macro, cite)
210
- end
253
+ end
211
254
 
212
255
  result << StringUtils.html_to_asciidoc(cite_text)
213
256
  result << '>>' if @links
@@ -229,13 +272,13 @@ module AsciidoctorBibtex
229
272
  result << ' '
230
273
  # use p.x for single numerical page and pp.x for all others. This will
231
274
  # produce pp. 1 seq for complex locators, which is the correct behavior.
232
- if @style.include? 'chicago'
233
- result << cite.locator
234
- elsif /^\d+$/ =~ cite.locator
235
- result << "p.&#160;#{cite.locator}"
236
- else
237
- result << "pp.&#160;#{cite.locator}"
238
- end
275
+ result << if @style.include? 'chicago'
276
+ cite.locator
277
+ elsif /^\d+$/ =~ cite.locator
278
+ "p.&#160;#{cite.locator}"
279
+ else
280
+ "pp.&#160;#{cite.locator}"
281
+ end
239
282
  end
240
283
 
241
284
  result
@@ -266,7 +309,7 @@ module AsciidoctorBibtex
266
309
  cite_text = @citeproc.render :citation, id: cite.key
267
310
  cite_text = cite_text.gsub('(', '')
268
311
  cite_text = cite_text.gsub(')', '')
269
- cite_text = cite_text + format_locator(cite)
312
+ cite_text += format_locator(cite)
270
313
  year = @biblio[cite.key].year
271
314
  if !year.nil? && macro.type == 'citenp'
272
315
  segs = cite_text.partition(year.to_s)
@@ -1,3 +1,3 @@
1
1
  module AsciidoctorBibtex
2
- VERSION = '0.6.0'
2
+ VERSION = '0.7.1'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: asciidoctor-bibtex
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zhang YANG
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-09-04 00:00:00.000000000 Z
11
+ date: 2020-02-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: asciidoctor
@@ -122,6 +122,7 @@ files:
122
122
  - LICENSE.txt
123
123
  - README.adoc
124
124
  - lib/asciidoctor-bibtex.rb
125
+ - lib/asciidoctor-bibtex/bibitem_macro.rb
125
126
  - lib/asciidoctor-bibtex/citation_macro.rb
126
127
  - lib/asciidoctor-bibtex/citation_utils.rb
127
128
  - lib/asciidoctor-bibtex/extensions.rb
@@ -149,7 +150,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
149
150
  - !ruby/object:Gem::Version
150
151
  version: '0'
151
152
  requirements: []
152
- rubygems_version: 3.0.3
153
+ rubygems_version: 3.0.6
153
154
  signing_key:
154
155
  specification_version: 4
155
156
  summary: An Asciidoctor extension that adds bibtex integration to AsciiDoc