From e26831b3cceb24098854988037552028f9811c18 Mon Sep 17 00:00:00 2001 From: Rafael Volpato Date: Mon, 28 Nov 2022 17:36:28 -0300 Subject: [PATCH] Receiving theme parameter to use the default or github --- bin/code2pdf | 25 +++++++++++++++++++++---- lib/code2pdf/convert_to_pdf.rb | 10 ++++++++-- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/bin/code2pdf b/bin/code2pdf index 7a9cfaf..41233e4 100755 --- a/bin/code2pdf +++ b/bin/code2pdf @@ -5,8 +5,10 @@ require 'optparse' $:.push File.expand_path('../../lib', __FILE__) require 'code2pdf' +options = {:theme => 'default'} + optparse = OptionParser.new do |opts| - opts.banner = "Usage: code2pdf \n\nYou can use flags as such:" + opts.banner = "Usage: code2pdf [OPTIONS] \n\nYou can use flags as such:" opts.on('-h', '--help', 'Display this screen') do puts opts @@ -19,6 +21,14 @@ optparse = OptionParser.new do |opts| exit end + opts.on("-t", "--theme=github", String, "PDF Theme to use: default or github") do |t| + options[:theme] = t + end + + opts.on("-f", "--from=DIR", String, "Source dir") do |f| + options[:from] = f + end + if ARGV.empty? puts opts exit 1 @@ -26,14 +36,21 @@ optparse = OptionParser.new do |opts| end begin - optparse.parse! + rest = optparse.parse! rescue OptionParser::InvalidOption => exception puts exception exit 1 end -PATH = ARGV[0].gsub(/\/$/, '') +if rest.empty? && options[:from] == nil + puts optparse + exit 1 +elsif options[:from] == nil + options[:from] = rest[0]; +end + +PATH = options[:from].gsub(/\/$/, '') BLACK_LIST_YAML_FILE = "#{PATH}/.code2pdf".freeze filename = "#{PATH.gsub(/(\.|\/)/, '_')}.pdf" -ConvertToPDF.new from: PATH, to: filename, except: BLACK_LIST_YAML_FILE +ConvertToPDF.new from: PATH, to: filename, except: BLACK_LIST_YAML_FILE, theme: options[:theme] diff --git a/lib/code2pdf/convert_to_pdf.rb b/lib/code2pdf/convert_to_pdf.rb index 6885bcc..5469c19 100644 --- a/lib/code2pdf/convert_to_pdf.rb +++ b/lib/code2pdf/convert_to_pdf.rb @@ -14,7 +14,7 @@ def initialize(params = {}) elsif !params.key?(:to) || params[:to].nil? raise ArgumentError.new 'where should I save the generated pdf file?' else - @from, @to, @except = params[:from], params[:to], params[:except].to_s + @from, @to, @except, @themeName = params[:from], params[:to], params[:except].to_s, params[:theme] if File.exist?(@except) && invalid_blacklist? raise LoadError.new "#{@except} is not a valid blacklist YAML file" @@ -50,10 +50,16 @@ def syntax_highlight(file) file_lexer = Rouge::Lexer.find(file_type) return CGI.escapeHTML(file.last) unless file_lexer - theme = Rouge::Themes::Base16.mode(:light) + if (@themeName == "github") + theme = Rouge::Themes::Github.new() + else + theme = Rouge::Themes::Base16.mode(:light) + end + formatter = Rouge::Formatters::HTMLInline.new(theme) formatter = Rouge::Formatters::HTMLTable.new(formatter, start_line: 1) code_data = file.last.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '') + puts "Processing file #{file.first}" formatter.format(file_lexer.lex(code_data)) end