Class: Orgmode::OutputBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/org-ruby/output_buffer.rb

Overview

The OutputBuffer is used to accumulate multiple lines of orgmode text, and then emit them to the output all in one go. The class will do the final textile substitution for inline formatting and add a newline character prior emitting the output.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output) ⇒ OutputBuffer

Creates a new OutputBuffer object that is bound to an output object. The output will get flushed to =output=.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/org-ruby/output_buffer.rb', line 19

def initialize(output)
  # This is the accumulation buffer. It's a holding pen so
  # consecutive lines of the right type can get stuck together
  # without intervening newlines.
  @buffer = ""

  # This stack is used to do proper outline numbering of
  # headlines.
  @headline_number_stack = []

  @output = output
  @output_type = :start
  @list_indent_stack = []
  @mode_stack = []
  @code_block_indent = nil

  @logger = Logger.new(STDERR)
  if ENV['DEBUG'] or $DEBUG
    @logger.level = Logger::DEBUG
  else
    @logger.level = Logger::WARN
  end

  @re_help = RegexpHelper.new
end

Instance Attribute Details

#outputObject (readonly)

This is the overall output buffer



12
13
14
# File 'lib/org-ruby/output_buffer.rb', line 12

def output
  @output
end

#output_typeObject

This is the current type of output being accumulated.



15
16
17
# File 'lib/org-ruby/output_buffer.rb', line 15

def output_type
  @output_type
end

Instance Method Details

#current_modeObject



45
46
47
# File 'lib/org-ruby/output_buffer.rb', line 45

def current_mode
  @mode_stack.last
end

#get_next_headline_number(level) ⇒ Object

Gets the next headline number for a given level. The intent is this function is called sequentially for each headline that needs to get numbered. It does standard outline numbering.



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/org-ruby/output_buffer.rb', line 115

def get_next_headline_number(level)
  raise "Headline level not valid: #{level}" if level <= 0
  while level > @headline_number_stack.length do
    @headline_number_stack.push 0
  end
  while level < @headline_number_stack.length do
    @headline_number_stack.pop
  end
  raise "Oops, shouldn't happen" unless level == @headline_number_stack.length
  @headline_number_stack[@headline_number_stack.length - 1] += 1
  @headline_number_stack.join(".")
end

#insert(line) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/org-ruby/output_buffer.rb', line 60

def insert(line)
  # Prepares the output buffer to receive content from a line.
  # As a side effect, this may flush the current accumulated text.
  @logger.debug "Looking at #{line.paragraph_type}|#{line.assigned_paragraph_type}(#{current_mode}) : #{line.to_s}"

  # We try to get the lang from #+BEGIN_SRC blocks
  @block_lang = line.block_lang if line.begin_block?
  unless should_accumulate_output?(line)
    flush!
    maintain_mode_stack(line)
  end

  # Adds the current line to the output buffer
  case
  when line.assigned_paragraph_type == :comment
    # Don't add to buffer
  when line.title?
    @buffer << line.output_text
  when line.raw_text?
    @buffer << "\n" << line.output_text if line.raw_text_tag == @buffer_tag
  when preserve_whitespace?
    @buffer << "\n" << line.output_text unless line.block_type
  when line.assigned_paragraph_type == :code
    # If the line is contained within a code block but we should
    # not preserve whitespaces, then we do nothing.
  when (line.kind_of? Headline)
    add_line_attributes line
    @buffer << "\n" << line.output_text.strip
  when ([:definition_term, :list_item, :table_row, :table_header,
         :horizontal_rule].include? line.paragraph_type)
    @buffer << "\n" << line.output_text.strip
  when line.paragraph_type == :paragraph
    @buffer << "\n"
    buffer_indentation
    @buffer << line.output_text.strip
  end

  if mode_is_code? current_mode and not line.block_type
    # Determines the amount of whitespaces to be stripped at the
    # beginning of each line in code block.
    if line.paragraph_type != :blank
      if @code_block_indent
        @code_block_indent = [@code_block_indent, line.indent].min
      else
        @code_block_indent = line.indent
      end
    end
  end

  @output_type = line.assigned_paragraph_type || line.paragraph_type
end

#list_indent_levelObject

Gets the current list indent level.



129
130
131
# File 'lib/org-ruby/output_buffer.rb', line 129

def list_indent_level
  @list_indent_stack.length
end

#pop_mode(mode = nil) ⇒ Object



54
55
56
57
58
# File 'lib/org-ruby/output_buffer.rb', line 54

def pop_mode(mode = nil)
  m = @mode_stack.pop
  @logger.warn "Modes don't match. Expected to pop #{mode}, but popped #{m}" if mode && mode != m
  m
end

#preserve_whitespace?Boolean

Test if we’re in an output mode in which whitespace is significant.

Returns:

  • (Boolean)


134
135
136
# File 'lib/org-ruby/output_buffer.rb', line 134

def preserve_whitespace?
  [:example, :inline_example, :raw_text, :src].include? current_mode
end

#push_mode(mode, indent) ⇒ Object



49
50
51
52
# File 'lib/org-ruby/output_buffer.rb', line 49

def push_mode(mode, indent)
  @mode_stack.push(mode)
  @list_indent_stack.push(indent)
end