class BayeuxTextGen
Public Class Methods
new(syntax_tree)
click to toggle source
Default Constructor
# File lib/bayeux/text_gen.rb, line 6 def initialize(syntax_tree) @syntax_tree = syntax_tree @text_string = String.new @text = String.new end
Public Instance Methods
generate()
click to toggle source
Return the AST as an HTML string (stored in @text_string)
# File lib/bayeux/text_gen.rb, line 13 def generate # Create a walker for the trees in the forest walker = TreeWalker.new walker.on_before_down = self.method(:generate_node_start) walker.on_after_up = self.method(:generate_node_end) walker.on_no_siblings = self.method(:generate_full_node) walker.on_no_children = self.method(:generate_full_node) # Clear the internal HTML representation @text_string.clear # Walk the forest @syntax_tree.block_forest.each{|tree| walker.walk_tree(tree) } return @text_string end
generate_full_node(block)
click to toggle source
Output the full contents of the node, properly bracketed as an HTML expression. This is only called if we have no sub-nodes to deal with
# File lib/bayeux/text_gen.rb, line 43 def generate_full_node(block) # Check if we need to add a space # Check if we need to add a space if not block.content[0] =~ /\s[:;.,?!]/ @text_string << " " end case block.type # Headers when :h1, :h2, :h3, :h4, :h5, :h6 @text_string << "#{block.content.strip}\n" # Ordinary paragraphs when :paragraph @text_string << "#{block.content.word_wrap(70)}\n\n" when :none @text_string << "#{block.content.word_wrap(70)}" # Special paragraphs when :block_quote @text_string << "#{block.content.word_wrap(60).indent(5)}\n\n" when :single_quote @text_string << "\'#{block.content}\'" when :double_quote @text_string << "\"#{block.content}\"" when :note @text_string << "Note: #{block.content}\n\n" when :command @text_string << "#{block.content.strip.indent(5)}\n\n" when :code_language @code_language = block.content when :code_start_number @code_start_number = block.content when :file @text_string << "#{block.content}\n\n" when :output 70.downto(0){|index| @text_string << "-" } @text_string << "\n#{block.content.strip}\n" 70.downto(0){|index| @text_string << "-" } @text_string << "\n\n" # Special Characters when :em_dash @text_string << " - " when :en_dash @text_string << "-" when :elipses @text_string << "..." when :elipses_stop @text_string << "...." # Links when :link_target @link_target = block.content when :link_text @link_text = block.content # Lists when :item @text_string << " * #{block.content}" when :dl_header @text_string << "#{block.content}: " when :dl_text @text_string << "#{block.content}" # Tags when :ac # Do we know anything about this acronymn? if $reference.acronym_list.include?(block.content) then # Assemble from the reference list if $reference.acronym_list[block.content].include?('text_text') then acronym = $reference.acronym_list[block.content]['text_text'] else acronym = $reference.acronym_list[block.content]['text'] end definition = $reference.acronym_list[block.content]['def'] @text_string << "#{acronym}" else # Do what we can @text_string << "#{block.content}" end when :emph @text_string << "#{block.content}" when :tt @text_string << "#{block.content}" end end
generate_node_end(block)
click to toggle source
# File lib/bayeux/text_gen.rb, line 243 def generate_node_end(block) case block.type # Headers when :h1, :h2, :h3, :h4, :h5, :h6 @text_string << @text.word_wrap(70) @text = "" # Ordinary paragraphs when :paragraph @text_string << @text.indent(2).word_wrap(70) + "\n" @text = "" # Special paragraphs when :block_quote @text_string << @text.indent(5).word_wrap(60) + "\n" @text = "" when :single_quote @text_string << "'" when :double_quote @text_string << "\"" when :note @text_string << @text.indent(2).word_wrap(70) + "\n" @text = "" # Special Characters when :em_dash @text << " - " when :en_dash @text << "-" when :elipses @text << "..." when :elipses_stop @text << "...." # Links when :link # Assemble the link from the sub-tree nodes @text << "#{@link_text}" when :link_target @link_target = block.content when :link_text @link_text = block.content # Lists when :item @text_string << @text.indent(2).word_wrap(70) + "\n" @text = "" when :dl @text_string << "\n" @text = "" when :dl_text @text_string << @text.indent(2).word_wrap(70) + "\n" @text = "" end end
generate_node_start(block)
click to toggle source
Output only the start of a node
# File lib/bayeux/text_gen.rb, line 150 def generate_node_start(block) # Check if we need to add a space if not block.content[0] =~ /\s[:;.,?!]/ @text << " " end case block.type # Headers when :h1, :h2, :h3, :h4, :h5, :h6 @text_string << "#{block.content.strip}" # Ordinary paragraphs when :paragraph @text << "#{block.content}" when :none @text << "#{block.content}" # Special paragraphs when :block_quote @text << "#{block.content}" when :single_quote @text << "'#{block.content}" when :double_quote @text << "\"#{block.content}" when :note @text << "Note: #{block.content}" when :command @text_string << "#{block.content.lstrip.indent(5)}\n" when :code_language @code_language = block.content when :code_start_number @code_start_number = block.content # Special Characters when :em_dash @text << " - " when :en_dash @text << "-" when :elipses @text << "..." when :elipses_stop @text << "...." # Links when :link_target @link_target = block.content when :link_text @link_text = block.content # Lists when :item @text << " * #{block.content}" when :dl_header @text << "#{block.content}: " when :dl_text @text << "#{block.content}" # Tags when :ac # Do we know anything about this acronymn? if $reference.acronym_list.include?(block.content) then # Assemble from the reference list if $reference.acronym_list[block.content].include?('text_text') then acronym = $reference.acronym_list[block.content]['text_text'] else acronym = $reference.acronym_list[block.content]['text'] end definition = $reference.acronym_list[block.content]['def'] @text_string << "#{acronym}" else # Do what we can @text_string << "#{block.content}" end when :emph @text << "#{block.content}" when :tt @text << "#{block.content}" end end