From e0385205cd85bdcb8506725550e44bc4f7f51e53 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Thu, 27 Jan 2022 14:11:20 +0100 Subject: [PATCH] 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. --- build-docs.rb | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/build-docs.rb b/build-docs.rb index f17bb55..880fc74 100755 --- a/build-docs.rb +++ b/build-docs.rb @@ -82,19 +82,58 @@ def build_page_index(full_docs_dir, project_docs_dir) next if !file.match(/.*(\d+)-(.*)$/) filepath = file.gsub(full_docs_dir + '/', project_docs_dir + '/') - filename = filepath.match(/.*(\d+)-(.*)$/) + filename = filepath.match(/.*?(\d+)-(.*?)(\.d)?$/) if(File.directory?("#{file}")) subdirectory = [] 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| subfile_path = subfile.gsub(full_docs_dir + '/', project_docs_dir + '/') 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) subdirectory.push(header => subfile_path) if header 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 else