Class: RelatonBib::FormattedString
- Inherits:
-
LocalizedString
- Object
- LocalizedString
- RelatonBib::FormattedString
- Defined in:
- lib/relaton_bib/formatted_string.rb
Overview
Formatted string
Direct Known Subclasses
Constant Summary collapse
- FORMATS =
%w[text/plain text/html application/docbook+xml application/tei+xml text/x-asciidoc text/markdown application/x-metanorma+xml].freeze
Constants included from RelatonBib
Instance Attribute Summary collapse
- #format ⇒ String readonly
Attributes inherited from LocalizedString
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#cleanup(str) ⇒ String
Remove HTML tags except <em>, <strong>, <stem>, <sup>, <sub>, <tt>,
, <p>. -
#encode(cnt) ⇒ String
Encode content.
-
#initialize(content: "", language: nil, script: nil, format: "text/plain") ⇒ FormattedString
constructor
A new instance of FormattedString.
-
#scan_xml(parts) ⇒ String
Scan XML and escape HTML entities.
- #to_asciibib(prefix = "", count = 1, has_attrs = false) ⇒ String
- #to_hash ⇒ Hash
- #to_xml(builder) ⇒ Object
Methods inherited from LocalizedString
Methods included from RelatonBib
array, format_date, grammar_hash, parse_date, parse_yaml
Methods included from Config
Constructor Details
#initialize(content: "", language: nil, script: nil, format: "text/plain") ⇒ FormattedString
Returns a new instance of FormattedString.
19 20 21 22 23 24 25 26 |
# File 'lib/relaton_bib/formatted_string.rb', line 19 def initialize(content: "", language: nil, script: nil, format: "text/plain") # if format && !FORMATS.include?(format) # raise ArgumentError, %{Format "#{format}" is invalid.} # end @format = format super(content, language, script) end |
Instance Attribute Details
#format ⇒ String (readonly)
13 14 15 |
# File 'lib/relaton_bib/formatted_string.rb', line 13 def format @format end |
Instance Method Details
#==(other) ⇒ Object
28 29 30 |
# File 'lib/relaton_bib/formatted_string.rb', line 28 def ==(other) super && format == other.format end |
#cleanup(str) ⇒ String
Remove HTML tags except <em>, <strong>, <stem>, <sup>, <sub>, <tt>,
, <p>. Replace <i> with <em>, <b> with <strong>.
123 124 125 126 127 128 129 130 131 |
# File 'lib/relaton_bib/formatted_string.rb', line 123 def cleanup(str) return str unless format == "text/html" str.gsub(/(?<=<)\w+:(?=\w+>)/, "").gsub(/(?<=<\/)\w+:(?=\w+>)/, "") .gsub(/<i>/, "<em>").gsub(/<\/i>/, "</em>") .gsub(/<b>/, "<strong>").gsub(/<\/b>/, "</strong>") .gsub(/<(?!\/?(em|strong|stem|sup|sub|tt|br\s?\/|p))[^\s!]\/?.*?>/, "") .gsub(/\s+([.,:;!?<])/, "\\1").strip.squeeze(" ") end |
#encode(cnt) ⇒ String
Encode content.
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/relaton_bib/formatted_string.rb', line 45 def encode(cnt) # rubocop:disable Metrics/MethodLength return escp(cnt) unless format == "text/html" parts = cnt.scan(%r{ <(?<tago>\w+)(?<attrs>[^>]*)> | # tag open </(?<tagc>\w+)> | # tag close (?<cmt><!--.*?-->) | # comment (?<cnt>.+?)(?=<|$) # content }x) scan_xml parts end |
#scan_xml(parts) ⇒ String
Scan XML and escape HTML entities.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/relaton_bib/formatted_string.rb', line 64 def scan_xml(parts) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity,Metrics/MethodLength return "" unless parts.any? out = "" while parts.any? && (parts.first[3] || parts.first[4]) _, _, _, cmt, cnt = parts.shift out += "#{cmt}#{escp(cnt)}" end unless out.empty? out += scan_xml(parts) if parts.any? && parts.first[0] return out end tago, attrs, tagc, = parts.shift out = if tago && attrs && attrs[-1] == "/" "<#{tago}#{attrs}>" elsif tago inr = scan_xml parts _, _, tagc, = parts.shift if tago == tagc "<#{tago}#{attrs}>#{inr}</#{tagc}>" else "#{escp("<#{tago}#{attrs}>")}#{inr}#{escp("</#{tagc}>")}" end end out += scan_xml(parts) if parts.any? && (parts.first[0] || parts.first[3] || parts.first[4]) out end |
#to_asciibib(prefix = "", count = 1, has_attrs = false) ⇒ String
106 107 108 109 110 111 112 113 |
# File 'lib/relaton_bib/formatted_string.rb', line 106 def to_asciibib(prefix = "", count = 1, has_attrs = false) has_attrs ||= !(format.nil? || format.empty?) pref = prefix.empty? ? prefix : "#{prefix}." # out = count > 1 ? "#{prefix}::\n" : "" out = super out += "#{pref}format:: #{format}\n" if format out end |
#to_hash ⇒ Hash
94 95 96 97 98 99 100 101 |
# File 'lib/relaton_bib/formatted_string.rb', line 94 def to_hash hash = super return hash unless format hash = { "content" => hash } unless hash.is_a? Hash hash["format"] = format hash end |
#to_xml(builder) ⇒ Object
33 34 35 36 |
# File 'lib/relaton_bib/formatted_string.rb', line 33 def to_xml(builder) builder.parent["format"] = format if format super end |