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 = opts end |
Instance Method Details
#accept_blank_line(*_arg) ⇒ Object
Add a blank line
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
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
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
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
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
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
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
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.
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.
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_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 |