class Slidox::Builder

Public Instance Methods

build() click to toggle source
# File lib/slidox/builder.rb, line 12
def build
  prepare_target_dirs

  html = styles + slides.join(page_break)

  save_html(html)
  save_pdf(html)
end

Private Instance Methods

assets_path() click to toggle source
# File lib/slidox/builder.rb, line 78
def assets_path
  File.join(root_path, 'assets')
end
config() click to toggle source
# File lib/slidox/builder.rb, line 102
def config
  @config ||= YAML.load_file(File.join(root_path, 'config.yml'))
end
html_target() click to toggle source
# File lib/slidox/builder.rb, line 86
def html_target
  Pathname.new(config['target']['html'])
end
page_break() click to toggle source
# File lib/slidox/builder.rb, line 94
def page_break
  "<div style='page-break-before:always'></div>"
end
pdf_options() click to toggle source
# File lib/slidox/builder.rb, line 82
def pdf_options
  config['pdf']
end
pdf_target() click to toggle source
# File lib/slidox/builder.rb, line 90
def pdf_target
  Pathname.new(config['target']['pdf'])
end
prepare_target_dirs() click to toggle source
# File lib/slidox/builder.rb, line 61
def prepare_target_dirs
  FileUtils::mkdir_p(html_target.dirname)
  FileUtils::mkdir_p(pdf_target.dirname)
end
root_path() click to toggle source
# File lib/slidox/builder.rb, line 98
def root_path
  Dir.pwd
end
save_html(html) click to toggle source
# File lib/slidox/builder.rb, line 66
def save_html(html)
  File.write(html_target, html)
end
save_pdf(html) click to toggle source
# File lib/slidox/builder.rb, line 70
def save_pdf(html)
  PDFKit.new(html, pdf_options).to_file(pdf_target)
end
slides() click to toggle source
# File lib/slidox/builder.rb, line 23
def slides
  Dir.glob("#{slides_path}/*.md").to_a.sort.map do |file|
    text = File.read(file)

    html = GitHub::Markdown.to_html(text, :markdown)

    doc = Nokogiri::HTML::DocumentFragment.parse(html)

    doc.search('pre').each do |node|
      next unless lang = node['lang']

      html = Pygments.highlight(node.inner_text, lexer: lang.to_sym, options: {linespans: 'line'})
      node = node.replace(html).first
      node["class"] = [node["class"], "highlight-#{lang}"].compact.join(" ")
    end

    doc.search('img').each do |node|
      node['src'] = "#{assets_path}/#{node['src']}"
    end

    doc.to_s
  end

end
slides_path() click to toggle source
# File lib/slidox/builder.rb, line 74
def slides_path
  File.join(root_path, 'slides')
end
styles() click to toggle source
# File lib/slidox/builder.rb, line 48
def styles
  styles = Dir.glob("#{assets_path}/*.css").map { |file| File.read(file) }
  styles = styles.map { |style| "<style>#{style}</style>" }.join

  styles.gsub(/url\(['"](?<file>.*)['"]\)/) do |match|
    if $~[:file][0] == "/"
      match
    else
      match.gsub($~[:file], "#{assets_path}/#{$~[:file]}")
    end
  end
end