How to call Jekyll commands from Ruby -


i have rails app creates/builds jekyll sites on same server. right now, i'm calling jekyll commands backticks this:

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

this works fine feels little un-ruby like. if jekyll gem in rails gemfile, is there way can build jekyll site using ruby?

(from docs, looks call jekyll::commands::build.build i'm not sure how initialize site parameter).

tl;dr

require 'jekyll'  conf = jekyll.configuration({   'source'      => 'path/to/source',   'destination' => 'path/to/destination' })  jekyll::site.new(conf).process 

but how did find out?

i figured out looking @ the source code. when run jekyll build, enter source file bin/jekyll. interesting part here is

command :build |c|   # ommitted    c.action |args, options|     options = normalize_options(options.__hash__)     options = jekyll.configuration(options)     jekyll::commands::build.process(options)   end end 

hm, looks actual work done in jekyll::commands::build.process, let's take method in lib/jekyll/commands/build.rb:

def self.process(options)   site = jekyll::site.new(options)   self.build(site, options)    # other stuff end 

again, actual magic happens somewhere else, namely in jekyll::commands::build.build, in lib/jekyll/commands/build.rb

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

this in turn calls class method called process_site, comes superclass jekyll::command defined in lib/jekyll/command.rb

def self.process_site(site)   site.process rescue jekyll::fatalexception => e   # error handling end 

so want call process on jekyll::site. 1 thing have yet find out how specify options jekyll::site instance. let's take closer @ 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 

so apparently need supply hash 'source' , 'destination' keys pointing desired directories. rest of configuration generated jekyll jekyll.configuration method saw earlier in bin/jekyll. that's it. now, thing left putting pieces ;-)


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -