Skip to content
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
150 changes: 140 additions & 10 deletions lib/rabbit/command/rabbit-slide.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ def run(*arguments)
class Data < Struct.new(:title,
:allotted_time,
:slide_conf,
:author_conf)
:author_conf,
:pdf)
def available_markup_languages
{
:markdown => "Markdown",
Expand All @@ -51,10 +52,30 @@ def default_markup_language
:rd
end

def available_readme_markup_languages
{
:markdown => "Markdown",
:rd => "RD",
:hiki => "Hiki",
}
end

def default_readme_markup_language
if markup_language == :pdf
:markdown
else
markup_language
end
end

def markup_language
slide_conf.markup_language || author_conf.markup_language || default_markup_language
end

def readme_markup_language
slide_conf.readme_markup_language || default_readme_markup_language
end

def save
author_conf.save
end
Expand Down Expand Up @@ -162,6 +183,26 @@ def setup_options(parser, options)
@data.slide_conf.markup_language = language
end

available_readme_markup_languages = @data.available_readme_markup_languages
label = "[" + available_readme_markup_languages.keys.join(", ") + "]"
messages = [
"Markup language for README of the new slide",
_("(e.g.: %s)") % "--readme-markup-language=rd",
_("(available markup languages: %s)") % label,
]
messages << "(default: If --markup-language is pdf, then markdown. Otherwise, the same as --markup-language.)"
messages << _("(optional)")
parser.on("--readme-markup-language=LANGUAGE", available_readme_markup_languages.keys,
*messages) do |language|
@data.slide_conf.readme_markup_language = language
end

parser.on("--pdf=FILE",
"Specify the PDF file to copy when using the pdf markup language.",
"(must only when --markup-language=pdf)") do |file|
Comment on lines +201 to +202
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

TODO: I18n. I don't know how to do it.
I guess we need to update files under po/.

@data.pdf = file
end

parser.on("--title=TITLE",
_("Title of the new slide"),
_("(e.g.: %s)") % _("--title=\"Rabbit Introduction\""),
Expand Down Expand Up @@ -449,6 +490,25 @@ def apply_value(value)
end
end

class SlidePDFMapper < TextMapper
private
def valid?(value)
if @data.markup_language == :pdf
not value.empty?
else
value.empty?
end
end

def value
@data.pdf
end

def apply_value(value)
@data.pdf = value
end
end

def show_gui
require_relative "../gtk"

Expand Down Expand Up @@ -491,6 +551,9 @@ def show_window(&on_success)
mappers << add_width_widget(grid, nth_row)
nth_row += 1

mappers << add_pdf_widget(grid, nth_row)
nth_row += 1

nth_column = 0
cancel_button = Gtk::Button.new(mnemonic: _("_Cancel"))
cancel_button.signal_connect(:clicked) do
Expand Down Expand Up @@ -602,11 +665,51 @@ def add_width_widget(grid, nth_row)
SlideWidthMapper.new(@data, widget)
end

def add_source_dialog_filter(dialog, name, pattern)
filter = Gtk::FileFilter.new
filter.name = "#{name} (#{pattern})"
filter.add_pattern(pattern)
dialog.add_filter(filter)
end

def add_pdf_widget(grid, nth_row)
nth_column = 0
label_widget = Gtk::Label.new("PDF")
label_widget.halign = :end
grid.attach(label_widget,
nth_column, nth_row,
1, 1)

nth_column += 1
hbox_widget = Gtk::Box.new(:horizontal, 0)
entry_widget = Gtk::Entry.new
hbox_widget.pack_start(entry_widget, expand: true, fill: true, padding: 0)
button_widget = Gtk::Button.new(label: "Open")
button_widget.signal_connect("clicked") do
dialog = Gtk::FileChooserDialog.new(:title => "Choose a PDF file",
:action => :open,
:buttons => [[Gtk::Stock::CANCEL, :cancel],
[Gtk::Stock::OPEN, :accept]])
dialog.set_filename(@data.pdf) if @data.pdf
add_source_dialog_filter(dialog, "PDF files", "*.pdf")
add_source_dialog_filter(dialog, "All files", "*")
if dialog.run == Gtk::ResponseType::ACCEPT
entry_widget.text = dialog.filename
end
dialog.destroy
end
hbox_widget.pack_start(button_widget, expand: false, fill: false, padding: 0)
grid.attach(hbox_widget, nth_column, nth_row, 1, 1)

SlidePDFMapper.new(@data, label_widget, entry_widget)
end

def validate
@validation_errors = []
validate_command
validate_id
validate_base_name
validate_pdf
end

def validate_command
Expand All @@ -633,6 +736,17 @@ def validate_base_name
end
end

def validate_pdf
return unless @data.markup_language == :pdf
if @data.pdf.nil?
@validation_errors << (_("%s is missing") % "--pdf")
return
end
unless File.file?(@data.pdf)
@validation_errors << (_("not a file: %s") % @data.pdf)
end
end

def run_command
__send__("run_command_#{@command}")
end
Expand All @@ -657,6 +771,7 @@ def merge_config_yaml

def generate_directory
create_directory(base_directory)
create_directory(File.join(base_directory, "pdf")) if @data.markup_language == :pdf
end

def generate_template
Expand All @@ -670,12 +785,13 @@ def generate_template

def generate_dot_gitignore
create_file(".gitignore") do |dot_gitignore|
dot_gitignore.puts(<<-EOD)
.DS_Store
/.tmp/
/pkg/
/pdf/
EOD
lines = [
".DS_Store",
"/.tmp/",
"/pkg/",
]
lines << "/pdf/" unless @data.markup_language == :pdf
dot_gitignore.puts(lines.join("\n"))
end
end

Expand Down Expand Up @@ -703,7 +819,7 @@ def generate_readme
end

def readme_content
markup_language = @data.markup_language
markup_language = @data.readme_markup_language
generator = Rabbit::SourceGenerator.find(markup_language)

content = ""
Expand Down Expand Up @@ -763,6 +879,11 @@ def generate_rakefile
end

def generate_slide
if @data.markup_language == :pdf
copy_file(@data.pdf, slide_path)
return
end

source = slide_source
return if source.nil?
create_file(slide_path) do |slide|
Expand All @@ -771,7 +892,12 @@ def generate_slide
end

def slide_path
"#{@data.slide_conf.base_name}.#{slide_source_extension}"
case @data.markup_language
when :pdf
File.join("pdf", @data.slide_conf.pdf_base_path)
else
"#{@data.slide_conf.base_name}.#{slide_source_extension}"
end
end

def slide_source_extension
Expand All @@ -788,7 +914,7 @@ def slide_source_extension
end

def readme_extension
case @data.markup_language
case @data.readme_markup_language
when :rd
"rd"
when :hiki
Expand Down Expand Up @@ -893,6 +1019,10 @@ def base_directory
def create_file(path, &block)
super(File.join(base_directory, path), &block)
end

def copy_file(from, to)
super(from, File.join(base_directory, to))
end
end
end
end
5 changes: 5 additions & 0 deletions lib/rabbit/path-manipulatable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,10 @@ def create_file(path, &block)
Rabbit.logger.info(_("Creating file: %s") % path)
File.open(path, "w", &block)
end

def copy_file(from, to)
Rabbit.logger.info("Copying file: #{from} to #{to}")
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ditto

FileUtils.cp(from, to)
end
end
end
8 changes: 8 additions & 0 deletions lib/rabbit/slide-configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class SlideConfiguration
attr_accessor :height
attr_accessor :source_code_uri
attr_writer :markup_language
attr_accessor :readme_markup_language
def initialize
clear
end
Expand Down Expand Up @@ -100,6 +101,7 @@ def clear
@height = 540
@source_code_uri = nil
@markup_language = nil
@readme_markup_language = nil
end


Expand Down Expand Up @@ -128,6 +130,7 @@ def merge!(conf)
@height = conf["height"] || @height
@source_code_uri = conf["source_code_uri"] || @source_code_uri
@markup_language = conf["markup_language"] || @markup_language
@readme_markup_language = conf["readme_markup_language"] || @readme_markup_language
end

def to_hash
Expand All @@ -148,6 +151,7 @@ def to_hash
"height" => @height,
"source_code_uri" => @source_code_uri,
"markup_language" => @markup_language,
"readme_markup_language" => @readme_markup_language,
}
config["author"] = @author.to_hash if @author
config
Expand Down Expand Up @@ -182,6 +186,10 @@ def markup_language
@markup_language || @author.markup_language
end

def pdf_base_path
"#{@id}-#{@base_name}.pdf"
end

private
def ensure_date(value)
if value.is_a?(String)
Expand Down
24 changes: 12 additions & 12 deletions lib/rabbit/task/slide.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,18 @@ def define_gem_validate_task
end

def define_pdf_task
file pdf_path => [options_path, *(spec.files - [pdf_path])] do
mkdir_p(@pdf_dir)
rabbit("--print",
"--output-filename", pdf_path)
if @slide.markup_language == :pdf
# Does nothing. Not list in "rake -T" but "rake pdf" is accepted..
task :pdf
else
file pdf_path => [options_path, *(spec.files - [pdf_path])] do
mkdir_p(@pdf_dir)
rabbit("--print",
"--output-filename", pdf_path)
end
desc(_("Generate PDF: %{pdf_path}") % {:pdf_path => pdf_path})
task :pdf => pdf_path
end

desc(_("Generate PDF: %{pdf_path}") % {:pdf_path => pdf_path})
task :pdf => pdf_path
end

def define_publish_task
Expand Down Expand Up @@ -197,11 +201,7 @@ def gem_path
end

def pdf_path
File.join(@pdf_dir, pdf_base_path)
end

def pdf_base_path
"#{@slide.id}-#{@slide.base_name}.pdf"
File.join(@pdf_dir, @slide.pdf_base_path)
end

def homepage
Expand Down
1 change: 1 addition & 0 deletions test/test-slide-configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def test_merge!
"width" => 800,
"height" => 600,
"markup_language" => "rd",
"readme_markup_language" => nil,
}
@slide.id = "RubyKaigi2012"
@slide.merge!(conf)
Expand Down
Loading