icinga-docs-tools/build-docs.rb

69 lines
2.0 KiB
Ruby
Raw Normal View History

2017-07-18 13:36:11 +02:00
#!/usr/bin/env ruby
require 'fileutils'
require 'yaml'
require 'git'
config = YAML::load_file('config.yml')
mkdocs = YAML::load_file('mkdocs.template.yml')
2017-07-19 09:49:12 +02:00
categories = {}
2017-07-18 13:36:11 +02:00
config['projects'].each do |project_name, project_config|
puts "== #{project_name}"
project_dir = config['projects_dir'] + '/' + project_config['target']
if project_config['latest'] == true
clone_target = project_dir + '/latest'
2017-07-19 13:49:39 +02:00
elsif project_config['ref'] == 'master'
clone_target = project_dir + '/snapshot'
2017-07-18 13:36:11 +02:00
else
clone_target = project_dir + '/' + project_config['ref'].gsub('tags/', '')
end
project_docs_dir = clone_target + '/' + project_config['docs_dir']
pages = []
if !File.directory?(clone_target)
puts 'Cloning ...'
FileUtils.mkdir_p(project_dir)
repo = Git.clone(project_config['git'], clone_target)
puts "Checkout ref '#{project_config['ref']}'"
repo.branch(project_config['ref']).checkout
else
repo = Git.open(clone_target)
repo.fetch()
puts "Checkout ref '#{project_config['ref']}'"
repo.branch(project_config['ref']).checkout
repo.pull('origin', project_config['ref'])
2017-07-18 13:36:11 +02:00
end
puts "Building page index from #{project_docs_dir}"
Dir.glob("#{project_docs_dir}/*.md", File::FNM_CASEFOLD).sort.each do |file|
2017-07-18 13:36:11 +02:00
filepath = file.gsub('projects/', '')
filename = filepath.match(/(\d+)-(.*).md$/)
header = filename[2].gsub('-', ' ').split.map(&:capitalize).join(' ')
pages.push(header => filepath)
end
2017-07-19 09:49:12 +02:00
if project_config['category']
categories[project_config['category']] = [] unless categories[project_config['category']]
categories[project_config['category']].push(project_name => pages)
else
mkdocs['pages'].push(project_name => pages)
end
end
if categories
categories.each do |cat, proj|
mkdocs['pages'].push(cat => proj)
end
2017-07-18 13:36:11 +02:00
end
mkdocs['extra']['append_pages'].each do |name, target|
mkdocs['pages'].push(name => target)
end
File.write('mkdocs.yml', mkdocs.to_yaml)
%x( mkdocs build )