Class: RDoc::Markup::ToICalPal

Inherits:
Formatter
  • Object
show all
Defined in:
lib/ToICalPal.rb

Overview

Render an RDoc::Markup::Document, closely mimicking icalBuddy

Constant Summary collapse

ANSI =

Standard ANSI colors

{
  'black':   30,  '#000000': '38;5;0',
  'red':     31,  '#ff0000': '38;5;1',
  'green':   32,  '#00ff00': '38;5;2',
  'yellow':  33,  '#ffff00': '38;5;3',
  'blue':    34,  '#0000ff': '38;5;4',
  'magenta': 35,  '#ff00ff': '38;5;5',
  'cyan':    36,  '#00ffff': '38;5;6',
  'white':   37,  '#ffffff': '38;5;255',
  'default': 39,  'custom': nil,

  # Reminders custom colors
  'brown':     '38;2;162;132;94',
  'gray':      '38;2;91;98;106',
  'indigo':    '38;2;88;86;214',
  'lightblue': '38;2;90;200;250',
  'orange':    '38;2;255;149;0',
  'pink':      '38;2;255;45;85',
  'purple':    '38;2;204;115;225',
  'rose':      '38;2;217;166;159',
}.freeze
BOLD =

Increased intensity

format('%c[1m', 27.chr)
NORM =

Default rendition

format('%c[0m', 27.chr)
NO_LABEL =

Properties for which we don’t include labels

%w[ title datetime ].freeze
COLOR_LABEL =

Properties that are always colorized

%w[ title calendar ].freeze
LABEL_COLOR =

Default color for labels

[ 'cyan', '#00ffff' ].freeze
DATE_COLOR =

Color for datetime value

[ 'yellow', '#ffff00' ].freeze

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ ToICalPal

Returns a new instance of ToICalPal.

Parameters:

  • opts (Hash)

    Used for conditional formatting

Options Hash (opts):

  • :bullet (String)

    Bullet

  • :ab (String)

    Alert bullet

  • :nb (Boolean)

    No bullet

  • :nc (Boolean)

    No calendar names

  • :npn (Boolean)

    No property names

  • :palette (Integer) — default: nil

    8 for -f, 24 for --color

  • :ps (Array<String>)

    List of property separators

  • :ss (String)

    Section separator



57
58
59
60
# File 'lib/ToICalPal.rb', line 57

def initialize(opts)
  super(opts)
  @opts = opts
end

Instance Method Details

#accept_blank_line(*_arg) ⇒ Object

Add a blank line

Parameters:

  • *_arg (Array)

    Ignored



107
108
109
# File 'lib/ToICalPal.rb', line 107

def accept_blank_line(*_arg)
  @res << "\n"
end

#accept_heading(h) ⇒ Object

Add either a section header or the first property of an item

Parameters:

  • h (RDoc::Markup::Heading)

Options Hash (h):

  • :level (Integer)

    1 for a section header

  • :level (Integer)

    2 for a property name

  • :text (String)

    The header’s text



117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/ToICalPal.rb', line 117

def accept_heading(h)
  h.text = colorize(@item['symbolic_color_name'], @item['color'], h.text) if (h.level == 2) || COLOR_LABEL.any?(@prop)
  @res << h.text

  case h.level
  when 1
    @res << ':'
  when 2
    if @prop == 'title' && @item['calendar']
      @res << bold(" (#{@item['calendar']})") unless @opts[:nc] || @item['title'] == @item['calendar']
    end
  end
end

#accept_list_item_start(arg) ⇒ Object

Add a property name

Parameters:

  • arg (RDoc::Markup::ListItem)

Options Hash (arg):

  • .label (String)

    Contains the property name



97
98
99
100
101
102
# File 'lib/ToICalPal.rb', line 97

def accept_list_item_start(arg)
  @res << @opts[:ps][@ps] || '    ' unless @item['placeholder']
  @res << colorize(*LABEL_COLOR, arg.label) << ': ' unless @opts[:npn] || NO_LABEL.any?(arg.label)

  @ps += 1 unless @ps == @opts[:ps].count - 1
end

#accept_list_start(_arg) ⇒ Object

Add a bullet for the first property of an item

Parameters:

  • _arg (Array)

    Ignored



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/ToICalPal.rb', line 76

def accept_list_start(_arg)
  begin
    return if @item['placeholder']
  rescue
  end

  begin
    if (@item['due_date'] + ICalPal::ITIME).between?(ICalPal::ITIME + 1, $now.to_i)
      @res << "#{@opts[:ab]} " unless @opts[:nb]
      return
    end
  rescue
  end

  @res << "#{@opts[:bullet]} " unless @opts[:nb]
end

#accept_paragraph(p) ⇒ Object

Add the property value

Parameters:

  • p (RDoc::Markup::Paragraph)

Options Hash (p):

  • :parts (Array<String>)

    The property’s text



135
136
137
138
139
# File 'lib/ToICalPal.rb', line 135

def accept_paragraph(p)
  t = p.parts.join('; ').gsub(/\n/, "\n    ")
  t = colorize(*DATE_COLOR, t) if @prop == 'datetime'
  @res << t
end

#accept_raw(arg) ⇒ Object

Don’t add anything to the document, just save the property name for later

Parameters:

  • arg (RDoc::Markup::Raw)

Options Hash (arg):

  • :parts (Object)

    The property



162
163
164
# File 'lib/ToICalPal.rb', line 162

def accept_raw(arg)
  @prop = arg.parts[0]
end

#accept_rule(_weight) ⇒ Object

Add a section separator

Parameters:

  • _weight

    Ignored



144
145
146
147
# File 'lib/ToICalPal.rb', line 144

def accept_rule(_weight)
  @res << @opts[:ss]
  accept_blank_line
end

#accept_verbatim(arg) ⇒ Object

Don’t add anything to the document, just save the item for later

Parameters:

  • arg (RDoc::Markup::Verbatim)

Options Hash (arg):

  • :format (Object)

    The item



153
154
155
# File 'lib/ToICalPal.rb', line 153

def accept_verbatim(arg)
  @item = arg.format
end

#bold(str) ⇒ String

Returns str with increased intensity.

Parameters:

  • str (String)

Returns:



168
169
170
171
172
# File 'lib/ToICalPal.rb', line 168

def bold(str)
  return str unless @opts[:palette]

  BOLD + str + NORM
end

#colorize(c8, c24, str) ⇒ String

Returns str in color, depending on opts.

Parameters:

  • c8 (String)

    Color used for -f

  • c24 (String)

    Color used for --color

Returns:

  • (String)

    str in color, depending on opts



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/ToICalPal.rb', line 177

def colorize(c8, c24, str)
  return str unless c8 && c24 && @opts[:palette]

  case @opts[:palette]
  when 8                      # Default colour table
    c = ANSI[c8.downcase.to_sym]
    c ||= ANSI[c24[0..6].downcase.to_sym]
    c ||= ANSI[:white]

  when 24                     # Direct colour in RGB space
    rgb = c24[1..].split(/(\h\h)(\h\h)(\h\h)/)
    rgb.map! { |i| i.to_i(16) }
    c = [ 38, 2, rgb[1..] ].join(';')
  end

  # esc c str esc ansi
  format('%<esc>c[%<color>sm%<string>s%<esc>c[%<ansi_default>sm',
         { esc: 27.chr, color: c, string: str, ansi_default: ANSI[:default] })
end

#end_acceptingObject

Close the document



69
70
71
# File 'lib/ToICalPal.rb', line 69

def end_accepting
  @res.join
end

#start_acceptingObject

Start a new document



63
64
65
66
# File 'lib/ToICalPal.rb', line 63

def start_accepting
  @res = []
  @ps = 0
end