Permalink
Browse files

Corrects code syntax highlighting.

  • Loading branch information...
1 parent 44d48ca commit 0ed172a88fa9bdfcda737083ec2fd471d250c84f @vonavi committed Dec 20, 2012
Showing with 42 additions and 30 deletions.
  1. +3 −2 lib/org-ruby/html_output_buffer.rb
  2. +3 −1 lib/org-ruby/line.rb
  3. +34 −25 lib/org-ruby/output_buffer.rb
  4. +2 −2 lib/org-ruby/parser.rb
@@ -55,7 +55,7 @@ def initialize(output, opts = {})
# Output buffer is entering a new mode. Use this opportunity to
# write out one of the block tags in the HtmlBlockTag constant to
# put this information in the HTML stream.
- def push_mode(mode, indent = 0)
+ def push_mode(mode, indent)
@list_indent_stack.push(indent)
if HtmlBlockTag[mode] then
css_class = @title_decoration
@@ -109,6 +109,7 @@ def flush!
# 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
@@ -126,7 +127,7 @@ def flush!
@logger.debug "FLUSH SRC CODE ==========> #{@buffer.inspect}"
@output << @buffer
- elsif mode_is_code(@buffer_mode) then
+ elsif mode_is_code?(@buffer_mode) then
escape_buffer!
# Whitespace is significant in :code mode. Always output the buffer
@@ -242,7 +242,7 @@ def determine_paragraph_type
when table_header?
:table_header
when inline_example?
- :inline_example
+ :example_line
when horizontal_rule?
:horizontal_rule
else :paragraph
@@ -260,6 +260,8 @@ def determine_major_mode
:unordered_list
when table?
:table
+ when inline_example?
+ :inline_example
when (begin_block? and block_type.casecmp("QUOTE") == 0)
:blockquote
when (begin_block? and block_type.casecmp("CENTER") == 0)
@@ -53,9 +53,6 @@ def initialize(output)
@re_help = RegexpHelper.new
end
- HeadingModes = [:heading1, :heading2, :heading3, :heading4, :heading5, :heading6]
- BlockModes = [:blockquote, :center, :example, :src]
-
def current_mode
@mode_stack.last
end
@@ -79,7 +76,7 @@ def pop_mode(mode = nil)
def prepare(line)
@logger.debug "Looking at #{line.paragraph_type}(#{current_mode}) : #{line.to_s}"
# We try to get the lang from #+BEGIN_SRC blocks
- @block_lang = line.block_lang if line.code_block?
+ @block_lang = line.block_lang if line.begin_block?
if not should_accumulate_output?(line)
flush!
maintain_mode_stack(line)
@@ -135,24 +132,38 @@ def list_indent_level
# Test if we're in an output mode in which whitespace is significant.
def preserve_whitespace?
- mode_is_code current_mode
+ mode_is_code? current_mode
end
######################################################################
private
- def mode_is_code(mode)
- case mode
- when :src, :inline_example, :example
- true
- else
- false
- end
+ def mode_is_heading?(mode)
+ [:heading1, :heading2, :heading3,
+ :heading4, :heading5, :heading6].include? mode
+ end
+
+ def mode_is_block?(mode)
+ [:blockquote, :center, :example, :src].include? mode
+ end
+
+ def mode_is_code?(mode)
+ [:example, :inline_example, :src].include? mode
+ end
+
+ def boundary_of_block?(line)
+ # Boundary of inline example
+ return true if ((line.paragraph_type == :example_line) ^
+ (@output_type == :example_line))
+ # Boundary of begin...end block
+ return true if (@output_type == :begin_block and
+ not mode_is_code?(current_mode))
+ return true if @output_type == :end_block
end
def maintain_mode_stack(line)
# Always close heading line
- pop_mode if HeadingModes.include?(current_mode)
+ pop_mode if mode_is_heading? current_mode
# Always close paragraph mode
pop_mode if current_mode == :paragraph
@@ -165,7 +176,7 @@ def maintain_mode_stack(line)
end
while ((not @list_indent_stack.empty?) and
@list_indent_stack.last == line.indent)
- if BlockModes.include?(current_mode)
+ if mode_is_block? current_mode
# Special case: Only end-block line closes the block
pop_mode if line.end_block?
break
@@ -187,8 +198,8 @@ def maintain_mode_stack(line)
@list_indent_stack.last <= line.indent)
push_mode(line.paragraph_type, line.indent) unless line.begin_block?
end
- else # If blank line, close preceding paragraph
- pop_mode if current_mode == :paragraph
+ else # If blank line, close preceding paragraph or inline example
+ pop_mode if current_mode == :paragraph or current_mode == :inline_example
end
end
@@ -202,14 +213,12 @@ def should_accumulate_output?(line)
# Special case: Assign mode if not yet done.
return false if not current_mode
- # Special case: We are accumulating code block content
- return true if mode_is_code(current_mode) and not line.end_block?
-
- # Special case: Don't accumulate output when block starts or ends
- return false if @output_type == :begin_block or @output_type == :end_block
+ # Special case: Handles accumulating block content
+ return false if boundary_of_block?(line)
+ return true if mode_is_code?(current_mode) and not line.end_block?
# Special case: Don't accumulate headings, comments and horizontal rules.
- return false if (HeadingModes.include?(@output_type) or
+ return false if (mode_is_heading?(@output_type) or
@output_type == :comment or
@output_type == :horizontal_rule)
@@ -218,9 +227,9 @@ def should_accumulate_output?(line)
if line.paragraph_type == :paragraph
# Paragraph gets accumulated only if its indent level is
- # greater than the indent level of the previous modes.
- @list_indent_stack[0..-2].each do |indent|
- return false if line.indent <= indent
+ # greater than the indent level of the previous mode.
+ if (@mode_stack[-2] and not mode_is_block? @mode_stack[-2])
+ return false if line.indent <= @list_indent_stack[-2]
end
# Special case: Multiple "paragraphs" get accumulated.
return true
@@ -276,9 +276,9 @@ def self.translate(lines, output_buffer)
output_buffer << line.output_text << "\n"
- when :inline_example
+ when :example_line
- output_buffer << line.output_text
+ output_buffer << line.output_text << "\n"
else
if output_buffer.preserve_whitespace? then

0 comments on commit 0ed172a

Please sign in to comment.