From b37e67f664e55daec65cc08b7061c1e3abed4e3b Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Sat, 17 Dec 2016 11:12:15 +0100 Subject: [PATCH 1/6] whitelist options accepted by pandoc --- Gemfile.lock | 10 ++++------ lib/pandoc-ruby.rb | 26 ++++++++++++++++++++++---- test/test_pandoc_ruby.rb | 17 ----------------- 3 files changed, 26 insertions(+), 27 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 5ed44a7..1c32749 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,14 +1,12 @@ GEM remote: http://rubygems.org/ specs: - json (1.8.3) metaclass (0.0.4) - minitest (5.8.4) + minitest (5.8.5) mocha (1.1.0) metaclass (~> 0.0.1) - rake (11.1.2) - rdoc (4.2.2) - json (~> 1.4) + rake (12.0.0) + rdoc (5.0.0) PLATFORMS ruby @@ -20,4 +18,4 @@ DEPENDENCIES rdoc BUNDLED WITH - 1.12.2 + 1.12.5 diff --git a/lib/pandoc-ruby.rb b/lib/pandoc-ruby.rb index c7c47f5..e8ee615 100644 --- a/lib/pandoc-ruby.rb +++ b/lib/pandoc-ruby.rb @@ -57,6 +57,26 @@ class PandocRuby # All of the available Writers. WRITERS = STRING_WRITERS.merge(BINARY_WRITERS) + # Options understood by pandoc, taken from http://pandoc.org/MANUAL.html. + # Ignore all other options passed to pandoc + AVAILABLE_OPTIONS = Set.new %w(from read to write output data-dir strict + parse-raw smart old-dashes base-header-level indented-code-classes filter + normalize preserve-tabs tab-stop track-changes extract-media standalone + template metadata variable print-default-template print-default-data-file + no-wrap columns toc table-of-contents toc-depth no-highlight + highlight-style include-in-header include-before-body include-after-body + self-contained offline html5 html-q-tags ascii reference-links atx-headers + chapters number-sections number-offsetS no-tex-ligatures listings + incremental slide-level section-divs default-image-extension + email-obfuscation id-prefix title-prefix css reference-odt reference-docx + epub-stylesheet epub-cover-image epub-metadata epub-embed-font + epub-chapter-level latex-engine latex-engine-opt bibliography csl + citation-abbreviations natbib biblatex latexmathml asciimathml mathml + mimetex webtex jsmath mathjax katex katex-stylesheet gladtex trace + dump-args ignore-args verbose bash-completion) + ALIAS_OPTIONS = Set.new %w(f r t w o R S F p s M V D H B A 5 N i T c m) + ALLOWED_OPTIONS = AVAILABLE_OPTIONS + ALIAS_OPTIONS + # To use run the pandoc command with a custom executable path, the path # to the pandoc executable can be set here. def self.pandoc_path=(path) @@ -243,11 +263,11 @@ def prepare_options(opts = []) # Takes a flag and optional argument, uses it to set any relevant options # used by the library, and returns string with the option formatted as a # command line options. If the option has an argument, it is also included. + # Only whitelisted options are sent to pandoc. def create_option(flag, argument = nil) - return '' unless flag + return '' unless flag && ALLOWED_OPTIONS.include?(flag.to_s.gsub('_', '-')) flag = flag.to_s set_pandoc_ruby_options(flag, argument) - return '' if flag == 'timeout' # pandoc doesn't accept timeouts yet if !argument.nil? "#{format_flag(flag)} #{argument}" else @@ -272,8 +292,6 @@ def set_pandoc_ruby_options(flag, argument = nil) when 't', 'to' self.writer = argument.to_s self.binary_output = true if BINARY_WRITERS.keys.include?(self.writer) - when 'timeout' - @timeout = argument end end diff --git a/test/test_pandoc_ruby.rb b/test/test_pandoc_ruby.rb index 11f6e52..6d3c5e4 100644 --- a/test/test_pandoc_ruby.rb +++ b/test/test_pandoc_ruby.rb @@ -92,12 +92,6 @@ assert converter.convert end - it 'raises RuntimeError from pandoc executable error' do - assert_raises(RuntimeError) do - PandocRuby.new('# hello', 'badopt').to_html5 - end - end - PandocRuby::READERS.each_key do |r| it "converts from #{r} with PandocRuby.#{r}" do converter = PandocRuby.send(r, @string) @@ -152,17 +146,6 @@ end end - it 'gracefully times out when pandoc hangs due to malformed input' do - file = File.join(File.dirname(__FILE__), 'files', 'bomb.tex') - contents = File.read(file) - - assert_raises(RuntimeError) do - PandocRuby.convert( - contents, :from => :latex, :to => :html, :timeout => 1 - ) - end - end - it 'has reader and writer constants' do assert_equal PandocRuby::READERS, 'html' => 'HTML', From a2754007cf29a37d1f09045992bde66c7d794fb5 Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Sat, 17 Dec 2016 12:03:13 +0100 Subject: [PATCH 2/6] added ignore_whitelist option --- lib/pandoc-ruby.rb | 12 +++++++++++- test/test_pandoc_ruby.rb | 23 +++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/lib/pandoc-ruby.rb b/lib/pandoc-ruby.rb index e8ee615..b5b513c 100644 --- a/lib/pandoc-ruby.rb +++ b/lib/pandoc-ruby.rb @@ -101,6 +101,11 @@ def options @options ||= [] end + attr_writer :ignore_whitelist + def ignore_whitelist + @ignore_whitelist + end + attr_writer :option_string def option_string @option_string ||= '' @@ -130,6 +135,7 @@ def initialize(*args) self.input_files = args.shift.join(' ') end self.options = args + self.ignore_whitelist = self.options.find { |o| o == :ignore_whitelist } end # Run the conversion. The convert method can take any number of arguments, @@ -265,9 +271,11 @@ def prepare_options(opts = []) # command line options. If the option has an argument, it is also included. # Only whitelisted options are sent to pandoc. def create_option(flag, argument = nil) - return '' unless flag && ALLOWED_OPTIONS.include?(flag.to_s.gsub('_', '-')) + return '' unless flag flag = flag.to_s set_pandoc_ruby_options(flag, argument) + return '' unless @ignore_whitelist || + ALLOWED_OPTIONS.include?(flag.gsub('_', '-')) if !argument.nil? "#{format_flag(flag)} #{argument}" else @@ -292,6 +300,8 @@ def set_pandoc_ruby_options(flag, argument = nil) when 't', 'to' self.writer = argument.to_s self.binary_output = true if BINARY_WRITERS.keys.include?(self.writer) + when 'timeout' + @timeout = argument end end diff --git a/test/test_pandoc_ruby.rb b/test/test_pandoc_ruby.rb index 6d3c5e4..b5f41f8 100644 --- a/test/test_pandoc_ruby.rb +++ b/test/test_pandoc_ruby.rb @@ -92,6 +92,18 @@ assert converter.convert end + it 'ignores options not whitelisted' do + converter = PandocRuby.new('# hello', 'badopt') + converter.expects(:execute).with('pandoc').returns(true) + assert converter.convert + end + + it 'raises RuntimeError from pandoc executable error' do + assert_raises(RuntimeError) do + PandocRuby.new('# hello', 'badopt', :ignore_whitelist).to_html5 + end + end + PandocRuby::READERS.each_key do |r| it "converts from #{r} with PandocRuby.#{r}" do converter = PandocRuby.send(r, @string) @@ -146,6 +158,17 @@ end end + it 'gracefully times out when pandoc hangs due to malformed input' do + file = File.join(File.dirname(__FILE__), 'files', 'bomb.tex') + contents = File.read(file) + + assert_raises(RuntimeError) do + PandocRuby.convert( + contents, :from => :latex, :to => :html, :timeout => 1 + ) + end + end + it 'has reader and writer constants' do assert_equal PandocRuby::READERS, 'html' => 'HTML', From 59c2ed49c30fb5be007ff94b6d0e0f6168c649ff Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Sat, 17 Dec 2016 12:16:52 +0100 Subject: [PATCH 3/6] simplified :ignore_whitelist option --- lib/pandoc-ruby.rb | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/lib/pandoc-ruby.rb b/lib/pandoc-ruby.rb index b5b513c..40c2c09 100644 --- a/lib/pandoc-ruby.rb +++ b/lib/pandoc-ruby.rb @@ -101,11 +101,6 @@ def options @options ||= [] end - attr_writer :ignore_whitelist - def ignore_whitelist - @ignore_whitelist - end - attr_writer :option_string def option_string @option_string ||= '' @@ -135,7 +130,6 @@ def initialize(*args) self.input_files = args.shift.join(' ') end self.options = args - self.ignore_whitelist = self.options.find { |o| o == :ignore_whitelist } end # Run the conversion. The convert method can take any number of arguments, @@ -274,8 +268,8 @@ def create_option(flag, argument = nil) return '' unless flag flag = flag.to_s set_pandoc_ruby_options(flag, argument) - return '' unless @ignore_whitelist || - ALLOWED_OPTIONS.include?(flag.gsub('_', '-')) + return '' unless ALLOWED_OPTIONS.include?(flag.gsub('_', '-')) || + @options.find { |o| o == :ignore_whitelist } if !argument.nil? "#{format_flag(flag)} #{argument}" else From e73c8082f982bab355683f52033ecfdb55d4538c Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Sat, 17 Dec 2016 12:22:06 +0100 Subject: [PATCH 4/6] updated documentation --- README.md | 4 ++++ lib/pandoc-ruby.rb | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 487250b..36e88ff 100644 --- a/README.md +++ b/README.md @@ -121,6 +121,10 @@ the correct header and footer are added. PandocRuby.new("# Some title", :standalone).to_rtf ``` +My default only whitelisted options (those known to Pandoc) are passed on to +Pandoc. You can send all options to Pandoc by using the `:ignore_whitelist` +option. + ## Note on Patches/Pull Requests * Fork the project. diff --git a/lib/pandoc-ruby.rb b/lib/pandoc-ruby.rb index 40c2c09..dd08899 100644 --- a/lib/pandoc-ruby.rb +++ b/lib/pandoc-ruby.rb @@ -58,7 +58,7 @@ class PandocRuby WRITERS = STRING_WRITERS.merge(BINARY_WRITERS) # Options understood by pandoc, taken from http://pandoc.org/MANUAL.html. - # Ignore all other options passed to pandoc + # Ignore all other options passed to pandoc, unless overriden. AVAILABLE_OPTIONS = Set.new %w(from read to write output data-dir strict parse-raw smart old-dashes base-header-level indented-code-classes filter normalize preserve-tabs tab-stop track-changes extract-media standalone @@ -263,7 +263,7 @@ def prepare_options(opts = []) # Takes a flag and optional argument, uses it to set any relevant options # used by the library, and returns string with the option formatted as a # command line options. If the option has an argument, it is also included. - # Only whitelisted options are sent to pandoc. + # Only whitelisted options are sent to pandoc, unless overridden. def create_option(flag, argument = nil) return '' unless flag flag = flag.to_s From d8f9a19b965c5cb06cb2433a3cc5185f9bcfde6d Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Sat, 17 Dec 2016 13:37:25 +0100 Subject: [PATCH 5/6] added pandoc-citeproc options --- lib/pandoc-ruby.rb | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/lib/pandoc-ruby.rb b/lib/pandoc-ruby.rb index dd08899..4771329 100644 --- a/lib/pandoc-ruby.rb +++ b/lib/pandoc-ruby.rb @@ -61,21 +61,25 @@ class PandocRuby # Ignore all other options passed to pandoc, unless overriden. AVAILABLE_OPTIONS = Set.new %w(from read to write output data-dir strict parse-raw smart old-dashes base-header-level indented-code-classes filter - normalize preserve-tabs tab-stop track-changes extract-media standalone - template metadata variable print-default-template print-default-data-file - no-wrap columns toc table-of-contents toc-depth no-highlight - highlight-style include-in-header include-before-body include-after-body - self-contained offline html5 html-q-tags ascii reference-links atx-headers - chapters number-sections number-offsetS no-tex-ligatures listings - incremental slide-level section-divs default-image-extension - email-obfuscation id-prefix title-prefix css reference-odt reference-docx - epub-stylesheet epub-cover-image epub-metadata epub-embed-font - epub-chapter-level latex-engine latex-engine-opt bibliography csl - citation-abbreviations natbib biblatex latexmathml asciimathml mathml - mimetex webtex jsmath mathjax katex katex-stylesheet gladtex trace - dump-args ignore-args verbose bash-completion) + normalize preserve-tabs tab-stop track-changes file-scope extract-media + standalone template metadata variable print-default-template + print-default-data-file no-wrap wrap columns toc table-of-contents toc-depth + no-highlight highlight-style include-in-header include-before-body + include-after-body self-contained offline html5 html-q-tags ascii + reference-links reference-location atx-headers chapters top-level-division + number-sections number-offsetS no-tex-ligatures listings incremental + slide-level section-divs default-image-extension email-obfuscation id-prefix + title-prefix css reference-odt reference-docx epub-stylesheet + epub-cover-image epub-metadata epub-embed-font epub-chapter-level + latex-engine latex-engine-opt bibliography csl citation-abbreviations natbib + biblatex latexmathml asciimathml mathml mimetex webtex jsmath mathjax katex + katex-stylesheet gladtex trace dump-args ignore-args verbose bash-completion + list-input-formats list-output-formats list-extensions + list-highlight-languages list-highlight-styles) + PANDOC_CITEPROC_OPTIONS = Set.new %w(bib2yaml bib2json reference-section-title + suppress-bibliography notes-after-punctuation y j) ALIAS_OPTIONS = Set.new %w(f r t w o R S F p s M V D H B A 5 N i T c m) - ALLOWED_OPTIONS = AVAILABLE_OPTIONS + ALIAS_OPTIONS + ALLOWED_OPTIONS = AVAILABLE_OPTIONS + PANDOC_CITEPROC_OPTIONS + ALIAS_OPTIONS # To use run the pandoc command with a custom executable path, the path # to the pandoc executable can be set here. From c0ae6aa79c4676268fa29e6a08f9c686fc7af41b Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Sat, 17 Dec 2016 15:18:50 +0100 Subject: [PATCH 6/6] don't use pandoc-citeproc options --- lib/pandoc-ruby.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/pandoc-ruby.rb b/lib/pandoc-ruby.rb index 4771329..a2b4be7 100644 --- a/lib/pandoc-ruby.rb +++ b/lib/pandoc-ruby.rb @@ -76,10 +76,8 @@ class PandocRuby katex-stylesheet gladtex trace dump-args ignore-args verbose bash-completion list-input-formats list-output-formats list-extensions list-highlight-languages list-highlight-styles) - PANDOC_CITEPROC_OPTIONS = Set.new %w(bib2yaml bib2json reference-section-title - suppress-bibliography notes-after-punctuation y j) ALIAS_OPTIONS = Set.new %w(f r t w o R S F p s M V D H B A 5 N i T c m) - ALLOWED_OPTIONS = AVAILABLE_OPTIONS + PANDOC_CITEPROC_OPTIONS + ALIAS_OPTIONS + ALLOWED_OPTIONS = AVAILABLE_OPTIONS + ALIAS_OPTIONS # To use run the pandoc command with a custom executable path, the path # to the pandoc executable can be set here.