class Zenithal::Slide::WholeSlideConverter

Constants

SCRIPT
TEMPLATE

Public Class Methods

new(args) click to toggle source
# File source/zenml/slide/converter.rb, line 9
def initialize(args)
  @mode, @open = nil, false
  @dirs = {:output => "out", :document => "document", :template => "template"}
  @asset_paths = {:style => ["style/style.scss"]}
  @title = "Slide"
  @image_size = [1920, 1080]
  options, rest_args = args.partition{|s| s =~ /^\-\w$/}
  if options.include?("-i")
    @mode = :image
  else
    if options.include?("-o")
      @open = true
    end
    @mode = :normal
  end
  @rest_args = rest_args
  @paths = create_paths
  @parser = create_parser
  @converter = create_converter
  @driver = create_driver
end

Public Instance Methods

convert_image(path) click to toggle source
# File source/zenml/slide/converter.rb, line 87
def convert_image(path)
  extension = File.extname(path).gsub(/^\./, "")
  page_path = path.gsub(@dirs[:document], @dirs[:output]).then(&method(:modify_extension))
  output_path = path.gsub(@dirs[:document], @dirs[:output]).gsub("slide", "image").gsub(".zml", "")
  count_path = path.gsub(@dirs[:document], @dirs[:output]).gsub("slide", "image").gsub(".zml", ".txt")
  output_dir = File.dirname(output_path)
  FileUtils.mkdir_p(output_dir)
  case extension
  when "zml"
    count = File.read(count_path).to_i
    @driver.navigate.to("file:///#{File.join(Dir.pwd, page_path)}")
    @driver.manage.window.resize_to(*@image_size)
    @driver.execute_script("document.body.classList.add('simple');")
    count.times do |index|
      @driver.execute_script("document.querySelectorAll('*[class$=\\'slide\\']')[#{index}].scrollIntoView();")
      @driver.save_screenshot("#{output_path}-#{index}.png")
    end
  end
end
convert_normal(path) click to toggle source
# File source/zenml/slide/converter.rb, line 54
def convert_normal(path)
  extension = File.extname(path).gsub(/^\./, "")
  output_path = path.gsub(@dirs[:document], @dirs[:output]).then(&method(:modify_extension))
  count_path = path.gsub(@dirs[:document], @dirs[:output]).gsub("slide", "image").gsub(".zml", ".txt")
  output_dir = File.dirname(output_path)
  count_dir = File.dirname(count_path)
  FileUtils.mkdir_p(output_dir)
  FileUtils.mkdir_p(count_dir)
  case extension
  when "zml"
    @parser.update(File.read(path))
    document = @parser.run
    @converter.update(document)
    header_string = ""
    main_string = @converter.convert
    count = @converter.variables[:slide_count].to_i.to_s
    header_string << "<script type=\"text/javascript\">#{SCRIPT}</script>\n"
    header_string << @asset_paths[:style].map{|s| "<link rel=\"stylesheet\" type=\"text/css\" href=\"../#{modify_extension(s)}\">\n"}.join
    output = TEMPLATE.gsub(/#\{(.*?)\}/){instance_eval($1)}.gsub(/\r/, "")
    File.write(output_path, output)
    File.write(count_path, count)
  when "scss"
    option = {}
    option[:style] = :expanded
    option[:filename] = path
    output = SassC::Engine.new(File.read(path), option).render
    File.write(output_path, output)
  when "html", "js", "svg"
    output = File.read(path)
    File.write(output_path, output)
  end
end
convert_open(path) click to toggle source
# File source/zenml/slide/converter.rb, line 107
def convert_open(path)
  output_path = path.gsub(@dirs[:document], @dirs[:output]).then(&method(:modify_extension))
  Kernel.spawn("start #{output_path}")
end
create_converter() click to toggle source
# File source/zenml/slide/converter.rb, line 155
def create_converter
  converter = ZenithalConverter.new(nil, :text)
  Dir.each_child(@dirs[:template]) do |entry|
    if entry.end_with?(".rb")
      binding = TOPLEVEL_BINDING
      binding.local_variable_set(:converter, converter)
      Kernel.eval(File.read(File.join(@dirs[:template], entry)), binding, entry)
    end
  end
  return converter
end
create_driver() click to toggle source
# File source/zenml/slide/converter.rb, line 167
def create_driver
  if @mode == :image
    options = Selenium::WebDriver::Chrome::Options.new
    options.add_argument("--headless")
    options.add_option("excludeSwitches", ["enable-logging"])
    driver = Selenium::WebDriver.for(:chrome, options: options)
  else
    driver = nil
  end
  return driver
end
create_parser(main = true) click to toggle source
# File source/zenml/slide/converter.rb, line 137
def create_parser(main = true)
  parser = ZenithalParser.new("")
  parser.brace_name = "x"
  parser.bracket_name = "xn"
  parser.slash_name = "i"
  if main
    parser.register_macro("import") do |attributes, _|
      import_path = attributes["src"]
      import_parser = create_parser(false)
      import_parser.update(File.read(File.join(@dirs[:document], import_path)))
      document = import_parser.run
      import_nodes = (attributes["expand"]) ? document.root.children : [document.root]
      next import_nodes
    end
  end
  return parser
end
create_paths() click to toggle source
# File source/zenml/slide/converter.rb, line 112
def create_paths
  paths = []
  if @rest_args.empty?
    dirs = []
    dirs << File.join(@dirs[:document], "slide")
    if @mode == :normal
      dirs << File.join(@dirs[:document], "asset") 
      @asset_paths[:style].each do |style_path|
        paths << File.join(@dirs[:document], style_path)
      end
    end
    dirs.each do |dir|
      Dir.each_child(dir) do |entry|
        if entry =~ /\.\w+$/
          paths << File.join(dir, entry)
        end
      end
    end
  else
    path = @rest_args.map{|s| s.gsub("\\", "/").gsub("c:/", "C:/")}[0].encode("utf-8")
    paths << path
  end
  return paths
end
document_dir=(dir) click to toggle source
# File source/zenml/slide/converter.rb, line 191
def document_dir=(dir)
  @dirs[:document] = dir
end
execute() click to toggle source
# File source/zenml/slide/converter.rb, line 31
def execute
  case @mode
  when :image
    execute_image
  when :normal
    execute_normal
  end
end
execute_image() click to toggle source
# File source/zenml/slide/converter.rb, line 47
def execute_image
  @paths.each_with_index do |path, index|
    convert_image(path)
  end
  @driver.quit
end
execute_normal() click to toggle source
# File source/zenml/slide/converter.rb, line 40
def execute_normal
  @paths.each_with_index do |path, index|
    convert_normal(path)
    convert_open(path) if @open
  end
end
image_size=(image_size) click to toggle source
# File source/zenml/slide/converter.rb, line 207
def image_size=(image_size)
  @image_size = image_size
end
modify_extension(path) click to toggle source
# File source/zenml/slide/converter.rb, line 179
def modify_extension(path)
  result = path.clone
  result.gsub!(/\.zml$/, ".html")
  result.gsub!(/\.scss$/, ".css")
  result.gsub!(/\.ts$/, ".js")
  return result
end
output_dir=(dir) click to toggle source
# File source/zenml/slide/converter.rb, line 187
def output_dir=(dir)
  @dirs[:output] = dir
end
script_paths=(paths) click to toggle source
# File source/zenml/slide/converter.rb, line 199
def script_paths=(paths)
  @asset_paths[:script] = paths
end
template_dir=(dir) click to toggle source
# File source/zenml/slide/converter.rb, line 195
def template_dir=(dir)
  @dirs[:template] = dir
end
title=(title) click to toggle source
# File source/zenml/slide/converter.rb, line 203
def title=(title)
  @title = title
end