asciidoctor-bibtex 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE.txt +24 -0
- data/README.rdoc +166 -0
- data/bin/asciidoc-bibtex +19 -0
- data/bin/asciidoctor-bibtex +58 -0
- data/lib/asciidoctor-bibtex.rb +25 -0
- data/lib/asciidoctor-bibtex/asciidoctor.rb +6 -0
- data/lib/asciidoctor-bibtex/bibextension.rb +66 -0
- data/lib/asciidoctor-bibtex/citation.rb +25 -0
- data/lib/asciidoctor-bibtex/citationdata.rb +23 -0
- data/lib/asciidoctor-bibtex/citations.rb +45 -0
- data/lib/asciidoctor-bibtex/citationutils.rb +67 -0
- data/lib/asciidoctor-bibtex/extensions.rb +64 -0
- data/lib/asciidoctor-bibtex/filehandlers.rb +32 -0
- data/lib/asciidoctor-bibtex/options.rb +141 -0
- data/lib/asciidoctor-bibtex/processor.rb +270 -0
- data/lib/asciidoctor-bibtex/processorutils.rb +34 -0
- data/lib/asciidoctor-bibtex/styles.rb +27 -0
- data/lib/asciidoctor-bibtex/version.rb +3 -0
- data/samples/biblio.bib +31 -0
- data/samples/sample-1.txt +23 -0
- data/samples/sample-2.txt +18 -0
- metadata +134 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3d66291de5bde00c8773c279133b29d5c2a70735
|
4
|
+
data.tar.gz: a86cb9606d51b1ce9110d2a93c64fa0aa19c92c4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e7c0a804b7ce6f69df4a80bccb8ad22f62a1b946ca5eeba9840b93563311f5efe5ba3c32d236e829b0ce98d685a3e070ca677fbec92b46ecf9fbd3f5c1083b5b
|
7
|
+
data.tar.gz: 76e579bd5924ed9c38d6c0142b9bfa4ca4b29b36bf5a573817d72de911ebe430d009d182b9878dd51911b3290cdd425f05a574a98c73b4b50d90b6e8edd32f41
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# Open Works License
|
2
|
+
|
3
|
+
This is version 0.9.2 of the Open Works License
|
4
|
+
|
5
|
+
## Terms
|
6
|
+
|
7
|
+
Permission is hereby granted by the copyright holder(s), author(s), and
|
8
|
+
contributor(s) of this work, to any person who obtains a copy of this work in
|
9
|
+
any form, to reproduce, modify, distribute, publish, sell, use, or otherwise
|
10
|
+
deal in the licensed material without restriction, provided the following
|
11
|
+
conditions are met:
|
12
|
+
|
13
|
+
Redistributions, modified or unmodified, in whole or in part, must retain
|
14
|
+
applicable copyright notices, the above license notice, these conditions, and
|
15
|
+
the following disclaimer.
|
16
|
+
|
17
|
+
NO WARRANTY OF ANY KIND IS IMPLIED BY, OR SHOULD BE INFERRED FROM, THIS LICENSE
|
18
|
+
OR THE ACT OF DISTRIBUTION UNDER THE TERMS OF THIS LICENSE, INCLUDING BUT NOT
|
19
|
+
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
|
20
|
+
AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
21
|
+
LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
22
|
+
CONTRACT, TORT, OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION WITH THE
|
23
|
+
WORK, OR THE USE OF OR OTHER DEALINGS IN THE WORK.
|
24
|
+
|
data/README.rdoc
ADDED
@@ -0,0 +1,166 @@
|
|
1
|
+
= asciidoctor-bibtex: add bibtex functionality to asciidoc
|
2
|
+
|
3
|
+
asciidoctor-bibtex is a fork of
|
4
|
+
{asciidoc-bib}[https://github.com/petercrlane/asciidoc-bib] It generates in-text
|
5
|
+
references and a reference list for an asciidoc file, using a bibtex file as a
|
6
|
+
source of citation information. However, asciidoctor-bibtex proposes a
|
7
|
+
different citation and reference grammar, which resembles the bibtex grammar
|
8
|
+
in LaTeX. The grammar follows asciidoc inline and block macro semantics.
|
9
|
+
|
10
|
+
== Why a fork
|
11
|
+
|
12
|
+
When I began switching from latex/word to asciidoc, I searched for an easy way
|
13
|
+
to integrate bibtex like references into asciidoc. Then I came across
|
14
|
+
asciidoc-bib, which nearly meet my needs. I can insert citations, generate
|
15
|
+
automatically a complete reference list. But there are aspects in asciidoc-bib
|
16
|
+
that are not pleasing to work with.
|
17
|
+
|
18
|
+
The first is the overriding of `[bibliography]`. Asciidoctor introduces
|
19
|
+
`[xxx]` as an mechanism to customize block styles, roles, etc.
|
20
|
+
`[bibliography]` is reserved for the bibliography section style. asciidoc-bib
|
21
|
+
breaks it and there is no apparent way to fix it.
|
22
|
+
|
23
|
+
The second is the grammar inconsistency with the rest of asciidoc. The
|
24
|
+
`[cite:xxx]` is actually an inline macro but it does not follow the grammar of
|
25
|
+
inline macros. This makes asciidoc-bib not that confortable to write with
|
26
|
+
since it breaks the semantic memory.
|
27
|
+
|
28
|
+
The last is asciidoc-bib does not support asciidoctor arguments and
|
29
|
+
extensions. So I can not use asciidoctor-pdf with it.
|
30
|
+
|
31
|
+
To accommodate the above problems, I create this fork. This fork tries to be
|
32
|
+
as consistent with asciidoctor as possible.
|
33
|
+
|
34
|
+
== Features
|
35
|
+
|
36
|
+
- bibtex-like syntax for adding a citation within text and placing bibliography
|
37
|
+
- formatting of references and reference list according to range of styles supported by citeproc-ruby
|
38
|
+
- supports some styling of citation text (page numbers, bracket placement)
|
39
|
+
- can be used as an asciidoctor extension
|
40
|
+
|
41
|
+
== Install
|
42
|
+
|
43
|
+
> gem install asciidoctor-bibtex
|
44
|
+
|
45
|
+
Installs two executable programs:
|
46
|
+
|
47
|
+
- 'asciidoc-bibtex' for transforming source text into asciidoc
|
48
|
+
- 'asciidoctor-bibtex' uses asciidoctor extension for single-pass output
|
49
|
+
|
50
|
+
asciidoctor-bibtex depends on bibtex-ruby[http://github.com/inukshuk/bibtex-ruby],
|
51
|
+
citeproc-ruby[http://github.com/inukshuk/citeproc-ruby] and
|
52
|
+
csl-styles[http://github.com/inukshuk/csl-styles].
|
53
|
+
(Ensure 'ruby-dev' and 'libxslt1-dev' are installed, so the dependencies will
|
54
|
+
compile.)
|
55
|
+
|
56
|
+
asciidoctor[https://github.com/asciidoctor/asciidoctor] must also be installed
|
57
|
+
for 'asciidoctor-bibtex' to work. asciidoctor version 1.5.2 or higher is
|
58
|
+
required.
|
59
|
+
|
60
|
+
== Use
|
61
|
+
|
62
|
+
There are three ways of using asciidoctor-bibtex.
|
63
|
+
|
64
|
+
The first is required if using _asciidoc_.
|
65
|
+
'asciidoc-bibtex' works by transforming an asciidoc document containing syntax
|
66
|
+
to include citations and a bibliography. The transformed document will
|
67
|
+
contain a complete reference and bibliography list where indicated, and
|
68
|
+
can then be processed further by asciidoc's toolchain to produce a completed
|
69
|
+
document.
|
70
|
+
|
71
|
+
The second is to use 'asciidoctor-bibtex' to transform your bibtex-enabled
|
72
|
+
documents directly to any backend format supported by asciidoctor. It uses the
|
73
|
+
asciidoctor extension mechanism to hook in the asciidoctor preprocessing
|
74
|
+
process. It support all asciidoctor command-line options.
|
75
|
+
|
76
|
+
The third is to use asciidoctor-bibtex as an asciidoctor extension. It works
|
77
|
+
the same way as the second approach. In addition, one can use other extensions
|
78
|
+
together to provides even richer functionality.
|
79
|
+
|
80
|
+
Styles must be one of those supported by CSL: https://github.com/citation-style-language/styles
|
81
|
+
|
82
|
+
=== Citation syntax
|
83
|
+
|
84
|
+
Syntax for inserting a citation is the following inline macro:
|
85
|
+
|
86
|
+
cite|citenp:[ref(pages)]
|
87
|
+
|
88
|
+
where '(pages)' is optional. The ref and optional pages may be repeated
|
89
|
+
multiple times, separated by ','. A citation _must_ be complete on a single
|
90
|
+
line of text.
|
91
|
+
|
92
|
+
Examples of +chicago-author-date+ style:
|
93
|
+
|
94
|
+
[cite:[Lane12]] becomes "(Lane 2012)"
|
95
|
+
|
96
|
+
[citenp:[Lane12]] becomes "Lane (2012)"
|
97
|
+
|
98
|
+
[cite:[Lane12(59)]] becomes "(Lane 2012, 59)"
|
99
|
+
|
100
|
+
For +apa+ (Harvard-like) style:
|
101
|
+
|
102
|
+
[cite:[Lane12]] becomes "(Lane, 2012)"
|
103
|
+
|
104
|
+
[citenp:[Lane12]] becomes "Lane (2012)"
|
105
|
+
|
106
|
+
[cite:[Lane12(59)]] becomes "(Lane, 2012, p.59)"
|
107
|
+
|
108
|
+
For +ieee+, a numeric style:
|
109
|
+
|
110
|
+
[cite:[Lane12,Lane11]] becomes "[1, 2]"
|
111
|
+
|
112
|
+
=== Place bibliography in text
|
113
|
+
|
114
|
+
[bibliography::[]] on a line by itself.
|
115
|
+
|
116
|
+
=== Processing Text: Asciidoctor
|
117
|
+
|
118
|
+
> asciidoctor-bibtex [OPTIONS] filename
|
119
|
+
|
120
|
+
Looks for a bib file in current folder and in ~/Documents.
|
121
|
+
|
122
|
+
Outputs an html file, including all citations and references.
|
123
|
+
|
124
|
+
asciidoctor-bibtex support all command-line options of asciidoctor, for
|
125
|
+
example, use the follow command to output docbook file:
|
126
|
+
|
127
|
+
asciidoctor-bibtex -b docbook filename
|
128
|
+
|
129
|
+
One may also use as an asciidoctor extension, for example:
|
130
|
+
|
131
|
+
asciidoctor -r asciidoctor-bibtex -r asciidoctor-pdf -b pdf filename
|
132
|
+
|
133
|
+
Options are set through the command-line using the asciidoctor attributes
|
134
|
+
setting syntax:
|
135
|
+
|
136
|
+
> asciidoctor-bibtex -h
|
137
|
+
|
138
|
+
...
|
139
|
+
AsciidoctorBibtex related options:
|
140
|
+
|
141
|
+
-a bib-file=FILENAME Set BibTex filename (default: auto-find)
|
142
|
+
-a bib-style=STYLE Set BibTex items style (default: apa)
|
143
|
+
-a bib-numeric-order=<alphabetical|appearance>
|
144
|
+
Set citation order scheme (default: alphabetical)
|
145
|
+
-a bib-no-links=1 Do not use links (default: use links)
|
146
|
+
|
147
|
+
=== Processing text: Asciidoc
|
148
|
+
|
149
|
+
> asciidoc-bibtex filename.txt
|
150
|
+
|
151
|
+
Looks for a bib file in current folder and in ~/Documents.
|
152
|
+
|
153
|
+
Outputs a new file: filename-ref.txt
|
154
|
+
which includes your references.
|
155
|
+
|
156
|
+
asciidoc-bibtex supports the same command-line options as asciidoc-bib. The
|
157
|
+
only difference is the citation and bibliography syntex.
|
158
|
+
|
159
|
+
== License
|
160
|
+
|
161
|
+
The files within this project may be distributed under the terms of
|
162
|
+
the Open Works License: http://owl.apotheon.org
|
163
|
+
|
164
|
+
== Links
|
165
|
+
|
166
|
+
See [https://github.com/petercrlane/asciidoc-bib] for the original asciidoc-bib.
|
data/bin/asciidoc-bibtex
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
# Executable for asciidoc-bibtex
|
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
|
+
# Copyright (c) Peter Lane, 2012-13.
|
10
|
+
# Released under Open Works License, 0.9.2
|
11
|
+
|
12
|
+
unless $:.include?(libdir = File.expand_path('../lib', File.dirname(__FILE__)))
|
13
|
+
$:.unshift libdir
|
14
|
+
end
|
15
|
+
require 'asciidoctor-bibtex'
|
16
|
+
|
17
|
+
options = AsciidoctorBibtex::Options.new
|
18
|
+
options.parse!
|
19
|
+
AsciidoctorBibtex::Processor.run options
|
@@ -0,0 +1,58 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
# Executable asciidoctor-bib
|
4
|
+
#
|
5
|
+
# Uses asciidoctor preprocessor extension to include citations and bibliography
|
6
|
+
# in a single pass.
|
7
|
+
#
|
8
|
+
# Copyright (c) Peter Lane, 2013.
|
9
|
+
# Released under Open Works License, 0.9.2
|
10
|
+
|
11
|
+
unless $:.include?(libdir = File.expand_path('../lib', File.dirname(__FILE__)))
|
12
|
+
$:.unshift libdir
|
13
|
+
end
|
14
|
+
require 'asciidoctor-bibtex'
|
15
|
+
require 'asciidoctor/cli'
|
16
|
+
|
17
|
+
# Treat '--version' specially, like that in asciidoctor-pdf
|
18
|
+
options = Asciidoctor::Cli::Options.new
|
19
|
+
unless (ARGV & ['-V', '--version']).empty?
|
20
|
+
$stdout.write %(Asciidoctor Bib #{AsciidoctorBibtex::VERSION} using )
|
21
|
+
# NOTE the print_version method was added in Asciidoctor 1.5.2
|
22
|
+
if options.respond_to? :print_version
|
23
|
+
options.print_version
|
24
|
+
else
|
25
|
+
puts %(Asciidoctor #{::Asciidoctor::VERSION} [http://asciidoctor.org])
|
26
|
+
end
|
27
|
+
exit 0
|
28
|
+
end
|
29
|
+
|
30
|
+
# Add help information on how to set bibtex related values via asciidoctor
|
31
|
+
# commandline. This is the usual CLI attributes definition.
|
32
|
+
if not (ARGV & ['-h', '--help']).empty?
|
33
|
+
# parse argument with asciidoctor to print asciidoctor's helps
|
34
|
+
options.parse! ARGV
|
35
|
+
puts ""
|
36
|
+
puts "AsciidoctorBibtex related options:"
|
37
|
+
puts ""
|
38
|
+
puts " -a bib-file=FILENAME Set BibTex filename (default: auto-find)"
|
39
|
+
puts " -a bib-style=STYLE Set BibTex items style (default: apa)"
|
40
|
+
puts " -a bib-numeric-order=<alphabetical|appearance>"
|
41
|
+
puts " Set citation order scheme (default: alphabetical)"
|
42
|
+
puts " -a bib-no-links=1 Do not use links (default: use links)"
|
43
|
+
puts ""
|
44
|
+
exit 0
|
45
|
+
end
|
46
|
+
|
47
|
+
# Use standard asciidoctor CLI mechanism to call asciidoctor, so we can accept
|
48
|
+
# all asciidoctor options. Note that we only accept asciidoctor options here.
|
49
|
+
# This code is stolen shamlessly from asciidoctor-pdf
|
50
|
+
case (result = options.parse! ARGV)
|
51
|
+
when Integer
|
52
|
+
exit result
|
53
|
+
else
|
54
|
+
invoker = Asciidoctor::Cli::Invoker.new options
|
55
|
+
GC.start
|
56
|
+
invoker.invoke!
|
57
|
+
exit invoker.code
|
58
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# asciidoctor-bibtex.rb
|
2
|
+
#
|
3
|
+
# Copyright (c) Peter Lane, 2012-13.
|
4
|
+
# Released under Open Works License, 0.9.2
|
5
|
+
|
6
|
+
require 'bibtex'
|
7
|
+
require 'citeproc'
|
8
|
+
require 'csl/styles'
|
9
|
+
require 'set'
|
10
|
+
|
11
|
+
# Only require asciidoctor file if asciidoctor gem is installed
|
12
|
+
unless Gem::Specification.find_all_by_name('asciidoctor').empty?
|
13
|
+
require_relative 'asciidoctor-bibtex/asciidoctor'
|
14
|
+
end
|
15
|
+
require_relative 'asciidoctor-bibtex/citation'
|
16
|
+
require_relative 'asciidoctor-bibtex/citationdata'
|
17
|
+
require_relative 'asciidoctor-bibtex/citationutils'
|
18
|
+
require_relative 'asciidoctor-bibtex/citations'
|
19
|
+
require_relative 'asciidoctor-bibtex/extensions'
|
20
|
+
require_relative 'asciidoctor-bibtex/filehandlers'
|
21
|
+
require_relative 'asciidoctor-bibtex/options'
|
22
|
+
require_relative 'asciidoctor-bibtex/processorutils'
|
23
|
+
require_relative 'asciidoctor-bibtex/processor'
|
24
|
+
require_relative 'asciidoctor-bibtex/styles'
|
25
|
+
require_relative 'asciidoctor-bibtex/version'
|
@@ -0,0 +1,66 @@
|
|
1
|
+
# Uses Asciidoctor extension mechanism to insert asciidoc-bib processing
|
2
|
+
# as a preprocessor step. This provides single-pass compilation of
|
3
|
+
# documents, including citations and references.
|
4
|
+
#
|
5
|
+
# Copyright (c) Peter Lane, 2013.
|
6
|
+
# Released under Open Works License, 0.9.2
|
7
|
+
|
8
|
+
require 'asciidoctor'
|
9
|
+
require 'asciidoctor/extensions'
|
10
|
+
require 'asciidoctor/cli'
|
11
|
+
require_relative 'options'
|
12
|
+
|
13
|
+
module AsciidoctorBibtex
|
14
|
+
module Asciidoctor
|
15
|
+
|
16
|
+
class AsciidoctorBibtexExtension < ::Asciidoctor::Extensions::Preprocessor
|
17
|
+
|
18
|
+
def process document, reader
|
19
|
+
return reader if reader.eof?
|
20
|
+
|
21
|
+
options = Options.new
|
22
|
+
options.parse_attributes document.attributes
|
23
|
+
|
24
|
+
# -- read in all lines from reader, processing the lines
|
25
|
+
|
26
|
+
lines = reader.readlines
|
27
|
+
biblio = BibTeX.open options.bibfile
|
28
|
+
|
29
|
+
processor = Processor.new biblio, options.links, options.style
|
30
|
+
lines.each do |line|
|
31
|
+
processor.citations.add_from_line line
|
32
|
+
end
|
33
|
+
|
34
|
+
# -- replace cites with correct text
|
35
|
+
|
36
|
+
lines.each do |line|
|
37
|
+
processor.citations.retrieve_citations(line).each do |citation|
|
38
|
+
line.gsub!(citation.original, processor.complete_citation(citation))
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# -- add in bibliography
|
43
|
+
|
44
|
+
biblio_index = lines.index do |line|
|
45
|
+
# find bibliography macro on line by itself, with or without newline
|
46
|
+
(line =~ BIBMACRO_FULL) != nil
|
47
|
+
end
|
48
|
+
unless biblio_index.nil?
|
49
|
+
lines.delete_at biblio_index
|
50
|
+
processor.sorted_cites.reverse.each do |ref|
|
51
|
+
lines.insert biblio_index, "\n"
|
52
|
+
lines.insert biblio_index, processor.get_reference(ref)
|
53
|
+
lines.insert biblio_index, "[normal]\n" # ? needed to force paragraph breaks
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
reader.unshift_lines lines
|
58
|
+
|
59
|
+
return reader
|
60
|
+
end
|
61
|
+
|
62
|
+
BIBMACRO_FULL = /bibliography::(.*?)\[(\w+)?\]/
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# citation class
|
2
|
+
#
|
3
|
+
# Copyright (c) Peter Lane, 2012.
|
4
|
+
# Released under Open Works License, 0.9.2
|
5
|
+
|
6
|
+
module AsciidoctorBibtex
|
7
|
+
# Class to hold information about a single citation:
|
8
|
+
# its reference and any page numbers
|
9
|
+
class Citation
|
10
|
+
attr_reader :ref, :pages
|
11
|
+
|
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,23 @@
|
|
1
|
+
# citationdata class
|
2
|
+
#
|
3
|
+
# Copyright (c) Peter Lane, 2013.
|
4
|
+
# Released under Open Works License, 0.9.2
|
5
|
+
|
6
|
+
module AsciidoctorBibtex
|
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
|
@@ -0,0 +1,45 @@
|
|
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 AsciidoctorBibtex
|
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
|
@@ -0,0 +1,67 @@
|
|
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 AsciidoctorBibtex
|
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 = /(\w+)(\(\d+(-\d+)*\))?/
|
59
|
+
# matches a citation type
|
60
|
+
CITATION_TYPE = /cite|citenp/
|
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
|
@@ -0,0 +1,64 @@
|
|
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 AsciidoctorBibtexArrayExtensions
|
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 AsciidoctorBibtexArrayExtensions
|
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
|
+
|
@@ -0,0 +1,32 @@
|
|
1
|
+
#
|
2
|
+
# filehandlers.rb
|
3
|
+
# Contains top-level file utility methods
|
4
|
+
#
|
5
|
+
|
6
|
+
module AsciidoctorBibtex
|
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
|
+
|
@@ -0,0 +1,141 @@
|
|
1
|
+
# Class to read in asciidoc-bibtex options from command-line, and
|
2
|
+
# store results in an accessible form.
|
3
|
+
#
|
4
|
+
# Copyright (c) Peter Lane, 2013.
|
5
|
+
# Released under Open Works License, 0.9.2
|
6
|
+
|
7
|
+
require 'optparse'
|
8
|
+
|
9
|
+
module AsciidoctorBibtex
|
10
|
+
class Options
|
11
|
+
attr_reader :bibfile, :filename, :links, :style
|
12
|
+
|
13
|
+
def initialize(program_name = 'asciidoc-bibtex')
|
14
|
+
@bibfile = ''
|
15
|
+
@links = true
|
16
|
+
@numeric_order = :alphabetical
|
17
|
+
@style = AsciidoctorBibtex::Styles.default_style
|
18
|
+
@program_name = program_name
|
19
|
+
end
|
20
|
+
|
21
|
+
# Public: Parse options from commandline.
|
22
|
+
# This function is used by asciidoc-bib command.
|
23
|
+
def parse!(args = ARGV)
|
24
|
+
options = OptionParser.new do |opts|
|
25
|
+
opts.banner = "Usage: #{@program_name} filename"
|
26
|
+
opts.on("-h", "--help", "help message") do |v|
|
27
|
+
puts "#{@program_name} #{AsciidoctorBibtex::VERSION}"
|
28
|
+
puts
|
29
|
+
puts options
|
30
|
+
puts
|
31
|
+
puts "All styles available through CSL are supported."
|
32
|
+
puts "The default style is 'apa'."
|
33
|
+
exit!
|
34
|
+
end
|
35
|
+
opts.on("-b", "--bibfile FILE", "location of bib file") do |v|
|
36
|
+
@bibfile = v
|
37
|
+
end
|
38
|
+
opts.on("-n", "--no-links", "do not add internal links") do |v|
|
39
|
+
@links = false
|
40
|
+
end
|
41
|
+
opts.on('', '--numeric-alphabetic-order', 'sort numeric styles in alphabetical order (DEFAULT)') do |v|
|
42
|
+
@numeric_order = :alphabetical
|
43
|
+
end
|
44
|
+
opts.on('', '--numeric-appearance-order', 'sort numeric styles in order of appearance') do |v|
|
45
|
+
@numeric_order = :appearance
|
46
|
+
end
|
47
|
+
opts.on("-s", "--style STYLE", "reference style") do |v|
|
48
|
+
@style = v
|
49
|
+
end
|
50
|
+
opts.on("-v", "--version", "show version") do |v|
|
51
|
+
puts "#{@program_name} version #{AsciidoctorBibtex::VERSION}"
|
52
|
+
exit!
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
begin
|
57
|
+
options.parse! args
|
58
|
+
rescue
|
59
|
+
puts options
|
60
|
+
exit!
|
61
|
+
end
|
62
|
+
|
63
|
+
# unless specified by caller, try to find the bibliography
|
64
|
+
if @bibfile.empty?
|
65
|
+
@bibfile = AsciidoctorBibtex::FileHandlers.find_bibliography "."
|
66
|
+
if @bibfile.empty?
|
67
|
+
@bibfile = AsciidoctorBibtex::FileHandlers.find_bibliography "#{ENV['HOME']}/Documents"
|
68
|
+
end
|
69
|
+
end
|
70
|
+
if @bibfile.empty?
|
71
|
+
puts "Error: could not find a bibliography file"
|
72
|
+
exit
|
73
|
+
end
|
74
|
+
unless AsciidoctorBibtex::Styles.valid? @style
|
75
|
+
puts "Error: style #{@style} was not one of the available styles"
|
76
|
+
exit
|
77
|
+
end
|
78
|
+
|
79
|
+
if args.length == 1
|
80
|
+
@filename = args[0]
|
81
|
+
else
|
82
|
+
puts "Error: a single file to convert must be given"
|
83
|
+
exit
|
84
|
+
end
|
85
|
+
|
86
|
+
puts "Reading biblio: #{@bibfile}"
|
87
|
+
puts "Reference style: #{@style}"
|
88
|
+
puts "Numerical order: #{@numeric_order}"
|
89
|
+
end
|
90
|
+
|
91
|
+
# Public: Parse values given `attrs`
|
92
|
+
# This function is used by asciidoctor preprocessor to determine options
|
93
|
+
# from document attributes.
|
94
|
+
def parse_attributes(attrs)
|
95
|
+
if attrs['bib-style']
|
96
|
+
@style = attrs['bib-style']
|
97
|
+
end
|
98
|
+
if attrs['bib-file']
|
99
|
+
@bibfile = attrs['bib-file']
|
100
|
+
end
|
101
|
+
if attrs['bib-numeric-order']
|
102
|
+
order = attrs['bib-numeric-order']
|
103
|
+
if order == "appearance"
|
104
|
+
@numeric_order = :appearance
|
105
|
+
elsif order == "alphabetical"
|
106
|
+
@numeric_order = :alphabetical
|
107
|
+
else
|
108
|
+
raise RuntimeError.new "Unknown numeric order: #{order}"
|
109
|
+
end
|
110
|
+
end
|
111
|
+
if attrs['bib-no-links']
|
112
|
+
@links = false
|
113
|
+
end
|
114
|
+
|
115
|
+
# unless specified by caller, try to find the bibliography
|
116
|
+
if @bibfile.empty?
|
117
|
+
@bibfile = AsciidoctorBibtex::FileHandlers.find_bibliography "."
|
118
|
+
if @bibfile.empty?
|
119
|
+
@bibfile = AsciidoctorBibtex::FileHandlers.find_bibliography "#{ENV['HOME']}/Documents"
|
120
|
+
end
|
121
|
+
end
|
122
|
+
if @bibfile.empty?
|
123
|
+
puts "Error: could not find a bibliography file"
|
124
|
+
exit
|
125
|
+
end
|
126
|
+
unless AsciidoctorBibtex::Styles.valid? @style
|
127
|
+
puts "Error: style #{@style} was not one of the available styles"
|
128
|
+
exit
|
129
|
+
end
|
130
|
+
|
131
|
+
puts "Reading biblio: #{@bibfile}"
|
132
|
+
puts "Reference style: #{@style}"
|
133
|
+
puts "Numerical order: #{@numeric_order}"
|
134
|
+
end
|
135
|
+
|
136
|
+
def numeric_in_appearance_order?
|
137
|
+
@numeric_order == :appearance
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
@@ -0,0 +1,270 @@
|
|
1
|
+
#
|
2
|
+
# Manage the current set of citations, the document settings,
|
3
|
+
# and main operations.
|
4
|
+
#
|
5
|
+
|
6
|
+
module AsciidoctorBibtex
|
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
|
+
# Top-level method to include citations in given asciidoc file
|
15
|
+
def Processor.run options
|
16
|
+
processor = Processor.new BibTeX.open(options.bibfile), options.links, options.style, options.numeric_in_appearance_order?
|
17
|
+
processor.read_filenames options.filename
|
18
|
+
processor.read_citations
|
19
|
+
processor.add_citations
|
20
|
+
end
|
21
|
+
|
22
|
+
attr_reader :biblio, :links, :style, :citations
|
23
|
+
|
24
|
+
def initialize biblio, links, style, numeric_in_appearance_order = false
|
25
|
+
@biblio = biblio
|
26
|
+
@links = links
|
27
|
+
@numeric_in_appearance_order = numeric_in_appearance_order
|
28
|
+
@style = style
|
29
|
+
@citations = Citations.new
|
30
|
+
@filenames = Set.new
|
31
|
+
|
32
|
+
@citeproc = CiteProc::Processor.new style: @style, format: :html
|
33
|
+
@citeproc.import @biblio.to_citeproc
|
34
|
+
end
|
35
|
+
|
36
|
+
# Given an asciidoc filename, reads in all dependent files based on 'include::' statements
|
37
|
+
# Leaving a list of files in @filenames
|
38
|
+
def read_filenames filename
|
39
|
+
puts "Reading file: #{filename}"
|
40
|
+
files_to_process = [filename]
|
41
|
+
|
42
|
+
begin
|
43
|
+
@filenames.add files_to_process.first
|
44
|
+
File.new(files_to_process.shift).each_line do |line|
|
45
|
+
if line.include?("include::")
|
46
|
+
line.split("include::").drop(1).each do |filetxt|
|
47
|
+
file = File.expand_path(filetxt.partition(/\s|\[/).first)
|
48
|
+
files_to_process << file unless @filenames.include?(file)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end until files_to_process.empty?
|
53
|
+
end
|
54
|
+
|
55
|
+
# Scans each filename and extracts citations
|
56
|
+
def read_citations
|
57
|
+
@filenames.each do |file|
|
58
|
+
IO.foreach(file) do |line|
|
59
|
+
@citations.add_from_line line
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
# Read given text to add cites and biblio to a new file
|
65
|
+
# Order is always decided by author surname first with year.
|
66
|
+
# If no author present, then use editor field.
|
67
|
+
# Links indicates if internal links to be added.
|
68
|
+
# Assumes @filenames has been set to list of filenames to process.
|
69
|
+
def add_citations
|
70
|
+
@filenames.each do |curr_file|
|
71
|
+
ref_filename = FileHandlers.add_ref(curr_file)
|
72
|
+
puts "Writing file: #{ref_filename}"
|
73
|
+
output = File.new(ref_filename, "w")
|
74
|
+
|
75
|
+
IO.foreach(curr_file) do |line|
|
76
|
+
begin # catch any errors, and ensure the lines of text are written
|
77
|
+
case
|
78
|
+
when line.include?('include::')
|
79
|
+
output_include_line output, line
|
80
|
+
when (line =~ BIBMACRO_FULL) != nil
|
81
|
+
output_bibliography output
|
82
|
+
else
|
83
|
+
output_cite_completed_line output, line
|
84
|
+
end
|
85
|
+
rescue # Any errors, just output the line
|
86
|
+
output.puts line
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
output.close
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
# Output bibliography to given output
|
95
|
+
def output_bibliography output
|
96
|
+
cites = if Styles.is_numeric?(@style) and @numeric_in_appearance_order
|
97
|
+
@citations.cites_used
|
98
|
+
else
|
99
|
+
sorted_cites
|
100
|
+
end
|
101
|
+
cites.each do |ref|
|
102
|
+
output.puts get_reference(ref)
|
103
|
+
output.puts
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
def output_include_line output, line
|
108
|
+
line.split("include::").drop(1).each do |filetxt|
|
109
|
+
ifile = filetxt.partition(/\s|\[/).first
|
110
|
+
file = File.expand_path ifile
|
111
|
+
# make sure included file points to the -ref version
|
112
|
+
line.gsub!("include::#{ifile}", "include::#{FileHandlers.add_ref(file)}")
|
113
|
+
end
|
114
|
+
output.puts line
|
115
|
+
end
|
116
|
+
|
117
|
+
# For each citation in given line, expand into complete citation text
|
118
|
+
# before outputting the line
|
119
|
+
def output_cite_completed_line output, line
|
120
|
+
@citations.retrieve_citations(line).each do |citation|
|
121
|
+
line.gsub!(citation.original, complete_citation(citation))
|
122
|
+
end
|
123
|
+
output.puts line
|
124
|
+
end
|
125
|
+
|
126
|
+
# Return the complete citation text for given cite_data
|
127
|
+
def complete_citation cite_data
|
128
|
+
result = ''
|
129
|
+
ob, cb = '(', ')'
|
130
|
+
|
131
|
+
cite_data.cites.each_with_index do |cite, index|
|
132
|
+
# before all items apart from the first, insert appropriate separator
|
133
|
+
result << "#{separator} " unless index.zero?
|
134
|
+
|
135
|
+
# @links requires adding hyperlink to reference
|
136
|
+
result << "<<#{cite.ref}," if @links
|
137
|
+
|
138
|
+
# if found, insert reference information
|
139
|
+
unless biblio[cite.ref].nil?
|
140
|
+
item = biblio[cite.ref].clone
|
141
|
+
cite_text, ob, cb = make_citation item, cite.ref, cite_data, cite
|
142
|
+
else
|
143
|
+
puts "Unknown reference: #{cite.ref}"
|
144
|
+
cite_text = "#{cite.ref}"
|
145
|
+
end
|
146
|
+
|
147
|
+
result << cite_text.html_to_asciidoc
|
148
|
+
# @links requires finish hyperlink
|
149
|
+
result << ">>" if @links
|
150
|
+
end
|
151
|
+
|
152
|
+
unless @links
|
153
|
+
# combine numeric ranges
|
154
|
+
if Styles.is_numeric? @style
|
155
|
+
result = combine_consecutive_numbers result
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
include_pretext result, cite_data, ob, cb
|
160
|
+
end
|
161
|
+
|
162
|
+
# Retrieve text for reference in given style
|
163
|
+
# - ref is reference for item to give reference for
|
164
|
+
def get_reference ref
|
165
|
+
result = ""
|
166
|
+
result << ". " if Styles.is_numeric? @style
|
167
|
+
|
168
|
+
begin
|
169
|
+
cptext = @citeproc.render :bibliography, id: ref
|
170
|
+
rescue Exception => e
|
171
|
+
puts "Failed to render #{ref}: #{e}"
|
172
|
+
end
|
173
|
+
result << "[[#{ref}]]" if @links
|
174
|
+
if cptext.nil?
|
175
|
+
return result+ref
|
176
|
+
else
|
177
|
+
result << cptext.first
|
178
|
+
end
|
179
|
+
|
180
|
+
return result.html_to_asciidoc
|
181
|
+
end
|
182
|
+
|
183
|
+
def separator
|
184
|
+
if Styles.is_numeric? @style
|
185
|
+
','
|
186
|
+
else
|
187
|
+
';'
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
191
|
+
# Format pages with pp/p as appropriate
|
192
|
+
def with_pp pages
|
193
|
+
return '' if pages.empty?
|
194
|
+
|
195
|
+
if @style.include? "chicago"
|
196
|
+
pages
|
197
|
+
elsif pages.include? '-'
|
198
|
+
"pp. #{pages}"
|
199
|
+
else
|
200
|
+
"p. #{pages}"
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
# Return page string for given cite
|
205
|
+
def page_str cite
|
206
|
+
result = ''
|
207
|
+
unless cite.pages.empty?
|
208
|
+
result << "," unless Styles.is_numeric? @style
|
209
|
+
result << " #{with_pp(cite.pages)}"
|
210
|
+
end
|
211
|
+
|
212
|
+
return result
|
213
|
+
end
|
214
|
+
|
215
|
+
def include_pretext result, cite_data, ob, cb
|
216
|
+
pretext = cite_data.pretext
|
217
|
+
pretext += ' ' unless pretext.empty? # add space after any content
|
218
|
+
|
219
|
+
if Styles.is_numeric? @style
|
220
|
+
"#{pretext}#{ob}#{result}#{cb}"
|
221
|
+
elsif cite_data.type == "cite"
|
222
|
+
"#{ob}#{pretext}#{result}#{cb}"
|
223
|
+
else
|
224
|
+
"#{pretext}#{result}"
|
225
|
+
end
|
226
|
+
end
|
227
|
+
|
228
|
+
# Numeric citations are handled by computing the position of the reference
|
229
|
+
# in the list of used citations.
|
230
|
+
# Other citations are formatted by citeproc.
|
231
|
+
def make_citation item, ref, cite_data, cite
|
232
|
+
if Styles.is_numeric? @style
|
233
|
+
cite_text = if @numeric_in_appearance_order
|
234
|
+
"#{@citations.cites_used.index(cite.ref) + 1}"
|
235
|
+
else
|
236
|
+
"#{sorted_cites.index(cite.ref) + 1}"
|
237
|
+
end
|
238
|
+
fc = '['
|
239
|
+
lc = ']'
|
240
|
+
else
|
241
|
+
cite_text = @citeproc.process id: ref, mode: :citation
|
242
|
+
|
243
|
+
fc = cite_text[0,1]
|
244
|
+
lc = cite_text[-1,1]
|
245
|
+
cite_text = cite_text[1..-2]
|
246
|
+
end
|
247
|
+
|
248
|
+
if Styles.is_numeric? @style
|
249
|
+
cite_text << "#{page_str(cite)}"
|
250
|
+
elsif cite_data.type == "citenp"
|
251
|
+
cite_text.gsub!(item.year, "#{fc}#{item.year}#{page_str(cite)}#{lc}")
|
252
|
+
cite_text.gsub!(", #{fc}", " #{fc}")
|
253
|
+
else
|
254
|
+
cite_text << page_str(cite)
|
255
|
+
end
|
256
|
+
|
257
|
+
cite_text.gsub!(",", ",") if @links # replace comma
|
258
|
+
|
259
|
+
return cite_text, fc, lc
|
260
|
+
end
|
261
|
+
|
262
|
+
def sorted_cites
|
263
|
+
@citations.sorted_cites @biblio
|
264
|
+
end
|
265
|
+
|
266
|
+
BIBMACRO_FULL = /bibliography::(.*?)\[(\w+)?\]/
|
267
|
+
end
|
268
|
+
end
|
269
|
+
|
270
|
+
|
@@ -0,0 +1,34 @@
|
|
1
|
+
|
2
|
+
module AsciidoctorBibtex
|
3
|
+
module ProcessorUtils
|
4
|
+
# Used with numeric styles to combine consecutive numbers into ranges
|
5
|
+
# e.g. 1,2,3 -> 1-3, or 1,2,3,6,7,8,9,12 -> 1-3,6-9,12
|
6
|
+
# leave references with page numbers alone
|
7
|
+
def combine_consecutive_numbers str
|
8
|
+
nums = str.split(",").collect(&:strip)
|
9
|
+
res = ""
|
10
|
+
# Loop through ranges
|
11
|
+
start_range = 0
|
12
|
+
while start_range < nums.length do
|
13
|
+
end_range = start_range
|
14
|
+
while (end_range < nums.length-1 and
|
15
|
+
nums[end_range].is_i? and
|
16
|
+
nums[end_range+1].is_i? and
|
17
|
+
nums[end_range+1].to_i == nums[end_range].to_i + 1) do
|
18
|
+
end_range += 1
|
19
|
+
end
|
20
|
+
if end_range - start_range >= 2
|
21
|
+
res += "#{nums[start_range]}-#{nums[end_range]}, "
|
22
|
+
else
|
23
|
+
start_range.upto(end_range) do |i|
|
24
|
+
res += "#{nums[i]}, "
|
25
|
+
end
|
26
|
+
end
|
27
|
+
start_range = end_range + 1
|
28
|
+
end
|
29
|
+
# finish by removing last comma
|
30
|
+
res.gsub(/, $/, '')
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
@@ -0,0 +1,27 @@
|
|
1
|
+
#
|
2
|
+
# styles.rb
|
3
|
+
# Simple checks on available styles through CSL
|
4
|
+
#
|
5
|
+
|
6
|
+
module AsciidoctorBibtex
|
7
|
+
|
8
|
+
module Styles
|
9
|
+
|
10
|
+
def Styles.available
|
11
|
+
CSL::Style.ls
|
12
|
+
end
|
13
|
+
|
14
|
+
def Styles.default_style
|
15
|
+
'apa'
|
16
|
+
end
|
17
|
+
|
18
|
+
def Styles.valid? style
|
19
|
+
Styles.available.include? style
|
20
|
+
end
|
21
|
+
|
22
|
+
def Styles.is_numeric? style
|
23
|
+
CSL::Style.load(style).citation_format == :numeric
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
data/samples/biblio.bib
ADDED
@@ -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: See cite:[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: See cite:[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: See cite:[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,134 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: asciidoctor-bibtex
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Zhang YANG
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-07-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: '4.0'
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 4.0.11
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - "~>"
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '4.0'
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 4.0.11
|
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: |
|
74
|
+
asciidoctor-bibtex is a fork of asciidoc-bib. It generates in-text references
|
75
|
+
and a reference list for an asciidoc file, using a bibtex file as a source of
|
76
|
+
citation information. The citation syntax tries to assemble asciidoc inline
|
77
|
+
macros and block macros, with `cite:[bibref]` for in-text citation and
|
78
|
+
`bibliography::[]` for reference list. It can be used standalone or as an
|
79
|
+
asciidoctor extension. See the README for more examples and further options.
|
80
|
+
The references are formatted using styles provided by CSL.
|
81
|
+
email: zyangmath@gmail.com
|
82
|
+
executables:
|
83
|
+
- asciidoctor-bibtex
|
84
|
+
- asciidoc-bibtex
|
85
|
+
extensions: []
|
86
|
+
extra_rdoc_files:
|
87
|
+
- README.rdoc
|
88
|
+
files:
|
89
|
+
- LICENSE.txt
|
90
|
+
- README.rdoc
|
91
|
+
- bin/asciidoc-bibtex
|
92
|
+
- bin/asciidoctor-bibtex
|
93
|
+
- lib/asciidoctor-bibtex.rb
|
94
|
+
- lib/asciidoctor-bibtex/asciidoctor.rb
|
95
|
+
- lib/asciidoctor-bibtex/bibextension.rb
|
96
|
+
- lib/asciidoctor-bibtex/citation.rb
|
97
|
+
- lib/asciidoctor-bibtex/citationdata.rb
|
98
|
+
- lib/asciidoctor-bibtex/citations.rb
|
99
|
+
- lib/asciidoctor-bibtex/citationutils.rb
|
100
|
+
- lib/asciidoctor-bibtex/extensions.rb
|
101
|
+
- lib/asciidoctor-bibtex/filehandlers.rb
|
102
|
+
- lib/asciidoctor-bibtex/options.rb
|
103
|
+
- lib/asciidoctor-bibtex/processor.rb
|
104
|
+
- lib/asciidoctor-bibtex/processorutils.rb
|
105
|
+
- lib/asciidoctor-bibtex/styles.rb
|
106
|
+
- lib/asciidoctor-bibtex/version.rb
|
107
|
+
- samples/biblio.bib
|
108
|
+
- samples/sample-1.txt
|
109
|
+
- samples/sample-2.txt
|
110
|
+
homepage: https://github.com/ProgramFan/asciidoctor-bibtex
|
111
|
+
licenses:
|
112
|
+
- OWL
|
113
|
+
metadata: {}
|
114
|
+
post_install_message:
|
115
|
+
rdoc_options: []
|
116
|
+
require_paths:
|
117
|
+
- lib
|
118
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
119
|
+
requirements:
|
120
|
+
- - "~>"
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: '2.0'
|
123
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
124
|
+
requirements:
|
125
|
+
- - ">="
|
126
|
+
- !ruby/object:Gem::Version
|
127
|
+
version: '0'
|
128
|
+
requirements: []
|
129
|
+
rubyforge_project:
|
130
|
+
rubygems_version: 2.4.6
|
131
|
+
signing_key:
|
132
|
+
specification_version: 4
|
133
|
+
summary: asciidoctor-bibtex adds bibtex references to an asciidoc file.
|
134
|
+
test_files: []
|