Permalink
Browse files

Added follosing formats: rst (reStructuredText), odt (OpenOffice text…

… document).
  • Loading branch information...
1 parent a09f83d commit bd8f5aa1790f1f96ab41263a52fe27ab6fbb21bd @dahlia dahlia committed Oct 8, 2009
Showing with 109 additions and 81 deletions.
  1. +1 −0 .gitignore
  2. +1 −2 README
  3. +1 −1 lib/pandoku.rb
  4. +10 −8 lib/pandoku/document.rb
  5. +4 −8 lib/pandoku/format.rb
  6. +47 −43 lib/pandoku/formats/html.rb
  7. +11 −11 lib/pandoku/formats/markdown.rb
  8. +33 −7 lib/pandoku/formats/odt.rb
  9. +1 −1 pandoku.gemspec
View
@@ -1 +1,2 @@
doc
+*.gem
View
3 README
@@ -1,4 +1,3 @@
-Pandoku
-=======
+= Pandoku
Pandoku is loose Ruby interface for Pandoc, the most powerful markup processor.
View
@@ -3,7 +3,7 @@
# %w<markdown rst html latex
# context man mediawiki texinfo docbook opendocument odt s5 rtf>.each do |f|
-%w<markdown html>.each do |f|
+%w<markdown rst html odt>.each do |f|
require "#{File.dirname(__FILE__)}/pandoku/formats/#{f}"
end
View
@@ -1,7 +1,7 @@
require File.dirname(__FILE__) + '/format'
module Pandoku
- PANDOC_PATH = 'pandoc'
+ PANDOC_PATH = ENV['PANDOC_PATH'] || 'pandoc'
class Document
attr_reader :format, :text
@@ -16,16 +16,18 @@ def initialize(format, text)
# Makes a command string to execute Pandoc.
def command_for(format)
- <<-CMD
- #{PANDOC_PATH} -f #{self.format.class.name}
- -t #{format.class.name}
- #{self.format.cliopts}
- #{format.cliopts}
- CMD
+ commands = [PANDOC_PATH, '-f', self.format.class.name,
+ '-t', format.class.name]
+ commands += self.format.cliopts
+ commands += format.cliopts
+ escapeshellarg = lambda do |arg|
+ "'" + arg.to_s.gsub(/[^\\]'/) {|s| %<#{s.chars.first}\\'> } + "'"
+ end
+ commands.select {|v| v }.collect(&escapeshellarg).join(' ')
end
# Compiles the document to given +format+.
- # If a second argument +io+ is true, returns +IO+ instead of +String+.
+ # If a second argument +io+ is +true+, returns +IO+ instead of +String+.
def compile(format, io = false)
unless format.is_a?(OutputFormat)
raise TypeError, 'format must be OutputFormat'
View
@@ -21,13 +21,9 @@ def initialize(options = {})
end
def cliopts
- escapeshellarg = lambda do |arg|
- "'" + arg.gsub(/[^\\]'/) {|s| %<#{s.chars.first}\\'> } + "'"
- end
- @options.select {|k, v| v } \
- .collect {|p| %<--#{p[0].gsub('_', '-')}> +
- (p[1] != true ? %<=#{p[1]}> : '') } \
- .join(' ')
+ self.options.select {|k, v| v } \
+ .collect {|p| %<--#{p[0].to_s.gsub('_', '-')}> +
+ (p[1] != true ? %<=#{p[1]}> : '') }
end
end
@@ -39,7 +35,7 @@ def parse(text)
module OutputFormat
# Compiles the given +document+ to the format.
- # If a second argument +io+ is true, returns +IO+ instead of +String+.
+ # If a second argument +io+ is +true+, returns +IO+ instead of +String+.
def compile(document, io = false)
cin, cout, cerr = Open3.popen3(document.command_for(self))
cin.print(document.text)
@@ -5,49 +5,53 @@ module Pandoku::Formats
#
# == Available Options
#
- # * <tt>:standalone</tt> - Produce output with an appropriate header
- # and footer.
- # * <tt>:parse_raw</tt> - Parse untranslatable HTML codes as raw HTML,
- # instead of ignoring them.
- # * <tt>latexmathml</tt> - Use LaTeXMathML to display embedded TeX math in
- # HTML output. To insert a link to a local copy of the <b>LaTeXMathML.js</b>
- # script, provide a URL string. If just <tt>true</tt> is provided, the
- # contents of the script will be inserted directly into the HTML header.
- # * <tt>:jsmath</tt> - Use jsMath to display embedded TeX math in HTML output.
- # The URL should point to the jsMath load script; if provided,
- # it will be linked to in the header of standalone HTML documents.
- # * <tt>:gladtex</tt> - Enclose TeX math in +<eq>+ tags in HTML output.
- # These can then be processed by gladTeX to produce links
- # to images of the typeset formulas.
- # * <tt>:mimetex</tt> - Render TeX math using the mimeTeX CGI script.
- # If URL is not specified, it is assumed that the script is at
- # <b>/cgi-bin/mimetex.cgi</b>.
- # * <tt>:no_wrap</tt> - Disable text wrapping in output.
- # (Default is to wrap text.)
- # * <tt>:sanitize_html</tt> - Sanitizes HTML using a whitelist.
- # Unsafe tags are replaced by HTML comments; unsafe attributes are omitted.
- # URIs in links and images are also checked against a whitelist of URI
- # schemes.
- # * <tt>:email_obfuscation</tt> -
- # (<tt>nil</tt>|<tt>:javascript</tt>|<tt>:references</tt>)
- # Specify a method for obfuscating <tt>mailto:</tt> links in HTML documents.
- # * <tt>:nil</tt> leaves <tt>mailto:</tt> links as they are.
- # * <tt>:javascript</tt> obfuscates them using javascript.
- # * <tt>:references</tt> obfuscates them by printing their letters
- # as decimal or hexadecimal character references.
- # If <tt>:strict => true</tt> is specified, references is used regardless
- # of the presence of this option.
- # * <tt>:toc</tt> - Include an automatically generated table of contents.
- # * <tt>:css</tt> - Link to a CSS style sheet.
- # The pathname of the style sheet goes value.
- # * <tt>:include_in_header</tt> - Include contents of the given filename
- # at the end of the header. Implies <tt>:standalone</tt>.
- # * <tt>:include_before_body</tt> - Include contents of the given filename
- # at the beginning of the document body.
- # * <tt>:include_after_body</tt> - Include contents of the given filename
- # at the end of the document body.
- # * <tt>:custom_header</tt> - Use contents of the given filename as the
- # document header. Implies <tt>:standalone</tt>.
+ # <tt>:standalone</tt>:: Produce output with an appropriate header and footer.
+ # <tt>:parse_raw</tt>:: Parse untranslatable HTML codes as raw HTML,
+ # instead of ignoring them.
+ # <tt>latexmathml</tt>::
+ # Use LaTeXMathML to display embedded TeX math in HTML output.
+ # To insert a link to a local copy of the <b>LaTeXMathML.js</b> script,
+ # provide a URL string. If just <tt>true</tt> is provided, the contents
+ # of the script will be inserted directly into the HTML header.
+ # <tt>:jsmath</tt>::
+ # Use jsMath to display embedded TeX math in HTML output.
+ # The URL should point to the jsMath load script; if provided,
+ # it will be linked to in the header of standalone HTML documents.
+ # <tt>:gladtex</tt>::
+ # Enclose TeX math in +<eq>+ tags in HTML output.
+ # These can then be processed by gladTeX to produce links
+ # to images of the typeset formulas.
+ # <tt>:mimetex</tt>::
+ # Render TeX math using the mimeTeX CGI script. If URL is not specified,
+ # it is assumed that the script is at <b>/cgi-bin/mimetex.cgi</b>.
+ # <tt>:no_wrap</tt>:: Disable text wrapping in output.
+ # (Default is to wrap text.)
+ # <tt>:sanitize_html</tt>::
+ # Sanitizes HTML using a whitelist. Unsafe tags are replaced by
+ # HTML comments; unsafe attributes are omitted. URIs in links and
+ # images are also checked against a whitelist of URI schemes.
+ # <tt>:email_obfuscation</tt> <i>(<tt>nil</tt>|<tt>:javascript</tt>|<tt>:references</tt>)</i>::
+ # Specify a method for obfuscating <tt>mailto:</tt> links
+ # in HTML documents.
+ # * <tt>:nil</tt> leaves <tt>mailto:</tt> links as they are.
+ # * <tt>:javascript</tt> obfuscates them using javascript.
+ # * <tt>:references</tt> obfuscates them by printing their letters
+ # as decimal or hexadecimal character references.
+ # If <tt>:strict => true</tt> is specified, references is
+ # used regardless of the presence of this option.
+ # <tt>:toc</tt>:: Include an automatically generated table of contents.
+ # <tt>:css</tt>:: Link to a CSS style sheet.
+ # The pathname of the style sheet goes value.
+ # <tt>:include_in_header</tt>::
+ # Include contents of the given filename at the end of the header.
+ # Implies <tt>:standalone</tt>.
+ # <tt>:include_before_body</tt>:: Include contents of the given filename
+ # at the beginning of the document body.
+ # <tt>:include_after_body</tt>:: Include contents of the given filename
+ # at the end of the document body.
+ # <tt>:custom_header</tt>::
+ # Use contents of the given filename as the document header.
+ # Implies <tt>:standalone</tt>.
class HTML < Pandoku::Format
include Pandoku::InputFormat, Pandoku::OutputFormat
@@ -5,17 +5,17 @@ module Pandoku::Formats
#
# == Available Options
#
- # * <tt>:strict</tt> - Use strict syntax, with no extensions or variants.
- # * <tt>:reference_links</tt> - Use reference-style links,
- # rather than inline links, in writing.
- # * <tt>:smart</tt> - Use smart quotes, dashes, and ellipses.
- # * <tt>:no_wrap</tt> - Disable text wrapping in output.
- # (Default is to wrap text.)
- # * <tt>:sanitize_html</tt> - Sanitizes HTML using a whitelist.
- # Unsafe tags are replaced by HTML comments; unsafe attributes are omitted.
- # URIs in links and images are also checked against a whitelist of URI
- # schemes.
- # * <tt>:toc</tt> - Include an automatically generated table of contents.
+ # <tt>:strict</tt>:: Use strict syntax, with no extensions or variants.
+ # <tt>:reference_links</tt>:: Use reference-style links,
+ # rather than inline links, in writing.
+ # <tt>:smart</tt>:: Use smart quotes, dashes, and ellipses.
+ # <tt>:no_wrap</tt>:: Disable text wrapping in output.
+ # (Default is to wrap text.)
+ # <tt>:sanitize_html</tt>::
+ # Sanitizes HTML using a whitelist. Unsafe tags are replaced by
+ # HTML comments; unsafe attributes are omitted. URIs in links and
+ # images are also checked against a whitelist of URI schemes.
+ # <tt>:toc</tt>:: Include an automatically generated table of contents.
class Markdown < Pandoku::Format
include Pandoku::InputFormat, Pandoku::OutputFormat
View
@@ -1,27 +1,53 @@
+require 'tempfile'
require File.dirname(File.dirname(__FILE__)) + '/format'
module Pandoku::Formats
# ODT (OpenOffice text document) format.
- class ODT
- include OutputFormat
+ #
+ # == Available Options
+ #
+ # <tt>:toc</tt>:: Include an automatically generated table of contents.
+ class ODT < Pandoku::Format
+ include Pandoku::OutputFormat
def self.name
:odt
end
+ def self.default_options
+ { :toc => false }
+ end
+
def cliopts
- @tmpfile = @tmpfile || self._tmpfile
- "--output=#{@tmpfile} #{super.cliopts}"
+ @tmpfile = @tmpfile || _tmpfile
+ super + ["--output=#{@tmpfile}"]
end
- def compile(document)
- @tmpfile = self._tmpfile
+ # Compiles the given +document+ to the format.
+ # If a second argument +file+ is +true+, returns +File+ instead of +String+.
+ #
+ # *Note* that it creates a temporary file for the result.
+ # If +file+ is +false+, the temporary file become deleted.
+ def compile(document, file = false)
+ @tmpfile = _tmpfile
+ ps = IO.popen(document.command_for(self), 'w')
+ ps.print(document.text)
+ ps.close
+ f = File.new(@tmpfile, 'r')
+ return f if file
+ result = f.read
+ f.close
+ File.unlink(@tmpfile)
+ return result
end
private
def _tmpfile
- Dir.tmpdir
+ f = Tempfile.new('pandoku')
+ path = f.path
+ f.close(false)
+ path
end
end
end
View
@@ -3,7 +3,7 @@ require 'pathname'
Gem::Specification.new do |s|
s.name = 'pandoku'
- s.version = '0.1'
+ s.version = '0.2'
s.homepage = 'http://github.com/dahlia/pandoku'
s.summary = s.description = 'Loose Ruby interface for Pandoc, ' \
'the most powerful markup processor.'

0 comments on commit bd8f5aa

Please sign in to comment.