-
Notifications
You must be signed in to change notification settings - Fork 28
/
processor.rb
207 lines (177 loc) · 5.64 KB
/
processor.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#
# Manage the current set of citations, the document settings,
# and main operations.
#
module AsciidoctorBibtex
# Class used through utility method to hold data about citations for
# current document, and run the different steps to add the citations
# and bibliography
class Processor
include ProcessorUtils
attr_reader :biblio, :links, :style, :citations
def initialize biblio, links, style, locale, numeric_in_appearance_order = false, output = :asciidoc, bibfile = ""
@biblio = biblio
@links = links
@numeric_in_appearance_order = numeric_in_appearance_order
@style = style
@locale = locale
@citations = Citations.new
@filenames = Set.new
@output = output
@bibfile = bibfile
if output != :latex and output != :bibtex and output != :biblatex
@citeproc = CiteProc::Processor.new style: @style, format: :html, locale: @locale
@citeproc.import @biblio.to_citeproc
end
end
# Return the complete citation text for given cite_data
def complete_citation cite_data
if @output == :latex or @output == :bibtex or @output == :biblatex
result = '+++'
cite_data.cites.each do |cite|
# NOTE: xelatex does not support "\citenp", so we output all
# references as "cite" here unless we're using biblatex.
if @output == :biblatex
if cite_data.type == "citenp"
result << "\\" << 'textcite'
else
result << "\\" << 'parencite'
end
else
result << "\\" << 'cite'
end
if cite.pages != ''
result << "[p. " << cite.pages << "]"
end
result << "{" << "#{cite.ref}" << "},"
end
if result[-1] == ','
result = result[0..-2]
end
result << "+++"
return result
else
result = ''
ob, cb = '(', ')'
cite_data.cites.each_with_index do |cite, index|
# before all items apart from the first, insert appropriate separator
result << "#{separator} " unless index.zero?
# @links requires adding hyperlink to reference
result << "<<#{cite.ref}," if @links
# if found, insert reference information
unless biblio[cite.ref].nil?
item = biblio[cite.ref].clone
cite_text, ob, cb = make_citation item, cite.ref, cite_data, cite
else
puts "Unknown reference: #{cite.ref}"
cite_text = "#{cite.ref}"
end
result << cite_text.html_to_asciidoc
# @links requires finish hyperlink
result << ">>" if @links
end
unless @links
# combine numeric ranges
if Styles.is_numeric? @style
result = combine_consecutive_numbers result
end
end
return include_pretext result, cite_data, ob, cb
end
end
# Retrieve text for reference in given style
# - ref is reference for item to give reference for
def get_reference ref
result = ""
result << ". " if Styles.is_numeric? @style
begin
cptext = @citeproc.render :bibliography, id: ref
rescue Exception => e
puts "Failed to render #{ref}: #{e}"
end
result << "[[#{ref}]]" if @links
if cptext.nil?
return result+ref
else
result << cptext.first
end
return result.html_to_asciidoc
end
def separator
if Styles.is_numeric? @style
','
else
';'
end
end
# Format pages with pp/p as appropriate
def with_pp pages
return '' if pages.empty?
if @style.include? "chicago"
pages
elsif pages.include? '-'
"pp. #{pages}"
else
"p. #{pages}"
end
end
# Return page string for given cite
def page_str cite
result = ''
unless cite.pages.empty?
result << "," unless Styles.is_numeric? @style
result << " #{with_pp(cite.pages)}"
end
return result
end
def include_pretext result, cite_data, ob, cb
pretext = cite_data.pretext
pretext += ' ' unless pretext.empty? # add space after any content
if Styles.is_numeric? @style
"#{pretext}#{ob}#{result}#{cb}"
elsif cite_data.type == "cite"
"#{ob}#{pretext}#{result}#{cb}"
else
"#{pretext}#{result}"
end
end
# Numeric citations are handled by computing the position of the reference
# in the list of used citations.
# Other citations are formatted by citeproc.
def make_citation item, ref, cite_data, cite
if Styles.is_numeric? @style
cite_text = if @numeric_in_appearance_order
"#{@citations.cites_used.index(cite.ref) + 1}"
else
"#{sorted_cites.index(cite.ref) + 1}"
end
fc = '['
lc = ']'
else
cite_text = @citeproc.process id: ref, mode: :citation
fc = ''
lc = ''
end
if Styles.is_numeric? @style
cite_text << "#{page_str(cite)}"
elsif cite_data.type == "citenp"
cite_text.gsub!(item.year, "#{fc}#{item.year}#{page_str(cite)}#{lc}")
cite_text.gsub!(", #{fc}", " #{fc}")
else
cite_text << page_str(cite)
end
cite_text.gsub!(",", ",") if @links # replace comma
return cite_text, fc, lc
end
def sorted_cites
@citations.sorted_cites @biblio
end
def cites
if Styles.is_numeric?(@style) and @numeric_in_appearance_order
@citations.cites_used
else
sorted_cites
end
end
end
end