Class: CiteProc::Ruby::Format
- Inherits:
-
Object
- Object
- CiteProc::Ruby::Format
- Defined in:
- lib/citeproc/ruby/format.rb
Direct Known Subclasses
CiteProc::Ruby::Formats::Debug, CiteProc::Ruby::Formats::Html, CiteProc::Ruby::Formats::Text
Class Attribute Summary collapse
-
.available ⇒ Object
readonly
Returns the value of attribute available.
-
.stopwords ⇒ Object
readonly
Returns the value of attribute stopwords.
Instance Attribute Summary collapse
-
#locale ⇒ Object
readonly
Returns the value of attribute locale.
Class Method Summary collapse
- .load(name = nil) ⇒ Object
- .squeezable ⇒ Object
- .squeezable?(string) ⇒ Boolean
- .stopword?(word, locale = :en) ⇒ Boolean
Instance Method Summary collapse
- #apply(input, node, locale = nil) ⇒ Object
- #apply_display ⇒ Object
- #apply_prefix ⇒ Object
- #apply_quotes ⇒ Object
- #apply_suffix ⇒ Object
- #apply_text_case ⇒ Object
- #bibliography(bibliography, locale = nil) ⇒ Object
- #close_inner_quote ⇒ Object
- #close_quote ⇒ Object
- #escape_quotes? ⇒ Boolean
- #join(list, delimiter = nil) ⇒ Object
- #keys ⇒ Object
- #prefix ⇒ Object
- #punctuation_in_quotes? ⇒ Boolean
- #split_closing_quotes(string) ⇒ Object
- #squeezable?(string) ⇒ Boolean
- #squeeze_prefix(string, prefix) ⇒ Object
- #squeeze_suffix(string, suffix) ⇒ Object (also: #concat)
- #strip(string) ⇒ Object
- #suffix ⇒ Object
Class Attribute Details
.available ⇒ Object (readonly)
Returns the value of attribute available.
29 30 31 |
# File 'lib/citeproc/ruby/format.rb', line 29 def available @available end |
.stopwords ⇒ Object (readonly)
Returns the value of attribute stopwords.
29 30 31 |
# File 'lib/citeproc/ruby/format.rb', line 29 def stopwords @stopwords end |
Instance Attribute Details
#locale ⇒ Object (readonly)
Returns the value of attribute locale.
64 65 66 |
# File 'lib/citeproc/ruby/format.rb', line 64 def locale @locale end |
Class Method Details
.load(name = nil) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/citeproc/ruby/format.rb', line 35 def load(name = nil) return new unless name return name if name.is_a?(Format) name = name.to_s.downcase klass = available.detect do |format| format.name.split('::')[-1].downcase == name end raise(Error, "unknown format: #{name}") unless klass klass.new end |
.squeezable ⇒ Object
59 60 61 |
# File 'lib/citeproc/ruby/format.rb', line 59 def squeezable @squeezable ||= Format.squeezable end |
.squeezable?(string) ⇒ Boolean
55 56 57 |
# File 'lib/citeproc/ruby/format.rb', line 55 def squeezable?(string) squeezable === string end |
.stopword?(word, locale = :en) ⇒ Boolean
50 51 52 53 |
# File 'lib/citeproc/ruby/format.rb', line 50 def stopword?(word, locale = :en) return unless stopwords.key?(locale) stopwords[locale].include?(word.downcase) end |
Instance Method Details
#apply(input, node, locale = nil) ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/citeproc/ruby/format.rb', line 149 def apply(input, node, locale = nil) return '' if input.nil? return input if input.empty? || node.nil? return ArgumentError unless node.respond_to?(:formatting_options) @input, @output, @node, @locale = input, input.dup, node, locale setup! # NB: Layout nodes apply formatting to # affixes; all other nodes do not! if node.is_a? CSL::Style::Layout apply_prefix if .key?(:prefix) apply_suffix if .key?(:suffix) end keys.each do |format| if .key?(format) method = "apply_#{format}".tr('-', '_') send method if respond_to?(method) end end unless .empty? output.gsub!(/\.+/, '') if node.strip_periods? apply_quotes if node.quotes? && !locale.nil? finalize_content! unless node.is_a? CSL::Style::Layout apply_prefix if .key?(:prefix) apply_suffix if .key?(:suffix) end apply_display if .key?(:display) finalize! output ensure cleanup! end |
#apply_display ⇒ Object
254 255 |
# File 'lib/citeproc/ruby/format.rb', line 254 def apply_display end |
#apply_prefix ⇒ Object
257 258 259 |
# File 'lib/citeproc/ruby/format.rb', line 257 def apply_prefix output.replace(squeeze_prefix(output, prefix)) end |
#apply_quotes ⇒ Object
210 211 212 |
# File 'lib/citeproc/ruby/format.rb', line 210 def apply_quotes output.replace locale.quote(output, escape_quotes?) end |
#apply_suffix ⇒ Object
261 262 263 |
# File 'lib/citeproc/ruby/format.rb', line 261 def apply_suffix output.replace(squeeze_suffix(output, suffix)) end |
#apply_text_case ⇒ Object
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/citeproc/ruby/format.rb', line 214 def apply_text_case case [:'text-case'] when 'lowercase' output.replace CiteProc.downcase output when 'uppercase' output.replace CiteProc.upcase output when 'capitalize-first' output.sub!(/^([^\p{L}]*)(\p{Ll})/) { "#{$1}#{CiteProc.upcase($2)}" } when 'capitalize-all' output.gsub!(/\b(\p{Ll})/) { CiteProc.upcase($1) } when 'sentence' output.sub!(/^([^\p{L}]*)(\p{Ll})/) { "#{$1}#{CiteProc.upcase($2)}" } output.gsub!(/\b(\p{Lu})(\p{Lu}+)\b/) { "#{$1}#{CiteProc.downcase($2)}" } when 'title' return if locale && locale.language != :en # TODO add support for stop words consisting of multiple words #output.gsub!(/\b(\p{Lu})(\p{Lu}+)\b/) { "#{$1}#{CiteProc.downcase($2)}" } # TODO exceptions: first, last word; followed by colon output.gsub!(/\b(\p{Ll})(\p{L}+)\b/) do |word| if Format.stopword?(word) word else "#{CiteProc.upcase($1)}#{$2}" end end end end |
#bibliography(bibliography, locale = nil) ⇒ Object
144 145 146 147 |
# File 'lib/citeproc/ruby/format.rb', line 144 def bibliography(bibliography, locale = nil) bibliography.connector = "\n" * bibliography.entry_spacing bibliography end |
#close_inner_quote ⇒ Object
202 203 204 |
# File 'lib/citeproc/ruby/format.rb', line 202 def close_inner_quote locale && locale.t('close-inner-quote') || "'" end |
#close_quote ⇒ Object
198 199 200 |
# File 'lib/citeproc/ruby/format.rb', line 198 def close_quote locale && locale.t('close-quote') || '"' end |
#escape_quotes? ⇒ Boolean
194 195 196 |
# File 'lib/citeproc/ruby/format.rb', line 194 def escape_quotes? false end |
#join(list, delimiter = nil) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/citeproc/ruby/format.rb', line 128 def join(list, delimiter = nil) raise ArgumentError unless list.is_a?(Enumerable) return '' if list.length.zero? return list[0] if list.length == 1 if delimiter.nil? || delimiter.empty? list.inject do |m, n| concat(m, n) end else list.inject do |m, n| concat(concat(m, delimiter), n) end end end |
#keys ⇒ Object
66 67 68 |
# File 'lib/citeproc/ruby/format.rb', line 66 def keys @keys ||= (CSL::Schema.attr(:formatting) - [:prefix, :suffix, :display]) end |
#prefix ⇒ Object
265 266 267 |
# File 'lib/citeproc/ruby/format.rb', line 265 def prefix [:prefix].to_s end |
#punctuation_in_quotes? ⇒ Boolean
250 251 252 |
# File 'lib/citeproc/ruby/format.rb', line 250 def punctuation_in_quotes? !locale.nil? && locale.punctuation_in_quotes? end |
#split_closing_quotes(string) ⇒ Object
206 207 208 |
# File 'lib/citeproc/ruby/format.rb', line 206 def split_closing_quotes(string) string.split(/([#{close_inner_quote}#{close_quote}]+)$/, 2) end |
#squeezable?(string) ⇒ Boolean
70 71 72 |
# File 'lib/citeproc/ruby/format.rb', line 70 def squeezable?(string) self.class.squeezable?(string) end |
#squeeze_prefix(string, prefix) ⇒ Object
115 116 117 118 119 120 121 122 123 124 |
# File 'lib/citeproc/ruby/format.rb', line 115 def squeeze_prefix(string, prefix) raise ArgumentError unless string.is_a?(::String) raise ArgumentError unless prefix.is_a?(::String) prefix = prefix.reverse.each_char.drop_while.with_index { |c, i| squeezable?(c) && string.start_with?(prefix[-(i + 1) .. -1]) }.join('').reverse "#{prefix}#{string}" end |
#squeeze_suffix(string, suffix) ⇒ Object Also known as: concat
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/citeproc/ruby/format.rb', line 74 def squeeze_suffix(string, suffix) raise ArgumentError unless string.is_a?(::String) raise ArgumentError unless suffix.is_a?(::String) return string.dup if suffix.empty? return suffix.dup if string.empty? string, stripped = strip(string) string, quotes = split_closing_quotes(string) suffix = decode_entities(suffix) suffix = suffix.each_char.drop_while.with_index { |c, i| squeezable?(c) && string.end_with?(suffix[0, i + 1]) }.join('') # Handle special cases like ?. or ;. if suffix.start_with?('.') && string.end_with?(';', ',', '!', '?', ':') suffix = suffix[1..-1] end # Handle special cases ;, and :, if suffix.start_with?(',') && string.end_with?(';', ':') suffix = suffix[1..-1] end # Handle special cases ,; and :; if suffix.start_with?(';') && string.end_with?(',', ':') suffix = suffix[1..-1] end # Handle punctiation-in-quote if !quotes.nil? && punctuation_in_quotes? if suffix.sub!(/^([\.,])/, '') punctuation = ($1).to_s end end "#{string}#{punctuation}#{quotes}#{stripped}#{suffix}" end |
#strip(string) ⇒ Object
273 274 275 |
# File 'lib/citeproc/ruby/format.rb', line 273 def strip(string) string end |
#suffix ⇒ Object
269 270 271 |
# File 'lib/citeproc/ruby/format.rb', line 269 def suffix [:suffix].to_s end |