1+ import logging
12import os
23
4+ import filters
5+ import mkdoxy
6+ from jinja2 import BaseLoader , Environment , Template
7+ from mkdocs import exceptions
38from mkdocs .structure import files
9+ from mkdoxy .filters import use_code_language
410from mkdoxy .generatorAuto import GeneratorAuto
11+ from mkdoxy .generatorBase import GeneratorBase
512from mkdoxy .node import Node
6- import inflection
13+ from mkdoxy .utils import parseTemplateFile
14+
15+ from scripts .docgen .filters import do_format_refid
16+
17+ log : logging .Logger = logging .getLogger ("mkdocs" )
18+
19+
20+ class BaseGenerator (GeneratorBase ):
21+ def __init__ (
22+ self , templateDir : str = "" , ignore_errors : bool = False , debug : bool = False
23+ ):
24+ """! Constructor.
25+ @details
26+ @param templateDir (str): Path to the directory with custom templates (default: "")
27+ @param ignore_errors (bool): If True, errors will be ignored (default: False)
28+ @param debug (bool): If True, debug messages will be printed (default: False)
29+ """
30+
31+ """! Constructor.
32+ @details
33+ @param templateDir (str): Path to the directory with custom templates (default: "")
34+ @param ignore_errors (bool): If True, errors will be ignored (default: False)
35+ @param debug (bool): If True, debug messages will be printed (default: False)
36+ """
37+
38+ self .debug : bool = debug # if True, debug messages will be printed
39+ self .templates : dict [str , Template ] = {}
40+ self .metaData : dict [str , list [str ]] = {}
41+
42+ environment = Environment (loader = BaseLoader ())
43+ environment .filters ["use_code_language" ] = use_code_language
44+ environment .filters ["format_refid" ] = do_format_refid
45+ # code from https://github.com/daizutabi/mkapi/blob/master/mkapi/core/renderer.py#L29-L38
46+ path = os .path .join (os .path .dirname (mkdoxy .__file__ ), "templates" )
47+ ENDING = (".jinja2" , ".j2" , ".jinja" )
48+ for fileName in os .listdir (path ):
49+ filePath = os .path .join (path , fileName )
50+
51+ # accept any case of the file ending
52+ if fileName .lower ().endswith (ENDING ):
53+ with open (filePath , "r" ) as file :
54+ name = os .path .splitext (fileName )[0 ]
55+ fileTemplate , metaData = parseTemplateFile (file .read ())
56+ self .templates [name ] = environment .from_string (fileTemplate )
57+ self .metaData [name ] = metaData
58+ else :
59+ log .error (
60+ f"Trying to load unsupported file '{ filePath } '. Supported file ends with { ENDING } ."
61+ f"Look at documentation: https://mkdoxy.kubaandrysek.cz/usage/#custom-jinja-templates."
62+ )
63+
64+ # test if templateDir is existing
65+ if templateDir :
66+ if not os .path .exists (templateDir ):
67+ raise exceptions .ConfigurationError (
68+ f"Custom template directory '{ templateDir } ' does not exist."
69+ )
70+ # load custom templates and overwrite default templates - if they exist
71+ for fileName in os .listdir (templateDir ):
72+ filePath = os .path .join (templateDir , fileName )
73+ if fileName .lower ().endswith (ENDING ):
74+ with open (filePath , "r" ) as file :
75+ name = os .path .splitext (fileName )[0 ]
76+ fileTemplate , metaData = parseTemplateFile (file .read ())
77+ self .templates [name ] = environment .from_string (fileTemplate )
78+ self .metaData [name ] = metaData
79+ log .info (f"Overwriting template '{ name } ' with custom template." )
80+ else :
81+ log .error (
82+ f"Trying to load unsupported file '{ filePath } '. Supported file ends with { ENDING } ."
83+ f"Look at documentation: https://mkdoxy.kubaandrysek.cz/usage/#custom-jinja-templates."
84+ )
785
886
987class Generator (GeneratorAuto ):
1088 def save (self , path : str , output : str ):
1189 rel_path = os .path .join (self .apiPath , path )
12- self .fullDocFiles .append (files .File (rel_path , self .tempDoxyDir , self .siteDir , self .useDirectoryUrls ))
90+ self .fullDocFiles .append (
91+ files .File (rel_path , self .tempDoxyDir , self .siteDir , self .useDirectoryUrls )
92+ )
1393 with open (os .path .join (self .siteDir , rel_path ), "w" , encoding = "utf-8" ) as file :
1494 file .write (output )
1595
1696 def member (self , node : Node , config : dict = None ):
17- path = inflection . underscore (node .name_short ) + ".mdx"
97+ path = filters . do_format_refid (node .refid ) + ".mdx"
1898
1999 output = ""
20- output += ' ---\n '
100+ output += " ---\n "
21101 output += f'title: "{ node .name_short } "\n '
22- output += ' ---\n \n '
102+ output += " ---\n \n "
23103 output += self .generatorBase .member (node , config )
24104 self .save (path , output )
25105
@@ -30,8 +110,8 @@ def classes(self, nodes: [Node], config: dict = None):
30110 path = "index.mdx"
31111
32112 output = ""
33- output += ' ---\n '
113+ output += " ---\n "
34114 output += f'asIndexPage: true"\n '
35- output += ' ---\n \n '
115+ output += " ---\n \n "
36116 output += self .generatorBase .classes (nodes , config )
37117 self .save (path , output )
0 commit comments