pandoku 0.1 → 0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,7 +3,7 @@ require File.dirname(__FILE__) + '/pandoku/format'
3
3
 
4
4
  # %w<markdown rst html latex
5
5
  # context man mediawiki texinfo docbook opendocument odt s5 rtf>.each do |f|
6
- %w<markdown html>.each do |f|
6
+ %w<markdown rst html odt>.each do |f|
7
7
  require "#{File.dirname(__FILE__)}/pandoku/formats/#{f}"
8
8
  end
9
9
 
@@ -1,7 +1,7 @@
1
1
  require File.dirname(__FILE__) + '/format'
2
2
 
3
3
  module Pandoku
4
- PANDOC_PATH = 'pandoc'
4
+ PANDOC_PATH = ENV['PANDOC_PATH'] || 'pandoc'
5
5
 
6
6
  class Document
7
7
  attr_reader :format, :text
@@ -16,16 +16,18 @@ module Pandoku
16
16
 
17
17
  # Makes a command string to execute Pandoc.
18
18
  def command_for(format)
19
- <<-CMD
20
- #{PANDOC_PATH} -f #{self.format.class.name}
21
- -t #{format.class.name}
22
- #{self.format.cliopts}
23
- #{format.cliopts}
24
- CMD
19
+ commands = [PANDOC_PATH, '-f', self.format.class.name,
20
+ '-t', format.class.name]
21
+ commands += self.format.cliopts
22
+ commands += format.cliopts
23
+ escapeshellarg = lambda do |arg|
24
+ "'" + arg.to_s.gsub(/[^\\]'/) {|s| %<#{s.chars.first}\\'> } + "'"
25
+ end
26
+ commands.select {|v| v }.collect(&escapeshellarg).join(' ')
25
27
  end
26
28
 
27
29
  # Compiles the document to given +format+.
28
- # If a second argument +io+ is true, returns +IO+ instead of +String+.
30
+ # If a second argument +io+ is +true+, returns +IO+ instead of +String+.
29
31
  def compile(format, io = false)
30
32
  unless format.is_a?(OutputFormat)
31
33
  raise TypeError, 'format must be OutputFormat'
@@ -21,13 +21,9 @@ module Pandoku
21
21
  end
22
22
 
23
23
  def cliopts
24
- escapeshellarg = lambda do |arg|
25
- "'" + arg.gsub(/[^\\]'/) {|s| %<#{s.chars.first}\\'> } + "'"
26
- end
27
- @options.select {|k, v| v } \
28
- .collect {|p| %<--#{p[0].gsub('_', '-')}> +
29
- (p[1] != true ? %<=#{p[1]}> : '') } \
30
- .join(' ')
24
+ self.options.select {|k, v| v } \
25
+ .collect {|p| %<--#{p[0].to_s.gsub('_', '-')}> +
26
+ (p[1] != true ? %<=#{p[1]}> : '') }
31
27
  end
32
28
  end
33
29
 
@@ -39,7 +35,7 @@ module Pandoku
39
35
 
40
36
  module OutputFormat
41
37
  # Compiles the given +document+ to the format.
42
- # If a second argument +io+ is true, returns +IO+ instead of +String+.
38
+ # If a second argument +io+ is +true+, returns +IO+ instead of +String+.
43
39
  def compile(document, io = false)
44
40
  cin, cout, cerr = Open3.popen3(document.command_for(self))
45
41
  cin.print(document.text)
@@ -5,49 +5,53 @@ module Pandoku::Formats
5
5
  #
6
6
  # == Available Options
7
7
  #
8
- # * <tt>:standalone</tt> - Produce output with an appropriate header
9
- # and footer.
10
- # * <tt>:parse_raw</tt> - Parse untranslatable HTML codes as raw HTML,
11
- # instead of ignoring them.
12
- # * <tt>latexmathml</tt> - Use LaTeXMathML to display embedded TeX math in
13
- # HTML output. To insert a link to a local copy of the <b>LaTeXMathML.js</b>
14
- # script, provide a URL string. If just <tt>true</tt> is provided, the
15
- # contents of the script will be inserted directly into the HTML header.
16
- # * <tt>:jsmath</tt> - Use jsMath to display embedded TeX math in HTML output.
17
- # The URL should point to the jsMath load script; if provided,
18
- # it will be linked to in the header of standalone HTML documents.
19
- # * <tt>:gladtex</tt> - Enclose TeX math in +<eq>+ tags in HTML output.
20
- # These can then be processed by gladTeX to produce links
21
- # to images of the typeset formulas.
22
- # * <tt>:mimetex</tt> - Render TeX math using the mimeTeX CGI script.
23
- # If URL is not specified, it is assumed that the script is at
24
- # <b>/cgi-bin/mimetex.cgi</b>.
25
- # * <tt>:no_wrap</tt> - Disable text wrapping in output.
26
- # (Default is to wrap text.)
27
- # * <tt>:sanitize_html</tt> - Sanitizes HTML using a whitelist.
28
- # Unsafe tags are replaced by HTML comments; unsafe attributes are omitted.
29
- # URIs in links and images are also checked against a whitelist of URI
30
- # schemes.
31
- # * <tt>:email_obfuscation</tt> -
32
- # (<tt>nil</tt>|<tt>:javascript</tt>|<tt>:references</tt>)
33
- # Specify a method for obfuscating <tt>mailto:</tt> links in HTML documents.
34
- # * <tt>:nil</tt> leaves <tt>mailto:</tt> links as they are.
35
- # * <tt>:javascript</tt> obfuscates them using javascript.
36
- # * <tt>:references</tt> obfuscates them by printing their letters
37
- # as decimal or hexadecimal character references.
38
- # If <tt>:strict => true</tt> is specified, references is used regardless
39
- # of the presence of this option.
40
- # * <tt>:toc</tt> - Include an automatically generated table of contents.
41
- # * <tt>:css</tt> - Link to a CSS style sheet.
42
- # The pathname of the style sheet goes value.
43
- # * <tt>:include_in_header</tt> - Include contents of the given filename
44
- # at the end of the header. Implies <tt>:standalone</tt>.
45
- # * <tt>:include_before_body</tt> - Include contents of the given filename
46
- # at the beginning of the document body.
47
- # * <tt>:include_after_body</tt> - Include contents of the given filename
48
- # at the end of the document body.
49
- # * <tt>:custom_header</tt> - Use contents of the given filename as the
50
- # document header. Implies <tt>:standalone</tt>.
8
+ # <tt>:standalone</tt>:: Produce output with an appropriate header and footer.
9
+ # <tt>:parse_raw</tt>:: Parse untranslatable HTML codes as raw HTML,
10
+ # instead of ignoring them.
11
+ # <tt>latexmathml</tt>::
12
+ # Use LaTeXMathML to display embedded TeX math in HTML output.
13
+ # To insert a link to a local copy of the <b>LaTeXMathML.js</b> script,
14
+ # provide a URL string. If just <tt>true</tt> is provided, the contents
15
+ # of the script will be inserted directly into the HTML header.
16
+ # <tt>:jsmath</tt>::
17
+ # Use jsMath to display embedded TeX math in HTML output.
18
+ # The URL should point to the jsMath load script; if provided,
19
+ # it will be linked to in the header of standalone HTML documents.
20
+ # <tt>:gladtex</tt>::
21
+ # Enclose TeX math in +<eq>+ tags in HTML output.
22
+ # These can then be processed by gladTeX to produce links
23
+ # to images of the typeset formulas.
24
+ # <tt>:mimetex</tt>::
25
+ # Render TeX math using the mimeTeX CGI script. If URL is not specified,
26
+ # it is assumed that the script is at <b>/cgi-bin/mimetex.cgi</b>.
27
+ # <tt>:no_wrap</tt>:: Disable text wrapping in output.
28
+ # (Default is to wrap text.)
29
+ # <tt>:sanitize_html</tt>::
30
+ # Sanitizes HTML using a whitelist. Unsafe tags are replaced by
31
+ # HTML comments; unsafe attributes are omitted. URIs in links and
32
+ # images are also checked against a whitelist of URI schemes.
33
+ # <tt>:email_obfuscation</tt> <i>(<tt>nil</tt>|<tt>:javascript</tt>|<tt>:references</tt>)</i>::
34
+ # Specify a method for obfuscating <tt>mailto:</tt> links
35
+ # in HTML documents.
36
+ # * <tt>:nil</tt> leaves <tt>mailto:</tt> links as they are.
37
+ # * <tt>:javascript</tt> obfuscates them using javascript.
38
+ # * <tt>:references</tt> obfuscates them by printing their letters
39
+ # as decimal or hexadecimal character references.
40
+ # If <tt>:strict => true</tt> is specified, references is
41
+ # used regardless of the presence of this option.
42
+ # <tt>:toc</tt>:: Include an automatically generated table of contents.
43
+ # <tt>:css</tt>:: Link to a CSS style sheet.
44
+ # The pathname of the style sheet goes value.
45
+ # <tt>:include_in_header</tt>::
46
+ # Include contents of the given filename at the end of the header.
47
+ # Implies <tt>:standalone</tt>.
48
+ # <tt>:include_before_body</tt>:: Include contents of the given filename
49
+ # at the beginning of the document body.
50
+ # <tt>:include_after_body</tt>:: Include contents of the given filename
51
+ # at the end of the document body.
52
+ # <tt>:custom_header</tt>::
53
+ # Use contents of the given filename as the document header.
54
+ # Implies <tt>:standalone</tt>.
51
55
  class HTML < Pandoku::Format
52
56
  include Pandoku::InputFormat, Pandoku::OutputFormat
53
57
 
@@ -5,17 +5,17 @@ module Pandoku::Formats
5
5
  #
6
6
  # == Available Options
7
7
  #
8
- # * <tt>:strict</tt> - Use strict syntax, with no extensions or variants.
9
- # * <tt>:reference_links</tt> - Use reference-style links,
10
- # rather than inline links, in writing.
11
- # * <tt>:smart</tt> - Use smart quotes, dashes, and ellipses.
12
- # * <tt>:no_wrap</tt> - Disable text wrapping in output.
13
- # (Default is to wrap text.)
14
- # * <tt>:sanitize_html</tt> - Sanitizes HTML using a whitelist.
15
- # Unsafe tags are replaced by HTML comments; unsafe attributes are omitted.
16
- # URIs in links and images are also checked against a whitelist of URI
17
- # schemes.
18
- # * <tt>:toc</tt> - Include an automatically generated table of contents.
8
+ # <tt>:strict</tt>:: Use strict syntax, with no extensions or variants.
9
+ # <tt>:reference_links</tt>:: Use reference-style links,
10
+ # rather than inline links, in writing.
11
+ # <tt>:smart</tt>:: Use smart quotes, dashes, and ellipses.
12
+ # <tt>:no_wrap</tt>:: Disable text wrapping in output.
13
+ # (Default is to wrap text.)
14
+ # <tt>:sanitize_html</tt>::
15
+ # Sanitizes HTML using a whitelist. Unsafe tags are replaced by
16
+ # HTML comments; unsafe attributes are omitted. URIs in links and
17
+ # images are also checked against a whitelist of URI schemes.
18
+ # <tt>:toc</tt>:: Include an automatically generated table of contents.
19
19
  class Markdown < Pandoku::Format
20
20
  include Pandoku::InputFormat, Pandoku::OutputFormat
21
21
 
@@ -1,27 +1,53 @@
1
+ require 'tempfile'
1
2
  require File.dirname(File.dirname(__FILE__)) + '/format'
2
3
 
3
4
  module Pandoku::Formats
4
5
  # ODT (OpenOffice text document) format.
5
- class ODT
6
- include OutputFormat
6
+ #
7
+ # == Available Options
8
+ #
9
+ # <tt>:toc</tt>:: Include an automatically generated table of contents.
10
+ class ODT < Pandoku::Format
11
+ include Pandoku::OutputFormat
7
12
 
8
13
  def self.name
9
14
  :odt
10
15
  end
11
16
 
17
+ def self.default_options
18
+ { :toc => false }
19
+ end
20
+
12
21
  def cliopts
13
- @tmpfile = @tmpfile || self._tmpfile
14
- "--output=#{@tmpfile} #{super.cliopts}"
22
+ @tmpfile = @tmpfile || _tmpfile
23
+ super + ["--output=#{@tmpfile}"]
15
24
  end
16
25
 
17
- def compile(document)
18
- @tmpfile = self._tmpfile
26
+ # Compiles the given +document+ to the format.
27
+ # If a second argument +file+ is +true+, returns +File+ instead of +String+.
28
+ #
29
+ # *Note* that it creates a temporary file for the result.
30
+ # If +file+ is +false+, the temporary file become deleted.
31
+ def compile(document, file = false)
32
+ @tmpfile = _tmpfile
33
+ ps = IO.popen(document.command_for(self), 'w')
34
+ ps.print(document.text)
35
+ ps.close
36
+ f = File.new(@tmpfile, 'r')
37
+ return f if file
38
+ result = f.read
39
+ f.close
40
+ File.unlink(@tmpfile)
41
+ return result
19
42
  end
20
43
 
21
44
  private
22
45
 
23
46
  def _tmpfile
24
- Dir.tmpdir
47
+ f = Tempfile.new('pandoku')
48
+ path = f.path
49
+ f.close(false)
50
+ path
25
51
  end
26
52
  end
27
53
  end
@@ -0,0 +1,27 @@
1
+ require File.dirname(File.dirname(__FILE__)) + '/format'
2
+
3
+ module Pandoku::Formats
4
+ # reStructuredText format. http://docutils.sourceforge.net/rst.html
5
+ #
6
+ # == Available Options
7
+ #
8
+ # <tt>:reference_links</tt>:: Use reference-style links,
9
+ # rather than inline links, in writing.
10
+ # <tt>:no_wrap</tt>:: Disable text wrapping in output.
11
+ # (Default is to wrap text.)
12
+ # <tt>:toc</tt>:: Include an instruction to create table of contents.
13
+ class ReStructuredText < Pandoku::Format
14
+ include Pandoku::InputFormat, Pandoku::OutputFormat
15
+
16
+ def self.name
17
+ :rst
18
+ end
19
+
20
+ def self.default_options
21
+ { :reference_links => false, :no_wrap => false, :toc => false }
22
+ end
23
+ end
24
+
25
+ # Aliases.
26
+ ReST = RST = reStructuredText = ReStructuredText
27
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pandoku
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.1"
4
+ version: "0.2"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hong, MinHee
@@ -25,6 +25,7 @@ files:
25
25
  - lib/pandoku.rb
26
26
  - lib/pandoku/formats/odt.rb
27
27
  - lib/pandoku/formats/html.rb
28
+ - lib/pandoku/formats/rst.rb
28
29
  - lib/pandoku/formats/markdown.rb
29
30
  - lib/pandoku/document.rb
30
31
  - lib/pandoku/format.rb