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::Latex, 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.
23 24 25 |
# File 'lib/citeproc/ruby/format.rb', line 23 def available @available end |
.stopwords ⇒ Object (readonly)
Returns the value of attribute stopwords.
23 24 25 |
# File 'lib/citeproc/ruby/format.rb', line 23 def stopwords @stopwords end |
Instance Attribute Details
#locale ⇒ Object (readonly)
Returns the value of attribute locale.
58 59 60 |
# File 'lib/citeproc/ruby/format.rb', line 58 def locale @locale end |
Class Method Details
.load(name = nil) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/citeproc/ruby/format.rb', line 29 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
53 54 55 |
# File 'lib/citeproc/ruby/format.rb', line 53 def squeezable @squeezable ||= Format.squeezable end |
.squeezable?(string) ⇒ Boolean
49 50 51 |
# File 'lib/citeproc/ruby/format.rb', line 49 def squeezable?(string) squeezable === string end |
.stopword?(word, locale = :en) ⇒ Boolean
44 45 46 47 |
# File 'lib/citeproc/ruby/format.rb', line 44 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
143 144 145 146 147 148 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 |
# File 'lib/citeproc/ruby/format.rb', line 143 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
264 265 |
# File 'lib/citeproc/ruby/format.rb', line 264 def apply_display end |
#apply_prefix ⇒ Object
267 268 269 |
# File 'lib/citeproc/ruby/format.rb', line 267 def apply_prefix output.replace(squeeze_prefix(output, prefix)) end |
#apply_quotes ⇒ Object
204 205 206 |
# File 'lib/citeproc/ruby/format.rb', line 204 def apply_quotes output.replace locale.quote(output, escape_quotes?) end |
#apply_suffix ⇒ Object
271 272 273 |
# File 'lib/citeproc/ruby/format.rb', line 271 def apply_suffix output.replace(squeeze_suffix(output, suffix)) end |
#apply_text_case ⇒ Object
208 209 210 211 212 213 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 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/citeproc/ruby/format.rb', line 208 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: word followed by colon first = true output.gsub!(/\b(\p{L})([\p{L}\.]+)\b/) do |word| first_letter = $1 rest_of_word = $2 result = word if first_letter.match(/^\p{Ll}/) && (!Format.stopword?(word) || first) result = "#{CiteProc.upcase(first_letter)}#{rest_of_word}" end first = false result end output.gsub!(/(\.|\b)(\p{Ll})([\p{L}\.]+)\b$/) do |word| word_boundary = $1 first_letter = $2 rest_of_word = $3 if word_boundary == '.' word else "#{CiteProc.upcase(first_letter)}#{rest_of_word}" end end end end |
#bibliography(bibliography, locale = nil) ⇒ Object
138 139 140 141 |
# File 'lib/citeproc/ruby/format.rb', line 138 def bibliography(bibliography, locale = nil) bibliography.connector = "\n" * bibliography.entry_spacing bibliography end |
#close_inner_quote ⇒ Object
196 197 198 |
# File 'lib/citeproc/ruby/format.rb', line 196 def close_inner_quote locale && locale.t('close-inner-quote') || "'" end |
#close_quote ⇒ Object
192 193 194 |
# File 'lib/citeproc/ruby/format.rb', line 192 def close_quote locale && locale.t('close-quote') || '"' end |
#escape_quotes? ⇒ Boolean
188 189 190 |
# File 'lib/citeproc/ruby/format.rb', line 188 def escape_quotes? false end |
#join(list, delimiter = nil) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/citeproc/ruby/format.rb', line 122 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
60 61 62 |
# File 'lib/citeproc/ruby/format.rb', line 60 def keys @keys ||= (CSL::Schema.attr(:formatting) - [:prefix, :suffix, :display]) end |
#prefix ⇒ Object
275 276 277 |
# File 'lib/citeproc/ruby/format.rb', line 275 def prefix [:prefix].to_s end |
#punctuation_in_quotes? ⇒ Boolean
260 261 262 |
# File 'lib/citeproc/ruby/format.rb', line 260 def punctuation_in_quotes? !locale.nil? && locale.punctuation_in_quotes? end |
#split_closing_quotes(string) ⇒ Object
200 201 202 |
# File 'lib/citeproc/ruby/format.rb', line 200 def split_closing_quotes(string) string.split(/((#{Regexp.quote(close_inner_quote)}|#{Regexp.quote(close_quote)})+)$/, 2) end |
#squeezable?(string) ⇒ Boolean
64 65 66 |
# File 'lib/citeproc/ruby/format.rb', line 64 def squeezable?(string) self.class.squeezable?(string) end |
#squeeze_prefix(string, prefix) ⇒ Object
109 110 111 112 113 114 115 116 117 118 |
# File 'lib/citeproc/ruby/format.rb', line 109 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
68 69 70 71 72 73 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 |
# File 'lib/citeproc/ruby/format.rb', line 68 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
283 284 285 |
# File 'lib/citeproc/ruby/format.rb', line 283 def strip(string) string end |
#suffix ⇒ Object
279 280 281 |
# File 'lib/citeproc/ruby/format.rb', line 279 def suffix [:suffix].to_s end |