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.

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_item_start, #accept_list_start, #accept_paragraph, #accept_raw, #accept_rule, #accept_verbatim, #attributes, #end_accepting, #handle_special_HARD_BREAK, #handle_special_SUPPRESSED_CROSSREF, #start_accepting, #use_prefix, #wrap

Methods inherited from Formatter

#accept_document, #add_special_RDOCLINK, #add_special_TIDYLINK, #add_tag, #convert, #convert_flow, gen_relative_url, #ignore, #in_tt?, #off_tags, #on_tags, #parse_url, #tt?

Constructor Details

#initialize(markup = nil) ⇒ ToBs

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



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

def initialize markup = nil
  super

  @in_b  = false
  @in_em = false
end

Instance Method Details

#accept_heading(heading) ⇒ Object

Makes heading text bold.



32
33
34
35
36
37
38
39
40
# File 'lib/rdoc/markup/to_bs.rb', line 32

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

#annotate(tag) ⇒ Object

Turns on or off special handling for convert_string



45
46
47
48
49
50
51
52
53
# File 'lib/rdoc/markup/to_bs.rb', line 45

def annotate tag
  case tag
  when '+b' then @in_b = true
  when '-b' then @in_b = false
  when '+_' then @in_em = true
  when '-_' then @in_em = false
  end
  ''
end

#convert_special(special) ⇒ Object

Calls convert_string on the result of convert_special



58
59
60
# File 'lib/rdoc/markup/to_bs.rb', line 58

def convert_special special
  convert_string super
end

#convert_string(string) ⇒ Object

Adds bold or underline mixed with backspaces



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rdoc/markup/to_bs.rb', line 65

def convert_string string
  return string unless string.respond_to? :chars # your ruby is lame
  return string unless @in_b or @in_em
  chars = if @in_b then
            string.chars.map do |char| "#{char}\b#{char}" end
          elsif @in_em then
            string.chars.map do |char| "_\b#{char}" end
          end

  chars.join
end

#init_tagsObject

Sets a flag that is picked up by #annotate to do the right thing in #convert_string



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

def init_tags
  add_tag :BOLD, '+b', '-b'
  add_tag :EM,   '+_', '-_'
  add_tag :TT,   ''  , ''   # we need in_tt information maintained
end