Class: Orgmode::MarkdownOutputBuffer

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

Constant Summary collapse

MarkdownMap =

Maps org markup to markdown markup.

{
  "*" => "**",
  "/" => "*",
  "_" => "*",
  "=" => "`",
  "~" => "`",
  "+" => "~~"
}

Instance Attribute Summary

Attributes inherited from OutputBuffer

#output, #output_type

Instance Method Summary collapse

Methods inherited from OutputBuffer

#current_mode, #do_custom_markup, #get_next_headline_number, #insert, #list_indent_level, #load_custom_markup, #no_custom_markup_file_exists, #no_valid_markup_found, #preserve_whitespace?, #set_custom_markup

Constructor Details

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

Returns a new instance of MarkdownOutputBuffer.



7
8
9
10
11
12
13
14
15
16
# File 'lib/org-ruby/markdown_output_buffer.rb', line 7

def initialize(output, opts = {})
  super(output)
  @options = opts
  @logger.debug "Markdown export options: #{@options.inspect}"
  @custom_blocktags = {} if @options[:markup_file]

  if @options[:markup_file]
    do_custom_markup
  end
end

Instance Method Details

#add_line_attributes(headline) ⇒ Object



111
112
113
114
# File 'lib/org-ruby/markdown_output_buffer.rb', line 111

def add_line_attributes headline
  @output << "#" * headline.level
  @output << " "
end

#flush!Object

Flushes the current buffer



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

def flush!
  return false if @buffer.empty? and @output_type != :blank
  @logger.debug "FLUSH ==========> #{@output_type}"
  @buffer.gsub!(/\A\n*/, "")

  case
  when mode_is_code?(current_mode)
    @output << "```#{@block_lang}\n"
    @output << @buffer << "\n"
    @output << "```\n"
  when preserve_whitespace?
    @output << @buffer << "\n"

  when @output_type == :blank
    @output << "\n"

  else
    case current_mode
    when :paragraph
      @output << "> " if @mode_stack[0] == :quote

    when :list_item
      @output << " " * @mode_stack.count(:list_item) << "* "

    when :horizontal_rule
      @output << "---"

    end
    @output << inline_formatting(@buffer) << "\n"
  end
  @buffer = ""
end

#inline_formatting(input) ⇒ Object

Handles inline formatting for markdown.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/org-ruby/markdown_output_buffer.rb', line 39

def inline_formatting(input)
  @re_help.rewrite_emphasis input do |marker, body|
    m = MarkdownMap[marker]
    "#{m}#{body}#{m}"
  end
  @re_help.rewrite_subp input do |type, text|
    if type == "_" then
      "<sub>#{text}</sub>"
    elsif type == "^" then
      "<sup>#{text}</sup>"
    end
  end
  @re_help.rewrite_links input do |link, defi|
    # We don't add a description for images in links, because its
    # empty value forces the image to be inlined.
    defi ||= link unless link =~ @re_help.org_image_file_regexp
    link = link.gsub(/ /, "%%20")

    if defi =~ @re_help.org_image_file_regexp
      "![#{defi}](#{defi})"
    elsif defi
      "[#{defi}](#{link})"
    else
      "[#{link}](#{link})"
    end
  end

  # Just reuse Textile special symbols for now?
  Orgmode.special_symbols_to_textile(input)
  input = @re_help.restore_code_snippets input
  input
end

#output_footnotes!Object

TODO: Implement this



73
74
75
# File 'lib/org-ruby/markdown_output_buffer.rb', line 73

def output_footnotes!
  return false
end

#pop_mode(mode = nil) ⇒ Object



22
23
24
25
26
# File 'lib/org-ruby/markdown_output_buffer.rb', line 22

def pop_mode(mode = nil)
  m = super(mode)
  @list_indent_stack.pop
  m
end

#push_mode(mode, indent) ⇒ Object



18
19
20
# File 'lib/org-ruby/markdown_output_buffer.rb', line 18

def push_mode(mode, indent)
  super(mode, indent)
end