citeproc-ruby 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (125) hide show
  1. data/README.md +78 -0
  2. data/lib/citeproc.rb +100 -0
  3. data/lib/citeproc/bibliography.rb +57 -0
  4. data/lib/citeproc/data.rb +149 -0
  5. data/lib/citeproc/date.rb +133 -0
  6. data/lib/citeproc/formatter.rb +38 -0
  7. data/lib/citeproc/item.rb +53 -0
  8. data/lib/citeproc/name.rb +284 -0
  9. data/lib/citeproc/processor.rb +166 -0
  10. data/lib/citeproc/selector.rb +61 -0
  11. data/lib/citeproc/variable.rb +82 -0
  12. data/lib/citeproc/version.rb +3 -0
  13. data/lib/csl/locale.rb +223 -0
  14. data/lib/csl/node.rb +72 -0
  15. data/lib/csl/nodes.rb +1364 -0
  16. data/lib/csl/renderer.rb +88 -0
  17. data/lib/csl/sort.rb +53 -0
  18. data/lib/csl/style.rb +110 -0
  19. data/lib/csl/term.rb +124 -0
  20. data/lib/extensions/core.rb +43 -0
  21. data/lib/plugins/filters/bibtex.rb +12 -0
  22. data/lib/plugins/formats/default.rb +134 -0
  23. data/lib/plugins/formats/html.rb +67 -0
  24. data/lib/support/attributes.rb +99 -0
  25. data/lib/support/tree.rb +80 -0
  26. data/resource/locale/locales-af-ZA.xml +304 -0
  27. data/resource/locale/locales-ar-AR.xml +304 -0
  28. data/resource/locale/locales-bg-BG.xml +304 -0
  29. data/resource/locale/locales-ca-AD.xml +304 -0
  30. data/resource/locale/locales-cs-CZ.xml +304 -0
  31. data/resource/locale/locales-da-DK.xml +304 -0
  32. data/resource/locale/locales-de-AT.xml +304 -0
  33. data/resource/locale/locales-de-CH.xml +304 -0
  34. data/resource/locale/locales-de-DE.xml +332 -0
  35. data/resource/locale/locales-el-GR.xml +303 -0
  36. data/resource/locale/locales-en-US.xml +313 -0
  37. data/resource/locale/locales-es-ES.xml +304 -0
  38. data/resource/locale/locales-et-EE.xml +304 -0
  39. data/resource/locale/locales-fr-FR.xml +304 -0
  40. data/resource/locale/locales-he-IL.xml +304 -0
  41. data/resource/locale/locales-hu-HU.xml +304 -0
  42. data/resource/locale/locales-is-IS.xml +304 -0
  43. data/resource/locale/locales-it-IT.xml +304 -0
  44. data/resource/locale/locales-ja-JP.xml +304 -0
  45. data/resource/locale/locales-kh-KH.xml +303 -0
  46. data/resource/locale/locales-ko-KR.xml +304 -0
  47. data/resource/locale/locales-mn-MN.xml +304 -0
  48. data/resource/locale/locales-nb-NO.xml +304 -0
  49. data/resource/locale/locales-nl-NL.xml +304 -0
  50. data/resource/locale/locales-nn-NO.xml +304 -0
  51. data/resource/locale/locales-pl-PL.xml +304 -0
  52. data/resource/locale/locales-pt-BR.xml +304 -0
  53. data/resource/locale/locales-pt-PT.xml +304 -0
  54. data/resource/locale/locales-ro-RO.xml +304 -0
  55. data/resource/locale/locales-ru-RU.xml +304 -0
  56. data/resource/locale/locales-sk-SK.xml +304 -0
  57. data/resource/locale/locales-sl-SI.xml +304 -0
  58. data/resource/locale/locales-sr-RS.xml +304 -0
  59. data/resource/locale/locales-sv-SE.xml +304 -0
  60. data/resource/locale/locales-th-TH.xml +304 -0
  61. data/resource/locale/locales-tr-TR.xml +304 -0
  62. data/resource/locale/locales-uk-UA.xml +304 -0
  63. data/resource/locale/locales-vi-VN.xml +304 -0
  64. data/resource/locale/locales-zh-CN.xml +304 -0
  65. data/resource/locale/locales-zh-TW.xml +304 -0
  66. data/resource/schema/csl-categories.rnc +39 -0
  67. data/resource/schema/csl-data.rnc +98 -0
  68. data/resource/schema/csl-terms.rnc +106 -0
  69. data/resource/schema/csl-types.rnc +39 -0
  70. data/resource/schema/csl-variables.rnc +182 -0
  71. data/resource/schema/csl.rnc +941 -0
  72. data/resource/style/acta-materialia-x.csl +128 -0
  73. data/resource/style/advanced-engineering-materials-x.csl +121 -0
  74. data/resource/style/ama.csl +185 -0
  75. data/resource/style/ama2-x.csl +179 -0
  76. data/resource/style/apa-x.csl +324 -0
  77. data/resource/style/apa.csl +254 -0
  78. data/resource/style/apsa-x.csl +163 -0
  79. data/resource/style/apsa.csl +176 -0
  80. data/resource/style/asa-x.csl +203 -0
  81. data/resource/style/asa.csl +216 -0
  82. data/resource/style/asm-journals-x.csl +131 -0
  83. data/resource/style/bibtex-x2.csl +175 -0
  84. data/resource/style/bluebook-demo-x.csl +392 -0
  85. data/resource/style/bluebook-demo.csl +942 -0
  86. data/resource/style/chicago-author-date-listing.csl +434 -0
  87. data/resource/style/chicago-author-date.csl +369 -0
  88. data/resource/style/chicago-fullnote-bibliography-bb.csl +928 -0
  89. data/resource/style/chicago-fullnote-bibliography.csl +695 -0
  90. data/resource/style/chicago-note-bibliography.csl +446 -0
  91. data/resource/style/chicago-note.csl +388 -0
  92. data/resource/style/greek-chicago-x.csl +1182 -0
  93. data/resource/style/harvard1-institution-italic.csl +190 -0
  94. data/resource/style/harvard1.csl +181 -0
  95. data/resource/style/ieee.csl +129 -0
  96. data/resource/style/mhra-x.csl +312 -0
  97. data/resource/style/mhra.csl +390 -0
  98. data/resource/style/mhra_note_without_bibliography-x.csl +330 -0
  99. data/resource/style/mhra_note_without_bibliography.csl +338 -0
  100. data/resource/style/mla-x.csl +178 -0
  101. data/resource/style/mla.csl +189 -0
  102. data/resource/style/nature-x.csl +81 -0
  103. data/resource/style/nature.csl +88 -0
  104. data/resource/style/nlm.csl +117 -0
  105. data/spec/citeproc/bibliography_spec.rb +45 -0
  106. data/spec/citeproc/citeproc_spec.rb +76 -0
  107. data/spec/citeproc/date_spec.rb +85 -0
  108. data/spec/citeproc/formatter_spec.rb +101 -0
  109. data/spec/citeproc/item_spec.rb +71 -0
  110. data/spec/citeproc/name_spec.rb +30 -0
  111. data/spec/citeproc/processor_spec.rb +61 -0
  112. data/spec/citeproc/selector_spec.rb +82 -0
  113. data/spec/citeproc/variable_spec.rb +69 -0
  114. data/spec/csl/locale_spec.rb +208 -0
  115. data/spec/csl/node_spec.rb +25 -0
  116. data/spec/csl/nodes_spec.rb +140 -0
  117. data/spec/csl/style_spec.rb +62 -0
  118. data/spec/csl/term_spec.rb +56 -0
  119. data/spec/fixtures/dates.yaml +80 -0
  120. data/spec/fixtures/names.yaml +115 -0
  121. data/spec/fixtures/nodes.yaml +245 -0
  122. data/spec/spec_helper.rb +18 -0
  123. data/spec/support/attributes_spec.rb +39 -0
  124. data/spec/support/tree_spec.rb +163 -0
  125. metadata +264 -0
@@ -0,0 +1,30 @@
1
+ module CiteProc
2
+
3
+ describe Name do
4
+ Test::Fixtures::Names.keys.each do |feature|
5
+ describe feature do
6
+
7
+ Test::Fixtures::Names[feature].each do |test|
8
+
9
+ it test['it'] do
10
+ names = test['names'].map { |name| Name.new(name) }
11
+ expected = test['expected']
12
+ options = test['options']
13
+
14
+ result = case feature
15
+ when 'sort'
16
+ names.sort.map(&:to_s)
17
+ else
18
+ names.map { |name| name.send(feature, options) }
19
+ end
20
+
21
+ result.should == expected
22
+ end
23
+
24
+ end
25
+
26
+ end
27
+ end
28
+ end
29
+
30
+ end
@@ -0,0 +1,61 @@
1
+ require 'spec_helper'
2
+
3
+ module CiteProc
4
+
5
+ describe Processor do
6
+
7
+ let(:processor) { Processor.new }
8
+ let(:item) { Hash['author', [{ 'given' => 'Edgar A.', 'family' => 'Poe' }], 'title', 'The Raven', 'type', 'book', 'issued', { 'date-parts' => [[1996]] }, 'publisher', 'Library of America', 'publisher-place', 'New York' ] }
9
+
10
+ describe '.process' do
11
+ it 'returns an empty string by default' do
12
+ Processor.process({}).should == ''
13
+ end
14
+
15
+ it 'returns a formatted citation' do
16
+ Processor.process(item, :mode => :citation, :style => :apa).should == '(Poe, 1996)'
17
+ Processor.process(item, :mode => :citation, :style => :mla).should == '(Poe)'
18
+ end
19
+
20
+ it 'returns a formatted bibliographic entry' do
21
+ Processor.process(item, :mode => :bibliography, :style => :apa).should == 'Poe, E. A. (1996). The Raven. New York: Library of America.'
22
+ end
23
+ end
24
+
25
+ describe '#style' do
26
+ it 'accepts a style name' do
27
+ processor.style = 'apa'
28
+ processor.style.should_not be_nil
29
+ end
30
+ end
31
+
32
+ describe '#abbreviations' do
33
+ it 'can be set and records new abbreviations' do
34
+ expected = { 'default' => { 'container-title' => { 'long' => 'short' } } }
35
+ processor.abbreviations = expected
36
+ processor.abbreviations.should == expected
37
+
38
+ alternate = { 'alternate' => { 'container-title' => { 'long' => 'short' } } }
39
+ processor.add_abbreviations(alternate)
40
+ processor.abbreviations.should == expected.merge(alternate)
41
+
42
+ result = processor.abbreviate('container-title', 'long')
43
+ result.should == 'short'
44
+
45
+ result = processor.abbreviate('container-title', 'long', 'alternate')
46
+ result.should == 'short'
47
+
48
+ result = processor.abbreviate('container-title', 'new')
49
+ result.should == 'new'
50
+
51
+ result = processor.abbreviate('container-title', 'new', 'alternate')
52
+ result.should == 'new'
53
+
54
+ result = processor.abbreviate('hereinafter', 'new')
55
+ result.should == 'new'
56
+ end
57
+ end
58
+
59
+ end
60
+
61
+ end
@@ -0,0 +1,82 @@
1
+ module CiteProc
2
+ describe Selector do
3
+ describe '#new' do
4
+ it { should_not be_nil }
5
+
6
+ %w{ all any none select include exclude }.each do |mode|
7
+ it "accepts string/symbol values (#{mode})" do
8
+ Selector.new(mode).type.should_not be_nil
9
+ # Selector.new(mode.to_sym).type.should_not be_nil
10
+ end
11
+ end
12
+
13
+ describe 'json API support' do
14
+
15
+ it 'accepts a json object (select)' do
16
+ Selector.new(
17
+ "select" => [
18
+ {
19
+ "field" => "type",
20
+ "value" => "book"
21
+ },
22
+ { "field" => "categories",
23
+ "value" => "1990s"
24
+ }
25
+ ]
26
+ ).select.should have(2).items
27
+ end
28
+
29
+ it 'accepts a json string (select)' do
30
+ Selector.new('{
31
+ "select" : [
32
+ {
33
+ "field" : "type",
34
+ "value" : "book"
35
+ },
36
+ { "field" : "categories",
37
+ "value" : "1990s"
38
+ }
39
+ ]
40
+ }
41
+ ').select.should have(2).items
42
+ end
43
+
44
+ end
45
+ end
46
+
47
+ describe '#conditions' do
48
+
49
+ let(:condition) { {'field' => 'type', 'value' => 'book'} }
50
+
51
+ %w{ select include exclude }.each do |mode|
52
+ it "returns the conditions (#{mode})" do
53
+ Selector.new(mode => [condition]).conditions.should == [condition]
54
+ end
55
+ end
56
+
57
+ end
58
+
59
+ describe '#to_proc' do
60
+
61
+ let(:books) { Selector.new('select' => [{'field' => 'type', 'value' => 'book'}]) }
62
+ let(:english_books) { Selector.new('select' => [{'field' => 'type', 'value' => 'book'}, {'field' => 'language', 'value' => 'en'}]) }
63
+
64
+
65
+ it 'can be used as a block to Array#select' do
66
+ [{ 'type' => 'book'}, { 'type' => 'article'}].select(&books).should have(1).item
67
+ end
68
+
69
+ it 'does not filter out anything by default' do
70
+ [1,2,3].select(&Selector.new).should == [1,2,3]
71
+ end
72
+
73
+ describe 'when the type is :select' do
74
+ it 'selects items that match all conditions' do
75
+ [{ 'type' => 'book'}, { 'type' => 'article'}, { 'type' => 'book', 'language' => 'en'}].select(&english_books).should have(1).item
76
+ end
77
+ end
78
+
79
+ end
80
+
81
+ end
82
+ end
@@ -0,0 +1,69 @@
1
+ module CiteProc
2
+
3
+ describe Variable do
4
+
5
+ describe '#new' do
6
+ it { should_not be_nil }
7
+
8
+ it 'accepts a string value' do
9
+ Variable.new('test').value.should == 'test'
10
+ end
11
+
12
+ it 'accepts an attributes hash' do
13
+ Variable.new('value' => 'test').value.should == 'test'
14
+ end
15
+
16
+ it 'supports self yielding block' do
17
+ Variable.new { |v| v.value = 'test' }.value.should == 'test'
18
+ end
19
+ end
20
+
21
+ describe '#to_s' do
22
+ it 'displays the value' do
23
+ Variable.new('test').to_s.should == 'test'
24
+ end
25
+ end
26
+
27
+ describe '#to_i' do
28
+ it 'returns zero by default' do
29
+ Variable.new.to_i.should == 0
30
+ end
31
+
32
+ context 'when the value is numeric' do
33
+ %w{ -23 -1 0 1 23 }.each do |value|
34
+ it "returns the integer value (#{value})" do
35
+ Variable.new(value).to_i.should == value.to_i
36
+ end
37
+ end
38
+
39
+ it 'returns only the first numeric value if there are several' do
40
+ Variable.new('testing 1, 2, 3...').to_i.should == 1
41
+ end
42
+ end
43
+ end
44
+
45
+
46
+ describe '#numeric?' do
47
+ it 'returns false by default' do
48
+ Variable.new.should_not be_numeric
49
+ end
50
+
51
+ context 'variable contains a number' do
52
+ it 'returns true (string initialized)' do
53
+ Variable.new('23').should be_numeric
54
+ Variable.new('foo 23').should be_numeric
55
+ end
56
+ it 'returns true (integer initialized)' do
57
+ Variable.new(23).should be_numeric
58
+ end
59
+ end
60
+
61
+ context 'variable does not contain a number' do
62
+ it 'returns false for strings' do
63
+ Variable.new('test').should_not be_numeric
64
+ end
65
+ end
66
+ end
67
+
68
+ end
69
+ end
@@ -0,0 +1,208 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ module CSL
4
+
5
+ describe Locale do
6
+
7
+ let(:locale) { Locale.new }
8
+
9
+ let(:en) { Locale.new('en-US') }
10
+ let(:de) { Locale.new('de-DE') }
11
+ let(:fr) { Locale.new('fr-FR') }
12
+
13
+ describe '#new' do
14
+ it { should_not be_nil }
15
+
16
+ it 'defaults to empty language' do
17
+ locale.language.should be_nil
18
+ end
19
+
20
+ it 'defaults to empty region' do
21
+ locale.region.should be_nil
22
+ end
23
+
24
+ it 'contains no terms, date, and options content by default' do
25
+ [:terms, :date, :options].each do |m|
26
+ locale.send(m).should_not be_nil
27
+ locale.send(m).should be_empty
28
+ end
29
+ end
30
+
31
+ it 'parses an XML string' do
32
+ locale = Locale.new <<-END
33
+ <locale xml:lang="en">
34
+ <terms>
35
+ <term name="editortranslator" form="short">
36
+ <single>ed. &amp; trans.</single>
37
+ <multiple>eds. &amp; trans.</multiple>
38
+ </term>
39
+ </terms>
40
+ </locale>
41
+ END
42
+
43
+ locale.language.should == 'en'
44
+ locale.options.should be_empty
45
+ locale.terms['editortranslator'].pluralize('form' => 'short').should == 'eds. & trans.'
46
+ end
47
+ end
48
+
49
+ describe '#set' do
50
+ it 'sets a new language' do
51
+ language = 'de'
52
+ locale.language = language
53
+ locale.language.should == language
54
+ locale.region.should == 'DE'
55
+ end
56
+
57
+ it 'sets a new language and region' do
58
+ language = 'de'
59
+ region = 'DE'
60
+
61
+ locale.region = region
62
+ locale.language.should == language
63
+ locale.region.should == region
64
+ end
65
+
66
+ end
67
+
68
+ describe '#terms' do
69
+ it 'contains common terms by default' do
70
+ en['book'].should_not be_nil
71
+ end
72
+
73
+ it 'contains variants for form and number' do
74
+ en['page']['long'].values.should == ['page', 'pages']
75
+ en['page']['short'].values.should == ['p.', 'pp.']
76
+ end
77
+
78
+ it 'returns different values for different languages' do
79
+ [en, de, fr].map do |l|
80
+ l['editor'].to_s
81
+ end.uniq.length.should == 3
82
+ end
83
+ end
84
+
85
+ describe '#ordinalize' do
86
+ context 'in English' do
87
+ it 'works' do
88
+ result = (1..13).map do |i|
89
+ en.ordinalize(i)
90
+ end
91
+
92
+ expected = %w{ 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th }
93
+
94
+ result.should == expected
95
+
96
+ result = [20, 21, 22, 23, 24, 100, 101, 102, 103, 104, 113, 123].map do |i|
97
+ en.ordinalize(i)
98
+ end
99
+
100
+ expected = %w{ 20th 21st 22nd 23rd 24th 100th 101st 102nd 103rd 104th 113th 123rd }
101
+
102
+ result.should == expected
103
+
104
+ end
105
+
106
+ it 'long-forms work in English' do
107
+ result = (1..13).map do |i|
108
+ en.ordinalize(i, 'form' => 'long-ordinal')
109
+ end
110
+
111
+ expected = %w{ first second third fourth fifth sixth seventh eighth ninth tenth 11th 12th 13th }
112
+
113
+ result.should == expected
114
+ end
115
+ end
116
+
117
+ context 'in German' do
118
+ it 'works' do
119
+ result = (1..13).map do |i|
120
+ de.ordinalize(i)
121
+ end
122
+
123
+ expected = %w{ 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. }
124
+
125
+ result.should == expected
126
+
127
+ result = [20, 21, 22, 23, 24, 100, 101, 102, 103, 104, 113, 123].map do |i|
128
+ de.ordinalize(i)
129
+ end
130
+
131
+ expected = %w{ 20. 21. 22. 23. 24. 100. 101. 102. 103. 104. 113. 123. }
132
+
133
+ result.should == expected
134
+
135
+ end
136
+
137
+ it 'long-forms work' do
138
+ result = (1..13).map do |i|
139
+ de.ordinalize(i, 'form' => 'long-ordinal')
140
+ end
141
+
142
+ expected = %w{ erster zweiter dritter vierter fünfter sechster siebter achter neunter zehnter 11. 12. 13. }
143
+
144
+ result.should == expected
145
+ end
146
+
147
+ it 'female long-forms work' do
148
+ result = (1..13).map do |i|
149
+ de.ordinalize(i, 'form' => 'long-ordinal', 'gender-form' => 'feminine')
150
+ end
151
+
152
+ expected = %w{ erste zweite dritte vierte fünfte sechste siebte achte neunte zehnte 11. 12. 13. }
153
+
154
+ result.should == expected
155
+ end
156
+
157
+ end
158
+
159
+ end
160
+
161
+ describe 'sorting' do
162
+
163
+ context 'default priority' do
164
+ [['da-DK', 'de-DE'], ['ar-AR', 'el-GR'], ['en-US', 'it-IT'], ['en-US', 'ar-AR'], ['en-US', 'de-DE']].each do |a,b|
165
+ it "sorts by default and alphabetically by language (#{a} < #{b})" do
166
+ (Locale.new(a) < Locale.new(b)).should be true
167
+ end
168
+ end
169
+
170
+ [['de-AT', 'de-CH'], ['de-DE', 'de-CH'], ['de-DE', 'de-AT'], ['pt-PT', 'pt-BR'], ['zh-CN', 'zh-TW']].each do |a,b|
171
+ it "sorts by default and alphabetically by region (#{a} < #{b})" do
172
+ (Locale.new(a) < Locale.new(b)).should be true
173
+ end
174
+ end
175
+
176
+ it 'sorts nil languages last' do
177
+ [Locale.new, Locale.new('de'), Locale.new('sv-SE')].sort.last.language.should be_nil
178
+ end
179
+
180
+ it 'sorts nil regions as default' do
181
+ %w{ de de-AT de-DE de-CH }.map { |lc| Locale.new(lc) }.sort.map(&:tag).should == %w{ de-DE de-DE de-AT de-CH }
182
+ end
183
+ end
184
+
185
+ context 'language priority (de)' do
186
+ [['de-DE', 'da-DK'], ['ar-AR', 'el-GR'], ['en-US', 'it-IT'], ['en-US', 'ar-AR'], ['de-DE', 'en-US']].each do |a,b|
187
+ it "sorts by default and alphabetically by language (#{a} < #{b})" do
188
+ [Locale.new(a), Locale.new(b)].sort(&Locale.sort('de')).map(&:tag).should == [a,b]
189
+ end
190
+ end
191
+
192
+ [['de-DE', 'de-AT'], ['de-AT', 'ar-AR'], ['en-US', 'ar-AR'], ['de-CH', 'en-US']].each do |a,b|
193
+ it "sorts by default and alphabetically by region (#{a} < #{b})" do
194
+ [Locale.new(a), Locale.new(b)].sort(&Locale.sort('de')).map(&:tag).should == [a,b]
195
+ end
196
+ end
197
+ end
198
+
199
+ context 'regional priority (CH)' do
200
+ it 'sorts by default and alphabetically' do
201
+ %w{ de-AT de-DE de-CH }.map { |lc| Locale.new(lc) }.sort(&Locale.sort(nil, 'CH')).map(&:tag).should == %w{ de-CH de-DE de-AT }
202
+ end
203
+ end
204
+
205
+ end
206
+
207
+ end
208
+ end