Class: RDoc::Markup::ToICalPal
- Inherits:
-
Formatter
- Object
- Formatter
- RDoc::Markup::ToICalPal
- 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
-
#accept_blank_line(*_arg) ⇒ Object
Add a blank line.
-
#accept_heading(h) ⇒ Object
Add either a section header or the first property of an item.
-
#accept_list_item_start(arg) ⇒ Object
Add a property name.
-
#accept_list_start(_arg) ⇒ Object
Add a bullet for the first property of an item.
-
#accept_paragraph(p) ⇒ Object
Add the property value.
-
#accept_raw(arg) ⇒ Object
Don’t add anything to the document, just save the property name for later.
-
#accept_rule(_weight) ⇒ Object
Add a section separator.
-
#accept_verbatim(arg) ⇒ Object
Don’t add anything to the document, just save the item for later.
-
#bold(str) ⇒ String
Str with increased intensity.
-
#colorize(c8, c24, str) ⇒ String
Str in color, depending on opts.
-
#end_accepting ⇒ Object
Close the document.
-
#initialize(opts) ⇒ ToICalPal
constructor
A new instance of ToICalPal.
-
#start_accepting ⇒ Object
Start a new document.
Constructor Details
#initialize(opts) ⇒ ToICalPal
Returns a new instance of ToICalPal.
57 58 59 60 |
# File 'lib/ToICalPal.rb', line 57 def initialize(opts) super @opts = opts end |
Instance Method Details
#accept_blank_line(*_arg) ⇒ Object
Add a blank line
101 102 103 |
# File 'lib/ToICalPal.rb', line 101 def accept_blank_line(*_arg) @res << "\n" end |
#accept_heading(h) ⇒ Object
Add either a section header or the first property of an item
111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/ToICalPal.rb', line 111 def accept_heading(h) h = RDoc::Markup::Heading.new(h.level, 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
91 92 93 94 95 96 |
# File 'lib/ToICalPal.rb', line 91 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
76 77 78 79 80 81 82 83 84 85 |
# File 'lib/ToICalPal.rb', line 76 def accept_list_start(_arg) return if @opts[:nb] || @item['placeholder'] if @item['due_date'] && (@item['due_date']).between?(0, $nowto_i) # Use alert bullet for overdue items @res << "#{@opts[:ab]} " else @res << "#{@opts[:bullet]} " end end |
#accept_paragraph(p) ⇒ Object
Add the property value
129 130 131 132 133 |
# File 'lib/ToICalPal.rb', line 129 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
156 157 158 |
# File 'lib/ToICalPal.rb', line 156 def accept_raw(arg) @prop = arg.parts[0] end |
#accept_rule(_weight) ⇒ Object
Add a section separator
138 139 140 141 |
# File 'lib/ToICalPal.rb', line 138 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
147 148 149 |
# File 'lib/ToICalPal.rb', line 147 def accept_verbatim(arg) @item = arg.format end |
#bold(str) ⇒ String
Returns str with increased intensity.
162 163 164 165 166 |
# File 'lib/ToICalPal.rb', line 162 def bold(str) return str unless @opts[:palette] BOLD + str + NORM end |
#colorize(c8, c24, str) ⇒ String
Returns str in color, depending on opts.
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/ToICalPal.rb', line 171 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_accepting ⇒ Object
Close the document
69 70 71 |
# File 'lib/ToICalPal.rb', line 69 def end_accepting @res.join end |
#start_accepting ⇒ Object
Start a new document
63 64 65 66 |
# File 'lib/ToICalPal.rb', line 63 def start_accepting @res = [] @ps = 0 end |