Skip to content

Commit ce2d0e3

Browse files
committed
Add html meta description to service and operation pages
1 parent 10128c7 commit ce2d0e3

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed

awscli/bcdoc/docevents.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414

1515
DOC_EVENTS = {
16+
'doc-meta-description': '.%s',
1617
'doc-breadcrumbs': '.%s',
1718
'doc-title': '.%s',
1819
'doc-description': '.%s',
@@ -39,6 +40,8 @@ def generate_events(session, help_command):
3940
# Now generate the documentation events
4041
session.emit('doc-breadcrumbs.%s' % help_command.event_class,
4142
help_command=help_command)
43+
session.emit('doc-meta-description.%s' % help_command.event_class,
44+
help_command=help_command)
4245
session.emit('doc-title.%s' % help_command.event_class,
4346
help_command=help_command)
4447
session.emit('doc-description.%s' % help_command.event_class,

awscli/clidocs.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from botocore.model import StringShape
1818
from botocore.utils import is_json_value_header
1919

20-
from awscli import SCALAR_TYPES
20+
from awscli import SCALAR_TYPES, __version__ as AWS_CLI_VERSION
2121
from awscli.argprocess import ParamShorthandDocGen
2222
from awscli.bcdoc.docevents import DOC_EVENTS
2323
from awscli.topictags import TopicTagDB
@@ -114,7 +114,9 @@ def doc_breadcrumbs(self, help_command, **kwargs):
114114
full_cmd_list.append(cmd)
115115
full_cmd_name = ' '.join(full_cmd_list)
116116
doc.write(f':ref:`{cmd} <cli:{full_cmd_name}>`')
117-
doc.write(' ]')
117+
doc.writeln(' ]')
118+
doc.writeln('')
119+
118120

119121
def doc_title(self, help_command, **kwargs):
120122
doc = help_command.doc
@@ -223,6 +225,9 @@ def doc_relateditem(self, help_command, related_item, **kwargs):
223225
)
224226
doc.write('\n')
225227

228+
def doc_meta_description(self, help_command, **kwargs):
229+
pass
230+
226231
def _document_enums(self, model, doc):
227232
"""Documents top-level parameter enums"""
228233
if isinstance(model, StringShape):
@@ -402,6 +407,13 @@ def doc_subitem(self, command_name, help_command, **kwargs):
402407
else:
403408
doc.style.tocitem(command_name)
404409

410+
def doc_meta_description(self, help_command, **kwargs):
411+
doc = help_command.doc
412+
reference = help_command.event_class.replace('.', ' ')
413+
doc.writeln("")
414+
doc.writeln(".. meta::")
415+
doc.writeln(f" :description: Learn about the AWS CLI {AWS_CLI_VERSION} {reference} commands.")
416+
405417

406418
class OperationDocumentEventHandler(CLIDocumentEventHandler):
407419
AWS_DOC_BASE = 'https://docs.aws.amazon.com/goto/WebAPI'
@@ -618,6 +630,13 @@ def doc_output(self, help_command, event_name, **kwargs):
618630
for member_name, member_shape in output_shape.members.items():
619631
self._doc_member(doc, member_name, member_shape, stack=[])
620632

633+
def doc_meta_description(self, help_command, **kwargs):
634+
doc = help_command.doc
635+
reference = help_command.event_class.replace('.', ' ')
636+
doc.writeln("")
637+
doc.writeln(".. meta::")
638+
doc.writeln(f" :description: Use the AWS CLI {AWS_CLI_VERSION} to run the {reference} command.")
639+
621640

622641
class TopicListerDocumentEventHandler(CLIDocumentEventHandler):
623642
DESCRIPTION = (

tests/unit/test_clidocs.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
from awscli.testutils import mock, unittest, FileCreator
1919
from awscli.clidocs import OperationDocumentEventHandler, \
2020
CLIDocumentEventHandler, TopicListerDocumentEventHandler, \
21-
TopicDocumentEventHandler, GlobalOptionsDocumenter
21+
TopicDocumentEventHandler, GlobalOptionsDocumenter, \
22+
ServiceDocumentEventHandler
2223
from awscli.bcdoc.restdoc import ReSTDocument
2324
from awscli.help import ServiceHelpCommand, TopicListerCommand, \
2425
TopicHelpCommand, HelpCommand
@@ -444,6 +445,32 @@ def test_tagged_union_comes_after_docstring_output(self):
444445
rendered = help_command.doc.getvalue().decode('utf-8')
445446
self.assertRegex(rendered, r'FooBar[\s\S]*Tagged Union')
446447

448+
def test_meta_description_operation_command_html(self):
449+
help_cmd = ServiceHelpCommand(
450+
self.session, self.obj, self.command_table, self.arg_table,
451+
self.name, 'ec2.run-instances'
452+
)
453+
help_cmd.doc.target = 'html'
454+
doc_handler = OperationDocumentEventHandler(help_cmd)
455+
doc_handler.doc_meta_description(help_cmd)
456+
457+
meta_description = help_cmd.doc.getvalue().decode('utf-8')
458+
self.assertIn(".. meta::\n :description: ", meta_description)
459+
self.assertIn('to run the ec2 run-instances command', meta_description)
460+
461+
def test_meta_description_service_html(self):
462+
help_cmd = ServiceHelpCommand(
463+
self.session, self.obj, self.command_table, self.arg_table,
464+
self.name, 'ec2'
465+
)
466+
help_cmd.doc.target = 'html'
467+
doc_handler = ServiceDocumentEventHandler(help_cmd)
468+
doc_handler.doc_meta_description(help_cmd)
469+
470+
meta_description = help_cmd.doc.getvalue().decode('utf-8')
471+
self.assertIn(".. meta::\n :description: Learn about the AWS CLI ", meta_description)
472+
self.assertIn(' ec2 commands', meta_description)
473+
447474

448475
class TestTopicDocumentEventHandlerBase(unittest.TestCase):
449476
def setUp(self):

0 commit comments

Comments
 (0)