build-docs.rb: Parse templates

Templates are files inside a directory which has
the same name as a root file plus the `.d` extension.
The root file itself then is omitted in the index.
This commit is contained in:
Johannes Meyer 2022-01-27 14:11:20 +01:00
parent c99eaf2b2d
commit e0385205cd
1 changed files with 40 additions and 1 deletions

View File

@ -82,20 +82,59 @@ def build_page_index(full_docs_dir, project_docs_dir)
next if !file.match(/.*(\d+)-(.*)$/) next if !file.match(/.*(\d+)-(.*)$/)
filepath = file.gsub(full_docs_dir + '/', project_docs_dir + '/') filepath = file.gsub(full_docs_dir + '/', project_docs_dir + '/')
filename = filepath.match(/.*(\d+)-(.*)$/) filename = filepath.match(/.*?(\d+)-(.*?)(\.d)?$/)
if(File.directory?("#{file}")) if(File.directory?("#{file}"))
subdirectory = [] subdirectory = []
nav_item = titleize(filename[2]) unless File.symlink?(filepath) nav_item = titleize(filename[2]) unless File.symlink?(filepath)
is_template_dir = !filename[3].nil?
Dir.glob("#{file}/*.md", File::FNM_CASEFOLD).sort.each do |subfile| Dir.glob("#{file}/*.md", File::FNM_CASEFOLD).sort.each do |subfile|
subfile_path = subfile.gsub(full_docs_dir + '/', project_docs_dir + '/') subfile_path = subfile.gsub(full_docs_dir + '/', project_docs_dir + '/')
subfile_name = subfile.match(/.*(\d+)-(.*)$/) subfile_name = subfile.match(/.*(\d+)-(.*)$/)
if(is_template_dir)
%x(./parse_template.py -D icingaDocs true #{full_docs_dir} #{subfile_path.gsub(/^doc\//, '')})
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
header = titleize(subfile_name[2]) unless File.symlink?(subfile) header = titleize(subfile_name[2]) unless File.symlink?(subfile)
subdirectory.push(header => subfile_path) if header subdirectory.push(header => subfile_path) if header
end end
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
pages.push(nav_item => subdirectory) if nav_item pages.push(nav_item => subdirectory) if nav_item
else else
header = titleize(filename[2]) unless File.symlink?(filepath) header = titleize(filename[2]) unless File.symlink?(filepath)