Skip to content

Commit 6ca4e3b

Browse files
committed
Add html meta description
This adds an html meta description only to service and operation pages.
1 parent 10128c7 commit 6ca4e3b

File tree

3 files changed

+54
-7
lines changed

3 files changed

+54
-7
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: 19 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,8 @@ 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('')
118119

119120
def doc_title(self, help_command, **kwargs):
120121
doc = help_command.doc
@@ -223,6 +224,9 @@ def doc_relateditem(self, help_command, related_item, **kwargs):
223224
)
224225
doc.write('\n')
225226

227+
def doc_meta_description(self, help_command, **kwargs):
228+
pass
229+
226230
def _document_enums(self, model, doc):
227231
"""Documents top-level parameter enums"""
228232
if isinstance(model, StringShape):
@@ -402,6 +406,13 @@ def doc_subitem(self, command_name, help_command, **kwargs):
402406
else:
403407
doc.style.tocitem(command_name)
404408

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

406417
class OperationDocumentEventHandler(CLIDocumentEventHandler):
407418
AWS_DOC_BASE = 'https://docs.aws.amazon.com/goto/WebAPI'
@@ -618,6 +629,12 @@ def doc_output(self, help_command, event_name, **kwargs):
618629
for member_name, member_shape in output_shape.members.items():
619630
self._doc_member(doc, member_name, member_shape, stack=[])
620631

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

622639
class TopicListerDocumentEventHandler(CLIDocumentEventHandler):
623640
DESCRIPTION = (

tests/unit/test_clidocs.py

Lines changed: 32 additions & 5 deletions
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
@@ -195,7 +196,7 @@ def test_breadcrumbs_html(self):
195196
doc_handler.doc_breadcrumbs(help_cmd)
196197
self.assertEqual(
197198
help_cmd.doc.getvalue().decode('utf-8'),
198-
'[ :ref:`aws <cli:aws>` ]'
199+
'[ :ref:`aws <cli:aws>` ]\n\n'
199200
)
200201

201202
def test_breadcrumbs_service_command_html(self):
@@ -208,7 +209,7 @@ def test_breadcrumbs_service_command_html(self):
208209
doc_handler.doc_breadcrumbs(help_cmd)
209210
self.assertEqual(
210211
help_cmd.doc.getvalue().decode('utf-8'),
211-
'[ :ref:`aws <cli:aws>` ]'
212+
'[ :ref:`aws <cli:aws>` ]\n\n'
212213
)
213214

214215
def test_breadcrumbs_operation_command_html(self):
@@ -221,7 +222,7 @@ def test_breadcrumbs_operation_command_html(self):
221222
doc_handler.doc_breadcrumbs(help_cmd)
222223
self.assertEqual(
223224
help_cmd.doc.getvalue().decode('utf-8'),
224-
'[ :ref:`aws <cli:aws>` . :ref:`ec2 <cli:aws ec2>` ]'
225+
'[ :ref:`aws <cli:aws>` . :ref:`ec2 <cli:aws ec2>` ]\n\n'
225226
)
226227

227228
def test_breadcrumbs_wait_command_html(self):
@@ -235,7 +236,7 @@ def test_breadcrumbs_wait_command_html(self):
235236
self.assertEqual(
236237
help_cmd.doc.getvalue().decode('utf-8'),
237238
('[ :ref:`aws <cli:aws>` . :ref:`s3api <cli:aws s3api>`'
238-
' . :ref:`wait <cli:aws s3api wait>` ]')
239+
' . :ref:`wait <cli:aws s3api wait>` ]\n\n')
239240
)
240241

241242
def test_documents_json_header_shape(self):
@@ -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)