class Mhc::Converter::Emacs

Public Instance Methods

to_calfw(ev) click to toggle source

return cfw:event structure

(defstruct cfw:event
  title       ; event title [string]
  start-date  ; start date of the event [cfw:date]
  start-time  ; start time of the event (optional)
  end-date    ; end date of the event [cfw:date] (optional)
  end-time    ; end of the event (optional)
  description ; event description [string] (optional)
  location    ; location [strting] (optional)
  source      ; [internal] source of the event
)
# File lib/mhc/converter.rb, line 16
def to_calfw(ev)
  hash = {
    :title       => ev.subject.to_s,
    :start_date  => "",
    :start_time  => "",
    :end_date    => "",
    :end_time    => "",
    :description => "",
    :location    => "",
    :source      => ""
  }
  to_emacs_plist(hash)
end
to_emacs(obj) click to toggle source
# File lib/mhc/converter.rb, line 30
def to_emacs(obj)
  case obj
  when Array
    to_emacs_list(obj)
  when Hash
    to_emacs_plist(obj)
  else
    to_emacs_string(obj)
  end
end
to_emacs_list(array) click to toggle source
# File lib/mhc/converter.rb, line 56
def to_emacs_list(array)
  wrap(array.map{|val| to_emacs(val)}.join(" "))
end
to_emacs_plist(hash) click to toggle source
# File lib/mhc/converter.rb, line 52
def to_emacs_plist(hash)
  wrap(hash.map{|key,val| "#{to_emacs_symbol(key)} #{to_emacs(val)}"}.join(" "))
end
to_emacs_string(str) click to toggle source
# File lib/mhc/converter.rb, line 45
def to_emacs_string(str)
  # 1. quote " and \
  # 2. LF => \n
  # 3. surround by "
  '"' + str.to_s.toutf8.gsub(/[\"\\]/, '\\\\\&').gsub("\n", "\\n") + '"'
end
to_emacs_symbol(obj) click to toggle source
# File lib/mhc/converter.rb, line 41
def to_emacs_symbol(obj)
  ":" + obj.to_s.downcase.gsub('_', '-')
end

Private Instance Methods

wrap(obj) click to toggle source
# File lib/mhc/converter.rb, line 61
def wrap(obj)
  "(" + obj.to_s + ")"
end