Class: Orgmode::HtmlOutputBuffer

Inherits:
OutputBuffer show all
Defined in:
lib/org-ruby/html_output_buffer.rb

Constant Summary collapse

HtmlBlockTag =
{
  :paragraph => "p",
  :ordered_list => "li",
  :unordered_list => "li",
  :definition_term => "dt",
  :definition_descr => "dd",
  :table_row => "tr",
  :table_header => "tr",
  :heading1 => "h1",
  :heading2 => "h2",
  :heading3 => "h3",
  :heading4 => "h4",
  :heading5 => "h5",
  :heading6 => "h6"
}
ModeTag =
{
  :unordered_list => "ul",
  :ordered_list => "ol",
  :definition_list => "dl",
  :table => "table",
  :blockquote => "blockquote",
  :example => "pre",
  :src => "pre",
  :inline_example => "pre",
  :center => "div"
}

Constants inherited from OutputBuffer

OutputBuffer::Modes

Instance Attribute Summary collapse

Attributes inherited from OutputBuffer

#buffer, #buffer_mode, #buffered_lines, #headline_number_stack, #output, #output_type

Instance Method Summary collapse

Methods inherited from OutputBuffer

#<<, #clear_accumulation_buffer!, #current_mode, #current_mode_list?, #enter_table?, #exit_table?, #get_next_headline_number, #list_indent_level, #prepare, #preserve_whitespace?

Constructor Details

#initialize(output, opts = {}) ⇒ HtmlOutputBuffer

Returns a new instance of HtmlOutputBuffer.



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/org-ruby/html_output_buffer.rb', line 46

def initialize(output, opts = {})
  super(output)
  if opts[:decorate_title] then
    @title_decoration = " class=\"title\""
  else
    @title_decoration = ""
  end
  @options = opts
  @footnotes = {}
  @unclosed_tags = []
  @logger.debug "HTML export options: #{@options.inspect}"
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



44
45
46
# File 'lib/org-ruby/html_output_buffer.rb', line 44

def options
  @options
end

Instance Method Details

#flush!Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/org-ruby/html_output_buffer.rb', line 107

def flush!
  if buffer_mode_is_src_block?

    # 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 not @block_lang.empty? and (defined? Pygments or 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
        begin
          @buffer = Pygments.highlight(@buffer, :lexer => lang)
        rescue ::RubyPython::PythonError
          # 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
        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.
    @logger.debug "FLUSH CODE ==========> #{@buffer.inspect}"
    @output << @buffer << "\n"
  else
    escape_buffer!
    if @buffer.length > 0 and @output_type == :horizontal_rule then
      @output << "<hr />\n"
    elsif @buffer.length > 0 and @output_type == :definition_list then
      unless buffer_mode_is_table? and skip_tables?
        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"
        end
        @title_decoration = ""
      end
    elsif @buffer.length > 0 then
      unless buffer_mode_is_table? and skip_tables?
        @logger.debug "FLUSH      ==========> #{@buffer_mode}"
        output_indentation
        if ((@buffered_lines[0].plain_list?) and 
            (@unclosed_tags.count == @list_indent_stack.count))
          @output << @unclosed_tags.pop
          output_indentation
        end
        @output << "<#{HtmlBlockTag[@output_type]}#{@title_decoration}>"
        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)

        # Only close the list when it is the last element from that list,
        # or when starting another list
        if (@output_type == :unordered_list or 
            @output_type == :ordered_list   or
            @output_type == :definition_list) and 
            (not @list_indent_stack.empty?)
          @unclosed_tags.push("</#{HtmlBlockTag[@output_type]}>\n")
          @output << "\n"
        else
          @output << "</#{HtmlBlockTag[@output_type]}>\n"
        end
        @title_decoration = ""
      else
        @logger.debug "SKIP       ==========> #{@buffer_mode}"
      end
    end
  end
  clear_accumulation_buffer!
end

#output_footnotes!Object



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/org-ruby/html_output_buffer.rb', line 210

def output_footnotes!
  return false unless @options[:export_footnotes] and not @footnotes.empty?

  @output << "<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>" \
            << inline_formatting(defi) \
            << "</p>\n"
  end

  @output << "</div>\n</div>\n"

  return true
end

#pop_mode(mode = nil) ⇒ Object

We are leaving a mode. Close any tags that were opened when entering this mode.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/org-ruby/html_output_buffer.rb', line 84

def pop_mode(mode = nil)
  m = super(mode)
  if ModeTag[m] then
    output_indentation
    # Need to close the floating li elements before closing the list
    if (m == :unordered_list or
        m == :ordered_list or
        m == :definition_list) and 
        (not @unclosed_tags.empty?)
      close_floating_li_tags
    end

    unless ((mode == :table and skip_tables?) or
            (mode == :src and defined? Pygments))
      @logger.debug "</#{ModeTag[m]}>\n"
      @output << "</#{ModeTag[m]}>\n"
    end

    # In case it was a sublist, close it here
    close_last_li_tag_maybe
  end
end

#push_mode(mode) ⇒ Object

Output buffer is entering a new mode. Use this opportunity to write out one of the block tags in the ModeTag constant to put this information in the HTML stream.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/org-ruby/html_output_buffer.rb', line 62

def push_mode(mode)
  if ModeTag[mode] then
    output_indentation
    css_class = ""
    css_class = " class=\"src\"" if mode == :src and @block_lang.empty?
    css_class = " class=\"src src-#{@block_lang}\"" if mode == :src and not @block_lang.empty?
    css_class = " class=\"example\"" if (mode == :example || mode == :inline_example)
    css_class = " style=\"text-align: center\"" if mode == :center

    unless ((mode == :table and skip_tables?) or
            (mode == :src and defined? Pygments))
      @logger.debug "#{mode}: <#{ModeTag[mode]}#{css_class}>\n"
      @output << "<#{ModeTag[mode]}#{css_class}>\n"
    end
    # Entering a new mode obliterates the title decoration
    @title_decoration = ""
  end
  super(mode)
end