Top Level Namespace

Defined Under Namespace

Modules: ForceArray, Jekyll, LiquiDocFilters, Liquidoc Classes: Array, AsciiDocument, Build, BuildConfig, BuildConfigStep, DataFiles, DataObj, DataSrc, Hash, String

Instance Method Summary collapse

Instance Method Details

#algolia_index_cmd(build, apikey = nil, args) ⇒ Object



1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
# File 'lib/liquidoc.rb', line 1154

def algolia_index_cmd build, apikey=nil, args
  unless build.search and build.search['index']
    @logger.warn "No index configuration found for build; jekyll-algolia operation skipped for this build."
    return false
  else
    unless apikey
      @logger.warn "No Algolia admin API key passed; skipping jekyll-algolia operation for this build."
      return false
    else
      return "ALGOLIA_INDEX_NAME='#{build.search['index']}' ALGOLIA_API_KEY='#{apikey}' bundle exec jekyll algolia #{@search_index_dry} #{args} "
    end
  end
end

#asciidocify(doc, build) ⇒ Object



1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
# File 'lib/liquidoc.rb', line 1041

def asciidocify doc, build
  @logger.debug "Executing Asciidoctor render operation for #{build.output}."
  to_file = build.output
  unless doc.type == build.doctype
    if build.doctype.nil? # set a default doctype equal to our LiquiDoc action doc type
      build.set("doctype", doc.type)
    end
  end
  # unfortunately we have to treat attributes accumilation differently for Jekyll vs Asciidoctor
  attrs = doc.attributes # Start with attributes added at the action level; no more writing to doc obj
  # Handle properties files array as attributes files and
  # add the ingested attributes to local var
  begin
    if build.prop_files_array
      ingested = ingest_attributes(build.prop_files_array)
      attrs.merge!(ingested)
    else
      puts build.prop_files_array
    end
  rescue Exception => ex
    @logger.warn "Attributes failed to merge. #{ex}" # Shd only trigger if build.props exists
    raise
  end
  if build.backend == "html5" # Insert a stylesheet
    attrs.merge!({"stylesheet"=>build.style}) if build.style
  end
  # Add attributes from config file build section
  attrs.merge!(build.attributes) # Finally merge attributes from the build step
  # Add attributes from command-line -a args
  @logger.debug "Final pre-Asciidoctor attributes: #{attrs.to_yaml}"
  # Perform the aciidoctor convert
  if build.backend == "pdf"
    @logger.info "Generating PDF. This can take some time..."
    attrs.merge!({"pdf-theme"=>build.style}) if build.style
  end
  Asciidoctor.convert_file(
    doc.index,
    to_file: to_file,
    attributes: attrs,
    require: "pdf",
    backend: build.backend,
    doctype: build.doctype,
    safe: "unsafe",
    sourcemap: true,
    verbose: @verbose,
    mkdirs: true,
  )
  @logger.info "Rendered file #{to_file}."
end

#cli_liquify(data_files = nil, template_file = nil, output_file = nil, passed_vars) ⇒ Object



889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
# File 'lib/liquidoc.rb', line 889

def cli_liquify data_files=nil, template_file=nil, output_file=nil, passed_vars
  # converts command-line options into liquify or regurgidata inputs
  data_obj = DataObj.new()
  if output_file
    output = output_file
  else
    output = "stdout"
  end
  if data_files
    payload = get_payload(data_files)
    data_obj.add_payload!(payload)
  end
  if template_file
    # data_obj.add_data!(ingested, "data") if df
    data_obj.add_data!(passed_vars, "vars") if passed_vars
    liquify(data_obj, template_file, output)
  else
    data_obj.remove_scope("vars")
    data_obj.add_data!(passed_vars) if passed_vars
    regurgidata(data_obj, output)
  end
end

#config_build(config_file, config_vars = {}, data_files = nil, parse = false) ⇒ Object

Establish source, template, index, etc details for build jobs from a config file



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/liquidoc.rb', line 84

def config_build config_file, config_vars={}, data_files=nil, parse=false
  @logger.debug "Using config file #{config_file}."
  validate_file_input(config_file, "config")
  config_base = File.read(config_file)
  if config_vars.length > 0 or data_files or parse or config_base.contains_liquid?
    @logger.debug "Config_vars: #{config_vars.length}"
  # 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)}"
    data_obj = DataObj.new()
    if data_files
      payload = get_payload(data_files)
      data_obj.add_payload!(payload)
    end
    data_obj.add_data!(config_vars, "vars")
    liquify(data_obj, config_file, config_out)
    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 Exception => ex
    unless File.exists?(config_file)
      @logger.error "Config file #{config_file} not found."
    else
      @logger.error "Problem loading config file #{config_file}. #{ex} Exiting."
    end
    raise "ConfigFileError"
  end
  # TESTS
  # puts config[0].argify
  cfg = BuildConfig.new(config) # convert the config file to a new object called 'cfg'
  if @safemode
    commands = ""
    cfg.steps.each do |step|
      if step['action'] == "execute"
        commands = commands + "> " + step['command'] + "\n"
      end
    end
    unless commands.to_s.strip.empty?
      puts "\nWARNING: This routine will execute the following shell commands:\n\n#{commands}"
      ui = HighLine.new
      answer = ui.ask("\nDo you approve? (YES/no): ")
      raise "CommandExecutionsNotAuthorized" unless answer.strip == "YES"
    end
  end
  iterate_build(cfg)
end

#copy_assets(src, dest, inclusive = true, missing = 'exit') ⇒ Object

Copy images and other files into target dir



941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
# File 'lib/liquidoc.rb', line 941

def copy_assets src, dest, inclusive=true, missing='exit'
  unless File.file?(src)
    unless inclusive then src = src + "/." end
  end
  src_to_dest = "#{src} to #{dest}"
  unless (File.file?(src) || File.directory?(src))
    case missing
    when "warn"
      @logger.warn "Skipping migrate action (#{src_to_dest}); source not found."
      return
    when "skip"
      @logger.debug "Skipping migrate action (#{src_to_dest}); source not found."
      return
    when "exit"
      @logger.error "Unexpected missing source in migrate action (#{src_to_dest})."
      raise "MissingSourceExit"
    end
  end
  @logger.debug "Copying #{src_to_dest}"
  begin
    FileUtils.mkdir_p(dest) unless File.directory?(dest)
    if File.directory?(src)
      FileUtils.cp_r(src, dest)
    else
      FileUtils.cp(src, dest)
    end
    @logger.info "Copied #{src} to #{dest}."
  rescue Exception => ex
    @logger.error "Problem while copying assets. #{ex.message}"
    raise
  end
end

#derive_backend(type, out_file) ⇒ Object



1018
1019
1020
1021
1022
1023
1024
1025
1026
# File 'lib/liquidoc.rb', line 1018

def derive_backend type, out_file
  case File.extname(out_file)
  when ".pdf"
    backend = "pdf"
  else
    backend = "html5"
  end
  return backend
end

#execute_command(cmd) ⇒ Object

Execute



1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
# File 'lib/liquidoc.rb', line 1172

def execute_command cmd
  stdout, stderr, status = Open3.capture3(cmd.command)
  failed = true if status.to_s.include?("exit 1")
  unless cmd.options
    puts stdout
    puts stderr if failed
  else
    if failed && cmd.options['error']
      @logger.warn cmd.options['error']['message'] if cmd.options['error']['message']
      if cmd.options['error']['response'] == "exit"
        @logger.error "Command failure: #{stderr}"
        raise "CommandExecutionException"
      end
    end
    if cmd.options['outfile']
      contents = stdout
      if cmd.options['outfile']
        contents = "#{cmd.options['outfile']['prepend']}\n#{stdout}" if cmd.options['outfile']['prepend']
        contents = "#{stdout}\n#{cmd.options['outfile']['append']}" if cmd.options['outfile']['append']
        generate_file(contents, cmd.options['outfile']['path'])
      end
      if cmd.options['stdout']
        puts stdout
      end
    end
  end
end

#explainer_init(out = nil) ⇒ Object



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/liquidoc.rb', line 224

def explainer_init out=nil
  unless @explainer
    if out == "STDOUT"
      @explainer = Logger.new(STDOUT)
    else
      out = "#{@build_dir}/pre/config-explainer.adoc" if out.nil?
      File.open(out, 'w') unless File.exists?(out)
      file = File.open(out, File::WRONLY)
      begin
        @explainer = Logger.new(file)
      rescue Exception => ex
        @logger.error ex
        raise "ExplainerCreateError"
      end
    end
    @explainer.formatter = proc do |severity, datetime, progname, msg|
      "#{msg}\n"
    end
  end
end

#generate_file(content, target) ⇒ Object



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/liquidoc.rb', line 245

def generate_file content, target
  base_path = File.dirname(target)
  begin
    FileUtils::mkdir_p(base_path) unless File.exists?(base_path)
    File.open(target, 'w') { |file| file.write(content) } # saves file
  rescue Exception => ex
    @logger.error "Failed to save output.\n#{ex.class} #{ex.message}"
    raise "FileNotBuilt"
  end
  if File.exists?(target)
    @logger.info "File built: #{target}"
  else
    @logger.error "Hrmp! File not built."
    raise "FileNotBuilt"
  end
end

#generate_site(doc, build) ⇒ Object



1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
# File 'lib/liquidoc.rb', line 1091

def generate_site doc, build
  case build.backend
  when "jekyll"
    attrs = doc.attributes
    build.add_config_file("_config.yml") unless build.prop_files_array
    jekyll = load_jekyll_data(build) # load the first Jekyll config file locally
    attrs.merge! ({"base_dir" => jekyll['source']}) # Sets default Asciidoctor base_dir to == Jekyll root
    # write all AsciiDoc attributes to a config file for Jekyll to ingest
    attrs.merge!(build.attributes) if build.attributes
    attrs = {"asciidoctor" => {"attributes" => attrs} }
    attrs_yaml = attrs.to_yaml # Convert it all back to Yaml, as we're going to write a file to feed back to Jekyll
    File.open("#{@build_dir}/pre/attributes_#{@render_count}.yml", 'w') { |file| file.write(attrs_yaml) }
    build.add_config_file("#{@build_dir}/pre/attributes_#{@render_count}.yml")
    config_list = build.prop_files_array.join(',') # flatten the Array back down for the CLI
    quiet = "--quiet" if @quiet || @explicit
    if build.props['arguments']
      opts_args_file = "#{@build_dir}/pre/jekyll_opts_args_#{@render_count}.yml"
      opts_args = build.props['arguments']
      File.open(opts_args_file, 'w') { |file|
      file.write(opts_args.to_yaml)}
      config_list << ",#{opts_args_file}"
    end
    base_args = "--config #{config_list}"
    base_args += " --trace" if @verbose
    command = "bundle exec jekyll build #{base_args} #{quiet}"
    if @search_index
      # TODO enable config-based admin api key ingest once config is dynamic
      command = algolia_index_cmd(build, @search_api_key, base_args)
      @logger.warn "Search indexing failed." unless command
    end
  end
  if command
    @logger.info "Running #{command}"
    @logger.debug "Final pre-jekyll-asciidoc attributes: #{doc.attributes.to_yaml} "
    system command
  end
  jekyll_serve(build) if @jekyll_serve
end

#get_payload(data_files) ⇒ Object

Action-specific procs

PARSE-type build procs



770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
# File 'lib/liquidoc.rb', line 770

def get_payload data_files
  # data_files: a proper DataFile object
  payload = []
  data_files.sources.each do |src|
    obj = {}
    begin
      data = ingest_data(src) # Extract data from file
    rescue Exception => ex
      @logger.error "#{ex.class}: #{ex.message}"
      raise "DataFileReadFail (#{src.file})"
    end
    obj['name'] = src.name
    obj['data'] = data
    payload << obj
  end
  return payload
end

#ingest_attributes(attr_file) ⇒ Object

Gather attributes from one or more fixed attributes files



979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
# File 'lib/liquidoc.rb', line 979

def ingest_attributes attr_file
  attr_files_array = attr_file.force_array
  attrs = {}
  attr_files_array.each do |f|
    if f.include? ":"
      file = f.split(":")
      filename = file[0]
      block_name = file[1]
    else
      filename = f
      block_name = false
    end
    validate_file_input(filename, "attributes")
    begin
      new_attrs = YAML.load_file(filename)
      if block_name
        begin
          new_attrs = new_attrs[block_name]
        rescue
          raise "InvalidAttributesBlock (#{filename}:#{block_name})"
        end
      end
    rescue Exception => ex
      @logger.error "Attributes block invalid. #{ex.class}: #{ex.message}"
      raise "AttributeBlockError"
    end
    begin
      if new_attrs.is_a? Hash
        attrs.merge!new_attrs
      else
        @logger.warn "The AsciiDoc attributes file #{filename} is not formatted as a hash, so its data was not ingested."
      end
    rescue Exception => ex
      raise "AttributesMergeError #{ex.message}"
    end
  end
  return attrs
end

#ingest_data(datasrc) ⇒ Object

Pull in a semi-structured data file, converting contents to a Ruby object



789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
# File 'lib/liquidoc.rb', line 789

def ingest_data datasrc
  raise "InvalidDataSrcObject" unless datasrc.is_a? DataSrc
  case datasrc.type
  when "yml"
    begin
      data = YAML.load_file(datasrc.file)
    rescue Exception => ex
      @logger.error "There was a problem with the data file. #{ex.message}"
    end
  when "json"
    begin
      data = JSON.parse(File.read(datasrc.file))
    rescue Exception => ex
      @logger.error "There was a problem with the data file. #{ex.message}"
    end
  when "xml"
    begin
      data = Crack::XML.parse(File.read(datasrc.file))
      data = data['root']
    rescue Exception => ex
      @logger.error "There was a problem with the data file. #{ex.message}"
    end
  when "csv"
    data = []
    i = 0
    begin
      CSV.foreach(datasrc.file, headers: true, skip_blanks: true) do |row|
        data[i] = row.to_hash
        i = i+1
      end
    rescue
      @logger.error "The CSV format is invalid."
    end
  when "regex"
    if datasrc.pattern
      data = parse_regex(datasrc.file, datasrc.pattern)
    else
      @logger.error "You must supply a regex pattern with your free-form data file."
      raise "MissingRegexPattern"
    end
  when "adoc"
    begin
      doc = Asciidoctor.load_file(datasrc.file)
      data = doc.attributes
    rescue
      @logger.error "Problem with AsciiDoc source file. Attributes not ingested."
    end
  end
  return data
end

#iterate_build(cfg) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/liquidoc.rb', line 134

def iterate_build cfg
  stepcount = 0
  for step in cfg.steps # iterate through each node in the 'config' object, which should start with an 'action' parameter
    stepcount = stepcount + 1
    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"
      builds = step.builds
      data_obj = DataObj.new()
      if step.data
        data_files = DataFiles.new(step.data)
        payload = get_payload(data_files)
        data_obj.add_payload!(payload)
      end
      builds.each do |bld|
        build = Build.new(bld, type, data_obj) # create an instance of the Build class; Build.new accepts a 'bld' hash & action 'type'
        if build.template
          # Prep & perform a Liquid-parsed build
          @explainer.info build.message
          build.add_data!(build.variables, "vars") if build.variables
          includes_dirs = @includes_dirs
          includes_dirs = build.includes_dirs if build.includes_dirs
          build.add_data!({:includes_dirs=>includes_dirs})
          liquify(build.data, build.template, build.output) # perform the liquify operation
        else # Prep & perform a direct conversion
          # Delete nested data and vars objects
          build.data.remove_scope("data")
          build.data.remove_scope("vars")
          # Add vars from CLI or config args
          build.data.add_data!(build.variables) unless build.variables.empty?
          regurgidata(build.data, build.output)
        end
      end
    when "migrate"
      inclusive = true
      missing = "exit"
      if step.options
        inclusive = step.options['inclusive'] if step.options.has_key?("inclusive")
        missing = step.options['missing'] if step.options.has_key?("missing")
      end
      copy_assets(step.source, step.target, inclusive, missing)
    when "render"
      validate_file_input(step.source, "source") if step.source
      builds = step.builds
      for bld in builds
        doc = AsciiDocument.new(step.source)
        if step.data
          attrs = ingest_attributes(step.data)
          doc.add_attrs!(attrs) # Set attributes from the action-level data file
        end
        build = Build.new(bld, type) # create an instance of the Build class; Build.new accepts a 'bld' hash & action 'type' string
        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."
      jekyll_serve(build)
    when "execute"
      @logger.info "Executing shell command: #{step.command}"
      execute_command(step)
    else
      @logger.warn "The action `#{type}` is not valid."
    end
  end
end

#jekyll_serve(build) ⇒ Object

DEPLOY procs



1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
# File 'lib/liquidoc.rb', line 1143

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'].argify
  end
  command = "bundle exec jekyll serve --config #{config_file} #{opts_args} --no-watch --skip-initial-build"
  system command
end

#liquify(data_obj, template_file, output = "stdout") ⇒ Object

Parse given data using given template, generating given output



867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
# File 'lib/liquidoc.rb', line 867

def liquify data_obj, template_file, output="stdout"
  validate_file_input(template_file, "template")
  # inject :includes_dirs as needed
  data_obj.add_data!({'includes_dirs' => @includes_dirs}) unless data_obj.data['includes_dirs']
  begin
    template = File.read(template_file) # reads the template file
    template = Liquid::Template.parse(template) # compiles template
    rendered = template.render(data_obj.data) # renders the output
  rescue Exception => ex
    message = "Problem rendering Liquid template. #{template_file}\n" \
      "#{ex.class} thrown. #{ex.message}"
    @logger.error message
    raise message
  end
  unless output == "stdout" || @output_type == "stdout"
    output_file = output
    generate_file(rendered, output_file)
  else # if stdout
    puts rendered
  end
end

#load_jekyll_data(build) ⇒ Object



1130
1131
1132
1133
1134
1135
1136
1137
# File 'lib/liquidoc.rb', line 1130

def load_jekyll_data build
  data = {}
  build.prop_files_array.each do |file|
    settings = YAML.load_file(file)
    data.merge!settings if settings
  end
  return data
end

#parse_regex(data_file, pattern) ⇒ Object



840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
# File 'lib/liquidoc.rb', line 840

def parse_regex data_file, pattern
  records = []
  pattern_re = /#{pattern}/
  @logger.debug "Using regular expression #{pattern} to parse data file."
  groups = pattern_re.names
  begin
    File.open(data_file, "r") do |file_proc|
      file_proc.each_line do |row|
        matches = row.match(pattern_re)
        if matches
          row_h = {}
          groups.each do |var| # loop over the named groups, adding their key & value to the row_h hash
            row_h.merge!(var => matches[var])
          end
          records << row_h # add the row to the records array
        end
      end
    end
    output = records
  rescue Exception => ex
    @logger.error "Something went wrong trying to parse the free-form file. #{ex.class} thrown. #{ex.message}"
    raise "Freeform parse error"
  end
  return output
end

#regurgidata(data_obj, output) ⇒ Object



912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
# File 'lib/liquidoc.rb', line 912

def regurgidata data_obj, output
  # converts data object from one format directly to another
  raise "UnrecognizedFileExtension" unless File.extname(output).match(/\.yml|\.json|\.xml|\.csv/)
  case File.extname(output)
    when ".yml"
      new_data = data_obj.data.to_yaml
    when ".json"
      new_data = data_obj.data.to_json
    when ".xml"
      @logger.warn "XML output not yet implemented."
    when ".csv"
      @logger.warn "CSV output not yet implemented."
  end
  if new_data
    begin
      generate_file(new_data, output)
      @logger.info "Data converted and saved to #{output}."
    rescue Exception => ex
      @logger.error "#{ex.class}: #{ex.message}"
      raise "FileWriteError"
    end
  end
end

#render_doc(doc, build) ⇒ Object



1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
# File 'lib/liquidoc.rb', line 1028

def render_doc doc, build
  @render_count += 1
  @logger.info "### Build ##{@render_count}"
  case build.backend
  when "html5", "pdf"
    asciidocify(doc, build)
  when "jekyll"
    generate_site(doc, build)
  else
    raise "UnrecognizedBackend"
  end
end

#validate_file_input(file, type) ⇒ Object

Verify files exist



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/liquidoc.rb', line 208

def validate_file_input file, type
  @logger.debug "Validating input file for #{type} file #{file}"
  error = false
  unless file.is_a?(String) and !file.nil?
    error = "The #{type} filename (#{file}) is not valid."
  else
    unless File.exists?(file)
      error = "The #{type} file (#{file}) was not found."
    end
  end
  if error
    @logger.error "Could not validate input file: #{error}"
    raise "InvalidInput"
  end
end