#! /usr/bin/env ruby

require 'yaml'
require 'erb'

keywords = []
Dir[File.expand_path ARGV[0]].each do |file|
  content = File.read file
  documentation = content.match(/DOCUMENTATION = r?'''\n(.*?)\n'''/m)
  next unless documentation

  begin
    yaml = YAML::load documentation[1]
    keywords << yaml['module']

    yaml['options'].to_a.each do |option_name, option|
      keywords << option_name

      if option['choices'] == 'BOOLEANS'
          keywords.concat(['yes', 'no', 'true', 'false'])
      else
        option['choices'].to_a.each do |choice|
          choice = choice.to_s
          next if choice.include? ' '
          next if choice == ''

          keywords << choice
        end
      end
    end
  rescue Psych::SyntaxError => e
    print "An error occurred during scanning #{file}: #{e}."
  end
end

keywords.uniq!.sort!

template = File.read '../company-ansible-keywords.el.erb'
erb = ERB.new(template, nil, '-')

File.write '../company-ansible-keywords.el', erb.result

#! /usr/bin/env ruby

require 'yaml'
require 'erb'

keywords = []
Dir[File.expand_path ARGV[0]].each do |file|
  content = File.read file
  documentation = content.match(/DOCUMENTATION = r?'''\n(.*?)\n'''/m)
  next unless documentation

  begin
    yaml = YAML::load documentation[1]
    keywords << yaml['module']

    yaml['options'].to_a.each do |option_name, option|
      keywords << option_name

      if option['choices'] == 'BOOLEANS'
          keywords.concat(['yes', 'no', 'true', 'false'])
      else
        option['choices'].to_a.each do |choice|
          choice = choice.to_s
          next if choice.include? ' '
          next if choice == ''

          # in some DOCUMENTATION sections choice might be double
          # quoted, which does not get interpreted by Ruby's YAML, but
          # is common in python. In the final generated list these
          # double quotes break elisp.
          if choice.start_with? '["' and choice.end_with? '"]'
            choice.gsub(/\A\[|]\Z/, '').split(',').each do |choice_quote|
              keywords << choice_quote.gsub(/\A"|"\Z/, '')
            end
          else
            # normally, add directly
            keywords << choice
          end
        end
      end
    end
  rescue Psych::SyntaxError => e
    print "An error occurred during scanning #{file}: #{e}."
  end
end

keywords.uniq!.sort!

template = File.read '../company-ansible-keywords.el.erb'
erb = ERB.new(template, nil, '-')

File.write '../company-ansible-keywords.el', erb.result

Allow for double quoted DOCUMENTATION choices
Allow for double quoted DOCUMENTATION choices

In github_issue.py in the ansible modules they have an options section
in the documentation that is a double quoted array. This looks like:

company-ansible/utils/repos/stable-2.4/lib/ansible/modules/source_control/github_issue.py
["get_status"]

After, this ends up being poorly quoted in the resulting in the
keywords file having a poorly formed string in the list.
the elisp

...
"Yearly"
"["get_status"]"
"[NO]BYPASSRLS"
"[NO]CREATEDB"
