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, #remove_pre_post, #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
105
106
107
108
109
110
# File 'lib/mdless/string.rb', line 55

def color_meta(cols)
  @cols = cols
  input = dup
  input.clean_empty_lines!
  MDLess.meta = {}

  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(?<content>[\s\S]*?)\n[-.]{3}[ \t]*\n/m) do
      m = Regexp.last_match
      MDLess.log.info('Processing YAML Header')
      YAML.unsafe_load(m['content']).map { |k, v| MDLess.meta[k.downcase] = v }
      lines = m['content'].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 marker')}%#{color('metadata color')}#{line}#{xc}"
        end

        line += "\u00A0" * (longest - line.uncolor.strip.length) if (longest - line.uncolor.strip.length).positive?
        line + xc
      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')
        parts = line.match(/[ \t]*(.*?): +(.*?)$/)
        key = parts[1].gsub(/[^a-z0-9\-_]/i, '')
        value = parts[2].strip
        MDLess.meta[key] = value
        line = "#{color('metadata color')}#{line}#{xc}"
        line += "\u00A0" * (longest - line.uncolor.strip.length) if (longest - line.uncolor.strip.length).positive?
        line + xc
      end.join("\n") + "#{"\u00A0" * longest}#{xc}\n"
    end
  end

  input
end

#highlight_tagsObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/mdless/string.rb', line 112

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

#remove_metaObject



148
149
150
151
152
153
154
155
156
157
# File 'lib/mdless/string.rb', line 148

def remove_meta
  first_line = split("\n").first
  if first_line =~ /(?i-m)^---[ \t]*?$/
    sub(/(?im)^---[ \t]*\n([\s\S\n]*?)\n[-.]{3}[ \t]*\n/, '')
  elsif first_line =~ /(?i-m)^[\w ]+:\s+\S+/
    sub(/(?im)^([\S ]+:[\s\S]*?)+(?=\n *\n)/, '')
  else
    self
  end
end

#scrubObject



134
135
136
# File 'lib/mdless/string.rb', line 134

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

#scrub!Object



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

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)


159
160
161
162
163
# File 'lib/mdless/string.rb', line 159

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

  MDLess.pygments_lexers.include?(downcase)
end

#valid_pygments_theme?Boolean

Returns:

  • (Boolean)


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

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

  MDLess.pygments_styles.include?(self)
end