Class: Orgmode::HtmlOutputBuffer

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

Constant Summary collapse

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

Instance Attribute Summary collapse

Attributes inherited from OutputBuffer

#output, #output_type

Instance Method Summary collapse

Methods inherited from OutputBuffer

#current_mode, #get_next_headline_number, #insert, #list_indent_level

Constructor Details

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

Returns a new instance of HtmlOutputBuffer.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/org-ruby/html_output_buffer.rb', line 41

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

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



39
40
41
# File 'lib/org-ruby/html_output_buffer.rb', line 39

def options
  @options
end

Instance Method Details

#add_line_attributes(headline) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
# File 'lib/org-ruby/html_output_buffer.rb', line 186

def add_line_attributes headline
  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

#flush!Object



106
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
# File 'lib/org-ruby/html_output_buffer.rb', line 106

def flush!
  return false if @buffer.empty?
  case
  when preserve_whitespace?
    strip_code_block! if mode_is_code? current_mode

    # NOTE: CodeRay and Pygments already escape the html once, so
    # no need to escape_string!(@buffer)
    case
    when (current_mode == :src and defined? Pygments)
      lang = normalize_lang @block_lang
      @output << "\n" unless @new_paragraph == :start

      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

      # 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
    when (current_mode == :html or current_mode == :raw_text)
      @buffer.gsub!(/\A\n/, "") if @new_paragraph == :start
      @new_paragraph = true
    else
      escape_string! @buffer
    end

    # 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

  when (mode_is_table? current_mode and skip_tables?)
    @logger.debug "SKIP       ==========> #{current_mode}"

  else
    @buffer.lstrip!
    @new_paragraph = nil
    @logger.debug "FLUSH      ==========> #{current_mode}"

    case current_mode
    when :definition_term
      d = @buffer.split(/\A(.*[ \t]+|)::(|[ \t]+.*?)$/, 4)
      d[1] = d[1].strip
      unless d[1].empty?
        @output << inline_formatting(d[1])
      else
        @output << "???"
      end
      indent = @list_indent_stack.last
      pop_mode

      @new_paragraph = :start
      push_mode(:definition_descr, indent)
      @output << inline_formatting(d[2].strip + d[3])
      @new_paragraph = nil

    when :horizontal_rule
      add_paragraph unless @new_paragraph == :start
      @new_paragraph = true
      @output << "<hr />"

    else
      @output << inline_formatting(@buffer)
    end
  end
  @buffer = ""
end

#output_footnotes!Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/org-ruby/html_output_buffer.rb', line 198

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

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

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

  return true
end

#pop_mode(mode = nil) ⇒ Object

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



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/org-ruby/html_output_buffer.rb', line 92

def pop_mode(mode = nil)
  m = super(mode)
  if HtmlBlockTag[m]
    unless ((mode_is_table?(m) and skip_tables?) or
            (m == :src and defined? Pygments))
      add_paragraph if @new_paragraph
      @new_paragraph = true
      @logger.debug "</#{HtmlBlockTag[m]}>"
      @output << "</#{HtmlBlockTag[m]}>"
    end
  end
  @list_indent_stack.pop
end

#preserve_whitespace?Boolean

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

Returns:

  • (Boolean)


215
216
217
# File 'lib/org-ruby/html_output_buffer.rb', line 215

def preserve_whitespace?
  super or current_mode == :html
end

#push_mode(mode, indent) ⇒ Object

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.



59
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
# File 'lib/org-ruby/html_output_buffer.rb', line 59

def push_mode(mode, indent)
  super(mode)
  @list_indent_stack.push(indent)

  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

      add_paragraph unless @new_paragraph == :start
      @new_paragraph = true

      @logger.debug "#{mode}: <#{HtmlBlockTag[mode]}#{css_class}>"
      @output << "<#{HtmlBlockTag[mode]}#{css_class}>"
      # Entering a new mode obliterates the title decoration
      @title_decoration = ""
    end
  end
end