Module: NRSER::RSpex::Format

Defined in:
lib/nrser/rspex/format.rb

Overview

String formatting utilities.

Constant Summary collapse

PASTEL =
Pastel.new

Class Method Summary collapse

Class Method Details

.description(*parts, type: nil) ⇒ String

TODO:

Document format method.

Parameters:

  • arg_name (type)

    @todo Add name param description.

Returns:

  • (String)


152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/nrser/rspex/format.rb', line 152

def self.description *parts, type: nil
  parts.
    map { |part|
      if part.respond_to? :to_desc
        part.to_desc
      elsif part.is_a? String
        part
      else
        short_s part
      end
    }.
    join( ' ' ).
    squish.
    thru { |description|
      render_shelldown prepend_type( type, description )
    }
end

.esc_seq_italic(string) ⇒ Object



39
40
41
# File 'lib/nrser/rspex/format.rb', line 39

def self.esc_seq_italic string
  PASTEL.italic string
end

.fix_esc_seq(commonmark) ⇒ Object



51
52
53
# File 'lib/nrser/rspex/format.rb', line 51

def self.fix_esc_seq commonmark
  commonmark.gsub( "\e\\[", "\e[" )
end

.italic(string) ⇒ Object



44
45
46
# File 'lib/nrser/rspex/format.rb', line 44

def self.italic string
  public_send "#{ RSpec.configuration.x_style }_#{ __method__ }", string
end

.pastel_node(name) ⇒ Object



81
82
83
# File 'lib/nrser/rspex/format.rb', line 81

def self.pastel_node name
  text_node PASTEL.lookup( name )
end

.prepend_type(type, description) ⇒ return_type

TODO:

Document format_type method.

Returns @todo Document return value.

Parameters:

  • arg_name (type)

    @todo Add name param description.

Returns:

  • (return_type)

    @todo Document return value.



133
134
135
136
137
138
139
140
141
142
# File 'lib/nrser/rspex/format.rb', line 133

def self.prepend_type type, description
  return description if type.nil?
  
  prefixes = RSpec.configuration.x_type_prefixes
  
  prefix = prefixes[type] ||
            PASTEL.magenta( i( type.to_s.upcase.gsub('_', ' ') ) )
  
  "#{ prefix } #{ description }"
end

.render_shelldown(*render_doc_args) ⇒ return_type

TODO:

Document render_commonmark method.

Returns @todo Document return value.

Parameters:

  • arg_name (type)

    @todo Add name param description.

Returns:

  • (return_type)

    @todo Document return value.



64
65
66
67
68
69
70
71
# File 'lib/nrser/rspex/format.rb', line 64

def self.render_shelldown *render_doc_args
  doc = CommonMarker.render_doc *render_doc_args
  
  transformed = transform_node( doc ).only!
  commonmark = transformed.to_commonmark
  ansi = fix_esc_seq commonmark
  ansi
end

.text_node(string_content) ⇒ Object



74
75
76
77
78
# File 'lib/nrser/rspex/format.rb', line 74

def self.text_node string_content
  CommonMarker::Node.new( :text ).tap { |node|
    node.string_content = string_content
  }
end

.transform_node(node) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/nrser/rspex/format.rb', line 86

def self.transform_node node
  case node.type
  when :emph
    [
      pastel_node( :italic ),
      node.map { |child| transform_node child },
      pastel_node( :clear ),
    ].flatten
  when :strong
    [
      pastel_node( :bold ),
      node.map { |child| transform_node child},
      pastel_node( :clear ),
    ].flatten
  when :text
    [node]
  when :code
    [
      pastel_node( :magenta ),
      text_node( node.string_content ),
      pastel_node( :clear ),
    ]
  else
    new_node = CommonMarker::Node.new node.type
    
    # new_node.string_content = node.string_content
    
    node.
      each { |child|
        transform_node( child ).each { |new_child|
          new_node.append_child new_child
        }
      }
    
    [new_node]
  end
end

.transpose_A_z(string, lower_a:, upper_a:) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/nrser/rspex/format.rb', line 15

def self.transpose_A_z string, lower_a:, upper_a:
  string
    .gsub( /[A-Z]/ ) { |char|
      [upper_a.ord + (char.ord - 'A'.ord)].pack 'U*'
    }
    .gsub( /[a-z]/ ) { |char|
      [lower_a.ord + (char.ord - 'a'.ord)].pack 'U*'
    }
end

.unicode_italic(string) ⇒ return_type

Italicize a string

Parameters:

  • arg_name (type)

    @todo Add name param description.

Returns:

  • (return_type)

    @todo Document return value.



34
35
36
# File 'lib/nrser/rspex/format.rb', line 34

def self.unicode_italic string
  transpose_A_z string, lower_a: '𝑎', upper_a: '𝐴'
end