Permalink
Browse files

Initial commit.

  • Loading branch information...
0 parents commit cae3fe697535de6bef88284776d481cd48c8784f @dahlia committed Oct 7, 2009
Showing with 431 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +165 −0 LICENSE
  3. +4 −0 README
  4. +17 −0 Rakefile
  5. +9 −0 lib/pandoku.rb
  6. +37 −0 lib/pandoku/document.rb
  7. +54 −0 lib/pandoku/format.rb
  8. +66 −0 lib/pandoku/formats/html.rb
  9. +31 −0 lib/pandoku/formats/markdown.rb
  10. +27 −0 lib/pandoku/formats/odt.rb
  11. +20 −0 pandoku.gemspec
1 .gitignore
@@ -0,0 +1 @@
+doc
165 LICENSE
@@ -0,0 +1,165 @@
+ GNU LESSER GENERAL PUBLIC LICENSE
+ Version 3, 29 June 2007
+
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+
+ This version of the GNU Lesser General Public License incorporates
+the terms and conditions of version 3 of the GNU General Public
+License, supplemented by the additional permissions listed below.
+
+ 0. Additional Definitions.
+
+ As used herein, "this License" refers to version 3 of the GNU Lesser
+General Public License, and the "GNU GPL" refers to version 3 of the GNU
+General Public License.
+
+ "The Library" refers to a covered work governed by this License,
+other than an Application or a Combined Work as defined below.
+
+ An "Application" is any work that makes use of an interface provided
+by the Library, but which is not otherwise based on the Library.
+Defining a subclass of a class defined by the Library is deemed a mode
+of using an interface provided by the Library.
+
+ A "Combined Work" is a work produced by combining or linking an
+Application with the Library. The particular version of the Library
+with which the Combined Work was made is also called the "Linked
+Version".
+
+ The "Minimal Corresponding Source" for a Combined Work means the
+Corresponding Source for the Combined Work, excluding any source code
+for portions of the Combined Work that, considered in isolation, are
+based on the Application, and not on the Linked Version.
+
+ The "Corresponding Application Code" for a Combined Work means the
+object code and/or source code for the Application, including any data
+and utility programs needed for reproducing the Combined Work from the
+Application, but excluding the System Libraries of the Combined Work.
+
+ 1. Exception to Section 3 of the GNU GPL.
+
+ You may convey a covered work under sections 3 and 4 of this License
+without being bound by section 3 of the GNU GPL.
+
+ 2. Conveying Modified Versions.
+
+ If you modify a copy of the Library, and, in your modifications, a
+facility refers to a function or data to be supplied by an Application
+that uses the facility (other than as an argument passed when the
+facility is invoked), then you may convey a copy of the modified
+version:
+
+ a) under this License, provided that you make a good faith effort to
+ ensure that, in the event an Application does not supply the
+ function or data, the facility still operates, and performs
+ whatever part of its purpose remains meaningful, or
+
+ b) under the GNU GPL, with none of the additional permissions of
+ this License applicable to that copy.
+
+ 3. Object Code Incorporating Material from Library Header Files.
+
+ The object code form of an Application may incorporate material from
+a header file that is part of the Library. You may convey such object
+code under terms of your choice, provided that, if the incorporated
+material is not limited to numerical parameters, data structure
+layouts and accessors, or small macros, inline functions and templates
+(ten or fewer lines in length), you do both of the following:
+
+ a) Give prominent notice with each copy of the object code that the
+ Library is used in it and that the Library and its use are
+ covered by this License.
+
+ b) Accompany the object code with a copy of the GNU GPL and this license
+ document.
+
+ 4. Combined Works.
+
+ You may convey a Combined Work under terms of your choice that,
+taken together, effectively do not restrict modification of the
+portions of the Library contained in the Combined Work and reverse
+engineering for debugging such modifications, if you also do each of
+the following:
+
+ a) Give prominent notice with each copy of the Combined Work that
+ the Library is used in it and that the Library and its use are
+ covered by this License.
+
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
+ document.
+
+ c) For a Combined Work that displays copyright notices during
+ execution, include the copyright notice for the Library among
+ these notices, as well as a reference directing the user to the
+ copies of the GNU GPL and this license document.
+
+ d) Do one of the following:
+
+ 0) Convey the Minimal Corresponding Source under the terms of this
+ License, and the Corresponding Application Code in a form
+ suitable for, and under terms that permit, the user to
+ recombine or relink the Application with a modified version of
+ the Linked Version to produce a modified Combined Work, in the
+ manner specified by section 6 of the GNU GPL for conveying
+ Corresponding Source.
+
+ 1) Use a suitable shared library mechanism for linking with the
+ Library. A suitable mechanism is one that (a) uses at run time
+ a copy of the Library already present on the user's computer
+ system, and (b) will operate properly with a modified version
+ of the Library that is interface-compatible with the Linked
+ Version.
+
+ e) Provide Installation Information, but only if you would otherwise
+ be required to provide such information under section 6 of the
+ GNU GPL, and only to the extent that such information is
+ necessary to install and execute a modified version of the
+ Combined Work produced by recombining or relinking the
+ Application with a modified version of the Linked Version. (If
+ you use option 4d0, the Installation Information must accompany
+ the Minimal Corresponding Source and Corresponding Application
+ Code. If you use option 4d1, you must provide the Installation
+ Information in the manner specified by section 6 of the GNU GPL
+ for conveying Corresponding Source.)
+
+ 5. Combined Libraries.
+
+ You may place library facilities that are a work based on the
+Library side by side in a single library together with other library
+facilities that are not Applications and are not covered by this
+License, and convey such a combined library under terms of your
+choice, if you do both of the following:
+
+ a) Accompany the combined library with a copy of the same work based
+ on the Library, uncombined with any other library facilities,
+ conveyed under the terms of this License.
+
+ b) Give prominent notice with the combined library that part of it
+ is a work based on the Library, and explaining where to find the
+ accompanying uncombined form of the same work.
+
+ 6. Revised Versions of the GNU Lesser General Public License.
+
+ The Free Software Foundation may publish revised and/or new versions
+of the GNU Lesser General Public License from time to time. Such new
+versions will be similar in spirit to the present version, but may
+differ in detail to address new problems or concerns.
+
+ Each version is given a distinguishing version number. If the
+Library as you received it specifies that a certain numbered version
+of the GNU Lesser General Public License "or any later version"
+applies to it, you have the option of following the terms and
+conditions either of that published version or of any later version
+published by the Free Software Foundation. If the Library as you
+received it does not specify a version number of the GNU Lesser
+General Public License, you may choose any version of the GNU Lesser
+General Public License ever published by the Free Software Foundation.
+
+ If the Library as you received it specifies that a proxy can decide
+whether future versions of the GNU Lesser General Public License shall
+apply, that proxy's public statement of acceptance of any version is
+permanent authorization for you to choose that version for the
+Library.
4 README
@@ -0,0 +1,4 @@
+Pandoku
+=======
+
+Pandoku is loose Ruby interface for Pandoc, the most powerful markup processor.
17 Rakefile
@@ -0,0 +1,17 @@
+require 'rubygems'
+require 'rake/rdoctask'
+
+task :doc => :rdoc
+
+Rake::RDocTask.new :rdoc do |rdoc|
+ rdoc.rdoc_dir = "doc/html"
+ rdoc.title = "Pandoku: Loose Ruby interface for Pandoc, " \
+ "the most powerful markup processor."
+ rdoc.options << "--inline-source" <<
+ "--title" << rdoc.title <<
+ "--charset" << "UTF-8" <<
+ "--main" << "Pandoku"
+ rdoc.rdoc_files.include('lib/pandoku.rb', 'lib/pandoku/*.rb',
+ 'lib/pandoku/formats/*.rb')
+end
+
9 lib/pandoku.rb
@@ -0,0 +1,9 @@
+require File.dirname(__FILE__) + '/pandoku/document'
+require File.dirname(__FILE__) + '/pandoku/format'
+
+# %w<markdown rst html latex
+# context man mediawiki texinfo docbook opendocument odt s5 rtf>.each do |f|
+%w<markdown html>.each do |f|
+ require "#{File.dirname(__FILE__)}/pandoku/formats/#{f}"
+end
+
37 lib/pandoku/document.rb
@@ -0,0 +1,37 @@
+require File.dirname(__FILE__) + '/format'
+
+module Pandoku
+ PANDOC_PATH = 'pandoc'
+
+ class Document
+ attr_reader :format, :text
+
+ def initialize(format, text)
+ unless format.is_a?(InputFormat)
+ raise TypeError, 'format must be InputFormat'
+ end
+ @format = format
+ @text = text
+ end
+
+ # 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
+ end
+
+ # Compiles the document to given +format+.
+ # 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'
+ end
+ format.compile(self, io)
+ end
+ end
+end
+
54 lib/pandoku/format.rb
@@ -0,0 +1,54 @@
+require 'open3'
+require File.dirname(__FILE__) + '/document'
+
+module Pandoku
+ class Format
+ attr_reader :options
+
+ def self.name
+ raise NotImplementedError,
+ 'Format.name should be overrided to return Symbol'
+ end
+
+ def self.default_options
+ {}
+ end
+
+ def initialize(options = {})
+ @options = self.class.default_options
+ keys = @options.keys
+ @options.merge!(Hash[options.select {|k, v| keys.include?(k) }])
+ 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(' ')
+ end
+ end
+
+ module InputFormat
+ def parse(text)
+ Document.new(self, text)
+ end
+ end
+
+ module OutputFormat
+ # Compiles the given +document+ to the format.
+ # 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)
+ cin.close
+ return cout if io
+ result = cout.read
+ cout.close
+ return result
+ end
+ end
+end
+
66 lib/pandoku/formats/html.rb
@@ -0,0 +1,66 @@
+require File.dirname(File.dirname(__FILE__)) + '/format'
+
+module Pandoku::Formats
+ # HTML format.
+ #
+ # == 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>.
+ class HTML < Pandoku::Format
+ include Pandoku::InputFormat, Pandoku::OutputFormat
+
+ def self.name
+ :html
+ end
+
+ def self.default_options
+ { :standalone => false, :parse_raw => false, :latexmathml => nil,
+ :jsmath => nil, :gladtex => false, :mimetex => nil, :no_wrap => false,
+ :sanitize_html => false, :email_obfuscation => false, :toc => false,
+ :css => nil, :include_in_header => nil, :include_before_body => nil,
+ :include_after_body => nil, :custom_header => nil }
+ end
+ end
+end
31 lib/pandoku/formats/markdown.rb
@@ -0,0 +1,31 @@
+require File.dirname(File.dirname(__FILE__)) + '/format'
+
+module Pandoku::Formats
+ # Markdown format. http://daringfireball.net/projects/markdown/
+ #
+ # == 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.
+ class Markdown < Pandoku::Format
+ include Pandoku::InputFormat, Pandoku::OutputFormat
+
+ def self.name
+ :markdown
+ end
+
+ def self.default_options
+ { :strict => false, :reference_links => false, :smart => false,
+ :no_wrap => false, :sanitize_html => false, :toc => false }
+ end
+ end
+end
27 lib/pandoku/formats/odt.rb
@@ -0,0 +1,27 @@
+require File.dirname(File.dirname(__FILE__)) + '/format'
+
+module Pandoku::Formats
+ # ODT (OpenOffice text document) format.
+ class ODT
+ include OutputFormat
+
+ def self.name
+ :odt
+ end
+
+ def cliopts
+ @tmpfile = @tmpfile || self._tmpfile
+ "--output=#{@tmpfile} #{super.cliopts}"
+ end
+
+ def compile(document)
+ @tmpfile = self._tmpfile
+ end
+
+ private
+
+ def _tmpfile
+ Dir.tmpdir
+ end
+ end
+end
20 pandoku.gemspec
@@ -0,0 +1,20 @@
+require 'rubygems'
+require 'pathname'
+
+Gem::Specification.new do |s|
+ s.name = 'pandoku'
+ s.version = '0.1'
+ s.homepage = 'http://github.com/dahlia/pandoku'
+ s.summary = s.description = 'Loose Ruby interface for Pandoc, ' \
+ 'the most powerful markup processor.'
+ s.author = 'Hong, MinHee'
+ s.email = 'minh' 'ee@da' 'hlia.kr'
+
+ s.files = Pathname.glob("lib/**/*").map(&:to_s)
+ s.files << 'Rakefile'
+
+ s.require_path = 'lib'
+
+ s.has_rdoc = true
+end
+

0 comments on commit cae3fe6

Please sign in to comment.