class MdtranslatorCLI

Public Class Methods

exit_on_failure?() click to toggle source

exit_on_failure added to exit with code 1 if thor cannot complete task such as if required parameters are missing

# File lib/adiwg/mdtranslator_cli.rb, line 24
def self.exit_on_failure?
   true
end

Public Instance Methods

translate(file) click to toggle source

accept command and options

# File lib/adiwg/mdtranslator_cli.rb, line 57
def translate(file)

   # test to see if file parameter is a local file name
   # if not ... assumed it is a JSON string
   # note: this will need to be modified if/when a reader is added that is not in JSON format
   if File.exist?(file)
      # read file
      my_file = File.open(file, 'r')
      fileObj = my_file.read
      my_file.close
   else
      fileObj = file
   end

   # for testing parameters
   # puts 'reader: ' + options[:reader]
   # puts 'writer: ' + options[:writer]
   # puts 'validation level: ' + options[:validate]
   # puts 'showAllTags: ' + options[:showAllTags].to_s
   # puts 'forceValid: ' + options[:forceValid].to_s
   # puts 'message format: ' + options[:messages]
   # puts 'return object: ' + options[:returnObject].to_s
   # puts 'css link: ' + options[:cssLink]
   # puts 'forceValid: ' + options[:forceValid].to_s

   # call mdtranslator
   mdReturn = ADIWG::Mdtranslator.translate(
      file: fileObj,
      reader: options[:reader],
      writer: options[:writer],
      validate: options[:validate],
      forceValid: options[:forceValid],
      showAllTags: options[:showAllTags],
      cssLink: options[:cssLink])

   # determine return content and type of return ...
   if mdReturn[:readerStructurePass] && mdReturn[:readerValidationPass] && mdReturn[:readerExecutionPass]

      # no problem was found with the input file
      if options[:writer].nil?
         # if no writer was specified the input was being validated only,
         # ...no writer output will have been generated,
         # ...and the return will be a string unless json was requested
         if options[:messages] == 'json'
            $stdout.write mdReturn.to_json
            return
         else
            $stdout.write 'Success'
            return
         end
      else
         # a writer was specified,
         # output is expected from the translator's writer
         if mdReturn[:writerPass]
            # writer output was generated
            # ...return the writer output in its native format unless json was requested
            if options[:returnObject]
               $stdout.write mdReturn.to_json
               return
            else
               $stdout.write mdReturn[:writerOutput].to_s
               return
            end
         else
            # the writer failed or generated warnings to be reported
            # ...return the messages as a string unless json was requested
            if options[:messages] == 'json'
               $stdout.write mdReturn.to_json
               return
            else
               # build a string with messages issues from parser and validator
               s = ''
               s += "Failed\n"
               s += "Writer failed to generate output or issued ERROR OR WARNING messages \n"
               s += "See following messages for further information\n"

               # post structure messages
               i = 0
               mdReturn[:writerMessages].each do |message|
                  i += 1
                  s += "\nMessage: #{i}\n"
                  s += message + "\n"
               end

               $stdout.write s
               return

            end
         end
      end

   else

      # problems were found with the input file

      # if no writer was specified the input was being validated only,
      # ...no writer output will have been generated,
      # ...and return is always expected to be a string
      if options[:messages] == 'json'
         $stdout.write mdReturn.to_json
         return
      else
         # build a string with messages issued from parser, validator, or reader
         s = ''
         s += "Failed\n"
         s += "Input failed to pass either file structure, validation, or content requirements\n"
         s += "See following messages for further information\n"

         # post structure messages
         if mdReturn[:readerStructurePass]
            s += "Success - Input structure is valid\n"
         else
            s += "Fail - Structure of input file is invalid - see following message(s):\n"
            i = 0
            mdReturn[:readerStructureMessages].each do |message|
               i += 1
               s += "\nMessage: #{i}\n"
               s += message.to_s + "\n"
            end
         end

         # post validator messages
         unless mdReturn[:readerValidationPass].nil?
            if mdReturn[:readerValidationPass]
               s += "Success - Input content passes schema definition\n"
            else
               s += "Fail - Input content did not pass schema validation - see following message(s):\n"
               i = 0
               mdReturn[:readerValidationMessages].each do |message|
                  i += 1
                  s += "\nMessage: #{i}\n"
                  s += message.to_s + "\n"
               end
            end
         end

         # post reader execution messages
         unless mdReturn[:readerExecutionPass].nil?
            if mdReturn[:readerExecutionPass]
               s += "Success - Reader execution successful\n"
            else
               s += "Fail - Reader execution failed - see following message(s):\n"
               i = 0
               mdReturn[:readerExecutionMessages].each do |message|
                  i += 1
                  s += "\nMessage: #{i}\n"
                  s += message.to_s + "\n"
               end
            end
         end

         $stdout.write s
         return

      end
   end

end
version() click to toggle source
# File lib/adiwg/mdtranslator_cli.rb, line 221
def version
   $stdout.write ADIWG::Mdtranslator::VERSION
end