Merge pull request #6 from Icinga/template-support

Template support
This commit is contained in:
Blerim Sheqa 2022-01-31 13:17:13 +01:00 committed by GitHub
commit b6b5923bfd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 141 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+)-(.*)$/)
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
header = titleize(filename[2]) unless File.symlink?(filepath)

View File

@ -8,6 +8,7 @@ theme:
logo: fontawesome/solid/book
features:
- navigation.instant
- navigation.indexes
site_dir: 'html'
extra:
social:

100
parse_template.py Executable file
View File

@ -0,0 +1,100 @@
#!/usr/bin/env python3
import os
import sys
import json
import argparse
from jinja2 import Environment, FileSystemLoader, select_autoescape
def parse_args():
parser = argparse.ArgumentParser(
prog='parse_template',
description='a jinja template parser',
add_help=False
)
parser.add_argument(
'-h', '--help',
action='help',
help='display help message',
)
parser.add_argument(
'-D', '--define',
action='append',
nargs=2,
type=str,
metavar=('key', 'value'),
help='define data with key-value pairs',
)
parser.add_argument(
'-o', '--output',
nargs='?',
type=str,
metavar='file',
help='output file or "-" for stdout',
)
parser.add_argument(
'docdir',
type=str,
metavar='docdir',
help='Full documentation directory path',
)
parser.add_argument(
'template',
type=str,
metavar='template',
help='template file path relative to docdir',
)
return parser.parse_args()
def parse_value(value):
try:
return json.loads(value)
except ValueError:
return value
def main():
args = parse_args()
doc_dir = args.docdir
template_path = args.template
output = args.output or os.path.join(doc_dir, template_path)
data = {k: parse_value(v) for k, v in args.define or []}
env = Environment(
loader=FileSystemLoader(doc_dir),
autoescape=False,
block_start_string='<!-- {%',
block_end_string='%} -->',
variable_start_string='<!-- {{',
variable_end_string='}} -->',
comment_start_string='<!-- {#',
comment_end_string='#} -->',
auto_reload=True,
trim_blocks=True,
lstrip_blocks=True
)
template = env.get_template(template_path)
if output == '-':
print(template.render(**data))
else:
fout = open(output, 'w')
try:
fout.write(template.render(**data))
finally:
fout.close()
if __name__ == '__main__':
main()