module NRSER::RSpex::Format

String formatting utilities.

Public Class Methods

bold(string) click to toggle source
# File lib/nrser/rspex/format.rb, line 71
def self.bold string
  public_send "#{ RSpec.configuration.x_style }_#{ __method__ }", string
end
code(string) click to toggle source
# File lib/nrser/rspex/format.rb, line 102
def self.code string
  if method_name? string
    pastel.bold.blue string
  else
    rspec_syntax_highlighter.highlight string.lines
  end
end
description(*parts, type: nil) click to toggle source
# File lib/nrser/rspex/format.rb, line 155
def self.description *parts, type: nil
  parts.
    flat_map { |part|
      if part.respond_to? :to_desc
        desc = part.to_desc
        if desc.empty?
          ''
        else
          md_code_quote desc
        end
      else
        case part
        when Module
          mod = part
          
          name_desc = if mod.anonymous?
            "(anonymous #{ part.class })"
          else
            md_code_quote mod.name
          end
          
          [name_desc, description( mod.source_location )]
          
        when NRSER::Meta::Source::Location
          if part.valid?
            "(#{ NRSER::RSpex.dot_rel_path( part.file ) }:#{ part.line })"
          else
            ''
          end
          
        when String
          part
        
        when Pathname
          pathname part
        
        when NRSER::Message
          [part.symbol, part.args].
            map( &NRSER::RSpex.method( :short_s ) ).join( ', ' )
          
        else
          NRSER::RSpex.short_s part
          
        end
      end
    }.
    join( ' ' ).
    squish.
    thru { |description|
      prepend_type type, mean_streak.render( description )
    }
end
esc_seq_bold(string) click to toggle source
# File lib/nrser/rspex/format.rb, line 56
def self.esc_seq_bold string
  pastel.bold string
end
esc_seq_italic(string) click to toggle source
# File lib/nrser/rspex/format.rb, line 44
def self.esc_seq_italic string
  pastel.italic string
end
italic(string) click to toggle source
# File lib/nrser/rspex/format.rb, line 49
def self.italic string
  public_send "#{ RSpec.configuration.x_style }_#{ __method__ }", string
end
md_code_quote(string) click to toggle source
# File lib/nrser/rspex/format.rb, line 111
def self.md_code_quote string
  quote = '`' * ((string.scan( /`+/ ).map( &:length ).max || 0) + 1)
  "#{ quote }#{ string }#{ quote }"
end
mean_streak() click to toggle source
# File lib/nrser/rspex/format.rb, line 17
def self.mean_streak
  @mean_streak ||= NRSER::MeanStreak.new do |ms|
    ms.render_type :emph do |doc, node|
      italic doc.render_children( node )
    end
    
    ms.render_type :strong do |doc, node|
      bold doc.render_children( node )
    end
    
    ms.render_type :code do |doc, node|
      code node.string_content
    end
  end
end
method_name?(string) click to toggle source
# File lib/nrser/rspex/format.rb, line 84
def self.method_name? string
  # Must start with `#` or `.`
  return false unless ['#', '.'].any? { |c| string[0] == c }
  
  name = string[1..-1]
  
  case name
  when  '!', '~', '+', '**', '-', '*', '/', '%', '+', '-', '<<', '>>', '&',
        '|', '^', '<', '<=', '>=', '>', '==', '===', '!=', '=~', '!~', '<=>',
        '[]',
        /\A[a-zA-Z_][a-zA-Z0-9_]*(?:\?|\!|=)?/
    true
  else
    false
  end
end
pastel() click to toggle source
# File lib/nrser/rspex/format.rb, line 12
def self.pastel
  @pastel ||= Pastel.new
end
pathname(pn) click to toggle source
# File lib/nrser/rspex/format.rb, line 130
def self.pathname pn
  if pn.absolute?
    rel = pn.relative_path_from Pathname.getwd
    
    if rel.split( File::SEPARATOR ).first == '..'
      File.join '.', rel
    else
      pn.to_s
    end
  else
    if pn.exist?
      File.join '.', pn
    else
      lib_pn = Pathname.getwd / 'lib' / pn
      
      if lib_pn.exist?
        File.join '.', lib_pn.relative_path_from( Pathname.getwd )
      else
        pn.to_s
      end
    end
  end
end
prepend_type(type, description) click to toggle source
# File lib/nrser/rspex/format.rb, line 117
def self.prepend_type type, description
  return description if type.nil?
  
  prefixes = RSpec.configuration.x_type_prefixes
  
  prefix = pastel.magenta(
    prefixes[type] || i( type.to_s.upcase.gsub('_', ' ') )
  )
  
  "#{ prefix } #{ description }"
end
rspec_syntax_highlighter() click to toggle source
# File lib/nrser/rspex/format.rb, line 78
def self.rspec_syntax_highlighter
  @rspec_syntax_highlighter ||= \
    RSpec::Core::Formatters::SyntaxHighlighter.new RSpec.configuration
end
unicode_bold(string) click to toggle source

Bold a string via “Unicode Math Bold” substitution.

@param [String] string @return [String]

# File lib/nrser/rspex/format.rb, line 66
def self.unicode_bold string
  NRSER.u_bold string
end
unicode_italic(string) click to toggle source

Italicize a string via “Unicode Math Italic” substitution.

@param [String] string @return [String]

# File lib/nrser/rspex/format.rb, line 39
def self.unicode_italic string
  NRSER.u_italic string
end