Class: String

Inherits:
Object
  • Object
show all
Includes:
CLIMarkdown::Colors
Defined in:
lib/mdless/string.rb,
lib/mdless/colors.rb

Overview

String helpers

Constant Summary

Constants included from CLIMarkdown::Colors

CLIMarkdown::Colors::COLORS, CLIMarkdown::Colors::ESCAPE_REGEX

Instance Method Summary collapse

Methods included from CLIMarkdown::Colors

#blackout, #c, #last_color_code, #size_clean, #uncolor, #uncolor!, #wrap

Instance Method Details

#color(key, theme, log) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mdless/string.rb', line 7

def color(key, theme, log)
  val = nil
  keys = key.split(/[ ,>]/)
  if theme.key?(keys[0])
    val = theme[keys.shift]
  else
    log.error("Invalid theme key: #{key}") unless keys[0] =~ /^text/
    return c([:reset])
  end
  keys.each do |k|
    if val.key?(k)
      val = val[k]
    else
      log.error("Invalid theme key: #{k}")
      return c([:reset])
    end
  end
  if val.is_a? String
    val = "x #{val}"
    res = val.split(/ /).map(&:to_sym)
    c(res)
  else
    c([:reset])
  end
end

#highlight_tags(theme, log) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/mdless/string.rb', line 33

def highlight_tags(theme, log)
  tag_color = color('at_tags tag', theme, log)
  value_color = color('at_tags value', theme, log)
  gsub(/(?<pre>\s|m)(?<tag>@[^ \].?!,("']+)(?:(?<lparen>\()(?<value>.*?)(?<rparen>\)))?/) do
    m = Regexp.last_match
    last_color = m.pre_match.last_color_code
    [
      m['pre'],
      tag_color,
      m['tag'],
      m['lparen'],
      value_color,
      m['value'],
      tag_color,
      m['rparen'],
      xc,
      last_color
    ].join
  end
end

#scrubObject



54
55
56
# File 'lib/mdless/string.rb', line 54

def scrub
  encode('utf-16', invalid: :replace).encode('utf-8')
end

#scrub!Object



58
59
60
# File 'lib/mdless/string.rb', line 58

def scrub!
  replace scrub
end