class Doing::SayExport

Plugin class

Public Class Methods

render(wwid, items, variables: {}) click to toggle source

Render data received from an output command

@param wwid The wwid object with config and public methods @param items An array of items to be output { <Date>date, <String>title, <String>section, <Array>note } @param variables Additional variables including flags passed to command (variables)

@return [String] Rendered output

# File lib/examples/plugins/say_export.rb, line 122
def self.render(wwid, items, variables: {})
  return if items.nil? || items.empty?

  # the :options key includes the flags passed to the
  # command that called the plugin use `puts
  # variables.inspect` to see properties and methods
  # when run
  opt = variables[:options]

  # This plugin just grabs the last item in the `items`
  # list (which could be the oldest or newest, depending
  # on the sort order of the command that called the
  # plugin). Most of the time you'll want to use :each
  # or :map to generate output.
  i = items[-1]

  # Format the item. Items are a hash with 3 keys: date,
  # title, and section (parent section) Start time is in
  # item.date. The wwid object has some methods for
  # calculation and formatting, including
  # wwid.item.end_date to convert the @done
  # timestamp to an end date.
  if opt[:times]
    interval = i.interval

    if interval
      took = '. You finished on '
      finished_at = i.end_date
      took += finished_at.strftime('%A %B %e at %I:%M%p')

      took += ' and it took'
      took += interval.time_string(format: :natural)
    end
  end

  date = i.date.strftime('%A %B %e at %I:%M%p')
  title = i.title.gsub(/ @done\(.*?\)/, '').gsub(/@/, 'hashtag ')
  tpl = template('say')

  if wwid.config['export_templates'].key?('say')
    cfg_tpl = wwid.config['export_templates']['say']
    tpl = cfg_tpl unless cfg_tpl.nil? || cfg_tpl.empty?
  end
  output = tpl.dup
  output.gsub!(/%date/, date)
  output.gsub!(/%title/, title)
  output.gsub!(/%section/, i.section)
  output.gsub!(/%took/, took || '')

  # Debugging output
  # warn "Saying: #{output}"

  # To provide results on the command line after the
  # command runs, add to the wwid.results array. Results
  # are provided on STDERR unless doing is run with
  # `--stdout`
  Doing.logger.info('Spoke the last entry. Did you hear it?')

  # This export runs a command for fun, most plugins won't
  voice = wwid.config['plugins']['say']['say_voice'] || 'Alex'
  `say -v "#{voice}" "#{output}"`

  # Return the result (don't output to terminal with puts or print)
  output
end
settings() click to toggle source
# File lib/examples/plugins/say_export.rb, line 72
def self.settings
  {
    trigger: 'say(?:it)?',
    templates: [
      { name: 'say', trigger: 'say(?:it)?', format: 'text', filename: 'say.txt' }
    ],
    config: {
      'say_voice' => 'Fiona'
    }
  }
end
template(trigger) click to toggle source
# File lib/examples/plugins/say_export.rb, line 101
def self.template(trigger)
  return unless trigger =~ /^say(it)?$/
  'On %date, you were %title, recorded in section %section%took'
end