citeproc-rb 0.0.1

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.
Files changed (52) hide show
  1. data/README +93 -0
  2. data/Rakefile +73 -0
  3. data/TODO +25 -0
  4. data/VERSION +1 -0
  5. data/data/locales/locales_en.xml +89 -0
  6. data/data/locales/locales_nl.xml +96 -0
  7. data/data/styles/ama.csl +184 -0
  8. data/data/styles/apa.csl +230 -0
  9. data/data/styles/apsa.csl +175 -0
  10. data/data/styles/asa.csl +215 -0
  11. data/data/styles/chicago-author-date.csl +361 -0
  12. data/data/styles/chicago-fullnote-bibliography.csl +665 -0
  13. data/data/styles/chicago-note-bibliography.csl +437 -0
  14. data/data/styles/chicago-note.csl +383 -0
  15. data/data/styles/harvard.csl +185 -0
  16. data/data/styles/ieee.csl +111 -0
  17. data/data/styles/mhra.csl +317 -0
  18. data/data/styles/mla.csl +173 -0
  19. data/data/styles/nature.csl +87 -0
  20. data/data/styles/nlm.csl +118 -0
  21. data/examples/citeproc_driver.rb +174 -0
  22. data/lib/citeproc.rb +28 -0
  23. data/lib/citeproc/csl.rb +647 -0
  24. data/lib/citeproc/csl_parser.rb +479 -0
  25. data/lib/citeproc/csl_processor.rb +485 -0
  26. data/lib/citeproc/csl_reference.rb +228 -0
  27. data/lib/citeproc/filters/bibo/bibo.rb +635 -0
  28. data/lib/citeproc/filters/bibo/bibo_input_filter.rb +242 -0
  29. data/lib/citeproc/filters/bibo/bibo_utils.rb +75 -0
  30. data/lib/citeproc/filters/csl_citation/csl_citation.rb +137 -0
  31. data/lib/citeproc/filters/csl_citation/csl_citation_input_filter.rb +274 -0
  32. data/lib/citeproc/formatters/base_formatter.rb +115 -0
  33. data/lib/citeproc/formatters/xhtml_formatter.rb +144 -0
  34. data/lib/citeproc/input_filter.rb +97 -0
  35. data/test/fixtures/bibo_test_data.n3 +191 -0
  36. data/test/fixtures/bibo_test_data.yaml +96 -0
  37. data/test/fixtures/bibo_test_how_to_cook.n3 +63 -0
  38. data/test/fixtures/bibo_test_zotero.rdf +129 -0
  39. data/test/fixtures/csl_test_data.json +62 -0
  40. data/test/fixtures/csl_test_data.yaml +8 -0
  41. data/test/fixtures/locales/test_locale_en.xml +89 -0
  42. data/test/fixtures/styles/test_csl_parse.csl +230 -0
  43. data/test/fixtures/styles/test_groups.csl +32 -0
  44. data/test/fixtures/styles/test_names.csl +46 -0
  45. data/test/fixtures/styles/test_sort.csl +125 -0
  46. data/test/fixtures/styles/test_text_terms.csl +40 -0
  47. data/test/test_all.rb +14 -0
  48. data/test/test_bibo.rb +20 -0
  49. data/test/test_csl.rb +136 -0
  50. data/test/test_csl_parser.rb +222 -0
  51. data/test/test_sample_bibo.rb +82 -0
  52. metadata +101 -0
@@ -0,0 +1,173 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <style xmlns="http://purl.org/net/xbiblio/csl" class="in-text" xml:lang="en">
3
+ <info>
4
+ <title>Modern Language Association</title>
5
+ <id>http://www.zotero.org/styles/mla</id>
6
+ <link href="http://www.zotero.org/styles/mla"/>
7
+ <author>
8
+ <name>Simon Kornblith</name>
9
+ <email>simon@simonster.com</email>
10
+ </author>
11
+ <contributor>
12
+ <name> modified by Christian Werthschulte</name>
13
+ <email>Christian.Werthschulte@rub.de</email>
14
+ </contributor>
15
+ <category term="generic-base"/>
16
+ <category term="author-date"/>
17
+ <updated>2008-05-13T01:26:11+00:00</updated>
18
+ </info>
19
+ <macro name="editor-translator">
20
+ <names variable="editor translator" delimiter=". ">
21
+ <label form="verb-short" text-case="capitalize-first" suffix=". "/>
22
+ <name and="symbol" delimiter=", "/>
23
+ </names>
24
+ </macro>
25
+ <macro name="author">
26
+ <names variable="author">
27
+ <name name-as-sort-order="first" and="text" sort-separator=", "
28
+ delimiter=", " delimiter-precedes-last="always"/>
29
+ <label form="short" prefix=", " suffix="."/>
30
+ <substitute>
31
+ <names variable="editor"/>
32
+ <names variable="translator"/>
33
+ <text macro="title"/>
34
+ </substitute>
35
+ </names>
36
+ </macro>
37
+ <macro name="author-short">
38
+ <names variable="author">
39
+ <name form="short" and="symbol" delimiter=", " initialize-with=". "/>
40
+ <substitute>
41
+ <names variable="editor"/>
42
+ <names variable="translator"/>
43
+ <text macro="title-short"/>
44
+ </substitute>
45
+ </names>
46
+ </macro>
47
+ <macro name="access">
48
+ <group delimiter=" ">
49
+ <date variable="accessed">
50
+ <date-part name="day" suffix=" "/>
51
+ <date-part name="month" form="short" include-period="true" suffix=" "/>
52
+ <date-part name="year"/>
53
+ </date>
54
+ <text variable="URL" prefix="&lt;" suffix="&gt;"/>
55
+ </group>
56
+ </macro>
57
+ <macro name="title">
58
+ <choose>
59
+ <if type="book">
60
+ <text variable="title" font-style="italic"/>
61
+ </if>
62
+ <else>
63
+ <text variable="title" quotes="true"/>
64
+ </else>
65
+ </choose>
66
+ </macro>
67
+ <macro name="title-short">
68
+ <choose>
69
+ <if type="book">
70
+ <text variable="title" form="short" font-style="italic"/>
71
+ </if>
72
+ <else>
73
+ <text variable="title" form="short" quotes="true"/>
74
+ </else>
75
+ </choose>
76
+ </macro>
77
+ <macro name="publisher-year">
78
+ <group delimiter=", ">
79
+ <group delimiter=": ">
80
+ <text variable="publisher-place"/>
81
+ <text variable="publisher"/>
82
+ </group>
83
+ <date variable="issued">
84
+ <date-part name="year"/>
85
+ </date>
86
+ </group>
87
+ </macro>
88
+ <citation>
89
+ <option name="et-al-min" value="4"/>
90
+ <option name="et-al-use-first" value="1"/>
91
+ <option name="disambiguate-add-names" value="true"/>
92
+ <option name="disambiguate-add-givenname" value="true"/>
93
+ <layout prefix="(" suffix=")" delimiter="; ">
94
+ <group delimiter=" ">
95
+ <choose>
96
+ <if variable="author editor translator" match="any">
97
+ <group delimiter=", ">
98
+ <text macro="author-short"/>
99
+ <choose>
100
+ <if disambiguate="true">
101
+ <text macro="title-short"/>
102
+ </if>
103
+ </choose>
104
+ </group>
105
+ </if>
106
+ <else>
107
+ <text macro="title-short"/>
108
+ </else>
109
+ </choose>
110
+ <text variable="locator"/>
111
+ </group>
112
+ </layout>
113
+ </citation>
114
+ <bibliography>
115
+ <option name="hanging-indent" value="true"/>
116
+ <option name="et-al-min" value="4"/>
117
+ <option name="et-al-use-first" value="1"/>
118
+ <option name="line-spacing" value="2"/>
119
+ <option name="subsequent-author-substitute" value="---"/>
120
+ <sort>
121
+ <key macro="author"/>
122
+ <key variable="title"/>
123
+ </sort>
124
+ <layout>
125
+ <text macro="author" suffix="."/>
126
+ <text macro="title" prefix=" " suffix="."/>
127
+ <choose>
128
+ <if type="book">
129
+ <text macro="editor-translator" prefix=" " suffix="."/>
130
+ <text macro="publisher-year" prefix=" " suffix="."/>
131
+ </if>
132
+ <else-if type="chapter">
133
+ <group class="container">
134
+ <text variable="container-title" font-style="italic" prefix=" " suffix="."/>
135
+ <text macro="editor-translator" prefix=" " suffix="."/>
136
+ <text macro="publisher-year" prefix=" " suffix="."/>
137
+ </group>
138
+ <text variable="page" prefix=" " suffix="."/>
139
+ </else-if>
140
+ <else>
141
+ <group class="container" prefix=" " suffix="." delimiter=": ">
142
+ <group delimiter=" ">
143
+ <text macro="editor-translator" suffix="."/>
144
+ <text variable="container-title" font-style="italic"/>
145
+ <choose>
146
+ <if type="article-journal">
147
+ <group delimiter=" ">
148
+ <group delimiter=".">
149
+ <text variable="volume"/>
150
+ <text variable="issue"/>
151
+ </group>
152
+ <date variable="issued" prefix="(" suffix=")">
153
+ <date-part name="year"/>
154
+ </date>
155
+ </group>
156
+ </if>
157
+ <else>
158
+ <date variable="issued">
159
+ <date-part name="day" suffix=" "/>
160
+ <date-part name="month" form="short" include-period="true" suffix=" "/>
161
+ <date-part name="year"/>
162
+ </date>
163
+ </else>
164
+ </choose>
165
+ </group>
166
+ <text variable="page"/>
167
+ </group>
168
+ </else>
169
+ </choose>
170
+ <text prefix=" " suffix="." macro="access"/>
171
+ </layout>
172
+ </bibliography>
173
+ </style>
@@ -0,0 +1,87 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <?oxygen RNGSchema="http://xbiblio.svn.sourceforge.net/viewvc/*checkout*/xbiblio/csl/schema/trunk/csl.rnc" type="compact"?>
3
+ <style xmlns="http://purl.org/net/xbiblio/csl" class="in-text" xml:lang="en">
4
+ <info>
5
+ <title>Nature Journal</title>
6
+ <id>http://www.zotero.org/styles/nature</id>
7
+ <link href="http://www.zotero.org/styles/nature"/>
8
+ <author>
9
+ <name>Michael Berkowitz</name>
10
+ <email>mberkowi@gmu.edu</email>
11
+ </author>
12
+ <category term="biology"/>
13
+ <category term="generic-base"/>
14
+ <category term="numeric"/>
15
+ <updated>2008-04-01T17:47:20+00:00</updated>
16
+ </info>
17
+ <macro name="title">
18
+ <choose>
19
+ <if type="book">
20
+ <text variable="title" font-style="italic"/>
21
+ </if>
22
+ <else>
23
+ <text variable="title"/>
24
+ </else>
25
+ </choose>
26
+ </macro>
27
+ <macro name="author">
28
+ <names variable="author">
29
+ <name sort-separator=", " delimiter=", " and="symbol" initialize-with="." delimiter-precedes-last="never" name-as-sort-order="all"/>
30
+ </names>
31
+ </macro>
32
+ <macro name="access">
33
+ <choose>
34
+ <if variable="volume"/>
35
+ <else-if variable="DOI">
36
+ <text variable="DOI" prefix="doi:"/>
37
+ </else-if>
38
+ <else-if variable="URL">
39
+ <text term="at"/>
40
+ <text variable="URL" prefix=" &lt;" suffix="&gt;"/>
41
+ </else-if>
42
+ </choose>
43
+ </macro>
44
+ <macro name="issuance">
45
+ <choose>
46
+ <if type="book">
47
+ <group prefix="(" suffix=").">
48
+ <text variable="publisher" suffix=": " />
49
+ <text variable="publisher-place" suffix=", " />
50
+ <date variable="issued">
51
+ <date-part name="year"/>
52
+ </date>
53
+ </group>
54
+ </if>
55
+ <else>
56
+ <date prefix="(" suffix=")." variable="issued">
57
+ <date-part name="year"/>
58
+ </date>
59
+ </else>
60
+ </choose>
61
+ </macro>
62
+ <citation>
63
+ <option name="collapse" value="citation-number"/>
64
+ <sort>
65
+ <key variable="citation-number"/>
66
+ </sort>
67
+ <layout vertical-align="sup" delimiter=",">
68
+ <text variable="citation-number"/>
69
+ </layout>
70
+ </citation>
71
+ <bibliography>
72
+ <option name="et-al-min" value="4"/>
73
+ <option name="et-al-use-first" value="1"/>
74
+ <option name="second-field-align" value="true"/>
75
+ <option name="entry-spacing" value="0"/>
76
+ <layout>
77
+ <text variable="citation-number" suffix="."/>
78
+ <text macro="author"/>
79
+ <text macro="title" prefix=" " suffix=". "/>
80
+ <text variable="container-title" font-style="italic" suffix=" " form="short"/>
81
+ <text variable="volume" suffix=", " font-weight="bold"/>
82
+ <text variable="page"/>
83
+ <text macro="issuance" />
84
+ <text macro="access" />
85
+ </layout>
86
+ </bibliography>
87
+ </style>
@@ -0,0 +1,118 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <?oxygen RNGSchema="http://xbiblio.svn.sourceforge.net/viewvc/*checkout*/xbiblio/csl/schema/trunk/csl.rnc" type="compact"?>
3
+ <style xmlns="http://purl.org/net/xbiblio/csl" class="in-text" xml:lang="en">
4
+ <info>
5
+ <title>National Library of Medicine</title>
6
+ <id>http://www.zotero.org/styles/nlm</id>
7
+ <link href="http://www.zotero.org/styles/nlm"/>
8
+ <author>
9
+ <name>Michael Berkowitz</name>
10
+ <email>mberkowi@gmu.edu</email>
11
+ </author>
12
+ <category term="generic-base"/>
13
+ <category term="numeric"/>
14
+ <updated>2008-02-02T00:00:00+00:00</updated>
15
+ </info>
16
+ <macro name="author">
17
+ <names variable="author" suffix=". ">
18
+ <name sort-separator=" " initialize-with="" name-as-sort-order="all" delimiter=", " delimiter-precedes-last="always"/>
19
+ </names>
20
+ </macro>
21
+ <macro name="editor">
22
+ <names variable="editor" suffix=", editor(s). ">
23
+ <name sort-separator=" " initialize-with="" name-as-sort-order="all" delimiter=", " delimiter-precedes-last="always"/>
24
+ </names>
25
+ </macro>
26
+ <macro name="publisher">
27
+ <text variable="publisher-place" suffix=": "/>
28
+ <text variable="publisher" suffix="; "/>
29
+ <date variable="issued">
30
+ <date-part name="year" suffix=". "/>
31
+ </date>
32
+ </macro>
33
+ <macro name="access">
34
+ <group delimiter=" ">
35
+ <group prefix="[" suffix="]" delimiter=" ">
36
+ <text term="cited" text-case="lowercase"/>
37
+ <date variable="accessed" suffix=" ">
38
+ <date-part name="year"/>
39
+ <date-part name="month" prefix=" " form="short"/>
40
+ <date-part name="day" prefix=" "/>
41
+ </date>
42
+ </group>
43
+ <group>
44
+ <text value="Available from: "/>
45
+ <text variable="URL"/>
46
+ </group>
47
+ </group>
48
+ </macro>
49
+ <macro name="title">
50
+ <group delimiter=" ">
51
+ <text variable="title"/>
52
+ <choose>
53
+ <if variable="URL">
54
+ <text term="internet" prefix="[" suffix="]" text-case="capitalize-first"/>
55
+ </if>
56
+ </choose>
57
+ </group>
58
+ </macro>
59
+ <macro name="edition">
60
+ <choose>
61
+ <if is-numeric="edition">
62
+ <group delimiter=" ">
63
+ <number variable="edition" form="ordinal"/>
64
+ <text term="edition" form="short" suffix="."/>
65
+ </group>
66
+ </if>
67
+ <else>
68
+ <text variable="edition" suffix="."/>
69
+ </else>
70
+ </choose>
71
+ </macro>
72
+ <citation>
73
+ <option name="collapse" value="citation-number"/>
74
+ <sort>
75
+ <key variable="citation-number"/>
76
+ </sort>
77
+ <layout prefix="(" suffix=")" delimiter="; ">
78
+ <text variable="citation-number"/>
79
+ </layout>
80
+ </citation>
81
+ <bibliography>
82
+ <option name="et-al-min" value="7"/>
83
+ <option name="et-al-use-first" value="6"/>
84
+ <option name="second-field-align" value="true"/>
85
+ <layout>
86
+ <text variable="citation-number" suffix=". "/>
87
+ <text macro="author"/>
88
+ <text macro="title" suffix=". "/>
89
+ <choose>
90
+ <if type="book">
91
+ <text macro="edition" prefix=" " suffix=" "/>
92
+ <text macro="publisher" prefix=" "/>
93
+ </if>
94
+ <else-if type="chapter">
95
+ <group prefix=" " suffix=". ">
96
+ <text term="in" suffix=": " text-case="capitalize-first"/>
97
+ <text macro="editor"/>
98
+ <text variable="container-title"/>
99
+ </group>
100
+ <text macro="publisher" prefix=" "/>
101
+ <text variable="page" prefix=" p. " suffix="."/>
102
+ </else-if>
103
+ <else>
104
+ <text variable="container-title" suffix=". " form="short"/>
105
+ <date variable="issued" suffix=";">
106
+ <date-part name="year" suffix=" "/>
107
+ <date-part name="month" form="short" suffix=" "/>
108
+ <date-part name="day"/>
109
+ </date>
110
+ <text variable="volume"/>
111
+ <text variable="issue" prefix="(" suffix="):"/>
112
+ <text variable="page" suffix="."/>
113
+ </else>
114
+ </choose>
115
+ <text macro="access"/>
116
+ </layout>
117
+ </bibliography>
118
+ </style>
@@ -0,0 +1,174 @@
1
+ #! /usr/bin/env ruby
2
+ $:.unshift File.join(File.dirname(__FILE__),'..','lib')
3
+
4
+ # == Synopsis
5
+ #
6
+ # Example of how to use Citeproc-rb.
7
+ #
8
+ # == Author
9
+ #
10
+ # Liam Magee
11
+ #
12
+ # == Copyright
13
+ #
14
+ # Copyright (c) 2008, Liam Magee.
15
+ # Licensed under the same terms as Ruby - see http://www.ruby-lang.org/en/LICENSE.txt.
16
+ #
17
+
18
+
19
+ require 'citeproc'
20
+
21
+
22
+ class CiteprocDriver
23
+ attr_accessor :csl, :input, :input_filter, :input_content_type, :output, :formatter_type, :format_type
24
+ attr_accessor :documents, :style, :formatter
25
+
26
+ def initialize
27
+ @input_filter_type = 'csl'
28
+ @processor_type = 'text'
29
+ @input_content_type = 'yaml'
30
+ @output = 'stdout'
31
+ end
32
+
33
+
34
+ def drive_csl
35
+ process_args
36
+
37
+ load_citations
38
+
39
+ load_csl
40
+
41
+ load_locale
42
+
43
+ load_processor
44
+
45
+ load_formatter
46
+
47
+ do_format
48
+ end
49
+
50
+
51
+ def load_citations
52
+ case @input_filter_type
53
+ when "bibo", "bibliontology"
54
+ @input_filter = Bibo::BiboInputFilter.new
55
+ else
56
+ @input_filter = CSL::CslInputFilter.new
57
+ end
58
+ @input_filter.parse(@input, { :content_type => @input_content_type })
59
+ end
60
+
61
+
62
+ def load_csl
63
+ parser = Citeproc::CslParser.new(@csl)
64
+ @style = parser.style
65
+ end
66
+
67
+
68
+ def load_locale
69
+ if @locale_input
70
+ parser = Citeproc::CslParser.new(@locale_input)
71
+ @locale = parser.terms.locales.first if parser.terms
72
+ end
73
+ end
74
+
75
+
76
+ def load_processor
77
+ @processor = Citeproc::CslProcessor.new
78
+ end
79
+
80
+
81
+ def load_formatter
82
+ case @format_type
83
+ when "xhtml"
84
+ @formatter = Citeproc::XhtmlFormatter.new
85
+ else
86
+ @formatter = Citeproc::BaseFormatter.new
87
+ end
88
+ end
89
+
90
+
91
+ def do_format
92
+ nodes = []
93
+ if @format_type == "bibliography"
94
+ nodes = @processor.process_bibliography(@input_filter, @style, @locale)
95
+ elsif @format_type == "citation"
96
+ nodes = @processor.process_citation(@input_filter, @style, @locale)
97
+ else
98
+ nodes = @processor.process_all(@input_filter, @style, @locale)
99
+ end
100
+ results = @formatter.format(nodes)
101
+ if @output == "stdout"
102
+ puts results
103
+ else
104
+ f = File.new(@output, "w+")
105
+ f.write(results)
106
+ f.close
107
+ end
108
+ end
109
+
110
+
111
+ def process_args
112
+ len = ARGV.length - 1
113
+ 0.upto len -1 do |i|
114
+ arg = ARGV[i]
115
+ case
116
+ when arg == '-c', arg == '--csl'
117
+ @csl = ARGV[i + 1]
118
+ when arg == '-i', arg == '--input'
119
+ @input = ARGV[i + 1]
120
+ when arg == '-if', arg == '--input-filter'
121
+ @input_filter_type = ARGV[i + 1]
122
+ when arg == '-ct', arg == '--content-type'
123
+ @input_content_type = ARGV[i + 1]
124
+ when arg == '-o', arg == '--output'
125
+ @output = ARGV[i + 1]
126
+ when arg == '-l', arg == '--locale'
127
+ @locale_input = ARGV[i + 1]
128
+ when arg == '-f', arg == '--formatter'
129
+ @processor_type = ARGV[i + 1]
130
+ when arg == '-ft', arg == '--format-type'
131
+ @format_type = ARGV[i + 1]
132
+ when arg == '-h', arg == '--help'
133
+ print_help
134
+ end
135
+ end
136
+
137
+ # Is there sufficient input?
138
+ if !@input or !@csl
139
+ print_help
140
+ elsif !File.exists?(@input)
141
+ print_bad_file("Input", @input)
142
+ elsif !File.exists?(@csl)
143
+ print_bad_file("CSL", @csl)
144
+ end
145
+ end
146
+
147
+ def print_help
148
+ puts "Correct syntax: ./lib/citeproc.rb -i [the input file] -c [the CSL style definition] [optionally: -o [the output file] -if [the input filter] -ct [the input content type] -of [the output format]]"
149
+ exit
150
+ end
151
+
152
+ def print_bad_file(type, file)
153
+ puts "#{type} file: #{file} seems to be missing, or is inaccessible."
154
+ exit
155
+ end
156
+
157
+ def print_type_error(type, msg)
158
+ puts msg
159
+ puts "Class received was instead: #{type}"
160
+ exit
161
+ end
162
+
163
+ end
164
+
165
+ # Provide a command-line interface to the Ruby Citeproc system
166
+ # Must have:
167
+ # a) a YAML file for the bibliontology references
168
+ # b) a CSL file for the formatting instructions
169
+ # c) a output
170
+ #
171
+ if __FILE__ == $0
172
+ driver = CiteprocDriver.new
173
+ driver.drive_csl
174
+ end