[DRE-commits] [ruby-org] 221/303: Newlines are handled carefully.

Jérémy Bobbio lunar at alioth.debian.org
Fri Aug 9 17:34:01 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 c29d00fc73a640086f6400d86e5ac55d8cd9b9b3
Author: vonavi <ivvl82 at gmail.com>
Date:   Fri Jan 11 16:12:30 2013 +0300

    Newlines are handled carefully.
---
 lib/org-ruby/html_output_buffer.rb    |   83 +++++++++++++++++++--------------
 lib/org-ruby/output_buffer.rb         |    2 +-
 lib/org-ruby/parser.rb                |    4 +-
 lib/org-ruby/textile_output_buffer.rb |    8 ++--
 4 files changed, 56 insertions(+), 41 deletions(-)

diff --git a/lib/org-ruby/html_output_buffer.rb b/lib/org-ruby/html_output_buffer.rb
index c752f01..ce180b3 100644
--- a/lib/org-ruby/html_output_buffer.rb
+++ b/lib/org-ruby/html_output_buffer.rb
@@ -47,6 +47,7 @@ module Orgmode
         @title_decoration = ""
       end
       @options = opts
+      @indentation = :start
       @footnotes = {}
       @unclosed_tags = []
       @logger.debug "HTML export options: #{@options.inspect}"
@@ -59,47 +60,58 @@ module Orgmode
       super(mode)
       @list_indent_stack.push(indent)
 
-      skip_tag = ((mode_is_table?(mode) and skip_tables?) or
-                  (mode == :src and defined? Pygments))
-      if HtmlBlockTag[mode] and not skip_tag
-        css_class = case
-                    when (mode == :src and @block_lang.empty?)
-                      " class=\"src\""
-                    when (mode == :src and not @block_lang.empty?)
-                      " class=\"src src-#{@block_lang}\""
-                    when (mode == :example || mode == :inline_example)
-                      " class=\"example\""
-                    when mode == :center
-                      " style=\"text-align: center\""
-                    else
-                      @title_decoration
-                    end
-
-        output_indentation
-        @logger.debug "#{mode}: <#{HtmlBlockTag[mode]}#{css_class}>\n"
-        @output << "<#{HtmlBlockTag[mode]}#{css_class}>"
-        # Entering a new mode obliterates the title decoration
-        @title_decoration = ""
+      if HtmlBlockTag[mode]
+        unless ((mode_is_table?(mode) and skip_tables?) or
+                (mode == :src and defined? Pygments))
+          css_class = case
+                      when (mode == :src and @block_lang.empty?)
+                        " class=\"src\""
+                      when (mode == :src and not @block_lang.empty?)
+                        " class=\"src src-#{@block_lang}\""
+                      when (mode == :example || mode == :inline_example)
+                        " class=\"example\""
+                      when mode == :center
+                        " style=\"text-align: center\""
+                      else
+                        @title_decoration
+                      end
+
+          @output << "\n" unless @indentation == :start
+          @indentation = :output
+          output_indentation
+          @logger.debug "#{mode}: <#{HtmlBlockTag[mode]}#{css_class}>"
+          @output << "<#{HtmlBlockTag[mode]}#{css_class}>"
+          # Entering a new mode obliterates the title decoration
+          @title_decoration = ""
+        end
       end
-      not skip_tag
     end
 
     # We are leaving a mode. Close any tags that were opened when
     # entering this mode.
     def pop_mode(mode = nil)
       m = super(mode)
-      if HtmlBlockTag[m] then
+      if HtmlBlockTag[m]
         unless ((mode_is_table?(m) and skip_tables?) or
                 (m == :src and defined? Pygments))
-          output_indentation
-          @logger.debug "</#{HtmlBlockTag[m]}>\n"
-          @output << "</#{HtmlBlockTag[m]}>\n"
+          if @indentation
+            @output << "\n"
+            output_indentation
+          end
+          @indentation = :output
+          @logger.debug "</#{HtmlBlockTag[m]}>"
+          @output << "</#{HtmlBlockTag[m]}>"
         end
       end
       @list_indent_stack.pop
     end
 
     def flush!
+      if @buffer.length > 0 and not preserve_whitespace?
+        @buffer.lstrip!
+        @indentation = nil
+      end
+
       if buffer_mode_is_src_block?
 
         # Only try to colorize #+BEGIN_SRC blocks with a specified language,
@@ -109,13 +121,13 @@ module Orgmode
 
           # NOTE: CodeRay and Pygments already escape the html once, so no need to escape_buffer!
           if defined? Pygments
+            @output << "\n"
             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
-            @buffer << "\n"
           elsif defined? CodeRay
             # CodeRay might throw a warning when unsupported lang is set,
             # then fallback to using the text lexer
@@ -143,18 +155,21 @@ module Orgmode
       else
         escape_buffer!
         if @buffer.length > 0 and @output_type == :horizontal_rule then
-          @output << "<hr />\n"
+          @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]}>\n"
-            else
-              @output << "\n"
+                      << "</#{HtmlBlockTag[:definition_descr]}>"
             end
             @title_decoration = ""
           end
@@ -167,11 +182,11 @@ module Orgmode
               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>"
+                @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>"
+                @output << "<span class=\"todo-keyword #{keyword}\">#{keyword} </span>"
               end
             end
             @output << inline_formatting(@buffer)
@@ -186,7 +201,7 @@ module Orgmode
     def output_footnotes!
       return false unless @options[:export_footnotes] and not @footnotes.empty?
 
-      @output << "<div id=\"footnotes\">\n<h2 class=\"footnotes\">Footnotes:\n</h2>\n<div id=\"text-footnotes\">\n"
+      @output << "\n<div id=\"footnotes\">\n<h2 class=\"footnotes\">Footnotes:</h2>\n<div id=\"text-footnotes\">\n"
 
       @footnotes.each do |name, defi|
         @output << "<p class=\"footnote\"><sup><a class=\"footnum\" name=\"fn.#{name}\" href=\"#fnr.#{name}\">#{name}</a></sup>" \
diff --git a/lib/org-ruby/output_buffer.rb b/lib/org-ruby/output_buffer.rb
index a513e81..67e51c2 100644
--- a/lib/org-ruby/output_buffer.rb
+++ b/lib/org-ruby/output_buffer.rb
@@ -188,7 +188,7 @@ module Orgmode
         if line.major_mode
           if (@list_indent_stack.empty? or
               @list_indent_stack.last < line.indent)
-            @output << "\n" if push_mode(line.major_mode, line.indent)
+            push_mode(line.major_mode, line.indent)
           end
         end
         # Open tag that precedes text immediately
diff --git a/lib/org-ruby/parser.rb b/lib/org-ruby/parser.rb
index 4d70607..948e67a 100644
--- a/lib/org-ruby/parser.rb
+++ b/lib/org-ruby/parser.rb
@@ -256,13 +256,13 @@ module Orgmode
         # it if we're about to switch to some other output type.
         output_buffer.prepare(line)
         if output_buffer.preserve_whitespace? and not line.begin_block?
-          output_buffer << line.output_text << "\n"
+          output_buffer << "\n" << line.output_text
         else
           case line.paragraph_type
           when :metadata, :table_separator, :blank, :comment, :property_drawer_item, :property_drawer_begin_block, :property_drawer_end_block, :begin_block, :end_block
             # Nothing
           else
-            output_buffer << line.output_text.strip << "\n"
+            output_buffer << "\n" << line.output_text.strip
           end
         end
       end
diff --git a/lib/org-ruby/textile_output_buffer.rb b/lib/org-ruby/textile_output_buffer.rb
index c8cad77..951ca0d 100644
--- a/lib/org-ruby/textile_output_buffer.rb
+++ b/lib/org-ruby/textile_output_buffer.rb
@@ -14,12 +14,11 @@ module Orgmode
     def push_mode(mode, indent)
       @list_indent_stack.push(indent)
       super(mode)
-      @output << "bc.. " if mode_is_code? mode
+      @output << "bc. " if mode_is_code? mode
       if mode == :center or mode == :blockquote
         @add_paragraph = false
         @output << "\n"
       end
-      false
     end
 
     def pop_mode(mode = nil)
@@ -85,7 +84,8 @@ module Orgmode
       @logger.debug "FLUSH ==========> #{@output_type}"
       if @output_type == :blank and not preserve_whitespace?
         @output << "\n"
-      elsif (@buffer.length > 0) then
+      elsif @buffer.length > 0
+        @buffer.gsub!(/\A\n*/, "")
         @output << "p. " if @add_paragraph and current_mode == :paragraph
         if @mode_stack[0] and current_mode == :paragraph
           @output << "p=. " if @mode_stack[0] == :center
@@ -106,7 +106,7 @@ module Orgmode
           raise "Cannot be more than one headline!" if @buffered_lines.length > 1
           @output << "h#{headline.level}. #{headline.headline_text}\n"
         else
-          @output << inline_formatting(@buffer)
+          @output << inline_formatting(@buffer) << "\n"
         end
       end
       clear_accumulation_buffer!

-- 
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