4

同じサーバー上にいくつかの Jekyll サイトを作成/構築する Rails アプリがあります。現在、私は次のようなバッククォートを使用して Jekyll コマンドを呼び出しています。

def build_jekyll
  result = `jekyll build -s /some/source/path -d /some/dest/path`
end

これは問題なく動作しますが、少しルビーらしくありません。jekyll gem が Rails Gemfile にある場合、ruby を使用して jekyll サイトを構築する方法はありますか?

(ドキュメントから、私が呼び出すように見えますJekyll::Commands::Build.buildが、サイトパラメーターを初期化する方法がわかりません)。

4

2 に答える 2

13

TL;DR

require 'jekyll'

conf = Jekyll.configuration({
  'source'      => 'path/to/source',
  'destination' => 'path/to/destination'
})

Jekyll::Site.new(conf).process

でもどうやって分かったの?

ソースコードを見てこれを理解しました。実行するjekyll buildと、ソース ファイルに入りますbin/jekyll。ここで興味深い部分は

command :build do |c|
  # ommitted

  c.action do |args, options|
    options = normalize_options(options.__hash__)
    options = Jekyll.configuration(options)
    Jekyll::Commands::Build.process(options)
  end
end

うーん、実際の作業は で行われているように見えるJekyll::Commands::Build.processので、 でそのメソッドを見てみましょうlib/jekyll/commands/build.rb

def self.process(options)
  site = Jekyll::Site.new(options)
  self.build(site, options)

  # other stuff
end

繰り返しますが、実際の魔法は別の場所で発生Jekyll::Commands::Build.buildします。lib/jekyll/commands/build.rb

def self.build(site, options)
  # some logging going on here

  self.process_site(site)
end

これは、 で定義されprocess_siteたスーパークラスに由来するというクラス メソッドを呼び出します。Jekyll::Commandlib/jekyll/command.rb

def self.process_site(site)
  site.process
rescue Jekyll::FatalException => e
  # some error handling
end

したがって、実際には を呼び出したいと思いprocessますJekyll::SiteJekyll::Siteまだわかっていないことの 1 つは、インスタンスのオプションを指定する方法です。詳しく見てみましょうlib/jekyll/site.rb

def initialize(config)
  # more options ...

  self.source          = File.expand_path(config['source'])
  self.dest            = File.expand_path(config['destination'])

  # more options ...
end

したがって、目的のディレクトリを指す'source'およびキーを含むハッシュを提供する必要があるようです。'destination'残りの構成は、Jekyll.configuration前に で見た方法でJekyll によって生成されbin/jekyllます。それはそれについてです。あとは部品を組み立てるだけです ;-)

于 2013-08-23T18:25:34.633 に答える