[DRE-commits] [ruby-org] 235/303: Refactoring of method "flush!" from HTML output buffer.

Jérémy Bobbio lunar at alioth.debian.org
Fri Aug 9 17:34:04 UTC 2013


This is an automated email from the git hooks/post-receive script.

lunar pushed a commit to branch master
in repository ruby-org.

commit 62f9191cfc377384473d9dcf259c6a2b3bf9398f
Author: vonavi <ivvl82 at gmail.com>
Date:   Thu Jan 24 02:32:59 2013 +0200

    Refactoring of method "flush!" from HTML output buffer.
---
 lib/org-ruby/html_output_buffer.rb |  136 +++++++++++++++++-------------------
 lib/org-ruby/output_buffer.rb      |   21 +-----
 2 files changed, 64 insertions(+), 93 deletions(-)

diff --git a/lib/org-ruby/html_output_buffer.rb b/lib/org-ruby/html_output_buffer.rb
index 4c58ed2..afa2a46 100644
--- a/lib/org-ruby/html_output_buffer.rb
+++ b/lib/org-ruby/html_output_buffer.rb
@@ -107,92 +107,86 @@ module Orgmode
     end
 
     def flush!
-      if @buffer.length > 0 and not preserve_whitespace?
-        @buffer.lstrip!
-        @indentation = nil
-      end
-
-      if buffer_mode_is_src_block?
+      case
+      when preserve_whitespace?
+        # NOTE: CodeRay and Pygments already escape the html once, so
+        # no need to escape_buffer!
+        case
+        when (current_mode == :src and defined? Pygments)
+          lang = normalize_lang(@block_lang)
+          @output << "\n"
 
-        # Only try to colorize #+BEGIN_SRC blocks with a specified language,
-        # but we still have to catch the cases when a lexer for the language was not available
-        if defined? Pygments or defined? CodeRay
+          begin
+            @buffer = Pygments.highlight(@buffer, :lexer => lang)
+          rescue
+            # Not supported lexer from Pygments, we fallback on using the text lexer
+            @buffer = Pygments.highlight(@buffer, :lexer => 'text')
+          end
+        when (current_mode == :src and defined? CodeRay)
           lang = normalize_lang(@block_lang)
 
-          # NOTE: CodeRay and Pygments already escape the html once, so no need to escape_buffer!
-          if defined? Pygments
-            @output << "\n"
+          # CodeRay might throw a warning when unsupported lang is set,
+          # then fallback to using the text lexer
+          silence_warnings do
             begin
-              @buffer = Pygments.highlight(@buffer, :lexer => lang)
-            rescue
-              # Not supported lexer from Pygments, we fallback on using the text lexer
-              @buffer = Pygments.highlight(@buffer, :lexer => 'text')
-            end
-          elsif defined? CodeRay
-            # CodeRay might throw a warning when unsupported lang is set,
-            # then fallback to using the text lexer
-            silence_warnings do
-              begin
-                @buffer = CodeRay.scan(@buffer, lang).html(:wrap => nil, :css => :style)
-              rescue ArgumentError
-                @buffer = CodeRay.scan(@buffer, 'text').html(:wrap => nil, :css => :style)
-              end
+              @buffer = CodeRay.scan(@buffer, lang).html(:wrap => nil, :css => :style)
+            rescue ArgumentError
+              @buffer = CodeRay.scan(@buffer, 'text').html(:wrap => nil, :css => :style)
             end
           end
         else
           escape_buffer!
         end
 
-        @logger.debug "FLUSH SRC CODE ==========> #{@buffer.inspect}"
-        @output << @buffer
-      elsif mode_is_code?(@buffer_mode) then
-        escape_buffer!
-
-        # Whitespace is significant in :code mode. Always output the buffer
-        # and do not do any additional translation.
+        # Whitespace is significant in :code mode. Always output the
+        # buffer and do not do any additional translation.
         @logger.debug "FLUSH CODE ==========> #{@buffer.inspect}"
         @output << @buffer
-      else
+
+      when (mode_is_table? current_mode and skip_tables?)
+        @logger.debug "SKIP       ==========> #{current_mode}"
+
+      when @buffer.length > 0
+        @buffer.lstrip!
         escape_buffer!
-        if @buffer.length > 0 and @output_type == :horizontal_rule then
+        @indentation = nil
+        @logger.debug "FLUSH      ==========> #{current_mode}"
+
+        case
+        when @buffered_lines[0].kind_of?(Headline)
+          headline = @buffered_lines[0]
+          raise "Cannot be more than one headline!" if @buffered_lines.length > 1
+          if @options[:export_heading_number] then
+            level = headline.level
+            heading_number = get_next_headline_number(level)
+            @output << "<span class=\"heading-number heading-number-#{level}\">#{heading_number} </span>"
+          end
+          if @options[:export_todo] and headline.keyword then
+            keyword = headline.keyword
+            @output << "<span class=\"todo-keyword #{keyword}\">#{keyword} </span>"
+          end
+          @output << inline_formatting(@buffer)
+
+        when current_mode == :definition_item
           @output << "\n"
           @indentation = :output
           output_indentation
-          @output << "<hr />"
-        elsif @buffer.length > 0 and @buffer_mode == :definition_item then
-          unless mode_is_table?(@buffer_mode) and skip_tables?
-            @output << "\n"
-            @indentation = :output
-            output_indentation
-            d = @buffer.split("::", 2)
-            @output << "<#{HtmlBlockTag[:definition_term]}#{@title_decoration}>" << inline_formatting(d[0].strip) \
-                    << "</#{HtmlBlockTag[:definition_term]}>"
-            if d.length > 1 then
-              @output << "<#{HtmlBlockTag[:definition_descr]}#{@title_decoration}>" << inline_formatting(d[1].strip) \
-                      << "</#{HtmlBlockTag[:definition_descr]}>"
-            end
-            @title_decoration = ""
-          end
-        elsif @buffer.length > 0 then
-          unless mode_is_table?(@buffer_mode) and skip_tables?
-            @logger.debug "FLUSH      ==========> #{@buffer_mode}"
-            if (@buffered_lines[0].kind_of?(Headline)) then
-              headline = @buffered_lines[0]
-              raise "Cannot be more than one headline!" if @buffered_lines.length > 1
-              if @options[:export_heading_number] then
-                level = headline.level
-                heading_number = get_next_headline_number(level)
-                @output << "<span class=\"heading-number heading-number-#{level}\">#{heading_number} </span>"
-              end
-              if @options[:export_todo] and headline.keyword then
-                keyword = headline.keyword
-                @output << "<span class=\"todo-keyword #{keyword}\">#{keyword} </span>"
-              end
-            end
-            @output << inline_formatting(@buffer)
-          else
-            @logger.debug "SKIP       ==========> #{@buffer_mode}"
+          d = @buffer.split("::", 2)
+          @output << "<#{HtmlBlockTag[:definition_term]}#{@title_decoration}>" << inline_formatting(d[0].strip) \
+          << "</#{HtmlBlockTag[:definition_term]}>"
+          if d.length > 1
+            @output << "<#{HtmlBlockTag[:definition_descr]}#{@title_decoration}>" << inline_formatting(d[1].strip) \
+            << "</#{HtmlBlockTag[:definition_descr]}>"
           end
+
+        when @output_type == :horizontal_rule
+          @output << "\n"
+          @indentation = :output
+          output_indentation
+          @output << "<hr />"
+
+        else
+          @output << inline_formatting(@buffer)
         end
       end
       clear_accumulation_buffer!
@@ -227,10 +221,6 @@ module Orgmode
        mode == :table_separator or mode == :table_header)
     end
 
-    def buffer_mode_is_src_block?
-      @buffer_mode == :src
-    end
-
     # Escapes any HTML content in the output accumulation buffer @buffer.
     def escape_buffer!
       @buffer.gsub!(/&/, "&")
diff --git a/lib/org-ruby/output_buffer.rb b/lib/org-ruby/output_buffer.rb
index 9a08795..ba5b19a 100644
--- a/lib/org-ruby/output_buffer.rb
+++ b/lib/org-ruby/output_buffer.rb
@@ -26,9 +26,6 @@ module Orgmode
       # accumulation buffer.
       @buffered_lines = []
 
-      # This is the output mode of the accumulation buffer.
-      @buffer_mode = nil
-
       # This stack is used to do proper outline numbering of
       # headlines.
       @headline_number_stack = []
@@ -36,8 +33,6 @@ module Orgmode
       @output = output
       @output_type = :start
       @list_indent_stack = []
-      @paragraph_modifier = nil
-      @cancel_modifier = false
       @mode_stack = []
 
       @logger = Logger.new(STDERR)
@@ -54,10 +49,6 @@ module Orgmode
       @mode_stack.last
     end
 
-    def current_mode_list?
-      (current_mode == :ordered_list) or (current_mode == :unordered_list)
-    end
-
     def push_mode(mode)
       @mode_stack.push(mode)
     end
@@ -78,11 +69,7 @@ module Orgmode
         flush!
         maintain_mode_stack(line)
       end
-      if line.assigned_paragraph_type
-        @output_type = line.assigned_paragraph_type
-      else
-        @output_type = line.paragraph_type
-      end
+      @output_type = line.assigned_paragraph_type || line.paragraph_type
 
       # Adds the current line to the output buffer
       @buffered_lines.push(line)
@@ -106,7 +93,6 @@ module Orgmode
     # method just does common bookkeeping cleanup.
     def clear_accumulation_buffer!
       @buffer = ""
-      @buffer_mode = nil
       @buffered_lines = []
     end
 
@@ -128,11 +114,6 @@ module Orgmode
 
     # Accumulate the string @str at .
     def << (str)
-      if @buffer_mode && @buffer_mode != current_mode then
-        raise "Accumulation buffer is mixing modes: @buffer_mode == #{@buffer_mode}, current_mode == #{current_mode}"
-      else
-        @buffer_mode = current_mode
-      end
       @buffer << str
     end
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-ruby-extras/ruby-org.git



More information about the Pkg-ruby-extras-commits mailing list