Class: RI::TextFormatter

Class | RI::TextFormatter
In: | rdoc/ri/ri_formatter.rb
Parent: | Object

Finally, fill in the list of known formatters

### Methods

blankline bold_print break_to_newline conv_html conv_markup display_flow display_flow_item display_heading display_list display_verbatim_flow_item draw_line for list new raw_print_line strip_attributes wrap

### Constants

FORMATTERS | = | { "ansi" => AnsiFormatter, "bs" => OverstrikeFormatter, "html" => HtmlFormatter, "plain" => TextFormatter, "simple" => SimpleFormatter, }

### Attributes

indent | [R] |

### Public Class methods

for(name)

[Source]

    # File rdoc/ri/ri_formatter.rb, line 666
        def TextFormatter.for(name)
          FORMATTERS[name.downcase]
        end

list()

[Source]

    # File rdoc/ri/ri_formatter.rb, line 662
        def TextFormatter.list
          FORMATTERS.keys.sort.join(", ")
        end

new(options, indent)

[Source]

    # File rdoc/ri/ri_formatter.rb, line 6
        def initialize(options, indent)
          @options = options
          @width   = options.width
          @indent  = indent
        end

### Public Instance methods

blankline()

[Source]

    # File rdoc/ri/ri_formatter.rb, line 51
        def blankline
          puts
        end

bold_print(txt)

[Source]

    # File rdoc/ri/ri_formatter.rb, line 66
        def bold_print(txt)
          print txt
        end

break_to_newline()

called when we want to ensure a nbew ‘wrap’ starts on a newline Only needed for HtmlFormatter, because the rest do their own line breaking

[Source]

    # File rdoc/ri/ri_formatter.rb, line 61
        def break_to_newline
        end

conv_html(txt)

convert HTML entities back to ASCII

[Source]

    # File rdoc/ri/ri_formatter.rb, line 79
        def conv_html(txt)
          txt.
              gsub(/&gt;/, '>').
              gsub(/&lt;/, '<').
              gsub(/&quot;/, '"').
              gsub(/&amp;/, '&')

        end

conv_markup(txt)

convert markup into display form

[Source]

    # File rdoc/ri/ri_formatter.rb, line 89
        def conv_markup(txt)
          txt.
              gsub(%r{<tt>(.*?)</tt>}) { "+#$1+" } .
              gsub(%r{<code>(.*?)</code>}) { "+#$1+" } .
              gsub(%r{<b>(.*?)</b>}) { "*#$1*" } .
              gsub(%r{<em>(.*?)</em>}) { "_#$1_" }
        end

display_flow(flow)

[Source]

    # File rdoc/ri/ri_formatter.rb, line 210
        def display_flow(flow)
          flow.each do |f|
            display_flow_item(f)
          end
        end

display_flow_item(item, prefix=@indent)

[Source]

    # File rdoc/ri/ri_formatter.rb, line 154
        def display_flow_item(item, prefix=@indent)
          case item
          when SM::Flow::P, SM::Flow::LI
            wrap(conv_html(item.body), prefix)
            blankline

          when SM::Flow::LIST
            display_list(item)

          when SM::Flow::VERB
            display_verbatim_flow_item(item, @indent)

          when SM::Flow::H
            display_heading(conv_html(item.text), item.level, @indent)

          when SM::Flow::RULE
            draw_line

          else
            fail "Unknown flow element: #{item.class}"
          end
        end

display_heading(text, level, indent)

[Source]

    # File rdoc/ri/ri_formatter.rb, line 188
        def display_heading(text, level, indent)
          text = strip_attributes(text)
          case level
          when 1
            ul = "=" * text.length
            puts
            puts text.upcase
            puts ul
    #        puts

          when 2
            ul = "-" * text.length
            puts
            puts text
            puts ul
    #        puts
          else
            print indent, text, "\n"
          end
        end

display_list(list)

[Source]

    # File rdoc/ri/ri_formatter.rb, line 99
        def display_list(list)
          case list.type

          when SM::ListBase::BULLET
            prefixer = proc { |ignored| @indent + "*   " }

          when SM::ListBase::NUMBER,
          SM::ListBase::UPPERALPHA,
          SM::ListBase::LOWERALPHA

            start = case list.type
                    when SM::ListBase::NUMBER      then 1
                    when  SM::ListBase::UPPERALPHA then 'A'
                    when SM::ListBase::LOWERALPHA  then 'a'
                    end
            prefixer = proc do |ignored|
              res = @indent + "#{start}.".ljust(4)
              start = start.succ
              res
            end

          when SM::ListBase::LABELED
            prefixer = proc do |li|
              li.label
            end

          when SM::ListBase::NOTE
            longest = 0
            list.contents.each do |item|
              if item.kind_of?(SM::Flow::LI) && item.label.length > longest
                longest = item.label.length
              end
            end

            prefixer = proc do |li|
              @indent + li.label.ljust(longest+1)
            end

          else
            fail "unknown list type"

          end

          list.contents.each do |item|
            if item.kind_of? SM::Flow::LI
              prefix = prefixer.call(item)
              display_flow_item(item, prefix)
            else
              display_flow_item(item)
            end
           end
        end

display_verbatim_flow_item(item, prefix=@indent)

[Source]

    # File rdoc/ri/ri_formatter.rb, line 179
        def display_verbatim_flow_item(item, prefix=@indent)
            item.body.split(/\n/).each do |line|
              print @indent, conv_html(line), "\n"
            end
            blankline
        end

draw_line(label=nil)

[Source]

    # File rdoc/ri/ri_formatter.rb, line 15
        def draw_line(label=nil)
          len = @width
          len -= (label.size+1) if label
          print "-"*len
          if label
            print(" ")
            bold_print(label)
          end
          puts
        end

raw_print_line(txt)

[Source]

    # File rdoc/ri/ri_formatter.rb, line 72
        def raw_print_line(txt)
          puts txt
        end

strip_attributes(txt)

[Source]

    # File rdoc/ri/ri_formatter.rb, line 216
        def strip_attributes(txt)
          tokens = txt.split(%r{(</?(?:b|code|em|i|tt)>)})
          text = []
          attributes = 0
          tokens.each do |tok|
            case tok
            when %r{^</(\w+)>$}, %r{^<(\w+)>$}
              ;
            else
              text << tok
            end
          end
          text.join
        end

wrap(txt, prefix=@indent, linelen=@width)

[Source]

    # File rdoc/ri/ri_formatter.rb, line 28
        def wrap(txt,  prefix=@indent, linelen=@width)
          return unless txt && !txt.empty?
          work = conv_markup(txt)
          textLen = linelen - prefix.length
          patt = Regexp.new("^(.{0,#{textLen}})[ \n]")
          next_prefix = prefix.tr("^ ", " ")

          res = []

          while work.length > textLen
            if work =~ patt
              res << $1
              work.slice!(0, $&.length)
            else
              res << work.slice!(0, textLen)
            end
          end
          res << work if work.length.nonzero?
          puts (prefix +  res.join("\n" + next_prefix))
        end

[Validate]