Class: RDoc::Markup::ToBs

Inherits:
ToRdoc 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.

Constant Summary

Constants inherited from ToRdoc

RDoc::Markup::ToRdoc::DEFAULT_HEADINGS

Instance Attribute Summary

Attributes inherited from ToRdoc

#indent, #list_index, #list_type, #list_width, #prefix, #res, #width

Instance Method Summary collapse

Methods inherited from ToRdoc

#accept_blank_line, #accept_block_quote, #accept_indented_paragraph, #accept_list_end, #accept_list_item_end, #accept_list_start, #accept_paragraph, #accept_raw, #accept_rule, #accept_table, #accept_verbatim, #attributes, #emit_inline, #end_accepting, #handle_BOLD, #handle_BOLD_WORD, #handle_EM, #handle_EM_WORD, #handle_HARD_BREAK, #handle_PLAIN_TEXT, #handle_REGEXP_HANDLING_TEXT, #handle_STRIKE, #handle_TIDYLINK, #handle_TT, #handle_regexp_SUPPRESSED_CROSSREF, #off, #on, #start_accepting, #use_prefix, #wrap

Methods inherited from Formatter

#accept_document, #add_regexp_handling_RDOCLINK, #annotate, #apply_regexp_handling, #convert, #convert_string, gen_relative_url, #handle_BOLD, #handle_BOLD_WORD, #handle_EM, #handle_EM_WORD, #handle_HARD_BREAK, #handle_PLAIN_TEXT, #handle_REGEXP_HANDLING_TEXT, #handle_STRIKE, #handle_TEXT, #handle_TIDYLINK, #handle_TT, #ignore, #parse_url, #traverse_inline_nodes, #tt?

Constructor Details

#initialize(markup = nil) ⇒ ToBs

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



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

def initialize(markup = nil)
  super

  @in_b  = false
  @in_em = false
end

Instance Method Details

#accept_heading(heading) ⇒ Object

Makes heading text bold.



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

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



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

def accept_list_item_start(list_item)
  type = @list_type.last

  case type
  when :NOTE, :LABEL then
    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



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

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



83
84
85
# File 'lib/rdoc/markup/to_bs.rb', line 83

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

#handle_inline(text) ⇒ Object



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

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