class String

String helpers

String helpers

Public Instance Methods

add_comment(key, value) click to toggle source
# File lib/conductor/filter.rb, line 267
def add_comment(key, value)
  if has_comment?(key)
    sub(/ *#{key}: .*?$/, "#{key}: #{value}")
  else
    lines = split(/\n/)
    lines.insert(meta_insert_point, "<!--\n#{key}: #{value}\n-->")
    lines.join("\n")
  end
end
add_mmd(key, value) click to toggle source
# File lib/conductor/filter.rb, line 249
def add_mmd(key, value)
  if match(/(\A|\n) *#{key}: *\S+/i)
    sub(/^ *#{key}:.*?\n/i, "#{key}: #{value}\n")
  else
    lines = split(/\n/)
    lines.insert(meta_insert_point, "#{key}: #{value}")
    lines.join("\n")
  end
end
add_yaml(key, value) click to toggle source
# File lib/conductor/filter.rb, line 231
def add_yaml(key, value)
  sub(/^---.*?\n(---|\.\.\.)/m) do
    m = Regexp.last_match
    yaml = YAML.load(m[0])
    yaml[key] = value
    "#{YAML.dump(yaml)}\n---\n"
  end
end
append(string) click to toggle source
# File lib/conductor/filter.rb, line 153
def append(string)
  "#{self}\n#{string}"
end
bool?() click to toggle source

Test if a string is a boolean

@return [Boolean] test result

# File lib/conductor/string.rb, line 108
def bool?
  dup.force_encoding("utf-8").match?(/^(?:y(?:es)?|no?|t(?:rue)?|f(?:alse)?)$/)
end
bool_to_symbol() click to toggle source

Convert a string boolean to symbol

@return [Symbol] symbolized version

# File lib/conductor/string.rb, line 41
def bool_to_symbol
  case self
  when /(NOT|!!)/
    :not
  when /(AND|&&)/
    :and
  else
    :or
  end
end
date?() click to toggle source

Test a string to see if it’s a UTC date

@return [Boolean] test result

# File lib/conductor/string.rb, line 57
def date?
  dup.force_encoding("utf-8").match?(/^\d{4}-\d{2}-\d{2}( \d\d:\d\d)?$/)
end
delete_meta(key) click to toggle source
# File lib/conductor/filter.rb, line 277
def delete_meta(key)
  case meta_type
  when :yaml
    delete_yaml(key)
  when :mmd
    delete_mmd(key)
  end
end
delete_mmd(key) click to toggle source
# File lib/conductor/filter.rb, line 259
def delete_mmd(key)
  sub(/^ *#{key}:.*?\n/i, "")
end
delete_yaml(key) click to toggle source
# File lib/conductor/filter.rb, line 240
def delete_yaml(key)
  sub(/^---.*?\n(---|\.\.\.)/m) do
    m = Regexp.last_match
    yaml = YAML.load(m[0])
    yaml.delete(key)
    "#{YAML.dump(yaml)}\n---\n"
  end
end
first_h1() click to toggle source
# File lib/conductor/filter.rb, line 50
def first_h1
  first = 0
  split(/\n/).each_with_index do |line, idx|
    if line =~ /^(# *\S|={2,} *$)/
      first = idx
      break
    end
  end
  first
end
first_h2() click to toggle source
# File lib/conductor/filter.rb, line 61
def first_h2
  first = 0
  split(/\n/).each_with_index do |line, idx|
    if line =~ /^(## *\S|-{2,} *$)/
      first = idx
      break
    end
  end
  first
end
get_title() click to toggle source
# File lib/conductor/filter.rb, line 186
def get_title
  title = nil

  case meta_type
  when :yaml
    m = match(/^---.*?\n(---|\.\.\.)/m)
    yaml = YAML.load(m[0])
    title = yaml["title"]
  when :mmd
    split(/\n/).each do |line|
      if line =~ /^ *title: *(\S.*?)$/i
        title = Regexp.last_match(1)
        break
      end
    end
  else
    m = match(/title: (.*?)$/i)
    title = m ? m[0] : nil
  end

  title ||= title_from_slug.titleize

  title
end
has_comment?(key) click to toggle source
# File lib/conductor/filter.rb, line 263
def has_comment?(key)
  match(/^<!--.*?#{key}: \S.*?-->/m)
end
insert_css(path) click to toggle source
# File lib/conductor/filter.rb, line 95
def insert_css(path)
  path.sub!(/(\.css)?$/, '.css')

  if path =~ %r{^[~/]}
    path = File.expand_path(path)
  elsif File.directory?(File.expand_path("~/.config/conductor/css"))
    new_path = File.expand_path("~/.config/conductor/css/#{path}")
    path = new_path if File.exist?(new_path)
  elsif File.directory?(File.expand_path("~/.config/conductor/files"))
    new_path = File.expand_path("~/.config/conductor/files/#{path}")
    path = new_path if File.exist?(new_path)
  end

  if File.exist?(path)
    content = IO.read(path)
    yui = YuiCompressor::Yui.new
    content = yui.compress(content)
    lines = split(/\n/)
    insert_point = meta_insert_point
    insert_at = insert_point.positive? ? insert_point + 1 : 0
    lines.insert(insert_at, "#{content.wrap_style}\n\n")
    lines.join("\n")
  else
    warn "File not found (#{path})"
    self
  end
end
insert_file(path, type = :file, position = :end) click to toggle source
# File lib/conductor/filter.rb, line 123
def insert_file(path, type = :file, position = :end)
  path.strip!

  if path =~ %r{^[~/]}
    path = File.expand_path(path)
  elsif File.directory?(File.expand_path("~/.config/conductor/files"))
    new_path = File.expand_path("~/.config/conductor/files/#{path}")
    path = new_path if File.exist?(new_path)
  end

  out = case type
        when :code
          "<<(#{path})"
        when :raw
          "<<{#{path}}"
        else
          "<<[#{path}]"
        end
  out = "\n#{out}\n"

  case position
  when :start
    "#{out}\n#{self}"
  when :h1
    split(/\n/).insert(first_h1 + 1, out).join("\n")
  else
    "#{self}\n#{out}"
  end
end
insert_script(path) click to toggle source
# File lib/conductor/filter.rb, line 157
def insert_script(path)
  path.strip!
  path = "#{path}.js" unless path =~ /\.js$/

  if path =~ %r{^[~/]}
    path = File.expand_path(path)
  else
    new_path = if File.directory?(File.expand_path("~/.config/conductor/javascript"))
                 File.expand_path("~/.config/conductor/javascript/#{path}")
               elsif File.directory?(File.expand_path("~/.config/conductor/javascripts"))
                 File.expand_path("~/.config/conductor/javascripts/#{path}")
               else
                 File.expand_path("~/.config/conductor/scripts/#{path}")
               end

    path = new_path if File.exist?(new_path)
  end

  %(#{self}\n<script type="javascript" src="#{path}"></script>\n)
end
insert_title() click to toggle source
# File lib/conductor/filter.rb, line 211
def insert_title
  title = get_title
  lines = split(/\n/)
  insert_point = meta_insert_point
  insert_at = insert_point.positive? ? insert_point + 1 : 0
  lines.insert(insert_at, "# #{title}\n")
  lines.join("\n")
end
insert_toc(max = nil, after = :h1) click to toggle source
# File lib/conductor/filter.rb, line 72
def insert_toc(max = nil, after = :h1)
  lines = split(/\n/)
  max = max && max.to_i.positive? ? " max#{max}" : ""
  line = case after.to_sym
         when :h2
           first_h2.positive? ? first_h2 + 1 : 0
         when :h1
           first_h1.positive? ? first_h1 + 1 : 0
         else
           0
         end

  lines.insert(line, "\n<!--toc#{max}-->\n").join("\n")
end
meta?() click to toggle source

Test if a string starts with MMD metadata

@return [Boolean] test result

# File lib/conductor/string.rb, line 126
def meta?
  dup.force_encoding('utf-8').match?(/^\w+: +\S+/m)
end
meta_insert_point() click to toggle source
# File lib/conductor/filter.rb, line 24
def meta_insert_point
  insert_point = 0

  case meta_type
  when :yaml
    lines = split(/\n/)
    lines.shift
    lines.each_with_index do |line, idx|
      next unless line =~ /^(...|---) *$/

      insert_point = idx + 1
      break
    end
  when :mmd
    lines = split(/\n/)
    lines.each_with_index do |line, idx|
      next if line =~ /^ *[ \w]+: +\S+/

      insert_point = idx
      break
    end
  end

  insert_point
end
meta_type() click to toggle source
# File lib/conductor/filter.rb, line 12
def meta_type
  lines = split(/\n/)
  case lines[0]
  when /^--- *$/
    :yaml
  when /^ *[ \w]+: +\S+/
    :mmd
  else
    :none
  end
end
normalize_filter() click to toggle source
# File lib/conductor/filter.rb, line 5
def normalize_filter
  parts = match(/(?<filter>[\w_]+)(?:\((?<paren>.*?)\))?$/i)
  filter = parts["filter"].downcase.gsub(/_/, "")
  params = parts["paren"]&.split(/ *, */)
  [filter, params]
end
normalize_include_type() click to toggle source
# File lib/conductor/string.rb, line 25
def normalize_include_type
  case self
  when /^c/
    :code
  when /^r/
    :raw
  else
    :file
  end
end
normalize_position() click to toggle source
# File lib/conductor/string.rb, line 14
def normalize_position
  case self
  when /^(be|s|t)/
    :start
  when /h1/
    :h1
  else
    :end
  end
end
number?() click to toggle source

Test if a string is a number

@return [Boolean] test result

# File lib/conductor/string.rb, line 99
def number?
  to_f.positive?
end
replace(regex, pattern) click to toggle source
# File lib/conductor/filter.rb, line 300
def replace(regex, pattern)
  sub(regex.to_rx, pattern.to_pattern)
end
replace_all(regex, pattern) click to toggle source
# File lib/conductor/filter.rb, line 296
def replace_all(regex, pattern)
  gsub(regex.to_rx, pattern.to_pattern)
end
set_meta(key, value, style: :comment) click to toggle source
# File lib/conductor/filter.rb, line 220
def set_meta(key, value, style: :comment)
  case style
  when :yaml
    add_yaml(key, value)
  when :mmd
    add_mmd(key, value)
  else # comment or none
    add_comment(key, value)
  end
end
strip_meta() click to toggle source
# File lib/conductor/filter.rb, line 286
def strip_meta
  case meta_type
  when :yaml
    sub(/^---.*?(---|...)/m, "")
  when :mmd
    lines = split(/\n/)
    lines[meta_insert_point..]
  end
end
strip_time() click to toggle source

Remove time from string

@return [String] string with time removed

# File lib/conductor/string.rb, line 85
def strip_time
  dup.force_encoding("utf-8").sub(/ \d{1,2}(:\d\d)? *([ap]m)?/i, "")
end
time?() click to toggle source

Test a string to see if it includes a time

@return [Boolean] test result

# File lib/conductor/string.rb, line 66
def time?
  dup.force_encoding("utf-8").match(/ \d{1,2}(:\d\d)? *([ap]m)?/i)
end
title_from_slug() click to toggle source
# File lib/conductor/filter.rb, line 178
def title_from_slug
  filename = File.basename(Conductor::Env.env[:filepath]).sub(/\.[a-z]+$/i, "")
  filename.sub!(/-?\d{4}-\d{2}-\d{2}-?/, "")
  filename.sub!(/\bdot\b/, ".")
  filename.sub!(/ dash /, "-")
  filename
end
titleize() click to toggle source

Titlecase a string

@return Titleized string

# File lib/conductor/string.rb, line 10
def titleize
  split(/(\W)/).map(&:capitalize).join
end
to_bool() click to toggle source

Returns a bool representation of the string.

@return [Boolean] Bool representation of the object.

# File lib/conductor/string.rb, line 145
def to_bool
  case self.force_encoding('utf-8')
  when /^[yt]/i
    true
  else
    false
  end
end
to_bool!() click to toggle source

Destructive version of to_bool

@see to_bool

# File lib/conductor/string.rb, line 136
def to_bool!
  replace to_bool
end
to_date() click to toggle source

Convert a natural language string to a Date

object

@return [Date] Resulting Date object

# File lib/conductor/string.rb, line 76
def to_date
  Chronic.parse(dup.force_encoding("utf-8"))
end
to_day(time = :end) click to toggle source
# File lib/conductor/string.rb, line 89
def to_day(time = :end)
  t = time == :end ? "23:59" : "00:00"
  Chronic.parse("#{strip_time} #{t}")
end
to_pattern() click to toggle source
# File lib/conductor/filter.rb, line 315
def to_pattern
  gsub(/\$(\d+)/, '\\\\\1').gsub(/(^["']|["']$)/, "")
end
to_rx() click to toggle source
# File lib/conductor/filter.rb, line 304
def to_rx
  if self =~ %r{^/(.*?)/([im]+)?$}
    m = Regexp.last_match
    regex = m[1]
    flags = m[2]
    Regexp.new(regex, flags)
  else
    Regexp.new(Regexp.escape(self))
  end
end
wrap_style() click to toggle source
# File lib/conductor/filter.rb, line 87
def wrap_style
  if match(%r{<style>.*?</style>}m)
    self
  else
    "<style>#{self}</style>"
  end
end
yaml?() click to toggle source

Test if string starts with YAML

@return [Boolean] test result

# File lib/conductor/string.rb, line 117
def yaml?
  dup.force_encoding('utf-8').match?(/^---/m)
end