asciidoctor-csl 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
+ SHA256:
3
+ metadata.gz: 2ce78146d55d20de4621859574c6bbdc2cefa673f8712c7149c0370bc121cf12
4
+ data.tar.gz: be41bf38def6db2dc0608d6e5d0dbedf50f2abeaa439e78429dc7bf4db11aff0
5
+ SHA512:
6
+ metadata.gz: 9449c142d9f2b667a8cd73e569eb61b889957ce49ceb9ad9221720f40e0e6870eb4154f9b2bffcd07b8b4db94ce203e0dccdd135781a1418f73358ea57d191cd
7
+ data.tar.gz: af718af924f6876712b042a24e40112327fddf1ceac50c51cea26c0f3e1e7e3b09139059fca5592189cd24d8a5dde028503def04bd49dbbd83b99409b35eb1eb
data/LICENSE.txt ADDED
@@ -0,0 +1,287 @@
1
+ EUROPEAN UNION PUBLIC LICENCE v. 1.2
2
+ EUPL © the European Union 2007, 2016
3
+
4
+ This European Union Public Licence (the ‘EUPL’) applies to the Work (as defined
5
+ below) which is provided under the terms of this Licence. Any use of the Work,
6
+ other than as authorised under this Licence is prohibited (to the extent such
7
+ use is covered by a right of the copyright holder of the Work).
8
+
9
+ The Work is provided under the terms of this Licence when the Licensor (as
10
+ defined below) has placed the following notice immediately following the
11
+ copyright notice for the Work:
12
+
13
+ Licensed under the EUPL
14
+
15
+ or has expressed by any other means his willingness to license under the EUPL.
16
+
17
+ 1. Definitions
18
+
19
+ In this Licence, the following terms have the following meaning:
20
+
21
+ - ‘The Licence’: this Licence.
22
+
23
+ - ‘The Original Work’: the work or software distributed or communicated by the
24
+ Licensor under this Licence, available as Source Code and also as Executable
25
+ Code as the case may be.
26
+
27
+ - ‘Derivative Works’: the works or software that could be created by the
28
+ Licensee, based upon the Original Work or modifications thereof. This Licence
29
+ does not define the extent of modification or dependence on the Original Work
30
+ required in order to classify a work as a Derivative Work; this extent is
31
+ determined by copyright law applicable in the country mentioned in Article 15.
32
+
33
+ - ‘The Work’: the Original Work or its Derivative Works.
34
+
35
+ - ‘The Source Code’: the human-readable form of the Work which is the most
36
+ convenient for people to study and modify.
37
+
38
+ - ‘The Executable Code’: any code which has generally been compiled and which is
39
+ meant to be interpreted by a computer as a program.
40
+
41
+ - ‘The Licensor’: the natural or legal person that distributes or communicates
42
+ the Work under the Licence.
43
+
44
+ - ‘Contributor(s)’: any natural or legal person who modifies the Work under the
45
+ Licence, or otherwise contributes to the creation of a Derivative Work.
46
+
47
+ - ‘The Licensee’ or ‘You’: any natural or legal person who makes any usage of
48
+ the Work under the terms of the Licence.
49
+
50
+ - ‘Distribution’ or ‘Communication’: any act of selling, giving, lending,
51
+ renting, distributing, communicating, transmitting, or otherwise making
52
+ available, online or offline, copies of the Work or providing access to its
53
+ essential functionalities at the disposal of any other natural or legal
54
+ person.
55
+
56
+ 2. Scope of the rights granted by the Licence
57
+
58
+ The Licensor hereby grants You a worldwide, royalty-free, non-exclusive,
59
+ sublicensable licence to do the following, for the duration of copyright vested
60
+ in the Original Work:
61
+
62
+ - use the Work in any circumstance and for all usage,
63
+ - reproduce the Work,
64
+ - modify the Work, and make Derivative Works based upon the Work,
65
+ - communicate to the public, including the right to make available or display
66
+ the Work or copies thereof to the public and perform publicly, as the case may
67
+ be, the Work,
68
+ - distribute the Work or copies thereof,
69
+ - lend and rent the Work or copies thereof,
70
+ - sublicense rights in the Work or copies thereof.
71
+
72
+ Those rights can be exercised on any media, supports and formats, whether now
73
+ known or later invented, as far as the applicable law permits so.
74
+
75
+ In the countries where moral rights apply, the Licensor waives his right to
76
+ exercise his moral right to the extent allowed by law in order to make effective
77
+ the licence of the economic rights here above listed.
78
+
79
+ The Licensor grants to the Licensee royalty-free, non-exclusive usage rights to
80
+ any patents held by the Licensor, to the extent necessary to make use of the
81
+ rights granted on the Work under this Licence.
82
+
83
+ 3. Communication of the Source Code
84
+
85
+ The Licensor may provide the Work either in its Source Code form, or as
86
+ Executable Code. If the Work is provided as Executable Code, the Licensor
87
+ provides in addition a machine-readable copy of the Source Code of the Work
88
+ along with each copy of the Work that the Licensor distributes or indicates, in
89
+ a notice following the copyright notice attached to the Work, a repository where
90
+ the Source Code is easily and freely accessible for as long as the Licensor
91
+ continues to distribute or communicate the Work.
92
+
93
+ 4. Limitations on copyright
94
+
95
+ Nothing in this Licence is intended to deprive the Licensee of the benefits from
96
+ any exception or limitation to the exclusive rights of the rights owners in the
97
+ Work, of the exhaustion of those rights or of other applicable limitations
98
+ thereto.
99
+
100
+ 5. Obligations of the Licensee
101
+
102
+ The grant of the rights mentioned above is subject to some restrictions and
103
+ obligations imposed on the Licensee. Those obligations are the following:
104
+
105
+ Attribution right: The Licensee shall keep intact all copyright, patent or
106
+ trademarks notices and all notices that refer to the Licence and to the
107
+ disclaimer of warranties. The Licensee must include a copy of such notices and a
108
+ copy of the Licence with every copy of the Work he/she distributes or
109
+ communicates. The Licensee must cause any Derivative Work to carry prominent
110
+ notices stating that the Work has been modified and the date of modification.
111
+
112
+ Copyleft clause: If the Licensee distributes or communicates copies of the
113
+ Original Works or Derivative Works, this Distribution or Communication will be
114
+ done under the terms of this Licence or of a later version of this Licence
115
+ unless the Original Work is expressly distributed only under this version of the
116
+ Licence — for example by communicating ‘EUPL v. 1.2 only’. The Licensee
117
+ (becoming Licensor) cannot offer or impose any additional terms or conditions on
118
+ the Work or Derivative Work that alter or restrict the terms of the Licence.
119
+
120
+ Compatibility clause: If the Licensee Distributes or Communicates Derivative
121
+ Works or copies thereof based upon both the Work and another work licensed under
122
+ a Compatible Licence, this Distribution or Communication can be done under the
123
+ terms of this Compatible Licence. For the sake of this clause, ‘Compatible
124
+ Licence’ refers to the licences listed in the appendix attached to this Licence.
125
+ Should the Licensee's obligations under the Compatible Licence conflict with
126
+ his/her obligations under this Licence, the obligations of the Compatible
127
+ Licence shall prevail.
128
+
129
+ Provision of Source Code: When distributing or communicating copies of the Work,
130
+ the Licensee will provide a machine-readable copy of the Source Code or indicate
131
+ a repository where this Source will be easily and freely available for as long
132
+ as the Licensee continues to distribute or communicate the Work.
133
+
134
+ Legal Protection: This Licence does not grant permission to use the trade names,
135
+ trademarks, service marks, or names of the Licensor, except as required for
136
+ reasonable and customary use in describing the origin of the Work and
137
+ reproducing the content of the copyright notice.
138
+
139
+ 6. Chain of Authorship
140
+
141
+ The original Licensor warrants that the copyright in the Original Work granted
142
+ hereunder is owned by him/her or licensed to him/her and that he/she has the
143
+ power and authority to grant the Licence.
144
+
145
+ Each Contributor warrants that the copyright in the modifications he/she brings
146
+ to the Work are owned by him/her or licensed to him/her and that he/she has the
147
+ power and authority to grant the Licence.
148
+
149
+ Each time You accept the Licence, the original Licensor and subsequent
150
+ Contributors grant You a licence to their contributions to the Work, under the
151
+ terms of this Licence.
152
+
153
+ 7. Disclaimer of Warranty
154
+
155
+ The Work is a work in progress, which is continuously improved by numerous
156
+ Contributors. It is not a finished work and may therefore contain defects or
157
+ ‘bugs’ inherent to this type of development.
158
+
159
+ For the above reason, the Work is provided under the Licence on an ‘as is’ basis
160
+ and without warranties of any kind concerning the Work, including without
161
+ limitation merchantability, fitness for a particular purpose, absence of defects
162
+ or errors, accuracy, non-infringement of intellectual property rights other than
163
+ copyright as stated in Article 6 of this Licence.
164
+
165
+ This disclaimer of warranty is an essential part of the Licence and a condition
166
+ for the grant of any rights to the Work.
167
+
168
+ 8. Disclaimer of Liability
169
+
170
+ Except in the cases of wilful misconduct or damages directly caused to natural
171
+ persons, the Licensor will in no event be liable for any direct or indirect,
172
+ material or moral, damages of any kind, arising out of the Licence or of the use
173
+ of the Work, including without limitation, damages for loss of goodwill, work
174
+ stoppage, computer failure or malfunction, loss of data or any commercial
175
+ damage, even if the Licensor has been advised of the possibility of such damage.
176
+ However, the Licensor will be liable under statutory product liability laws as
177
+ far such laws apply to the Work.
178
+
179
+ 9. Additional agreements
180
+
181
+ While distributing the Work, You may choose to conclude an additional agreement,
182
+ defining obligations or services consistent with this Licence. However, if
183
+ accepting obligations, You may act only on your own behalf and on your sole
184
+ responsibility, not on behalf of the original Licensor or any other Contributor,
185
+ and only if You agree to indemnify, defend, and hold each Contributor harmless
186
+ for any liability incurred by, or claims asserted against such Contributor by
187
+ the fact You have accepted any warranty or additional liability.
188
+
189
+ 10. Acceptance of the Licence
190
+
191
+ The provisions of this Licence can be accepted by clicking on an icon ‘I agree’
192
+ placed under the bottom of a window displaying the text of this Licence or by
193
+ affirming consent in any other similar way, in accordance with the rules of
194
+ applicable law. Clicking on that icon indicates your clear and irrevocable
195
+ acceptance of this Licence and all of its terms and conditions.
196
+
197
+ Similarly, you irrevocably accept this Licence and all of its terms and
198
+ conditions by exercising any rights granted to You by Article 2 of this Licence,
199
+ such as the use of the Work, the creation by You of a Derivative Work or the
200
+ Distribution or Communication by You of the Work or copies thereof.
201
+
202
+ 11. Information to the public
203
+
204
+ In case of any Distribution or Communication of the Work by means of electronic
205
+ communication by You (for example, by offering to download the Work from a
206
+ remote location) the distribution channel or media (for example, a website) must
207
+ at least provide to the public the information requested by the applicable law
208
+ regarding the Licensor, the Licence and the way it may be accessible, concluded,
209
+ stored and reproduced by the Licensee.
210
+
211
+ 12. Termination of the Licence
212
+
213
+ The Licence and the rights granted hereunder will terminate automatically upon
214
+ any breach by the Licensee of the terms of the Licence.
215
+
216
+ Such a termination will not terminate the licences of any person who has
217
+ received the Work from the Licensee under the Licence, provided such persons
218
+ remain in full compliance with the Licence.
219
+
220
+ 13. Miscellaneous
221
+
222
+ Without prejudice of Article 9 above, the Licence represents the complete
223
+ agreement between the Parties as to the Work.
224
+
225
+ If any provision of the Licence is invalid or unenforceable under applicable
226
+ law, this will not affect the validity or enforceability of the Licence as a
227
+ whole. Such provision will be construed or reformed so as necessary to make it
228
+ valid and enforceable.
229
+
230
+ The European Commission may publish other linguistic versions or new versions of
231
+ this Licence or updated versions of the Appendix, so far this is required and
232
+ reasonable, without reducing the scope of the rights granted by the Licence. New
233
+ versions of the Licence will be published with a unique version number.
234
+
235
+ All linguistic versions of this Licence, approved by the European Commission,
236
+ have identical value. Parties can take advantage of the linguistic version of
237
+ their choice.
238
+
239
+ 14. Jurisdiction
240
+
241
+ Without prejudice to specific agreement between parties,
242
+
243
+ - any litigation resulting from the interpretation of this License, arising
244
+ between the European Union institutions, bodies, offices or agencies, as a
245
+ Licensor, and any Licensee, will be subject to the jurisdiction of the Court
246
+ of Justice of the European Union, as laid down in article 272 of the Treaty on
247
+ the Functioning of the European Union,
248
+
249
+ - any litigation arising between other parties and resulting from the
250
+ interpretation of this License, will be subject to the exclusive jurisdiction
251
+ of the competent court where the Licensor resides or conducts its primary
252
+ business.
253
+
254
+ 15. Applicable Law
255
+
256
+ Without prejudice to specific agreement between parties,
257
+
258
+ - this Licence shall be governed by the law of the European Union Member State
259
+ where the Licensor has his seat, resides or has his registered office,
260
+
261
+ - this licence shall be governed by Belgian law if the Licensor has no seat,
262
+ residence or registered office inside a European Union Member State.
263
+
264
+ Appendix
265
+
266
+ ‘Compatible Licences’ according to Article 5 EUPL are:
267
+
268
+ - GNU General Public License (GPL) v. 2, v. 3
269
+ - GNU Affero General Public License (AGPL) v. 3
270
+ - Open Software License (OSL) v. 2.1, v. 3.0
271
+ - Eclipse Public License (EPL) v. 1.0
272
+ - CeCILL v. 2.0, v. 2.1
273
+ - Mozilla Public Licence (MPL) v. 2
274
+ - GNU Lesser General Public Licence (LGPL) v. 2.1, v. 3
275
+ - Creative Commons Attribution-ShareAlike v. 3.0 Unported (CC BY-SA 3.0) for
276
+ works other than software
277
+ - European Union Public Licence (EUPL) v. 1.1, v. 1.2
278
+ - Québec Free and Open-Source Licence — Reciprocity (LiLiQ-R) or Strong
279
+ Reciprocity (LiLiQ-R+).
280
+
281
+ The European Commission may update this Appendix to later versions of the above
282
+ licences without producing a new version of the EUPL, as long as they provide
283
+ the rights granted in Article 2 of this Licence and protect the covered Source
284
+ Code from exclusive appropriation.
285
+
286
+ All other changes or additions to this Appendix require the production of a new
287
+ EUPL version.
data/README.adoc ADDED
@@ -0,0 +1,116 @@
1
+ = asciidoctor-csl: CSL integration for AsciiDoc
2
+
3
+ asciidoctor-csl adds CSL integration for AsciiDoc.
4
+ The macros to use are heavily inspired by https://github.com/asciidoctor-contrib/asciidoctor-bibtex[asciidoctor-bibtex].
5
+ It is thus not possible to use both at the same time.
6
+
7
+ Note: this gem is new and subject to change.
8
+
9
+ == Use
10
+
11
+ Loading the extension:
12
+
13
+ [source,shell]
14
+ ----
15
+ asciidoctor -r asciidoctor-csl document.adoc
16
+ ----
17
+
18
+ You need to configure which bibliography file to use, in https://citeproc-js.readthedocs.io/en/latest/csl-json/markup.html[CSL-JSON] (`.json`), or as YAML (`.yml` or `.yaml`):
19
+
20
+ [source,asciidoc]
21
+ ----
22
+ = My document
23
+ :csl-file: references.json
24
+ :csl-style: apa
25
+
26
+ As shown by cite:smith2020[], ...
27
+
28
+ == References
29
+
30
+ bibliography::[]
31
+ ----
32
+
33
+ === Configuration
34
+
35
+ These document attributes configure the extension:
36
+
37
+ `csl-file`::
38
+ The bibliography file, relative to the document.
39
+ Required: without it, the macros are not rendered.
40
+
41
+ `csl-style`::
42
+ The citation style, as the name of a style from https://github.com/citation-style-language/styles[the CSL style repository], e.g. `apa`, `ieee`, or `chicago-notes-bibliography-17th-edition`.
43
+ Defaults to `ieee`.
44
+
45
+ `csl-lang`::
46
+ The language used for terms such as "p." or "and".
47
+ Defaults to the `lang` attribute of the document, or `en` if that is not set.
48
+
49
+ === Citations
50
+
51
+ `cite:key[]` cites a source.
52
+ Cite multiple sources in one citation with commas: `cite:smith2020,doe2019[]`.
53
+
54
+ The macro accepts these options:
55
+
56
+ [cols="1,3"]
57
+ |===
58
+ |Attribute |Description
59
+
60
+ |locator (1st positional)
61
+ |The location in the source, e.g. `cite:smith2020[12]`.
62
+ Quote values containing a comma: `cite:smith2020["12, 14"]`.
63
+
64
+ |label (2nd positional)
65
+ |The kind of locator, e.g. `cite:smith2020[3, chapter]`.
66
+ Defaults to `page`.
67
+ Other values are `book`, `column`, `figure`, `folio`, `issue`, `line`, `note`, `opus`, `paragraph`, `part`, `section`, `sub-verbo`, `verse` and `volume` (supports all options from citeproc).
68
+
69
+ |`prefix`
70
+ |Text placed before the citation, e.g. `cite:smith2020[prefix=see]`.
71
+
72
+ |`suffix`
73
+ |Text placed after the citation, e.g. `cite:smith2020[suffix=for details]`.
74
+
75
+ |`link`
76
+ |If the document has a bibliography, each cited item links to its entry.
77
+ Use `link=false` to disable this for a single citation.
78
+ |===
79
+
80
+ With multiple keys, the locator and suffix apply to the last source, and the prefix to the first.
81
+ Footnotes are supported as well.
82
+ Citations in section titles are not supported.
83
+
84
+ === Bibliography
85
+
86
+ `bibliography::[]` renders the bibliography.
87
+ By default, it only renders the cited sources.
88
+ Use `bibliography::[select=all]` to include everything in the bibliography.
89
+
90
+ Each entry gets an anchor with the source's key as ID, like `<<smith2020>>`.
91
+
92
+ === Single entries
93
+
94
+ `bibitem:key[]` renders the entry of a single source inline.
95
+ It does not count as a citation: the source is not added to the bibliography and does not affect the numbering.
96
+
97
+ == Citation Style Language support
98
+
99
+ The gem supports almost everything that its CSL processor (`citeproc`) supports.
100
+ A few tweaks are done to make it work better with AsciiDoc:
101
+
102
+ * The gem renders quotes as https://docs.asciidoctor.org/asciidoc/latest/text/quotation-marks-and-apostrophes/["curly"] quotes, so AsciiDoctor will render them. Use a good output template, like https://github.com/niknetniko/asciidoctor-html5s[`asciidoctor-html5s`]
103
+ * The source of the CSL bibliography is parsed as AsciiDoc. This means:
104
+ - You can add formatting inside your bibliography using AsciiDoc
105
+ - The gem does not support `citeproc-js`'s default for inline HTML. A straight export from Zotero probably won't work with all AsciiDoc's output formats (but should work when outputting HTML).
106
+
107
+ == How does it work?
108
+
109
+ This gem works by using https://github.com/inukshuk/citeproc-ruby[citeproc-ruby] and https://github.com/inukshuk/csl-styles[csl-styles] to render the items.
110
+ It includes a citeproc plugin to render to AsciiDoc natively.
111
+ Otherwise, it is mostly a thin layer between AsciiDoctor and those gems.
112
+
113
+ == License
114
+
115
+ This gem is published under the EUPL 1.2.
116
+ See the LICENSE.txt file.
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'asciidoctor'
4
+ require 'asciidoctor/extensions'
5
+ require 'citeproc'
6
+
7
+ module AsciidoctorCsl
8
+ # BibitemMacro
9
+ #
10
+ # The `bibitem:key1[]` macro.
11
+ class BibitemMacro < ::Asciidoctor::Extensions::InlineMacroProcessor
12
+ include ::Asciidoctor::Logging
13
+
14
+ use_dsl
15
+
16
+ named :bibitem
17
+
18
+ attr_accessor :processor
19
+
20
+ def process(parent, target, _attributes)
21
+ entry = processor&.render_entry(target)
22
+ if entry.nil?
23
+ logger.warn "bibitem: unknown reference: #{target}"
24
+ return create_inline parent, :quoted, "[#{target}]"
25
+ end
26
+
27
+ create_inline parent, :quoted, entry, attributes: { 'subs' => :normal }
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'asciidoctor'
4
+ require 'asciidoctor/extensions'
5
+
6
+ module AsciidoctorCsl
7
+ # BibliographyBlockMacro
8
+ #
9
+ # Provides the bibliography block macro. (bibliography::[select=cited])
10
+ class BibliographyBlockMacro < ::Asciidoctor::Extensions::BlockMacroProcessor
11
+ include ::Asciidoctor::Logging
12
+
13
+ use_dsl
14
+ named :bibliography
15
+
16
+ def blocks
17
+ @blocks ||= []
18
+ end
19
+
20
+ def render_all?
21
+ @render_all
22
+ end
23
+
24
+ def process(parent, _target, attrs)
25
+ case attrs['select']
26
+ when nil, 'cited' then nil
27
+ when 'all' then @render_all = true
28
+ else
29
+ logger.warn "bibliography: unknown value for select attribute: #{attrs['select']}"
30
+ end
31
+
32
+ block = create_block parent, :open, nil, { 'role' => 'bibliography' }
33
+ blocks << block
34
+ block
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'asciidoctor'
4
+ require 'asciidoctor/extensions'
5
+ require 'citeproc'
6
+
7
+ module AsciidoctorCsl
8
+ # CitationMacro
9
+ #
10
+ # The `cite:key1,key2[locator, label, prefix=..., suffix=..., link=false]` macro.
11
+ class CitationMacro < ::Asciidoctor::Extensions::InlineMacroProcessor
12
+ include ::Asciidoctor::Logging
13
+
14
+ use_dsl
15
+
16
+ named :cite
17
+ name_positional_attributes 'locator', 'label'
18
+
19
+ attr_accessor :csl
20
+
21
+ # Whether the document has a bibliography with entries to link to.
22
+ attr_writer :bibliography
23
+
24
+ def process(parent, target, attributes)
25
+ keys = target.split(',').map(&:strip)
26
+ unknown, known = keys.partition { |key| csl.nil? || !csl.key?(key) }
27
+
28
+ logger.warn "cite: unknown references: #{unknown.join(', ')}" unless unknown.empty?
29
+ return create_inline parent, :quoted, "[#{keys.join(', ')}]" if known.empty?
30
+
31
+ items = known.map { |key| { id: key } }
32
+ items.first[:prefix] = "#{attributes['prefix']} " if attributes['prefix']
33
+
34
+ if attributes['locator']
35
+ items.last[:locator] = attributes['locator']
36
+ label = attributes['label'] || 'page'
37
+ unless CiteProc::CitationItem.labels.include?(label.to_sym)
38
+ allowed = CiteProc::CitationItem.labels.join(', ')
39
+ logger.warn "cite: unknown locator label '#{label}', expected one of #{allowed}"
40
+ end
41
+ items.last[:label] = label
42
+ end
43
+
44
+ items.last[:suffix] = ", #{attributes['suffix']}" if attributes['suffix']
45
+
46
+ if csl.nil?
47
+ logger.warn 'cite: CSL style not found'
48
+ return nil
49
+ end
50
+
51
+ csl.engine.format.link_citations = @bibliography && attributes['link'] != 'false'
52
+ text = csl.render(:citation, items)
53
+ # Support footnote styles
54
+ text = "footnote:[#{text.gsub(']', '\\]')}]" if csl.engine.style.info.citation_format == :note
55
+
56
+ create_inline parent, :quoted, text, attributes: { 'subs' => :normal }
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AsciidoctorCsl
4
+ # CitationProcessor
5
+ #
6
+ # AsciiDoc tree processor for CSL
7
+ class CitationProcessor < ::Asciidoctor::Extensions::TreeProcessor
8
+ include ::Asciidoctor::Logging
9
+
10
+ def process(document)
11
+ csl_file = (document.attr 'csl-file').to_s
12
+ csl_style = ((document.attr 'csl-style') || 'ieee').to_s
13
+ csl_lang = (document.attr('csl-lang') || document.attr('lang') || 'en').to_s
14
+
15
+ return if csl_file.empty?
16
+
17
+ # The cite macros
18
+ cite = document.extensions.inline_macros.map(&:instance).find { |m| m.name == :cite }
19
+ bibitem = document.extensions.inline_macros.map(&:instance).find { |m| m.name == :bibitem }
20
+
21
+ path_to_csl = document.normalize_system_path csl_file, document.base_dir
22
+ processor = Processor.new path_to_csl, csl_style, csl_lang
23
+
24
+ # Collect all cited keys, in appearance order.
25
+ document.find_by(traverse_documents: true) { |b| prose?(b) }.each do |block|
26
+ raw_texts(block).each do |text|
27
+ text.scan(cite.regexp) do
28
+ match = Regexp.last_match
29
+ processor.add_citations match[1].split(',') unless match[0].start_with?('\\')
30
+ end
31
+ end
32
+ end
33
+
34
+ bibliography = document.extensions.find_block_macro_extension(:bibliography).instance
35
+ # Make processor finalize macro processing as required.
36
+ processor.finalize_macro_processing render_all: bibliography.render_all?
37
+ # Provide the Citeproc renderer
38
+ cite.csl = processor.citeproc
39
+ cite.bibliography = !bibliography.blocks.empty?
40
+ bibitem.processor = processor
41
+
42
+ bibliography.blocks.each { |block| parse_content block, processor.build_bibliography_list }
43
+
44
+ nil
45
+ end
46
+
47
+ private
48
+
49
+ def prose?(block)
50
+ block.content_model == :simple || block.context == :list_item ||
51
+ block.context == :table_cell || block.title?
52
+ end
53
+
54
+ def raw_texts(block)
55
+ texts = []
56
+ texts << block.instance_variable_get(:@title) if block.title?
57
+ if %i[list_item table_cell].include?(block.context)
58
+ texts << block.instance_variable_get(:@text)
59
+ elsif block.content_model == :simple
60
+ texts.concat block.lines
61
+ end
62
+ texts.compact
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'citeproc'
4
+ require 'citeproc/ruby'
5
+
6
+ module AsciidoctorCsl
7
+ module CiteprocPatches
8
+ # Leaks suppressed variables into later renders.
9
+ module SeparateSuppression
10
+ def initialize_copy(other)
11
+ super
12
+ @suppressed = nil
13
+ end
14
+ end
15
+
16
+ # Wrap each cited item in a cross-reference to its bibliography entry,
17
+ # leaving the prefix, suffix and delimiters outside the link.
18
+ module LinkCitations
19
+ def render_single_citation(item, node)
20
+ return super unless format.respond_to?(:link_citations) && format.link_citations
21
+
22
+ item.suppress! 'author' if item.suppress_author?
23
+
24
+ text = render_layout(item, node)
25
+ text = "<<#{item.id},#{text}>>" unless text.empty?
26
+ join [item.prefix, text, item.suffix].compact
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ CiteProc::Item.prepend AsciidoctorCsl::CiteprocPatches::SeparateSuppression
33
+ CiteProc::Ruby::Renderer.prepend AsciidoctorCsl::CiteprocPatches::LinkCitations
@@ -0,0 +1,81 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AsciidoctorCsl
4
+ # Render CSL directly to AsciiDoc.
5
+ class Asciidoc < CiteProc::Ruby::Format
6
+ include ::Asciidoctor::Logging
7
+
8
+ PROTECTED_RX = /<<[^,>]+,|\{[\w-]+}|\b[a-z][\w-]*:\S*?\[[^\]]*\]|\[[^\]]*\](?=#)|\+\+\+.*?\+\+\+|\+[^+]+\+/
9
+ PLACEHOLDER_RX = /\u0000(\d+)\u0000/
10
+
11
+ # Whether rendered citations link to their bibliography entry.
12
+ attr_accessor :link_citations
13
+
14
+ def bibliography(bibliography, _locale = nil)
15
+ bibliography.connector = "\n\n"
16
+ bibliography
17
+ end
18
+
19
+ def apply_font_style
20
+ case options[:'font-style']
21
+ when 'italic', 'oblique'
22
+ # AsciiDoc does not support oblique, so italicize it.
23
+ output.replace "__#{output}__"
24
+ when 'normal' then nil
25
+ else
26
+ logger.warn "csl: unsupported font-style '#{options[:'font-style']}'"
27
+ end
28
+ end
29
+
30
+ def apply_font_variant
31
+ case options[:'font-variant']
32
+ when 'small-caps' then output.replace "[.small-caps]###{output}##"
33
+ when 'normal' then nil
34
+ else
35
+ logger.warn "csl: unsupported font-variant '#{options[:'font-variant']}'"
36
+ end
37
+ end
38
+
39
+ def apply_font_weight
40
+ case options[:'font-weight']
41
+ when 'bold' then output.replace "**#{output}**"
42
+ when 'normal', 'light'
43
+ # AsciiDoc does not support light font variant, so use normal.
44
+ nil
45
+ else
46
+ logger.warn "csl: unsupported font-weight '#{options[:'font-weight']}'"
47
+ end
48
+ end
49
+
50
+ def apply_text_decoration
51
+ case options[:'text-decoration']
52
+ when 'underline' then output.replace "[.underline]###{output}##"
53
+ when 'none' then nil
54
+ else
55
+ logger.warn "csl: unsupported text-decoration '#{options[:'text-decoration']}'"
56
+ end
57
+ end
58
+
59
+ def apply_vertical_align
60
+ case options[:'vertical-align']
61
+ when 'sup' then output.replace "^#{output.gsub(' ', '{nbsp}')}^"
62
+ when 'sub' then output.replace "~#{output.gsub(' ', '{nbsp}')}~"
63
+ when 'baseline' then nil
64
+ else
65
+ logger.warn "csl: unsupported vertical-align '#{options[:'vertical-align']}'"
66
+ end
67
+ end
68
+
69
+ # Replace the AsciiDoc syntax by placeholders without letters, so we only transform actual text.
70
+ def apply_text_case
71
+ protected = []
72
+ output.gsub!(PROTECTED_RX) do
73
+ protected << Regexp.last_match(0)
74
+ "\u0000#{protected.size - 1}\u0000"
75
+ end
76
+ super
77
+ ensure
78
+ output.gsub!(PLACEHOLDER_RX) { protected[Regexp.last_match(1).to_i] } if protected
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'asciidoctor'
4
+ require 'asciidoctor/extensions'
5
+
6
+ require_relative 'processor'
7
+ require_relative 'citation_processor'
8
+ require_relative 'citation_macro'
9
+ require_relative 'bibitem_macro'
10
+ require_relative 'bibliography_macro'
11
+
12
+ # Register the extensions to asciidoctor
13
+ Asciidoctor::Extensions.register do
14
+ block_macro AsciidoctorCsl::BibliographyBlockMacro
15
+ inline_macro AsciidoctorCsl::CitationMacro
16
+ inline_macro AsciidoctorCsl::BibitemMacro
17
+ treeprocessor AsciidoctorCsl::CitationProcessor
18
+ end
@@ -0,0 +1,118 @@
1
+ # frozen_string_literal: true
2
+
3
+ #
4
+ # Manage the current set of citations, the document settings,
5
+ # and main operations.
6
+ #
7
+
8
+ require 'citeproc/ruby'
9
+ require 'csl/styles'
10
+ require 'json'
11
+ require 'yaml'
12
+ require 'date'
13
+
14
+ require_relative 'citeproc_patches'
15
+ require_relative 'csl_renderer'
16
+
17
+ module AsciidoctorCsl
18
+ # Class used through utility method to hold data about citations for
19
+ # current document, and run the different steps to add the citations
20
+ # and bibliography
21
+ class Processor
22
+ include ::Asciidoctor::Logging
23
+
24
+ # AsciiDoc curved quotes, so the converter decides how quotes look,
25
+ # consistent with the rest of the document.
26
+ QUOTE_TERMS = {
27
+ 'open-quote' => '"`',
28
+ 'close-quote' => '`"',
29
+ 'open-inner-quote' => "'`",
30
+ 'close-inner-quote' => "`'"
31
+ }.freeze
32
+
33
+ attr_reader :citeproc
34
+
35
+ def initialize(
36
+ bibliography_file,
37
+ style = 'ieee',
38
+ locale = 'en'
39
+ )
40
+ raise "File '#{bibliography_file}' is not found" unless FileTest.file? bibliography_file
41
+
42
+ @style = style
43
+ @locale = locale
44
+
45
+ # Citations in the order they appear in the document
46
+ @citations = []
47
+
48
+ @citeproc = new_citeproc
49
+ @bibliography = read_bibliography_file(bibliography_file).to_h { |item| [item['id'].to_s, item] }
50
+ end
51
+
52
+ # Register the appearance of a citation key.
53
+ def add_citations(keys)
54
+ @citations.concat keys.map(&:strip)
55
+ end
56
+
57
+ def render_entry(key)
58
+ return nil unless @bibliography.key?(key)
59
+
60
+ entries.render(:bibliography, id: key).first
61
+ end
62
+
63
+ # Finalize citation macro processing and build internal citation list.
64
+ #
65
+ # As this function being called, processor will clean up the list of
66
+ # citation keys to form a correct ordered citation list.
67
+ def finalize_macro_processing(render_all: false)
68
+ @citations = @citations.uniq
69
+
70
+ keys = render_all ? @citations | @bibliography.keys : @citations
71
+ @citeproc.import(keys.filter_map { |key| @bibliography[key] })
72
+
73
+ @rendered = @citeproc.bibliography
74
+
75
+ @rendered.ids.each_with_index do |id, i|
76
+ @citeproc[id][:'citation-number'] = i + 1
77
+ end
78
+ nil
79
+ end
80
+
81
+ def build_bibliography_list
82
+ @rendered.ids.zip(@rendered.references).flat_map do |id, reference|
83
+ ["[[#{id}]]#{reference}", '']
84
+ end
85
+ end
86
+
87
+ private
88
+
89
+ def read_bibliography_file(bibliography_file)
90
+ case File.extname(bibliography_file).downcase
91
+ when '.json' then JSON.parse(File.read(bibliography_file))
92
+ when '.yml', '.yaml' then YAML.safe_load_file(bibliography_file, permitted_classes: [Date])
93
+ else
94
+ raise "Bibliography file must have .json, .yml, or .yaml extension, got #{File.extname(bibliography_file)}"
95
+ end
96
+ end
97
+
98
+ def entries
99
+ @entries ||= new_citeproc.tap { |citeproc| citeproc.import @bibliography.values }
100
+ end
101
+
102
+ def new_citeproc
103
+ citeproc = CiteProc::Processor.new style: @style, format: :asciidoc, locale: @locale
104
+ use_asciidoc_quotes citeproc
105
+ citeproc
106
+ end
107
+
108
+ # Override the quote handling by inserting dynamic AsciiDoc quotes.
109
+ # That way, AsciiDoc handles the quotes, and it is consistent with the rest of the document.
110
+ def use_asciidoc_quotes(processor)
111
+ locale = processor.engine.renderer.locale
112
+ QUOTE_TERMS.each do |name, value|
113
+ term = locale.terms.lookup(name)
114
+ term ? term.set(value) : locale.store(name, value)
115
+ end
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AsciidoctorCsl
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'asciidoctor-csl/version'
4
+ require_relative 'asciidoctor-csl/extensions'
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: asciidoctor-csl
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Niko Strijbol
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: asciidoctor
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '2.0'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '2.0'
26
+ - !ruby/object:Gem::Dependency
27
+ name: citeproc-ruby
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '2.1'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '2.1'
40
+ - !ruby/object:Gem::Dependency
41
+ name: csl-styles
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '2.0'
47
+ type: :runtime
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '2.0'
54
+ - !ruby/object:Gem::Dependency
55
+ name: logger
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '1.7'
61
+ type: :runtime
62
+ prerelease: false
63
+ version_requirements: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '1.7'
68
+ description: asciidoctor-csl is an Asciidoctor extension that adds CSL support for
69
+ AsciiDoc documents.
70
+ email:
71
+ - niko@strijbol.be
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - LICENSE.txt
77
+ - README.adoc
78
+ - lib/asciidoctor-csl.rb
79
+ - lib/asciidoctor-csl/bibitem_macro.rb
80
+ - lib/asciidoctor-csl/bibliography_macro.rb
81
+ - lib/asciidoctor-csl/citation_macro.rb
82
+ - lib/asciidoctor-csl/citation_processor.rb
83
+ - lib/asciidoctor-csl/citeproc_patches.rb
84
+ - lib/asciidoctor-csl/csl_renderer.rb
85
+ - lib/asciidoctor-csl/extensions.rb
86
+ - lib/asciidoctor-csl/processor.rb
87
+ - lib/asciidoctor-csl/version.rb
88
+ homepage: https://github.com/niknetniko/asciidoctor-csl
89
+ licenses:
90
+ - EUPL-1.2
91
+ metadata:
92
+ source_code_uri: https://github.com/niknetniko/asciidoctor-csl
93
+ bug_tracker_uri: https://github.com/niknetniko/asciidoctor-csl/issues
94
+ changelog_uri: https://github.com/niknetniko/asciidoctor-csl/releases
95
+ rubygems_mfa_required: 'true'
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '3.2'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubygems_version: 4.0.20
111
+ specification_version: 4
112
+ summary: An Asciidoctor extension that adds CSL integration to AsciiDoc
113
+ test_files: []