Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

docs: sphinx: add a parser for yaml files for Netlink specs

Add a simple sphinx.Parser to handle yaml files and add the
the code to handle Netlink specs. All other yaml files are
ignored.

The code was written in a way that parsing yaml for different
subsystems and even for different parts of Netlink are easy.

All it takes to have a different parser is to add an
import line similar to:

from doc_generator import YnlDocGenerator

adding the corresponding parser somewhere at the extension:

netlink_parser = YnlDocGenerator()

And then add a logic inside parse() to handle different
doc outputs, depending on the file location, similar to:

if "/netlink/specs/" in fname:
msg = self.netlink_parser.parse_yaml_file(fname)

Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
Reviewed-by: Donald Hunter <donald.hunter@gmail.com>

+104
+104
Documentation/sphinx/parser_yaml.py
··· 1 + # SPDX-License-Identifier: GPL-2.0 2 + # Copyright 2025 Mauro Carvalho Chehab <mchehab+huawei@kernel.org> 3 + 4 + """ 5 + Sphinx extension for processing YAML files 6 + """ 7 + 8 + import os 9 + import re 10 + import sys 11 + 12 + from pprint import pformat 13 + 14 + from docutils.parsers.rst import Parser as RSTParser 15 + from docutils.statemachine import ViewList 16 + 17 + from sphinx.util import logging 18 + from sphinx.parsers import Parser 19 + 20 + srctree = os.path.abspath(os.environ["srctree"]) 21 + sys.path.insert(0, os.path.join(srctree, "tools/net/ynl/pyynl/lib")) 22 + 23 + from doc_generator import YnlDocGenerator # pylint: disable=C0413 24 + 25 + logger = logging.getLogger(__name__) 26 + 27 + class YamlParser(Parser): 28 + """ 29 + Kernel parser for YAML files. 30 + 31 + This is a simple sphinx.Parser to handle yaml files inside the 32 + Kernel tree that will be part of the built documentation. 33 + 34 + The actual parser function is not contained here: the code was 35 + written in a way that parsing yaml for different subsystems 36 + can be done from a single dispatcher. 37 + 38 + All it takes to have parse YAML patches is to have an import line: 39 + 40 + from some_parser_code import NewYamlGenerator 41 + 42 + To this module. Then add an instance of the parser with: 43 + 44 + new_parser = NewYamlGenerator() 45 + 46 + and add a logic inside parse() to handle it based on the path, 47 + like this: 48 + 49 + if "/foo" in fname: 50 + msg = self.new_parser.parse_yaml_file(fname) 51 + """ 52 + 53 + supported = ('yaml', ) 54 + 55 + netlink_parser = YnlDocGenerator() 56 + 57 + def rst_parse(self, inputstring, document, msg): 58 + """ 59 + Receives a ReST content that was previously converted by the 60 + YAML parser, adding it to the document tree. 61 + """ 62 + 63 + self.setup_parse(inputstring, document) 64 + 65 + result = ViewList() 66 + 67 + try: 68 + # Parse message with RSTParser 69 + for i, line in enumerate(msg.split('\n')): 70 + result.append(line, document.current_source, i) 71 + 72 + rst_parser = RSTParser() 73 + rst_parser.parse('\n'.join(result), document) 74 + 75 + except Exception as e: 76 + document.reporter.error("YAML parsing error: %s" % pformat(e)) 77 + 78 + self.finish_parse() 79 + 80 + # Overrides docutils.parsers.Parser. See sphinx.parsers.RSTParser 81 + def parse(self, inputstring, document): 82 + """Check if a YAML is meant to be parsed.""" 83 + 84 + fname = document.current_source 85 + 86 + # Handle netlink yaml specs 87 + if "/netlink/specs/" in fname: 88 + msg = self.netlink_parser.parse_yaml_file(fname) 89 + self.rst_parse(inputstring, document, msg) 90 + 91 + # All other yaml files are ignored 92 + 93 + def setup(app): 94 + """Setup function for the Sphinx extension.""" 95 + 96 + # Add YAML parser 97 + app.add_source_parser(YamlParser) 98 + app.add_source_suffix('.yaml', 'yaml') 99 + 100 + return { 101 + 'version': '1.0', 102 + 'parallel_read_safe': True, 103 + 'parallel_write_safe': True, 104 + }