Class: RelatonBib::FormattedString

Inherits:
LocalizedString show all
Defined in:
lib/relaton_bib/formatted_string.rb

Overview

Formatted string

Direct Known Subclasses

BiblioNote, FormattedRef

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

VERSION

Instance Attribute Summary collapse

Attributes inherited from LocalizedString

#content, #language, #script

Instance Method Summary collapse

Methods inherited from LocalizedString

#empty?, #escp, #to_s

Methods included from RelatonBib

array, format_date, grammar_hash, parse_date, parse_yaml

Methods included from Config

#configuration, #configure

Constructor Details

#initialize(content: "", language: nil, script: nil, format: "text/plain") ⇒ 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

#formatString (readonly)



13
14
15
# File 'lib/relaton_bib/formatted_string.rb', line 13

def format
  @format
end

Instance Method Details

#cleanup(str) ⇒ String

Remove HTML tags except <em>, <strong>, <stem>, <sup>, <sub>, <tt>,
, <p>. Replace <i> with <em>, <b> with <strong>.



119
120
121
122
123
124
125
126
127
# File 'lib/relaton_bib/formatted_string.rb', line 119

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.



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/relaton_bib/formatted_string.rb', line 41

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.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/relaton_bib/formatted_string.rb', line 60

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



102
103
104
105
106
107
108
109
# File 'lib/relaton_bib/formatted_string.rb', line 102

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_hashHash



90
91
92
93
94
95
96
97
# File 'lib/relaton_bib/formatted_string.rb', line 90

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



29
30
31
32
# File 'lib/relaton_bib/formatted_string.rb', line 29

def to_xml(builder)
  builder.parent["format"] = format if format
  super
end