Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Receiving theme parameter to use the default or github #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions bin/code2pdf
Original file line number Diff line number Diff line change
Expand Up @@ -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 <project path>\n\nYou can use flags as such:"
opts.banner = "Usage: code2pdf [OPTIONS] <project path>\n\nYou can use flags as such:"

opts.on('-h', '--help', 'Display this screen') do
puts opts
Expand All @@ -19,21 +21,36 @@ 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
end
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]
10 changes: 8 additions & 2 deletions lib/code2pdf/convert_to_pdf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/ParallelAssignment: Do not use parallel assignment.
Naming/VariableName: Use snake_case for variable names.
Metrics/LineLength: Line is too long. [104/80]


if File.exist?(@except) && invalid_blacklist?
raise LoadError.new "#{@except} is not a valid blacklist YAML file"
Expand Down Expand Up @@ -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")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/ConditionalAssignment: Use the return of the conditional for variable assignment and comparison.
Style/ParenthesesAroundCondition: Don't use parentheses around the condition of an if.
Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.

theme = Rouge::Themes::Github.new()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/MethodCallWithoutArgsParentheses: Do not use parentheses for method calls with no arguments.

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

Expand Down