Skip to content

Commit

Permalink
Implement first version of Markdown exporter
Browse files Browse the repository at this point in the history
  • Loading branch information
wallyqs committed Jan 13, 2014
1 parent a33da76 commit 3d6b233
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 3 deletions.
5 changes: 2 additions & 3 deletions bin/org-ruby
Expand Up @@ -17,7 +17,7 @@ options_parser = OptionParser.new do |opts|
options[:debug] = true
end

opts.on("-t", "--translate FORMAT", [:html, :textile],
opts.on("-t", "--translate FORMAT", [:html, :textile, :markdown],
"Translate the ORG file to the specified format.") do |v|
options[:format] = v
end
Expand All @@ -43,9 +43,8 @@ begin
$DEBUG = true if options[:debug]
puts p.to_html if options[:format] == :html
puts p.to_textile if options[:format] == :textile
puts p.to_markdown if options[:format] == :markdown
end
rescue OptionParser::ParseError
puts options_parser
end


3 changes: 3 additions & 0 deletions lib/org-ruby.rb
Expand Up @@ -15,6 +15,9 @@
require 'org-ruby/textile_output_buffer'
require 'org-ruby/textile_symbol_replace'

# Markdown exporter
require 'org-ruby/markdown_output_buffer'

# Tilt support
require 'org-ruby/tilt'

Expand Down
109 changes: 109 additions & 0 deletions lib/org-ruby/markdown_output_buffer.rb
@@ -0,0 +1,109 @@
require 'stringio'

module Orgmode

class MarkdownOutputBuffer < OutputBuffer

def initialize(output)
super(output)
end

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

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

# Maps org markup to markdown markup.
MarkdownMap = {
"*" => "**",
"/" => "*",
"_" => "*",
"=" => "`",
"~" => "`",
"+" => "~~"
}

# Handles inline formatting for markdown.
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

# TODO: Implement this
def output_footnotes!
return false
end

# Flushes the current buffer
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

def add_line_attributes headline
@output << "#" * headline.level
@output << " "
end
end # class MarkdownOutputBuffer
end # module Orgmode
12 changes: 12 additions & 0 deletions lib/org-ruby/parser.rb
Expand Up @@ -255,6 +255,18 @@ def to_textile
output
end

# Exports the Org mode content into Markdown format
def to_markdown
output = ""
output_buffer = MarkdownOutputBuffer.new(output)

translate(@header_lines, output_buffer)
@headlines.each do |headline|
translate(headline.body_lines, output_buffer)
end
output
end

# Converts the loaded org-mode file to HTML.
def to_html
mark_trees_for_export
Expand Down

0 comments on commit 3d6b233

Please sign in to comment.