Class: RDoc::Markup::ToAnsi
| Relationships & Source Files | |
| Super Chains via Extension / Inclusion / Inheritance | |
|
Class Chain:
|
|
|
Instance Chain:
|
|
| Inherits: |
RDoc::Markup::ToRdoc
|
| Defined in: | lib/rdoc/markup/to_ansi.rb |
Overview
Outputs RDoc markup with vibrant ANSI color!
Constant Summary
-
ANSI_STYLE_CODES_OFF =
# File 'lib/rdoc/markup/to_ansi.rb', line 29{ BOLD: 22, TT: 27, EM: 24, STRIKE: 29 } -
ANSI_STYLE_CODES_ON =
# File 'lib/rdoc/markup/to_ansi.rb', line 22
Maps attributes to ANSI sequences
{ BOLD: 1, TT: 7, EM: 4, STRIKE: 9 }
ToRdoc - Inherited
Class Method Summary
-
.new(markup = nil) ⇒ ToAnsi
constructor
Creates a new
ToAnsivisitor that is ready to output vibrant ANSI color!
ToRdoc - Inherited
| .new | Creates a new formatter that will output (mostly) RDoc markup. |
Formatter - Inherited
| .gen_relative_url | Converts a target url to one that is relative to a given path. |
| .new | Creates a new |
Instance Attribute Summary
ToRdoc - Inherited
| #indent | Current indent amount for output in characters. |
| #list_index | Stack of current list indexes for alphabetic and numeric lists. |
| #list_type | Stack of list types. |
| #list_width | Stack of list widths for indentation. |
| #prefix | Prefix for the next list item. |
| #res | Output accumulator. |
| #width | Output width in characters. |
Instance Method Summary
-
#accept_list_item_end(list_item)
Overrides indent width to ensure output lines up correctly.
-
#accept_list_item_start(list_item)
Adds coloring to note and label list items.
- #add_text(text)
-
#apply_attributes(attributes)
Apply the given attributes by emitting ANSI sequences.
- #calculate_text_width(text)
- #handle_inline(text)
-
#start_accepting
Starts accepting with a reset screen.
ToRdoc - Inherited
| #accept_blank_line | Adds |
| #accept_block_quote | Adds |
| #accept_heading | Adds |
| #accept_indented_paragraph | Adds |
| #accept_list_end | Finishes consumption of |
| #accept_list_item_end | Finishes consumption of |
| #accept_list_item_start | Prepares the visitor for consuming |
| #accept_list_start | Prepares the visitor for consuming |
| #accept_paragraph | Adds |
| #accept_raw | Adds |
| #accept_rule | Adds |
| #accept_table | Adds |
| #accept_verbatim | Outputs |
| #add_text, | |
| #attributes | Applies attribute-specific markup to |
| #calculate_text_width, #emit_inline, | |
| #end_accepting | Returns the generated output. |
| #handle_BOLD, #handle_BOLD_WORD, #handle_EM, #handle_EM_WORD, #handle_HARD_BREAK, #handle_inline, #handle_PLAIN_TEXT, #handle_REGEXP_HANDLING_TEXT, | |
| #handle_regexp_SUPPRESSED_CROSSREF | Removes preceding \ from the suppressed crossref |
| #handle_STRIKE, #handle_TIDYLINK, #handle_TT, #off, #on, | |
| #start_accepting | Prepares the visitor for text generation. |
| #use_prefix | Adds the stored |
| #wrap | Wraps |
Formatter - Inherited
| #accept_document | Adds |
| #add_regexp_handling_RDOCLINK | Adds a regexp handling for links of the form rdoc-…: |
| #annotate | Allows |
| #apply_regexp_handling | Applies regexp handling to |
| #convert | Marks up |
| #convert_string | Converts a string to be fancier if desired. |
| #handle_BOLD | Called when processing bold nodes while traversing inline nodes from handle_inline. |
| #handle_BOLD_WORD | Called when processing bold word nodes while traversing inline nodes from handle_inline. |
| #handle_EM | Called when processing emphasis nodes while traversing inline nodes from handle_inline. |
| #handle_EM_WORD | Called when processing emphasis word nodes while traversing inline nodes from handle_inline. |
| #handle_HARD_BREAK | Called when processing a hard break while traversing inline nodes from handle_inline. |
| #handle_inline | Parses inline |
| #handle_PLAIN_TEXT | Called when processing plain text while traversing inline nodes from handle_inline. |
| #handle_REGEXP_HANDLING_TEXT | Called when processing regexp-handling-processed text while traversing inline nodes from handle_inline. |
| #handle_STRIKE | Called when processing strike nodes while traversing inline nodes from handle_inline. |
| #handle_TEXT | Called when processing text node while traversing inline nodes from handle_inline. |
| #handle_TIDYLINK | Called when processing tidylink nodes while traversing inline nodes from handle_inline. |
| #handle_TT | Called when processing tt nodes while traversing inline nodes from handle_inline. |
| #ignore | Use ignore in your subclass to ignore the content of a node. |
| #parse_url | Extracts and a scheme, url and an anchor id from |
| #traverse_inline_nodes | Traverses |
| #tt? | Is |
Constructor Details
.new(markup = nil) ⇒ ToAnsi
Creates a new ToAnsi visitor that is ready to output vibrant ANSI color!
# File 'lib/rdoc/markup/to_ansi.rb', line 10
def initialize(markup = nil) super @headings.clear @headings[1] = ["\e[1;32m", "\e[m"] # bold @headings[2] = ["\e[4;32m", "\e[m"] # underline @headings[3] = ["\e[32m", "\e[m"] # just green end
Instance Method Details
#accept_list_item_end(list_item)
Overrides indent width to ensure output lines up correctly.
# File 'lib/rdoc/markup/to_ansi.rb', line 78
def accept_list_item_end(list_item) width = case @list_type.last when :BULLET then 2 when :NOTE, :LABEL then if @prefix then @res << @prefix.strip @prefix = nil end @res << "\n" unless res.length == 1 2 else bullet = @list_index.last.to_s @list_index[-1] = @list_index.last.succ bullet.length + 2 end @indent -= width end
#accept_list_item_start(list_item)
Adds coloring to note and label list items
# File 'lib/rdoc/markup/to_ansi.rb', line 102
def accept_list_item_start(list_item) bullet = case @list_type.last when :BULLET then '*' when :NOTE, :LABEL then labels = Array(list_item.label).map do |label| attributes(label).strip end.join "\n" labels << ":\n" unless labels.empty? labels else @list_index.last.to_s + '.' end case @list_type.last when :NOTE, :LABEL then @indent += 2 @prefix = bullet + (' ' * @indent) else @prefix = (' ' * @indent) + bullet.ljust(bullet.length + 1) width = bullet.gsub(/\e\[[\d;]*m/, '').length + 1 @indent += width end end
#add_text(text)
[ GitHub ]# File 'lib/rdoc/markup/to_ansi.rb', line 59
def add_text(text) attrs = @attributes.keys if @applied_attributes != attrs apply_attributes(attrs) end emit_inline(text) end
#apply_attributes(attributes)
Apply the given attributes by emitting ANSI sequences. Emitting attribute changes are deferred until new text is added and applied in batch. This method computes the necessary ANSI codes to transition from the current set of applied attributes to the new set of attributes.
# File 'lib/rdoc/markup/to_ansi.rb', line 41
def apply_attributes(attributes) before = @applied_attributes after = attributes.sort return if before == after if after.empty? emit_inline("\e[m") elsif !before.empty? && before.size > (before & after).size + 1 codes = after.map {|attr| ANSI_STYLE_CODES_ON[attr] }.compact emit_inline("\e[#{[0, *codes].join(';')}m") else off_codes = (before - after).map {|attr| ANSI_STYLE_CODES_OFF[attr] }.compact on_codes = (after - before).map {|attr| ANSI_STYLE_CODES_ON[attr] }.compact emit_inline("\e[#{(off_codes + on_codes).join(';')}m") end @applied_attributes = attributes end
#calculate_text_width(text)
[ GitHub ]# File 'lib/rdoc/markup/to_ansi.rb', line 131
def calculate_text_width(text) text.gsub(/\e\[[\d;]*m/, '').size end
#handle_inline(text)
[ GitHub ]# File 'lib/rdoc/markup/to_ansi.rb', line 67
def handle_inline(text) @applied_attributes = [] res = super res << "\e[m" unless @applied_attributes.empty? @applied_attributes = [] res end
#start_accepting
Starts accepting with a reset screen
# File 'lib/rdoc/markup/to_ansi.rb', line 138
def start_accepting super @res = ["\e[0m"] end