lib/liquidoc.rb in liquidoc-0.8.1 vs lib/liquidoc.rb in liquidoc-0.9.0
- old
+ new
@@ -43,10 +43,13 @@
@pdf_theme_file = 'theme/pdf-theme.yml'
@fonts_dir = 'theme/fonts/'
@output_filename = 'index'
@attributes = {}
@passed_attrs = {}
+@passed_vars = {}
+@passed_configvars = {}
+@parseconfig = false
@verbose = false
@quiet = false
@explicit = false
# Instantiate the main Logger object, which is always running
@@ -64,12 +67,20 @@
# ===
# Executive procs
# ===
# Establish source, template, index, etc details for build jobs from a config file
-def config_build config_file
+def config_build config_file, config_vars={}, parse=false
@logger.debug "Using config file #{config_file}."
+ if config_vars or parse
+ # If config variables are passed on the CLI, we want to parse the config file
+ # and use the parsed version for the rest fo this routine
+ config_out = "#{@build_dir}/pre/#{File.basename(config_file)}"
+ liquify(nil,config_file, config_out, config_vars)
+ config_file = config_out
+ @logger.debug "Config parsed! Using #{config_out} for build."
+ end
validate_file_input(config_file, "config")
begin
config = YAML.load_file(config_file)
rescue
unless File.exists?(config_file)
@@ -90,16 +101,19 @@
step = BuildConfigStep.new(step) # create an instance of the Action class, validating the top-level step hash (now called 'step') in the process
@explainer.info step.message
type = step.type
case type # a switch to evaluate the 'action' parameter for each step in the iteration...
when "parse"
- data = DataSrc.new(step.data)
+ if step.data
+ data = DataSrc.new(step.data)
+ end
builds = step.builds
- for bld in builds
+ builds.each do |bld|
build = Build.new(bld, type) # create an instance of the Build class; Build.new accepts a 'bld' hash & action 'type'
if build.template
@explainer.info build.message
+ build.add_vars!(@passed_vars) unless @passed_vars.empty?
liquify(data, build.template, build.output, build.variables) # perform the liquify operation
else
regurgidata(data, build.output)
end
end
@@ -118,11 +132,11 @@
build.set("backend", derive_backend(doc.type, build.output) ) unless build.backend
@explainer.info build.message
render_doc(doc, build) # perform the render operation
end
when "deploy"
- @logger.warn "Deploy actions are limited and experimental experimental."
+ @logger.warn "Deploy actions are limited and experimental."
jekyll_serve(build)
else
@logger.warn "The action `#{type}` is not valid."
end
end
@@ -274,11 +288,15 @@
text.concat("\n")
else
text = ". #{stage}Draws data from `#{self.data[0]}`"
end
else
- text = ". #{stage}Draws data from `#{self.data['file']}`"
+ if self.data
+ text = ". #{stage}Draws data from `#{self.data['file']}`"
+ else
+ text = ". #{stage}Uses data passed via CLI --var options."
+ end
end
text.concat("#{reason},") if reason
text.concat(" and parses it as follows:")
return text
when "render"
@@ -354,10 +372,15 @@
def variables
@build['variables']
end
+ def add_vars! vars
+ vars.to_h unless vars.is_a? Hash
+ self.variables.merge!vars
+ end
+
def message
# dynamically build a message, possibly appending a reason
unless @build['message']
reason = ", #{@build['reason']}" if @build['reason']
case @type
@@ -662,13 +685,23 @@
return output
end
# Parse given data using given template, generating given output
def liquify datasrc, template_file, output, variables=nil
- input = get_data(datasrc)
- nested = { "data" => get_data(datasrc)}
- input.merge!nested
+ if datasrc
+ input = get_data(datasrc)
+ nested = { "data" => get_data(datasrc)}
+ input.merge!nested
+ end
+ if variables
+ if input
+ input.merge!variables
+ else
+ input = variables
+ end
+ end
+ @logger.error "Parse operations need at least a data file or variables." unless input
validate_file_input(template_file, "template")
if variables
vars = { "vars" => variables }
input.merge!vars
end
@@ -904,10 +937,11 @@
# DEPLOY procs
# ===
def jekyll_serve build
# Locally serve Jekyll as per the primary Jekyll config file
+ @logger.debug "Attempting Jekyll serve operation."
config_file = build.props['files'][0]
if build.props['arguments']
opts_args = build.props['arguments'].to_opts_args
end
command = "bundle exec jekyll serve --config #{config_file} #{opts_args} --no-watch --skip-initial-build"
@@ -1094,10 +1128,28 @@
opts.on("--deploy", "EXPERIMENTAL: Trigger a jekyll serve operation against the destination dir of a Jekyll render step.") do
@jekyll_serve = true
end
+ opts.on("--var KEY=VALUE", "For passing variables directly to the 'vars.' scope template via command line, for non-config builds only.") do |n|
+ pair = {}
+ k,v = n.split('=')
+ pair[k] = v
+ @passed_vars.merge!pair
+ end
+
+ opts.on("-x", "--cvar KEY=VALUE", "For sending variables to the 'vars.' scope of the config file and triggering Liquid parsing of config.") do |n|
+ pair = {}
+ k,v = n.split('=')
+ pair[k] = v
+ @passed_configvars.merge!pair
+ end
+
+ opts.on("--parse-config", "Preprocess the designated configuration file as a Liquid template. Superfluous when passing -x/--cvar arguments.") do
+ @parseconfig = true
+ end
+
opts.on("-h", "--help", "Returns help.") do
puts opts
exit
end
@@ -1115,14 +1167,14 @@
# ===
unless @config_file
@logger.debug "Executing config-free build based on API/CLI arguments alone."
if @data_file
- liquify(@data_file, @template_file, @output_file)
+ liquify(@data_file, @template_file, @output_file, @passed_vars)
end
if @index_file
@logger.warn "Rendering via command line arguments is not yet implemented. Use a config file."
end
else
@logger.debug "Executing... config_build"
- config_build(@config_file)
+ config_build(@config_file, @passed_configvars, @parseconfig)
end