]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
doc: group commands by prefix
authorKefu Chai <kchai@redhat.com>
Fri, 12 Feb 2021 08:03:11 +0000 (16:03 +0800)
committerKefu Chai <kchai@redhat.com>
Sat, 13 Feb 2021 02:18:58 +0000 (10:18 +0800)
as there are lots commands, the toc in sidebar is clutterred with them,
so would be better to group them.

Signed-off-by: Kefu Chai <kchai@redhat.com>
doc/_ext/ceph_commands.py

index e0f1090be35b744504b8e6beebfdb1acd1221623..d52949f17ff177b6ab6bb95f05f9627213846ff8 100644 (file)
@@ -179,12 +179,19 @@ class Sig:
 
 
 TEMPLATE = '''
-.. This file is automatically generated. do not modify
+{% set punct_char = '-' %}
+{# add a header if we have multiple commands in this section #}
+{% if commands | length > 1 %}
+{{ section }}
+{{ section | length * '-' }}
+{# and demote the subsection #}
+{% set punct_char = '^' %}
+{% endif %}
 
 {% for command in commands %}
 
 {{ command.prefix }}
-{{ command.prefix | length * '^' }}
+{{ command.prefix | length * punct_char }}
 
 {{ command.help | wordwrap(70)}}
 
@@ -215,6 +222,36 @@ Required Permissions:
 '''
 
 
+def group_by_prefix(commands):
+    last_prefix = None
+    grouped = []
+    for cmd in commands:
+        prefix = cmd.prefix.split(' ', 1)[0]
+        if prefix == last_prefix:
+            grouped.append(cmd)
+        elif last_prefix is None:
+            last_prefix = prefix
+            grouped = [cmd]
+        else:
+            yield last_prefix, grouped
+            last_prefix = prefix
+            grouped = [cmd]
+    assert grouped
+    yield last_prefix, grouped
+
+
+def render_commands(commands):
+    rendered = io.StringIO()
+    for section, grouped in group_by_prefix(commands):
+        logger.debug('rendering commands: %s: %d', section, len(grouped))
+        for cmd in grouped:
+            logger.info('%s ==> %s', section, cmd.prefix)
+        rendered.write(Template(TEMPLATE).render(
+            section=section,
+            commands=grouped))
+    return rendered.getvalue().split('\n')
+
+
 class CephMgrCommands(Directive):
     """
     extracts commands from specified mgr modules
@@ -309,8 +346,7 @@ class CephMgrCommands(Directive):
         return command
 
     def _render_cmds(self, commands):
-        rendered = Template(TEMPLATE).render(commands=list(commands))
-        lines = rendered.split("\n")
+        lines = render_commands(commands)
         assert lines
         lineno = self.lineno - self.state_machine.input_offset - 1
         source = self.state_machine.input_lines.source(lineno)
@@ -413,8 +449,7 @@ class CephMonCommands(Directive):
         return command
 
     def _render_cmds(self, commands):
-        rendered = Template(TEMPLATE).render(commands=list(commands))
-        lines = rendered.split("\n")
+        lines = render_commands(commands)
         assert lines
         lineno = self.lineno - self.state_machine.input_offset - 1
         source = self.state_machine.input_lines.source(lineno)