1010from pathlib import Path
1111from typing import TYPE_CHECKING , Any , ClassVar , Protocol
1212
13- from mkdocstrings .handlers .base import BaseHandler , CollectionError , CollectorItem
14- from mkdocstrings .loggers import get_logger
13+ from mkdocstrings import BaseHandler , CollectionError , CollectorItem , get_logger
1514from pycparser import CParser , c_ast
1615
1716if TYPE_CHECKING :
1817 from collections .abc import Mapping , MutableMapping
1918
20- from markdown import Markdown
19+ from mkdocs . config . defaults import MkDocsConfig
2120 from pycparser .c_ast import FileAST
2221
2322
@@ -466,16 +465,16 @@ def lookup_type_html(data: CodeDoc, tp: TypeRef, *, name: str | None = None) ->
466465class CHandler (BaseHandler ):
467466 """The C handler class."""
468467
469- name : str = "c"
468+ name : ClassVar [ str ] = "c"
470469 """The handler's name."""
471470
472- domain : str = "c"
471+ domain : ClassVar [ str ] = "c"
473472 """The cross-documentation domain/language for this handler."""
474473
475- enable_inventory : bool = False
474+ enable_inventory : ClassVar [ bool ] = False
476475 """Whether this handler is interested in enabling the creation of the `objects.inv` Sphinx inventory file."""
477476
478- fallback_theme = "material"
477+ fallback_theme : ClassVar [ str ] = "material"
479478 """The theme to fallback to."""
480479
481480 fallback_config : ClassVar [dict ] = {"fallback" : True }
@@ -497,7 +496,25 @@ class CHandler(BaseHandler):
497496 **`heading_level`** | `int` | The initial heading level to use. | `2`
498497 """
499498
500- def collect (self , identifier : str , config : MutableMapping [str , Any ]) -> CollectorItem :
499+ def __init__ (self , config : Mapping [str , Any ], base_dir : Path , ** kwargs : Any ) -> None :
500+ """Initialize the handler.
501+
502+ Parameters:
503+ config: The handler configuration.
504+ base_dir: The base directory of the project.
505+ **kwargs: Arguments passed to the parent constructor.
506+ """
507+ super ().__init__ (** kwargs )
508+
509+ self .config = config
510+ self .base_dir = base_dir
511+ self .global_options = config .get ("options" , {})
512+
513+ def get_options (self , local_options : Mapping [str , Any ]) -> Mapping [str , Any ]:
514+ """Combine configuration options."""
515+ return {** self .default_config , ** self .global_options , ** local_options }
516+
517+ def collect (self , identifier : str , options : MutableMapping [str , Any ]) -> CollectorItem :
501518 """Collect data given an identifier and selection configuration.
502519
503520 In the implementation, you typically call a subprocess that returns JSON, and load that JSON again into
@@ -507,13 +524,13 @@ def collect(self, identifier: str, config: MutableMapping[str, Any]) -> Collecto
507524 identifier: An identifier that was found in a markdown document for which to collect data. For example,
508525 in Python, it would be 'mkdocstrings.handlers' to collect documentation about the handlers module.
509526 It can be anything that you can feed to the tool of your choice.
510- config : All configuration options for this handler either defined globally in `mkdocs.yml` or
527+ options : All configuration options for this handler either defined globally in `mkdocs.yml` or
511528 locally overridden in an identifier block by the user.
512529
513530 Returns:
514531 Anything you want, as long as you can feed it to the `render` method.
515532 """
516- if config .get ("fallback" , False ):
533+ if options .get ("fallback" , False ):
517534 raise CollectionError ("Not loading additional headers during fallback" )
518535
519536 source = Path (identifier ).read_text (encoding = "utf-8" )
@@ -590,39 +607,33 @@ def collect(self, identifier: str, config: MutableMapping[str, Any]) -> Collecto
590607
591608 return CodeDoc (macros , funcs , global_vars , types )
592609
593- # def get_templates_dir(self, handler: str | None = None) -> Path:
594- # return Path.cwd()
595-
596- def render (self , data : CodeDoc , config : Mapping [str , Any ]) -> str :
610+ def render (self , data : CodeDoc , options : MutableMapping [str , Any ]) -> str :
597611 """Render a template using provided data and configuration options.
598612
599613 Parameters:
600614 data: The data to render that was collected above in `collect()`.
601- config : All configuration options for this handler either defined globally in `mkdocs.yml` or
615+ options : All configuration options for this handler either defined globally in `mkdocs.yml` or
602616 locally overridden in an identifier block by the user.
603617
604618 Returns:
605619 The rendered template as HTML.
606620 """
607- final_config = {** self .default_config , ** config }
608- heading_level = final_config ["heading_level" ]
621+ heading_level = options ["heading_level" ]
609622 template = self .env .get_template ("header.html.jinja" )
610623 return template .render (
611- config = final_config ,
624+ config = options ,
612625 header = data ,
613626 heading_level = heading_level ,
614627 root = True ,
615628 )
616629
617- def update_env (self , md : Markdown , config : dict ) -> None :
630+ def update_env (self , config : dict ) -> None : # noqa: ARG002
618631 """Update the Jinja environment with any custom settings/filters/options for this handler.
619632
620633 Parameters:
621- md: The Markdown instance. Useful to add functions able to convert Markdown into the environment filters.
622634 config: Configuration options for `mkdocs` and `mkdocstrings`, read from `mkdocs.yml`. See the source code
623- of [mkdocstrings.plugin. MkdocstringsPlugin.on_config][] to see what's in this dictionary.
635+ of [mkdocstrings.MkdocstringsPlugin.on_config][] to see what's in this dictionary.
624636 """
625- super ().update_env (md , config ) # Add some mkdocstrings default filters such as highlight and convert_markdown
626637 self .env .trim_blocks = True
627638 self .env .lstrip_blocks = True
628639 self .env .keep_trailing_newline = False
@@ -632,27 +643,18 @@ def update_env(self, md: Markdown, config: dict) -> None:
632643
633644
634645def get_handler (
635- theme : str ,
636- custom_templates : str | None = None ,
637- config_file_path : str | None = None , # noqa: ARG001
638- ** config : Any , # noqa: ARG001
646+ handler_config : MutableMapping [str , Any ],
647+ tool_config : MkDocsConfig ,
648+ ** kwargs : Any ,
639649) -> CHandler :
640650 """Simply return an instance of `CHandler`.
641651
642- Parameters:
643- theme: The theme to use when rendering contents.
644- custom_templates: Directory containing custom templates.
645- config_file_path: The MkDocs configuration file path.
646- **config: Configuration passed to the handler.
652+ Arguments:
653+ handler_config: The handler configuration.
654+ tool_config: The tool (SSG) configuration.
647655
648656 Returns:
649- An instance of the handler .
657+ An instance of `CHandler` .
650658 """
651- return CHandler (
652- handler = "c" ,
653- theme = theme ,
654- custom_templates = custom_templates ,
655- # To pass the following argument,
656- # you'll need to override the handler's __init__ method.
657- # config_file_path=config_file_path,
658- )
659+ base_dir = Path (tool_config .config_file_path or "./mkdocs.yml" ).parent
660+ return CHandler (config = handler_config , base_dir = base_dir , ** kwargs )
0 commit comments