Class: RelatonBib::LocalizedString
- Includes:
- RelatonBib
- Defined in:
- lib/relaton_bib/localized_string.rb
Overview
Localized string.
Direct Known Subclasses
Constant Summary
Constants included from RelatonBib
Instance Attribute Summary collapse
- #content ⇒ String, Array<RelatonBib::LocalizedString>
-
#language ⇒ Array<String>
readonly
Language Iso639 code.
-
#script ⇒ Array<String>
readonly
Script Iso15924 code.
Instance Method Summary collapse
- #empty? ⇒ TrueClass, FalseClass
-
#encode(cnt) ⇒ String
Encode content.
-
#escp(str) ⇒ String
Escope HTML entities.
-
#initialize(content, language = nil, script = nil) ⇒ LocalizedString
constructor
A new instance of LocalizedString.
-
#scan_xml(parts) ⇒ String
Scan XML and escape HTML entities.
- #to_asciibib(prefix = "", count = 1, has_attrs = false) ⇒ String
- #to_hash ⇒ Hash
- #to_s ⇒ String
- #to_xml(builder) ⇒ Object
Methods included from RelatonBib
Constructor Details
#initialize(content, language = nil, script = nil) ⇒ LocalizedString
Returns a new instance of LocalizedString.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/relaton_bib/localized_string.rb', line 20 def initialize(content, language = nil, script = nil) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity if content.is_a?(Array) && content.none? raise ArgumentError, "LocalizedString content is empty" end @language = language.is_a?(String) ? [language] : language @script = script.is_a?(String) ? [script] : script @content = if content.is_a?(Array) content.map do |c| case c when Hash LocalizedString.new c[:content], c[:language], c[:script] when String then LocalizedString.new c else c end end else content end end |
Instance Attribute Details
#content ⇒ String, Array<RelatonBib::LocalizedString>
15 16 17 |
# File 'lib/relaton_bib/localized_string.rb', line 15 def content @content end |
#language ⇒ Array<String> (readonly)
Returns language Iso639 code.
9 10 11 |
# File 'lib/relaton_bib/localized_string.rb', line 9 def language @language end |
#script ⇒ Array<String> (readonly)
Returns script Iso15924 code.
12 13 14 |
# File 'lib/relaton_bib/localized_string.rb', line 12 def script @script end |
Instance Method Details
#empty? ⇒ TrueClass, FalseClass
46 47 48 |
# File 'lib/relaton_bib/localized_string.rb', line 46 def empty? content.empty? end |
#encode(cnt) ⇒ String
Encode content.
70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/relaton_bib/localized_string.rb', line 70 def encode(cnt) # rubocop:disable Metrics/MethodLength regex = /(?<prf>.*?)(?<xml><(?<tag>\w+)>.*<\/\k<tag>>)(?<sfx>.*)/m if cnt.match(regex) prf = Regexp.last_match(:prf).lstrip xml = Regexp.last_match[:xml] sfx = Regexp.last_match(:sfx).rstrip parts = xml.scan(/\s*<(?<tago>\w+)>(?<cnt1>.*?)(?=<\/?\w+>)|(?<cnt2>.*?)<\/(?<tagc>\w+)>/) out = scan_xml parts "#{escp(prf)}#{out}#{escp(sfx)}" else escp cnt end end |
#escp(str) ⇒ String
Escope HTML entities.
116 117 118 |
# File 'lib/relaton_bib/localized_string.rb', line 116 def escp(str) HTMLEntities.new.encode str end |
#scan_xml(parts) ⇒ String
Scan XML and escape HTML entities.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/relaton_bib/localized_string.rb', line 91 def scan_xml(parts) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity,Metrics/MethodLength return "" unless parts.any? && parts.first[0] tago, cnt1, = parts.shift if tago && tago == parts.first[3] _, _, cnt2, tagc = parts.shift "<#{tago}>#{escp(cnt1)}#{escp(cnt2)}</#{tagc}>" elsif tago inr = scan_xml parts _, _, cnt2, tagc = parts.shift if tago == tagc "<#{tago}>#{escp(cnt1)}#{inr}#{escp(cnt2)}</#{tagc}>" else "#{escp("<#{tago}>#{cnt1}")}#{inr}#{escp("#{cnt2}</#{tagc}>")}" end end + scan_xml(parts) end |
#to_asciibib(prefix = "", count = 1, has_attrs = false) ⇒ String
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/relaton_bib/localized_string.rb', line 136 def to_asciibib(prefix = "", count = 1, has_attrs = false) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity,Metrics/MethodLength pref = prefix.empty? ? prefix : "#{prefix}." if content.is_a? String unless language&.any? || script&.any? || has_attrs return "#{prefix}:: #{content}\n" end out = count > 1 ? "#{prefix}::\n" : "" out += "#{pref}content:: #{content}\n" language&.each { |l| out += "#{pref}language:: #{l}\n" } script&.each { |s| out += "#{pref}script:: #{s}\n" } out else content.map { |c| c.to_asciibib "#{pref}variant", content.size }.join end end |
#to_hash ⇒ Hash
121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/relaton_bib/localized_string.rb', line 121 def to_hash # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity if content.is_a? String return content unless language || script hash = { "content" => content } hash["language"] = single_element_array(language) if language&.any? hash["script"] = single_element_array(script) if script&.any? hash else content&.map &:to_hash end end |
#to_s ⇒ String
41 42 43 |
# File 'lib/relaton_bib/localized_string.rb', line 41 def to_s content.is_a?(Array) ? content.first.to_s : content.to_s end |
#to_xml(builder) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/relaton_bib/localized_string.rb', line 51 def to_xml(builder) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity return unless content if content.is_a?(Array) content.each { |c| builder.variant { c.to_xml builder } } else builder.parent["language"] = language.join(",") if language&.any? builder.parent["script"] = script.join(",") if script&.any? builder.parent.inner_html = encode content end end |