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

Instance Method Details

#accept_blank_line(*_arg) ⇒ Object

Add a blank line

Parameters:

  • _arg (Array)

    Ignored



86
87
88
# File 'lib/ToICalPal.rb', line 86

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

#accept_heading(h) ⇒ Object

Add a heading



91
92
93
# File 'lib/ToICalPal.rb', line 91

def accept_heading(h)
  @res << h.text
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



76
77
78
79
80
81
# File 'lib/ToICalPal.rb', line 76

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

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

#accept_list_start(_arg) ⇒ Object

Add a list

Parameters:

  • _arg (Array)

    Ignored



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

def accept_list_start(_arg) end

#accept_paragraph(p) ⇒ Object

Add a paragraph

Parameters:

  • p (RDoc::Markup::Paragraph)

Options Hash (p):

  • :parts (Array<String>)

    The property’s text



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

def accept_paragraph(p)
  @res << p.parts.join('; ').gsub("\n", "\n    ")
end

#accept_raw(arg) ⇒ Object

Add raw text

Parameters:

  • arg (RDoc::Markup::Raw)


114
115
116
# File 'lib/ToICalPal.rb', line 114

def accept_raw(arg)
  @res << arg.parts
end

#accept_rule(_weight) ⇒ Object

Add a section separator and a blank line

Parameters:

  • _weight

    Ignored



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

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

#bold(str) ⇒ String

Returns str with increased intensity.

Parameters:

  • str (String)

Returns:



120
121
122
123
124
# File 'lib/ToICalPal.rb', line 120

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

  BOLD + str + NORM
end

#COLOR_LABELObject



52
# File 'lib/ToICalPal.rb', line 52

def COLOR_LABEL() COLOR_LABEL 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



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/ToICalPal.rb', line 129

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

  case @options[: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

#DATE_COLORObject



54
# File 'lib/ToICalPal.rb', line 54

def DATE_COLOR() DATE_COLOR end

#end_acceptingObject

Close the document



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

def end_accepting
  @res.join
end

#LABEL_COLORObject



53
# File 'lib/ToICalPal.rb', line 53

def LABEL_COLOR() LABEL_COLOR end

#NO_LABELObject

Accessors for constants



51
# File 'lib/ToICalPal.rb', line 51

def NO_LABEL() NO_LABEL end

#start_acceptingObject

Start a new document



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

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