2017-07-18 13:36:11 +02:00
|
|
|
#!/usr/bin/env ruby
|
2017-10-10 14:10:19 +02:00
|
|
|
require 'optparse'
|
2017-07-18 13:36:11 +02:00
|
|
|
require 'fileutils'
|
|
|
|
require 'yaml'
|
2018-01-30 10:31:16 +01:00
|
|
|
require 'date'
|
2017-07-18 13:36:11 +02:00
|
|
|
|
2017-10-10 14:10:19 +02:00
|
|
|
options = {}
|
|
|
|
OptionParser.new { |opts|
|
|
|
|
opts.banner = "Usage: #{File.basename($0)} -c config.yml -t mkdocs.template.yml}"
|
|
|
|
|
|
|
|
options[:config] = 'config.yml'
|
|
|
|
opts.on('-f',
|
|
|
|
'--config FILENAME',
|
|
|
|
'Configuration file with project definition. Defaults to "config.yml"') do |config|
|
|
|
|
options[:config] = config
|
|
|
|
end
|
|
|
|
|
|
|
|
options[:template] = 'mkdocs.template.yml'
|
|
|
|
opts.on('-t',
|
|
|
|
'--template FILENAME',
|
|
|
|
'This file is used as template for the generated mkdocs.yaml. Defaults to "mkdocs.template.yml"') do |template|
|
|
|
|
options[:template] = template
|
|
|
|
end
|
|
|
|
|
2022-01-25 13:11:11 +01:00
|
|
|
options[:skip_clone] = false
|
|
|
|
opts.on('-s',
|
|
|
|
'--skip-clone',
|
|
|
|
'Do not update the repository. This option only works if the repo was already cloned once.') do |skip|
|
|
|
|
options[:skip_clone] = true
|
|
|
|
end
|
|
|
|
|
2017-10-10 14:10:19 +02:00
|
|
|
opts.on_tail('-h', '--help', 'Show this message') do
|
|
|
|
puts opts
|
|
|
|
exit
|
|
|
|
end
|
|
|
|
}.parse!
|
|
|
|
|
2022-10-26 12:12:06 +02:00
|
|
|
def cleanup_and_clone(clone_target, git, ref, inject_central_docs = false)
|
2017-10-19 09:58:31 +02:00
|
|
|
@git_options = "-b #{ref}" if ref =~ /tags/
|
2017-07-19 12:53:53 +02:00
|
|
|
if !File.directory?(clone_target)
|
2017-10-19 09:58:31 +02:00
|
|
|
puts "Cloning #{git} to #{clone_target} ..."
|
2017-10-12 15:01:41 +02:00
|
|
|
FileUtils.mkdir_p(clone_target)
|
2017-10-19 09:58:31 +02:00
|
|
|
%x(git clone #{git} #{clone_target})
|
|
|
|
|
|
|
|
puts "Checking out #{ref}"
|
|
|
|
%x(git --git-dir=#{clone_target}/.git --work-tree=#{clone_target} checkout #{ref} #{@git_options})
|
2022-10-26 12:12:06 +02:00
|
|
|
|
|
|
|
if inject_central_docs
|
|
|
|
puts "Inject Central Docs"
|
|
|
|
%x(git config --global user.email "info@icinga.com")
|
|
|
|
%x(git config --global user.name "Icinga Docs Tool")
|
|
|
|
%x(git --git-dir=#{clone_target}/.git --work-tree=#{clone_target} remote add -f package-installation-docs https://github.com/Icinga/package-installation-docs.git)
|
|
|
|
%x(git --git-dir=#{clone_target}/.git --work-tree=#{clone_target} merge --squash --allow-unrelated-histories -s subtree -Xsubtree="doc/02-Installation.md.d" package-installation-docs/main)
|
|
|
|
%x(git --git-dir=#{clone_target}/.git --work-tree=#{clone_target} commit -m "Merge package installation docs")
|
|
|
|
end
|
2017-07-19 12:53:53 +02:00
|
|
|
else
|
2017-10-18 15:06:30 +02:00
|
|
|
puts "Cleaning up #{clone_target}"
|
|
|
|
FileUtils::rm_rf(clone_target)
|
2022-10-26 12:12:06 +02:00
|
|
|
cleanup_and_clone(clone_target, git, ref, inject_central_docs)
|
2017-07-18 13:36:11 +02:00
|
|
|
end
|
2017-10-12 15:01:41 +02:00
|
|
|
end
|
|
|
|
|
2021-09-10 11:40:39 +02:00
|
|
|
def titleize(string)
|
|
|
|
# Remove .md
|
|
|
|
title = string.gsub('.md', '')
|
|
|
|
|
|
|
|
# Remove numbering on the front
|
|
|
|
title = title.gsub(/^\d+-/, '')
|
|
|
|
|
|
|
|
# Remove dashes and underscores
|
|
|
|
title = title.gsub(/(-|_)/, ' ')
|
|
|
|
|
|
|
|
# Uppercase only first letter of each word, ignore stopwords
|
2021-09-10 11:44:10 +02:00
|
|
|
stopwords = ['and', 'or', 'to', 'by', 'on', 'with', 'is', 'at', 'of', 'from', 'for']
|
2021-09-10 11:40:39 +02:00
|
|
|
title = title.split.map { |word|
|
|
|
|
if stopwords.include?(word)
|
|
|
|
word
|
|
|
|
else
|
|
|
|
word[0].upcase + word[1..-1]
|
|
|
|
end
|
|
|
|
}.join(" ")
|
|
|
|
|
|
|
|
return title
|
|
|
|
end
|
|
|
|
|
2022-10-26 12:12:06 +02:00
|
|
|
def build_page_index(full_docs_dir, project_docs_dir, package = "", product = "")
|
2017-10-12 15:01:41 +02:00
|
|
|
pages = []
|
|
|
|
puts "Building page index from #{full_docs_dir}"
|
2021-03-19 11:11:00 +01:00
|
|
|
|
|
|
|
Dir.glob("#{full_docs_dir}/**", File::FNM_CASEFOLD).sort.each do |file|
|
|
|
|
next if !file.match(/.*(\d+)-(.*)$/)
|
|
|
|
|
2017-10-12 15:01:41 +02:00
|
|
|
filepath = file.gsub(full_docs_dir + '/', project_docs_dir + '/')
|
2022-01-27 14:11:20 +01:00
|
|
|
filename = filepath.match(/.*?(\d+)-(.*?)(\.d)?$/)
|
2021-03-19 11:11:00 +01:00
|
|
|
|
|
|
|
if(File.directory?("#{file}"))
|
|
|
|
subdirectory = []
|
2021-09-10 11:40:39 +02:00
|
|
|
nav_item = titleize(filename[2]) unless File.symlink?(filepath)
|
2022-01-27 14:11:20 +01:00
|
|
|
is_template_dir = !filename[3].nil?
|
2021-03-19 11:11:00 +01:00
|
|
|
|
|
|
|
Dir.glob("#{file}/*.md", File::FNM_CASEFOLD).sort.each do |subfile|
|
|
|
|
subfile_path = subfile.gsub(full_docs_dir + '/', project_docs_dir + '/')
|
2022-10-25 13:03:59 +02:00
|
|
|
# Get everything after last slash
|
|
|
|
subfile_name = subfile.match(/([^\/]+$)/)
|
2022-01-27 14:11:20 +01:00
|
|
|
if(is_template_dir)
|
2022-10-26 12:12:06 +02:00
|
|
|
%x(./parse_template.py -D product "#{product}" -D package "#{package}" -D icingaDocs true "#{full_docs_dir}" "#{subfile_path.gsub(/^doc\//, '')}")
|
2022-01-27 14:11:20 +01:00
|
|
|
|
|
|
|
content = File.read(subfile)
|
|
|
|
# Adjust self references
|
|
|
|
content = content.gsub(/\[(.*)\]\(#{filename[1]}-#{Regexp.quote(filename[2])}(.*)\)/, '[\1](\2)')
|
|
|
|
# Adjust external references
|
|
|
|
content = content.gsub(/\[(.*)\]\((?!http|#)(.*)\)/, '[\1](../\2)')
|
|
|
|
File.write(subfile, content)
|
|
|
|
|
|
|
|
# Adjust path, the directory will be renamed soon
|
|
|
|
subdir_name = File.basename(file)
|
|
|
|
subfile_path = subfile_path.gsub(subdir_name, subdir_name.gsub(/\.md.d$/, ''))
|
|
|
|
end
|
2021-03-19 11:11:00 +01:00
|
|
|
|
2022-10-25 13:03:59 +02:00
|
|
|
header = titleize(subfile_name[1]) unless File.symlink?(subfile)
|
2021-03-19 11:11:00 +01:00
|
|
|
subdirectory.push(header => subfile_path) if header
|
|
|
|
end
|
2022-01-27 14:11:20 +01:00
|
|
|
|
2022-10-25 13:03:59 +02:00
|
|
|
# Sort the "From Source" installation guide explicitly to the end
|
|
|
|
subdirectory.each_with_index do |subdirectory_element, index|
|
|
|
|
if subdirectory_element.key?("From Source")
|
|
|
|
subdirectory.append(subdirectory_element)
|
|
|
|
subdirectory.delete_at(index)
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-01-27 14:11:20 +01:00
|
|
|
if(is_template_dir)
|
|
|
|
template_path = filepath.gsub(/\.d$/, '')
|
|
|
|
if(pages.include?(nav_item => template_path))
|
|
|
|
pages.delete_at(pages.find_index(nav_item => template_path))
|
|
|
|
end
|
|
|
|
|
|
|
|
# Rename template directory to mimic template references
|
|
|
|
newTemplateDirPath = file.gsub(/\.md.d$/, '')
|
|
|
|
File.rename(file, newTemplateDirPath)
|
|
|
|
|
|
|
|
# Attempt to create a index file
|
|
|
|
index_content = %x(./parse_template.py -o - -D index true #{full_docs_dir} #{template_path.gsub(/^doc\//, '')})
|
|
|
|
if(!index_content.empty?)
|
|
|
|
# Adjust self references
|
|
|
|
index_content = index_content.gsub(/\[(.*)\]\(#{filename[1]}-#{Regexp.quote(filename[2])}(.*)\)/, '[\1](\2)')
|
|
|
|
# Adjust external references
|
|
|
|
index_content = index_content.gsub(/\[(.*)\]\((?!http|#)(.*)\)/, '[\1](../\2)')
|
|
|
|
|
|
|
|
File.write(newTemplateDirPath + '/index.md', index_content)
|
|
|
|
subdirectory.unshift(filepath.gsub(/.md.d$/, '') + '/index.md')
|
|
|
|
end
|
|
|
|
end
|
2021-03-19 11:11:00 +01:00
|
|
|
|
|
|
|
pages.push(nav_item => subdirectory) if nav_item
|
|
|
|
else
|
2021-09-10 11:40:39 +02:00
|
|
|
header = titleize(filename[2]) unless File.symlink?(filepath)
|
2021-03-19 11:11:00 +01:00
|
|
|
pages.push(header => filepath) if header
|
2017-10-12 15:01:41 +02:00
|
|
|
end
|
2017-07-18 13:36:11 +02:00
|
|
|
end
|
|
|
|
|
2017-10-12 15:01:41 +02:00
|
|
|
return pages
|
2017-07-19 09:49:12 +02:00
|
|
|
end
|
|
|
|
|
2018-01-30 10:31:16 +01:00
|
|
|
def get_events(git, source_dir, categories)
|
|
|
|
events = []
|
|
|
|
event_categories = categories
|
|
|
|
clone_target = source_dir + '/events'
|
2022-01-25 13:11:11 +01:00
|
|
|
#cleanup_and_clone('events', clone_target, git, 'master')
|
2018-01-30 10:31:16 +01:00
|
|
|
event_categories.each do |category|
|
|
|
|
category_events = YAML::load_file(clone_target + '/' + category + '.yml')
|
2018-03-22 16:28:07 +01:00
|
|
|
if category_events
|
|
|
|
category_events_sorted = category_events.sort_by { |k| k['date'] }
|
2019-07-22 13:26:11 +02:00
|
|
|
category_events_sorted.each do |event|
|
|
|
|
if event['date'] > Date.today
|
|
|
|
events << event
|
|
|
|
break
|
|
|
|
else
|
|
|
|
next
|
|
|
|
end
|
|
|
|
end
|
2018-03-22 16:28:07 +01:00
|
|
|
end
|
2018-01-30 10:31:16 +01:00
|
|
|
end
|
|
|
|
|
2018-01-30 13:58:45 +01:00
|
|
|
events_sorted = events.sort_by { |k| k['date']}
|
2018-01-30 10:31:16 +01:00
|
|
|
|
|
|
|
events_sorted.each do |k|
|
2018-01-30 13:58:45 +01:00
|
|
|
k['date'] = Date::ABBR_MONTHNAMES[k['date'].month]
|
2018-01-30 10:31:16 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
return events_sorted
|
|
|
|
end
|
|
|
|
|
|
|
|
config = YAML::load_file('config.yml')
|
|
|
|
project_config = YAML::load_file(options[:config])
|
2017-10-12 15:01:41 +02:00
|
|
|
mkdocs = YAML::load_file(options[:template])
|
2020-11-03 13:59:00 +01:00
|
|
|
mkdocs['nav'] = []
|
2017-10-12 15:01:41 +02:00
|
|
|
|
2017-07-18 13:36:11 +02:00
|
|
|
|
2017-10-12 15:01:41 +02:00
|
|
|
|
2018-01-30 10:31:16 +01:00
|
|
|
puts "== #{project_config['site_name']}"
|
|
|
|
|
|
|
|
version = if project_config['project']['latest']
|
2017-10-12 15:01:41 +02:00
|
|
|
'latest'
|
2018-01-30 10:31:16 +01:00
|
|
|
elsif project_config['project']['ref'] == 'master'
|
2017-10-12 15:01:41 +02:00
|
|
|
'snapshot'
|
|
|
|
else
|
2018-01-30 10:31:16 +01:00
|
|
|
project_config['project']['ref'].gsub('tags/', '')
|
2021-02-09 13:41:52 +01:00
|
|
|
project_config['project']['ref'].gsub('support/', '')
|
2017-10-12 15:01:41 +02:00
|
|
|
end
|
|
|
|
|
2018-01-30 10:31:16 +01:00
|
|
|
source_dir = project_config['source_dir'] + '/' + project_config['project']['target']
|
2017-10-12 15:01:41 +02:00
|
|
|
clone_target = source_dir + '/' + version
|
2018-01-30 10:31:16 +01:00
|
|
|
full_docs_dir = clone_target + '/' + project_config['project']['docs_dir']
|
2022-10-26 12:12:06 +02:00
|
|
|
inject_central_docs = project_config['inject_central_docs']
|
2018-01-30 10:31:16 +01:00
|
|
|
|
2022-01-25 13:11:11 +01:00
|
|
|
if !options[:skip_clone]
|
2022-10-26 12:12:06 +02:00
|
|
|
cleanup_and_clone(clone_target,
|
2022-01-25 13:11:11 +01:00
|
|
|
project_config['project']['git'],
|
2022-10-26 12:12:06 +02:00
|
|
|
project_config['project']['ref'],
|
|
|
|
inject_central_docs)
|
2022-01-25 13:11:11 +01:00
|
|
|
end
|
2017-10-12 15:01:41 +02:00
|
|
|
|
2022-10-26 12:12:06 +02:00
|
|
|
main_pages = build_page_index(full_docs_dir, project_config['project']['docs_dir'], project_config['project']['package'], project_config['project']['product'])
|
2017-10-12 15:01:41 +02:00
|
|
|
# MKdocs allows only 'index.md' as homepage. This is a dirty workaround to use the first markdown file instead
|
|
|
|
#FileUtils.ln_s("#{clone_target}/#{main_pages[0].values[0]}", "#{clone_target}/index.md", :force => true)
|
|
|
|
index_file = "#{clone_target}/index.md"
|
|
|
|
FileUtils.cp("#{clone_target}/#{main_pages[0].values[0]}", index_file)
|
|
|
|
index_content = File.read(index_file)
|
2018-11-26 11:11:19 +01:00
|
|
|
index_new_content = index_content.gsub(/\(((?!http)\S+(\.md|\.png)(\w)?)/,
|
|
|
|
"(#{project_config['project']['docs_dir']}/\\1")
|
2018-01-30 10:31:16 +01:00
|
|
|
|
2017-10-12 15:01:41 +02:00
|
|
|
File.open(index_file, "w") {|file| file.puts index_new_content }
|
2020-11-03 13:59:00 +01:00
|
|
|
mkdocs['nav'].push('' => "index.md")
|
|
|
|
|
|
|
|
if project_config['project']['subprojects']
|
|
|
|
subproject_navigation = []
|
|
|
|
|
|
|
|
project_config['project']['subprojects'].each do |subproject|
|
2017-10-12 15:01:41 +02:00
|
|
|
|
2020-11-03 13:59:00 +01:00
|
|
|
if subproject[1]['git']
|
|
|
|
subproject_clone_target = clone_target + '/' + subproject[1]['target']
|
2022-10-26 12:12:06 +02:00
|
|
|
cleanup_and_clone(subproject_clone_target, subproject[1]['git'], subproject[1]['ref'])
|
2017-10-12 15:01:41 +02:00
|
|
|
end
|
2020-11-03 13:59:00 +01:00
|
|
|
|
|
|
|
subproject_navigation.push(subproject[0] => build_page_index(clone_target + '/' + subproject[1]['docs_dir'], subproject[1]['docs_dir']))
|
2017-10-10 14:10:19 +02:00
|
|
|
end
|
2017-07-18 13:36:11 +02:00
|
|
|
end
|
|
|
|
|
2018-01-30 10:31:16 +01:00
|
|
|
mkdocs['site_name'] = project_config['site_name']
|
2017-10-12 15:01:41 +02:00
|
|
|
mkdocs['docs_dir'] = clone_target
|
2018-01-30 10:31:16 +01:00
|
|
|
mkdocs['site_dir'] = project_config['site_dir'] + '/' + project_config['project']['target'] + '/' + version
|
2020-11-03 13:59:00 +01:00
|
|
|
mkdocs['repo_url'] = project_config['project']['git'].gsub('.git', '').downcase
|
2022-07-04 15:16:30 +02:00
|
|
|
mkdocs['repo_url'] = '' if project_config['repo_url'] == 'hide'
|
2020-11-03 13:59:00 +01:00
|
|
|
mkdocs['nav'].push(*main_pages)
|
|
|
|
mkdocs['nav'].push(*subproject_navigation) if subproject_navigation
|
|
|
|
#mkdocs['extra']['events'] = get_events(config['events']['git'],
|
|
|
|
# config['events']['source_dir'],
|
|
|
|
# config['events']['categories'])
|
2018-01-30 10:31:16 +01:00
|
|
|
|
2017-07-18 13:36:11 +02:00
|
|
|
File.write('mkdocs.yml', mkdocs.to_yaml)
|
2020-11-03 13:59:00 +01:00
|
|
|
%x(mkdocs build)
|
2023-04-17 11:36:54 +02:00
|
|
|
|
|
|
|
Dir.chdir(mkdocs['site_dir'])
|
|
|
|
|
|
|
|
Dir.glob('**/*.png').each do|f|
|
|
|
|
system("optipng #{f}") or exit!
|
|
|
|
end
|