class Miyano::Builder

Public Class Methods

new() click to toggle source
# File lib/miyano/builder.rb, line 10
def initialize
  @site = Site.new
end

Public Instance Methods

build_the_world() click to toggle source
# File lib/miyano/builder.rb, line 14
def build_the_world
  # post
  ["bear","html"].each do |attr|
    send "build_#{attr}"
  end

  # layout
  ["default_css", "index", "tags"].each do |attr|
    send "build_#{attr}"
  end
end

Protected Instance Methods

auto_orient_imgs(dir) click to toggle source
# File lib/miyano/build/bear.rb, line 62
def auto_orient_imgs(dir)
  return unless check_imagemagick
  imgs = Dir["#{dir}/*.{jpg,jpeg,JPG,JPEG}"]
  imgs.each do |img|
    `convert -auto-orient #{img.inspect} #{img.inspect}`
  end
end
build_bear() click to toggle source
# File lib/miyano/build/bear.rb, line 8
def build_bear
  require "beardown/compat" if File.exist? "post/.compat"
  files = Dir["post/*.bearnote"]
  files.each do |f|
    name = File.basename(f, ".*")
    bearnote = Zip::File.open f

    # the name of the zip file maybe different from the name of zip dir
    # i.e. first zip as `foo.zip`, the zip dir is `foo`, then rename to `bar.zip`, the zip dir still remains `foo`
    #zipdir = File.dirname bearnote.entries[0].name
    #zipdir = zipdir.force_encoding "UTF-8"
    zipdir = getZipDir bearnote.entries[0].name
    assets = bearnote.glob("#{zipdir}/assets/*")
    dir = "_site/#{name}/assets"
    assets.each do |asset|
      FileUtils.mkdir_p dir
      path = File.join dir, File.basename(asset.name)
      asset.extract path unless File.exist? path
    end

    text = bearnote.glob("#{zipdir}/text.txt")
                   .first.get_input_stream
                   .read.force_encoding("UTF-8")

    FileUtils.mkdir_p "_site/#{name}"
    File.open("_site/#{name}/index.html", "w") do |html|
      html.write render_bear(f, text)
    end
    auto_orient_imgs(dir) if Dir.exist? dir
  end
end
build_default_css() click to toggle source
# File lib/miyano/build/default_css.rb, line 4
def build_default_css
  css = "layout/default.scss"
  template = Tilt.new css
  File.open("_site/default.css", "w") do |f|
    f.write template.render
  end
end
build_html() click to toggle source
# File lib/miyano/build/html.rb, line 5
def build_html
  files = Dir["post/*.html"]
  files.each do |f|
    name = File.basename(f, ".*")
    FileUtils.mkdir_p "_site/#{name}"
    FileUtils.cp f, "_site/#{name}/index.html"
    render_html f
  end
end
build_index() click to toggle source
# File lib/miyano/build/index.rb, line 4
def build_index
  template = Tilt.new "layout/index.html.erb"
  File.open("_site/index.html", "w") do |html|
    html.write template.render @site
  end
end
build_tags() click to toggle source
# File lib/miyano/build/tags.rb, line 4
def build_tags
  @site._tags.each do |tag, _|
    site = Site.new
    site.current_tag = tag
    @site.posts.each do |post|
      site.add_post post if post.tags.include? tag
    end
    template = Tilt.new "layout/index.html.erb"
    FileUtils.mkdir_p "_site/tag/#{tag}"
    File.open("_site/tag/#{tag}/index.html", "w") do |html|
      html.write template.render site
    end
  end
end
check_imagemagick() click to toggle source
# File lib/miyano/build/bear.rb, line 70
def check_imagemagick
  system "convert -version >/dev/null"
  system "brew install imagemagick graphicsmagick" unless $?.success?
  unless $?.success?
    system "/usr/bin/ruby -e '$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)'"
    system "brew install imagemagick graphicsmagick" if $?.success?
  end
  system "convert -version >/dev/null"
end
getZipDir(str) click to toggle source
# File lib/miyano/build/bear.rb, line 55
def getZipDir(str)
  while(str.include? "/")
    str = File.dirname str
  end
  return str
end
render_bear(f, text) click to toggle source
# File lib/miyano/build/bear.rb, line 40
def render_bear(f, text)
  doc = Beardown.new text
  name = File.basename f, ".*"
  name = name
  cre_date = File.birthtime f
  mod_date = File.mtime f

  post = Post.new name, cre_date, mod_date, doc.title,
                  doc.html, doc.content, doc.tags
  @site.add_post post

  template = Tilt.new "layout/post.html.erb"
  template.render post
end
render_html(f) click to toggle source
# File lib/miyano/build/html.rb, line 15
def render_html(f)
  name = File.basename f, ".*"
  cre_date = File.birthtime f
  mod_date = File.mtime f

  post = Post.new name, cre_date, mod_date, name
  @site.add_post post
end