Class: RDoc::Markup::ToBs

Inherits:
Markup::ToRdoc
  • Object
show all
Defined in:
lib/rdoc/markup/to_bs.rb

Overview

Outputs RDoc markup with hot backspace action! You will probably need a pager to use this output format.

This formatter won't work on 1.8.6 because it lacks String#chars.

Instance Method Summary collapse

Constructor Details

#initialize ⇒ ToBs

Returns a new ToBs that is ready for hot backspace action!



15
16
17
18
19
20
# File 'lib/rdoc/markup/to_bs.rb', line 15

def initialize
  super

  @in_b  = false
  @in_em = false
end

Instance Method Details

#accept_heading(heading) ⇒ Object

Makes heading text bold.



50
51
52
53
54
55
56
57
58
# File 'lib/rdoc/markup/to_bs.rb', line 50

def accept_heading(heading)
  use_prefix or @res << ' ' * @indent
  @res << @headings[heading.level][0]
  @in_b = true
  @res << attributes(heading.text)
  @in_b = false
  @res << @headings[heading.level][1]
  @res << "\n"
end

#accept_list_item_start(list_item) ⇒ Object

Prepares the visitor for consuming list_item



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rdoc/markup/to_bs.rb', line 63

def accept_list_item_start(list_item)
  type = @list_type.last

  case type
  when :NOTE, :LABEL
    bullets = Array(list_item.label).map do |label|
      attributes(label).strip
    end.join "\n"

    bullets << ":\n" unless bullets.empty?

    @prefix = ' ' * @indent
    @indent += 2
    @prefix << bullets + (' ' * @indent)
  else
    bullet = type == :BULLET ? '*' :  @list_index.last.to_s + '.'
    @prefix = (' ' * @indent) + bullet.ljust(bullet.length + 1)
    width = bullet.length + 1
    @indent += width
  end
end

#add_text(text) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rdoc/markup/to_bs.rb', line 29

def add_text(text)
  attrs = @attributes.keys
  if attrs.include? :BOLD
    styled = +''
    text.chars.each do |c|
      styled << "#{c}\b#{c}"
    end
    text = styled
  elsif attrs.include? :EM
    styled = +''
    text.chars.each do |c|
      styled << "_\b#{c}"
    end
    text = styled
  end
  emit_inline(text)
end

#calculate_text_width(text) ⇒ Object



85
86
87
# File 'lib/rdoc/markup/to_bs.rb', line 85

def calculate_text_width(text)
  text.gsub(/_\x08/, '').gsub(/\x08./, '').size
end

#handle_inline(text) ⇒ Object



22
23
24
25
26
27
# File 'lib/rdoc/markup/to_bs.rb', line 22

def handle_inline(text)
  initial_style = []
  initial_style << :BOLD if @in_b
  initial_style << :EM   if @in_em
  super(text, initial_style)
end