Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/snibbets/colors.rb,
lib/snibbets/colors.rb,
lib/snibbets/string.rb

Overview

String helpers

Instance Method Summary collapse

Instance Method Details

#blocksObject



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

def blocks
  replace_blocks[1].count
end

#blocks?Boolean

Returns:

  • (Boolean)


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

def blocks?
  blocks.positive?
end

#clean_codeObject

remove outside comments, fences, and indentation



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/snibbets/string.rb', line 85

def clean_code
  block = dup

  # if it's a fenced code block, just discard the fence and everything
  # outside it
  if block.fenced?
    code_blocks = block.scan(/(`{3,})(\S+)?\s*\n(.*?)\n\1/m)
    code_blocks.map! { |b| b[2].strip }
    return code_blocks.join("\n\n")
  end

  # assume it's indented code, discard non-indented lines and outdent
  # the rest
  block = block.outdent if block.indented?

  block
end

#escape_filenameObject



6
7
8
# File 'lib/snibbets/string.rb', line 6

def escape_filename
  gsub(%r{/}, ':')
end

#fenced?Boolean

Is the snippet in this block fenced?

Returns:

  • (Boolean)


49
50
51
52
# File 'lib/snibbets/string.rb', line 49

def fenced?
  count = scan(/^```/).length
  count > 1 && count.even?
end

#fencesObject

Return array of fenced code blocks



67
68
69
70
71
72
73
74
# File 'lib/snibbets/string.rb', line 67

def fences
  return [] unless fenced?

  rx = /(?mi)^(?<fence>`{3,})(?<lang> *\S+)? *\n(?<code>[\s\S]*?)\n\k<fence> *(?=\n|\Z)/
  matches = []
  scan(rx) { matches << Regexp.last_match }
  matches.each_with_object([]) { |m, fenced| fenced.push({ code: m['code'], lang: m['lang'] }) }
end

#indented?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/snibbets/string.rb', line 76

def indented?
  self =~ /^( {4,}|\t+)/
end

#last_color_codeObject

Get the calculated ANSI color at the end of the string

Returns:

  • ANSI escape sequence to match color



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/snibbets/colors.rb', line 145

def last_color_code
  m = scan(ESCAPE_REGEX)

  em = ['0']
  fg = nil
  bg = nil
  rgbf = nil
  rgbb = nil

  m.each do |c|
    case c
    when '0'
      em = ['0']
      fg, bg, rgbf, rgbb = nil
    when /^[34]8/
      case c
      when /^3/
        fg = nil
        rgbf = c
      when /^4/
        bg = nil
        rgbb = c
      end
    else
      c.split(/;/).each do |i|
        x = i.to_i
        if x <= 9
          em << x
        elsif x >= 30 && x <= 39
          rgbf = nil
          fg = x
        elsif x >= 40 && x <= 49
          rgbb = nil
          bg = x
        elsif x >= 90 && x <= 97
          rgbf = nil
          fg = x
        elsif x >= 100 && x <= 107
          rgbb = nil
          bg = x
        end
      end
    end
  end

  escape = "\e[#{em.join(';')}m"
  escape += "\e[#{rgbb}m" if rgbb
  escape += "\e[#{rgbf}m" if rgbf
  escape + "\e[#{[fg, bg].delete_if(&:nil?).join(';')}m"
end

#multiple?Boolean

Are there multiple snippets (indicated by ATX headers)

Returns:

  • (Boolean)


44
45
46
# File 'lib/snibbets/string.rb', line 44

def multiple?
  gsub(/(`{3,}).*?\n\1/m, '').scan(/^#+/).length > 1
end

#normalize_colorString

Normalize a color name, removing underscores, replacing “bright” with “bold”, and converting bgbold to boldbg

Returns:

  • (String)

    Normalized color name



136
137
138
# File 'lib/snibbets/colors.rb', line 136

def normalize_color
  gsub(/_/, '').sub(/bright/i, 'bold').sub(/bgbold/, 'boldbg')
end

#notes?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/snibbets/string.rb', line 62

def notes?
  replace_blocks[0].split("\n").notes.positive?
end

#outdentObject



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/snibbets/string.rb', line 103

def outdent
  lines = split(/\n/)

  incode = false
  code = []
  lines.each do |line|
    next if line =~ /^\s*$/ && !incode

    incode = true
    code.push(line)
  end

  return self unless code[0]

  indent = code[0].match(/^( {4,}|\t+)(?=\S)/)

  return self if indent.nil?

  code.map! { |line| line.sub(/(?mi)^#{indent[1]}/, '') }.join("\n")
end

#parse_lang_marker(block) ⇒ Object



156
157
158
159
160
161
162
163
164
# File 'lib/snibbets/string.rb', line 156

def parse_lang_marker(block)
  lang = nil
  if block =~ /<lang:(.*?)>/
    lang = Regexp.last_match(1)
    block = block.gsub(/<lang:.*?>\n+/, '').strip_empty
  end

  [lang, block]
end

#remove_metaObject



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/snibbets/string.rb', line 27

def remove_meta
  input = dup
  lines = input.split(/\n/)
  loop do
    line = lines[0]
    lines.shift if line =~ /^\s*[A-Z\s]+\w:\s*\S+/i || line =~ /^-{3,}\s*$/

    break
  end
  lines.join("\n")
end

#remove_spotlight_tagsObject



10
11
12
13
14
15
16
17
# File 'lib/snibbets/string.rb', line 10

def remove_spotlight_tags
  words = Shellwords.shellsplit(self)
  words.delete_if do |word|
    word =~ /^\w+:/
  end

  words.join(' ').strip
end

#remove_spotlight_tags!Object



19
20
21
# File 'lib/snibbets/string.rb', line 19

def remove_spotlight_tags!
  replace remove_spotlight_tags
end

#replace_blocksObject



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/snibbets/string.rb', line 124

def replace_blocks
  sans_blocks = dup
  counter = 0
  code_blocks = {}

  if Snibbets.options[:include_blockquotes]
    sans_blocks = sans_blocks.gsub(/(?mi)(^(>.*?)(\n|$))+/) do
      counter += 1
      code_blocks["block#{counter}"] = Regexp.last_match(0).gsub(/^> *(?=\S)/, '# ')
      "<block#{counter}>\n"
    end
  end

  sans_blocks = sans_blocks.gsub(/(?mi)^(`{3,})( *\S+)? *\n([\s\S]*?)\n\1 *(\n|\Z)/) do
    counter += 1
    lang = Regexp.last_match(2)
    lang = "<lang:#{lang.strip}>\n" if lang
    code_blocks["block#{counter}"] = "#{lang}#{Regexp.last_match(3)}"
    "<block#{counter}>\n"
  end

  sans_blocks = sans_blocks.gsub(/(?mi)(?<=\n\n|\A)\n?((?: {4,}|\t+)\S[\S\s]*?)(?=\n\S|\Z)/) do
    counter += 1
    code = Regexp.last_match(1).split(/\n/)

    code_blocks["block#{counter}"] = code.join("\n").outdent
    "<block#{counter}>\n"
  end

  [sans_blocks, code_blocks]
end

#restore_blocks(parts, code_blocks) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/snibbets/string.rb', line 166

def restore_blocks(parts, code_blocks)
  sections = []

  parts.each do |part|
    lines = part.split(/\n/).strip_empty

    notes = part.notes?

    next if lines.blocks.zero? && !notes

    title = if lines.count > 1 && lines[0] !~ /<block\d+>/
              lines.shift.strip.sub(/[.:]$/, '')
            else
              'Default snippet'
            end

    block = lines.join("\n").gsub(/<(block\d+)>/) do
      code = code_blocks[Regexp.last_match(1)].strip_empty
      lang, code = parse_lang_marker(code)
      "\n```#{lang}\n#{code.strip}\n```"
    end

    lang, code = parse_lang_marker(block)

    next unless code && !code.empty?

    # code = code.clean_code unless notes || code.fences.count > 1

    sections << {
      'title' => title,
      'code' => code.strip_empty,
      'language' => lang
    }
  end

  sections
end

#rxObject



80
81
82
# File 'lib/snibbets/string.rb', line 80

def rx
  ".*#{gsub(/tags?:/, '').gsub(/\s+/, '.*')}.*"
end

#snippetsObject

Returns an array of snippets. Single snippets are returned without a title, multiple snippets get titles from header lines



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/snibbets/string.rb', line 206

def snippets
  content = dup.remove_meta
  # If there's only one snippet, just clean it and return
  # return [{ 'title' => '', 'code' => content.clean_code.strip }] unless multiple?

  # Split content by ATX headers. Everything on the line after the #
  # becomes the title, code is gleaned from text between that and the
  # next ATX header (or end)
  sans_blocks, code_blocks = content.replace_blocks
  parts = sans_blocks.gsub(/\n{2,}/, "\n\n").split(/^#+/)

  unless Snibbets.options[:all_notes]
    parts.map! do |part|
      if part =~ /<block\d+>/
        lines = part.split(/\n/)
        title = lines.shift
        out = [title.gsub(/(^#+|#+$)/, '').strip]
        out.concat(lines.each_with_object([]) do |line, arr|
          arr << line.gsub(/(^#+|#+$)/, '').strip if line =~ /^#/ || line =~ /<block\d+>/
        end)
        out.join("\n")
      else
        lines = part.split(/\n/)
        title = lines.shift
        out = title.nil? ? [] : [title.gsub(/(^#+|#+$)/, '').strip]
        out.concat(lines)
        out.join("\n")
      end
    end
  end

  restore_blocks(parts, code_blocks)
end

#strip_emptyObject



23
24
25
# File 'lib/snibbets/string.rb', line 23

def strip_empty
  split(/\n/).strip_empty.join("\n")
end

#strip_newlinesObject



39
40
41
# File 'lib/snibbets/string.rb', line 39

def strip_newlines
  split(/\n/).strip_empty.join("\n")
end

#uncolorObject



10
11
12
# File 'lib/snibbets/colors.rb', line 10

def uncolor
  Color.uncolor(self)
end

#validate_colorString

Extract the longest valid %color name from a string.

Allows %colors to bleed into other text and still be recognized, e.g. %greensomething still finds %green.

Returns:

  • (String)

    a valid color name



118
119
120
121
122
123
124
125
126
127
# File 'lib/snibbets/colors.rb', line 118

def validate_color
  valid_color = nil
  compiled = ''
  normalize_color.split('').each do |char|
    compiled += char
    valid_color = compiled if Color.attributes.include?(compiled.to_sym) || compiled =~ /^([fb]g?)?#([a-f0-9]{6})$/i
  end

  valid_color
end

#xObject



6
7
8
# File 'lib/snibbets/colors.rb', line 6

def x
  Color.template(self)
end