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!, #unpad, #wrap

Instance Method Details

#clean_empty_linesObject



7
8
9
# File 'lib/mdless/string.rb', line 7

def clean_empty_lines
  gsub(/^[ \t]+$/, '')
end

#clean_empty_lines!Object



11
12
13
# File 'lib/mdless/string.rb', line 11

def clean_empty_lines!
  replace clean_empty_lines
end

#clean_header_idsObject



51
52
53
# File 'lib/mdless/string.rb', line 51

def clean_header_ids
  gsub(/ +\[.*?\] *$/, '').gsub(/ *\{#.*?\} *$/, '').strip
end

#clean_header_ids!Object



47
48
49
# File 'lib/mdless/string.rb', line 47

def clean_header_ids!
  replace clean_header_ids
end

#color(key) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/mdless/string.rb', line 15

def color(key)
  val = nil
  keys = key.split(/[ ,>]/)
  if MDLess.theme.key?(keys[0])
    val = MDLess.theme[keys.shift]
  else
    MDLess.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
      MDLess.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

#color_meta(cols) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/mdless/string.rb', line 55

def color_meta(cols)
  @cols = cols
  input = dup
  input.clean_empty_lines!

  in_yaml = false
  first_line = input.split("\n").first
  if first_line =~ /(?i-m)^---[ \t]*?$/
    MDLess.log.info('Found YAML')
    # YAML
    in_yaml = true
    input.sub!(/(?i-m)^---[ \t]*\n([\s\S]*?)\n[-.]{3}[ \t]*\n/m) do
      m = Regexp.last_match
      MDLess.log.info('Processing YAML Header')
      lines = m[0].split(/\n/)
      longest = lines.inject { |memo, word| memo.length > word.length ? memo : word }.length
      longest = longest < @cols ? longest + 1 : @cols
      lines.map do |line|
        if line =~ /^[-.]{3}\s*$/
          line = "#{color('metadata marker')}#{'%' * longest}"
        else
          line.sub!(/^(.*?:)[ \t]+(\S)/, '\1 \2')
          line = "#{color('metadata color')}#{line}"
        end

        line += "\u00A0" * (longest - line.uncolor.strip.length) + xc
        line
      end.join("\n") + "#{xc}\n"
    end
  end

  if !in_yaml && first_line =~ /(?i-m)^[\w ]+:\s+\S+/
    MDLess.log.info('Found MMD Headers')
    input.sub!(/(?i-m)^([\S ]+:[\s\S]*?)+(?=\n\n)/) do |mmd|
      lines = mmd.split(/\n/)
      return mmd if lines.count > 20

      longest = lines.inject { |memo, word| memo.length > word.length ? memo : word }.length
      longest = longest < @cols ? longest + 1 : @cols
      lines.map do |line|
        line.sub!(/^(.*?:)[ \t]+(\S)/, '\1 \2')
        line = "#{color('metadata color')}#{line}"
        line += "\u00A0" * (longest - line.uncolor.strip.length)
        line + xc
      end.join("\n") + "#{"\u00A0" * longest}#{xc}\n"
    end
  end

  input
end

#highlight_tagsObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/mdless/string.rb', line 106

def highlight_tags
  log = MDLess.log
  tag_color = color('at_tags tag')
  value_color = color('at_tags value')
  gsub(/(?<pre>\s|m)(?<tag>@[^ \].?!,("'\n]+)(?:(?<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



128
129
130
# File 'lib/mdless/string.rb', line 128

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

#scrub!Object



132
133
134
# File 'lib/mdless/string.rb', line 132

def scrub!
  replace scrub
end

#to_rx(distance: 2, string_start: false) ⇒ Object



41
42
43
44
45
# File 'lib/mdless/string.rb', line 41

def to_rx(distance: 2, string_start: false)
  chars = downcase.split(//)
  pre = string_start ? '^' : '^.*?'
  /#{pre}#{chars.join(".{,#{distance}}")}.*?$/
end

#valid_lexer?Boolean

Returns:

  • (Boolean)


142
143
144
145
146
# File 'lib/mdless/string.rb', line 142

def valid_lexer?
  return false unless TTY::Which.exist?('pygmentize')

  MDLess.pygments_lexers.include?(self.downcase)
end

#valid_pygments_theme?Boolean

Returns:

  • (Boolean)


136
137
138
139
140
# File 'lib/mdless/string.rb', line 136

def valid_pygments_theme?
  return false unless TTY::Which.exist?('pygmentize')

  MDLess.pygments_styles.include?(self)
end