ruby-pandoc 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/ruby-pandoc.rb +2 -0
- data/lib/ruby-pandoc/converter.rb +218 -0
- data/lib/ruby-pandoc/dependencies.rb +60 -0
- data/lib/ruby-pandoc/rake_tasks.rb +14 -0
- data/lib/ruby-pandoc/version.rb +3 -0
- data/lib/tasks/publication.rake +11 -0
- metadata +185 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f65f227e9627ffa974724b20c8f311795e3ab614
|
4
|
+
data.tar.gz: 908bdd535ae3377320bdc035c742209e10dffa44
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5143828fac6cf89e6aceb00beb695f9527d1598f1d93e6d4badca397a66e01d40efd4bee7a233dc6280015939707bc9d1761cbef3706986b23b0758ea84d356d
|
7
|
+
data.tar.gz: 392b9106405efba44102482633125204b216f059a505f356f5f16313ce5423915424796abe7010b4d4fe5af3eb0ae14bc778689bf955add085b52eb13a3c1d9a
|
data/lib/ruby-pandoc.rb
ADDED
@@ -0,0 +1,218 @@
|
|
1
|
+
require 'open3'
|
2
|
+
require 'tempfile'
|
3
|
+
|
4
|
+
module RubyPandoc
|
5
|
+
class Converter
|
6
|
+
@@pandoc_path = 'pandoc'
|
7
|
+
|
8
|
+
# The available readers and their corresponding names. The keys are used to
|
9
|
+
# generate methods and specify options to Pandoc.
|
10
|
+
READERS = {
|
11
|
+
'native' => 'pandoc native',
|
12
|
+
'json' => 'pandoc JSON',
|
13
|
+
'markdown' => 'markdown',
|
14
|
+
'rst' => 'reStructuredText',
|
15
|
+
'textile' => 'textile',
|
16
|
+
'html' => 'HTML',
|
17
|
+
'latex' => 'LaTeX'
|
18
|
+
}.freeze
|
19
|
+
|
20
|
+
# The available string writers and their corresponding names. The keys are
|
21
|
+
# used to generate methods and specify options to Pandoc.
|
22
|
+
STRING_WRITERS = {
|
23
|
+
'native' => 'pandoc native',
|
24
|
+
'json' => 'pandoc JSON',
|
25
|
+
'html' => 'HTML',
|
26
|
+
'html5' => 'HTML5',
|
27
|
+
's5' => 'S5 HTML slideshow',
|
28
|
+
'slidy' => 'Slidy HTML slideshow',
|
29
|
+
'dzslides' => 'Dzslides HTML slideshow',
|
30
|
+
'docbook' => 'DocBook XML',
|
31
|
+
'opendocument' => 'OpenDocument XML',
|
32
|
+
'latex' => 'LaTeX',
|
33
|
+
'beamer' => 'Beamer PDF slideshow',
|
34
|
+
'context' => 'ConTeXt',
|
35
|
+
'texinfo' => 'GNU Texinfo',
|
36
|
+
'man' => 'groff man',
|
37
|
+
'markdown' => 'markdown',
|
38
|
+
'plain' => 'plain',
|
39
|
+
'rst' => 'reStructuredText',
|
40
|
+
'mediawiki' => 'MediaWiki markup',
|
41
|
+
'textile' => 'textile',
|
42
|
+
'rtf' => 'rich text format',
|
43
|
+
'org' => 'emacs org mode',
|
44
|
+
'asciidoc' => 'asciidoc'
|
45
|
+
}.freeze
|
46
|
+
|
47
|
+
# The available binary writers and their corresponding names. The keys are
|
48
|
+
# used to generate methods and specify options to Pandoc.
|
49
|
+
BINARY_WRITERS = {
|
50
|
+
'odt' => 'OpenDocument',
|
51
|
+
'docx' => 'Word docx',
|
52
|
+
'epub' => 'EPUB V2',
|
53
|
+
'epub3' => 'EPUB V3'
|
54
|
+
}.freeze
|
55
|
+
|
56
|
+
# All of the available Writers.
|
57
|
+
WRITERS = STRING_WRITERS.merge(BINARY_WRITERS)
|
58
|
+
|
59
|
+
# To use run the pandoc command with a custom executable path, the path
|
60
|
+
# to the pandoc executable can be set here.
|
61
|
+
def self.pandoc_path=(path)
|
62
|
+
@@pandoc_path = path
|
63
|
+
end
|
64
|
+
|
65
|
+
# Create a new RubyPandoc converter object. The first argument contains the
|
66
|
+
# input either as string or as an array of filenames.
|
67
|
+
#
|
68
|
+
# Any other arguments will be converted to pandoc options.
|
69
|
+
#
|
70
|
+
# Usage:
|
71
|
+
# new("# A String", :option1 => :value, :option2)
|
72
|
+
# new(["/path/to/file.md"], :option1 => :value, :option2)
|
73
|
+
# new(["/to/file1.html", "/to/file2.html"], :option1 => :value)
|
74
|
+
def initialize(*args)
|
75
|
+
@input_files = nil
|
76
|
+
@input_string = nil
|
77
|
+
if args[0].is_a?(String)
|
78
|
+
@input_string = args.shift
|
79
|
+
elsif args[0].is_a?(Array)
|
80
|
+
@input_files = args.shift.join(' ')
|
81
|
+
end
|
82
|
+
@options = args || []
|
83
|
+
@option_string = nil
|
84
|
+
@binary_output = false
|
85
|
+
@writer = 'html'
|
86
|
+
end
|
87
|
+
|
88
|
+
# Run the conversion. The convert method can take any number of arguments,
|
89
|
+
# which will be converted to pandoc options. If options were already
|
90
|
+
# specified in an initializer or reader method, they will be combined with
|
91
|
+
# any that are passed to this method.
|
92
|
+
#
|
93
|
+
# Returns a string with the converted content.
|
94
|
+
#
|
95
|
+
# Example:
|
96
|
+
#
|
97
|
+
# RubyPandoc.new("# text").convert
|
98
|
+
# # => "<h1 id=\"text\">text</h1>\n"
|
99
|
+
def convert(*args)
|
100
|
+
tmp_file = Tempfile.new('pandoc-conversion')
|
101
|
+
@options += args if args
|
102
|
+
@options += [{ output: tmp_file.path }]
|
103
|
+
@option_string = prepare_options(@options)
|
104
|
+
output = begin
|
105
|
+
run_pandoc
|
106
|
+
IO.binread(tmp_file)
|
107
|
+
ensure
|
108
|
+
tmp_file.close
|
109
|
+
tmp_file.unlink
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
# Generate class methods for each of the readers in RubyPandoc::READERS.
|
114
|
+
# When one of these methods is called, it simply calls the initializer
|
115
|
+
# with the `from` option set to the reader key, and returns the object.
|
116
|
+
#
|
117
|
+
# Example:
|
118
|
+
#
|
119
|
+
# RubyPandoc.markdown("# text")
|
120
|
+
# # => #<RubyPandoc:0x007 @input_string="# text", @options=[{:from=>"markdown"}]
|
121
|
+
class << self
|
122
|
+
READERS.each_key do |r|
|
123
|
+
define_method(r) do |*args|
|
124
|
+
args += [{ from: r }]
|
125
|
+
new(*args)
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
# Generate instance methods for each of the writers in RubyPandoc::WRITERS.
|
131
|
+
# When one of these methods is called, it simply calls the `#convert` method
|
132
|
+
# with the `to` option set to the writer key, thereby returning the
|
133
|
+
# converted string.
|
134
|
+
#
|
135
|
+
# Example:
|
136
|
+
#
|
137
|
+
# RubyPandoc.new("# text").to_html
|
138
|
+
# # => "<h1 id=\"text\">text</h1>\n"
|
139
|
+
WRITERS.each_key do |w|
|
140
|
+
define_method(:"to_#{w}") do |*args|
|
141
|
+
args += [{ to: w.to_sym }]
|
142
|
+
convert(*args)
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
private
|
147
|
+
|
148
|
+
# Execute the pandoc command for binary writers. A temp file is created
|
149
|
+
# and written to, then read back into the program as a string, then the
|
150
|
+
# temp file is closed and unlinked.
|
151
|
+
def convert_binary
|
152
|
+
end
|
153
|
+
|
154
|
+
# Wrapper to run pandoc in a consistent, DRY way
|
155
|
+
def run_pandoc
|
156
|
+
command = unless @input_files.nil? || @input_files.empty?
|
157
|
+
"#{@@pandoc_path} #{@input_files} #{@option_string}"
|
158
|
+
else
|
159
|
+
"#{@@pandoc_path} #{@option_string}"
|
160
|
+
end
|
161
|
+
output = error = exit_status = nil
|
162
|
+
options = {}
|
163
|
+
options[:stdin_data] = @input_string if @input_string
|
164
|
+
output, error, exit_status = Open3.capture3(command, **options)
|
165
|
+
raise error unless exit_status && exit_status.success?
|
166
|
+
output
|
167
|
+
end
|
168
|
+
|
169
|
+
# Builds the option string to be passed to pandoc by iterating over the
|
170
|
+
# opts passed in. Recursively calls itself in order to handle hash options.
|
171
|
+
def prepare_options(opts = [])
|
172
|
+
opts.inject('') do |string, (option, value)|
|
173
|
+
string += case
|
174
|
+
when value
|
175
|
+
create_option(option, value)
|
176
|
+
when option.respond_to?(:each_pair)
|
177
|
+
prepare_options(option)
|
178
|
+
else
|
179
|
+
create_option(option)
|
180
|
+
end
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
# Takes a flag and optional argument, uses it to set any relevant options
|
185
|
+
# used by the library, and returns string with the option formatted as a
|
186
|
+
# command line options. If the option has an argument, it is also included.
|
187
|
+
def create_option(flag, argument = nil)
|
188
|
+
return '' unless flag
|
189
|
+
flag = flag.to_s
|
190
|
+
set_pandoc_ruby_options(flag, argument)
|
191
|
+
if !argument.nil?
|
192
|
+
"#{format_flag(flag)} #{argument}"
|
193
|
+
else
|
194
|
+
format_flag(flag)
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
# Formats an option flag in order to be used with the pandoc command line
|
199
|
+
# tool.
|
200
|
+
def format_flag(flag)
|
201
|
+
if flag.length == 1
|
202
|
+
" -#{flag}"
|
203
|
+
else
|
204
|
+
" --#{flag.to_s.tr('_', '-')}"
|
205
|
+
end
|
206
|
+
end
|
207
|
+
|
208
|
+
# Takes an option and optional argument and uses them to set any flags
|
209
|
+
# used by RubyPandoc.
|
210
|
+
def set_pandoc_ruby_options(flag, argument = nil)
|
211
|
+
case flag
|
212
|
+
when 't', 'to'
|
213
|
+
@writer = argument.to_s
|
214
|
+
@binary_output = true if BINARY_WRITERS.keys.include?(@writer)
|
215
|
+
end
|
216
|
+
end
|
217
|
+
end
|
218
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'mkmf'
|
2
|
+
require 'tmpdir'
|
3
|
+
|
4
|
+
PANDOC_MIN_VERSION = '1.6' # min version with epub
|
5
|
+
PANDOC_URL = 'https://github.com/jgm/pandoc/releases/download/1.17.0.2/pandoc-1.17.0.2-1-amd64.deb' # FIXME
|
6
|
+
|
7
|
+
module MakeMakefile::Logging
|
8
|
+
@logfile = File::NULL
|
9
|
+
end
|
10
|
+
|
11
|
+
module RubyPandoc
|
12
|
+
module Dependencies
|
13
|
+
extend self
|
14
|
+
|
15
|
+
def satisfied?
|
16
|
+
has_pandoc
|
17
|
+
has_latex
|
18
|
+
end
|
19
|
+
|
20
|
+
def satisfy
|
21
|
+
get_pandoc
|
22
|
+
get_latex
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def has_pandoc
|
28
|
+
pandoc = find_executable 'pandoc'
|
29
|
+
unless pandoc
|
30
|
+
puts 'Pandoc is not installed'
|
31
|
+
return false
|
32
|
+
end
|
33
|
+
version = `#{pandoc} -v`.lines.first.split(/\s+/).last
|
34
|
+
unless Gem::Version.new(version) > Gem::Version.new(PANDOC_MIN_VERSION)
|
35
|
+
puts "Pandoc version #{version} too old (require #{PANDOC_MIN_VERSION})"
|
36
|
+
return false
|
37
|
+
end
|
38
|
+
true
|
39
|
+
end
|
40
|
+
|
41
|
+
def has_latex
|
42
|
+
find_executable 'pdflatex'
|
43
|
+
end
|
44
|
+
|
45
|
+
# FIXME make this conditional to different types of platforms
|
46
|
+
def get_pandoc
|
47
|
+
return if has_pandoc
|
48
|
+
Dir.mktmpdir do |dir|
|
49
|
+
Dir.chdir(dir) do
|
50
|
+
system("wget #{PANDOC_URL} -O pandoc.deb")
|
51
|
+
system("dpkg -i pandoc.deb")
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def get_latex
|
57
|
+
system('apt-get install -y --force-yes texlive-full')
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'rake'
|
2
|
+
|
3
|
+
module Publisher
|
4
|
+
# Loads all rake tasks when terraform_dsl is included by a rake script
|
5
|
+
module Tasks
|
6
|
+
extend self
|
7
|
+
|
8
|
+
def load_all
|
9
|
+
Dir.glob("#{File.expand_path('../../tasks', __FILE__)}/*.rake").each { |r| load r }
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
Publisher::Tasks.load_all
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'ruby-pandoc/dependencies'
|
2
|
+
|
3
|
+
desc 'Assert that dependencies are installed'
|
4
|
+
task 'check_depends' do
|
5
|
+
RubyPandoc::Dependencies.satisfied?
|
6
|
+
end
|
7
|
+
|
8
|
+
desc 'Install necessary dependencies'
|
9
|
+
task 'get_depends' do
|
10
|
+
RubyPandoc::Dependencies.satisfy
|
11
|
+
end
|
metadata
ADDED
@@ -0,0 +1,185 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ruby-pandoc
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dale Hamel
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-05-04 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: pry
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.10.3
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.10.3
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: pry-byebug
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 3.3.0
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 3.3.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: simplecov
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 0.10.0
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 0.10.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 3.2.0
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 3.2.0
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: mocha
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1.1'
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: 1.1.0
|
79
|
+
type: :development
|
80
|
+
prerelease: false
|
81
|
+
version_requirements: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - "~>"
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '1.1'
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: 1.1.0
|
89
|
+
- !ruby/object:Gem::Dependency
|
90
|
+
name: rake
|
91
|
+
requirement: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - "~>"
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '10.4'
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: 10.4.2
|
99
|
+
type: :development
|
100
|
+
prerelease: false
|
101
|
+
version_requirements: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - "~>"
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '10.4'
|
106
|
+
- - ">="
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: 10.4.2
|
109
|
+
- !ruby/object:Gem::Dependency
|
110
|
+
name: rdoc
|
111
|
+
requirement: !ruby/object:Gem::Requirement
|
112
|
+
requirements:
|
113
|
+
- - "~>"
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: '4.2'
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: 4.2.0
|
119
|
+
type: :development
|
120
|
+
prerelease: false
|
121
|
+
version_requirements: !ruby/object:Gem::Requirement
|
122
|
+
requirements:
|
123
|
+
- - "~>"
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '4.2'
|
126
|
+
- - ">="
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
version: 4.2.0
|
129
|
+
- !ruby/object:Gem::Dependency
|
130
|
+
name: minitest
|
131
|
+
requirement: !ruby/object:Gem::Requirement
|
132
|
+
requirements:
|
133
|
+
- - "~>"
|
134
|
+
- !ruby/object:Gem::Version
|
135
|
+
version: 5.8.3
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: 5.8.3
|
139
|
+
type: :development
|
140
|
+
prerelease: false
|
141
|
+
version_requirements: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - "~>"
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: 5.8.3
|
146
|
+
- - ">="
|
147
|
+
- !ruby/object:Gem::Version
|
148
|
+
version: 5.8.3
|
149
|
+
description: Fork of pandoc-ruby, lightweight pandoc wrapper for ruby
|
150
|
+
email: dale.hamel@srvthe.net
|
151
|
+
executables: []
|
152
|
+
extensions: []
|
153
|
+
extra_rdoc_files: []
|
154
|
+
files:
|
155
|
+
- lib/ruby-pandoc.rb
|
156
|
+
- lib/ruby-pandoc/converter.rb
|
157
|
+
- lib/ruby-pandoc/dependencies.rb
|
158
|
+
- lib/ruby-pandoc/rake_tasks.rb
|
159
|
+
- lib/ruby-pandoc/version.rb
|
160
|
+
- lib/tasks/publication.rake
|
161
|
+
homepage: https://github.com/dalehamel/ruby-pandoc
|
162
|
+
licenses:
|
163
|
+
- MIT
|
164
|
+
metadata: {}
|
165
|
+
post_install_message:
|
166
|
+
rdoc_options: []
|
167
|
+
require_paths:
|
168
|
+
- lib
|
169
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
170
|
+
requirements:
|
171
|
+
- - ">="
|
172
|
+
- !ruby/object:Gem::Version
|
173
|
+
version: '0'
|
174
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
175
|
+
requirements:
|
176
|
+
- - ">="
|
177
|
+
- !ruby/object:Gem::Version
|
178
|
+
version: '0'
|
179
|
+
requirements: []
|
180
|
+
rubyforge_project:
|
181
|
+
rubygems_version: 2.4.8
|
182
|
+
signing_key:
|
183
|
+
specification_version: 4
|
184
|
+
summary: Pandoc wrapper for ruby
|
185
|
+
test_files: []
|