Class: Trek::TypographyFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/trek/typography_formatter.rb

Overview

‘TypographyFormatter` formats strings to enforce French typography rules.

Supports both plain strings and ProseMirror JSONB hashes. When given a Hash, it walks the node tree and formats only ‘text` node values in place, leaving the structure intact.

Constant Summary collapse

NBSP =
[160].pack("U*").freeze
NNBSP =

U+202F narrow no-break space

[8239].pack("U*").freeze
SPACE_BEFORE_CHARS =
[
  [NBSP, %w[: %  \$ »  ]],
  [NNBSP, %w[; ! \?]]
].freeze
NBSP_AFTER_CHARS =
%w[«  ].freeze
NBSP_BEFORE_UNITS =
%w[°C °F g h ha kg km Ko ko kW L l m min ml Mo mo s].freeze

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ TypographyFormatter

Returns a new instance of TypographyFormatter.



17
18
19
20
21
# File 'lib/trek/typography_formatter.rb', line 17

def initialize(input)
  @source = input
  @prosemirror = input.is_a?(Hash)
  @output = @prosemirror ? deep_dup_hash(input) : input.dup
end

Instance Method Details

#formattedObject



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/trek/typography_formatter.rb', line 23

def formatted
  return @output if @output.nil?

  if @prosemirror
    format_prosemirror_node(@output)
  else
    format_string!
  end

  @output
end