Class: Metanorma::Iso::Sts::Transformer::NbspProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/metanorma/iso/sts/transformer/nbsp_processor.rb

Constant Summary collapse

NBSP =
" "
RULES =
[
  [/(Part) (\d)/i, "\\1#{NBSP}\\2"],
  [/(\d) (%)/, "\\1#{NBSP}\\2"],
  [/(ISO[\/&]?(?:TC|IEC)?) (\d)/i, "\\1#{NBSP}\\2"],
  [/(NOTE) (\d)/i, "\\1#{NBSP}\\2"],
  [/(Note)\s(\d)\s(to entry)/i, "\\1#{NBSP}\\2#{NBSP}\\3"],
  [/(Table|Figure|Clause|Volume) (([A-Za-z]\.)?\d)/i,
   "\\1#{NBSP}\\2"],
  [/(Formula) (\()/i, "\\1#{NBSP}\\2"],
  [/(Annex) ([A-Za-z])/i, "\\1#{NBSP}\\2"],
  [/ (— [A-Z])/, "#{NBSP}\\1"],
].freeze

Class Method Summary collapse

Class Method Details

.apply_to_text(xml) ⇒ String

Apply the NBSP rules to every text node of a serialised XML string (walks each >text< run). Used as the metanorma-core document_transformers :post_process hook, replacing the previously-private DocumentTransformer#apply_nbsp_to_text.

Parameters:

  • xml (String)

    serialised STS XML.

Returns:

  • (String)

    XML with NBSP rules applied to text content.



37
38
39
# File 'lib/metanorma/iso/sts/transformer/nbsp_processor.rb', line 37

def self.apply_to_text(xml)
  xml.gsub(/>([^<]+)</) { ">#{process(Regexp.last_match(1))}<" }
end

.process(text) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/metanorma/iso/sts/transformer/nbsp_processor.rb', line 22

def self.process(text)
  return text unless text.is_a?(String)

  RULES.reduce(text) do |t, (pattern, replacement)|
    t.gsub(pattern, replacement)
  end
end