Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/awscli/bcdoc/style.py
1566 views
1
# Copyright 2012-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
#
3
# Licensed under the Apache License, Version 2.0 (the "License"). You
4
# may not use this file except in compliance with the License. A copy of
5
# the License is located at
6
#
7
# http://aws.amazon.com/apache2.0/
8
#
9
# or in the "license" file accompanying this file. This file is
10
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11
# ANY KIND, either express or implied. See the License for the specific
12
# language governing permissions and limitations under the License.
13
14
import logging
15
16
logger = logging.getLogger('bcdocs')
17
18
19
class BaseStyle(object):
20
21
def __init__(self, doc, indent_width=2):
22
self.doc = doc
23
self.indent_width = indent_width
24
self._indent = 0
25
self.keep_data = True
26
27
@property
28
def indentation(self):
29
return self._indent
30
31
@indentation.setter
32
def indentation(self, value):
33
self._indent = value
34
35
def new_paragraph(self):
36
return '\n%s' % self.spaces()
37
38
def indent(self):
39
self._indent += 1
40
41
def dedent(self):
42
if self._indent > 0:
43
self._indent -= 1
44
45
def spaces(self):
46
return ' ' * (self._indent * self.indent_width)
47
48
def bold(self, s):
49
return s
50
51
def ref(self, link, title=None):
52
return link
53
54
def h2(self, s):
55
return s
56
57
def h3(self, s):
58
return s
59
60
def underline(self, s):
61
return s
62
63
def italics(self, s):
64
return s
65
66
67
class ReSTStyle(BaseStyle):
68
69
def __init__(self, doc, indent_width=2):
70
BaseStyle.__init__(self, doc, indent_width)
71
self.do_p = True
72
self.a_href = None
73
self.list_depth = 0
74
75
def new_paragraph(self):
76
self.doc.write('\n\n%s' % self.spaces())
77
78
def new_line(self):
79
self.doc.write('\n%s' % self.spaces())
80
81
def _start_inline(self, markup):
82
self.doc.write(markup)
83
84
def _end_inline(self, markup):
85
# Sometimes the HTML markup has whitespace between the end
86
# of the text inside the inline markup and the closing element
87
# (e.g. <b>foobar </b>). This trailing space will cause
88
# problems in the ReST inline markup so we remove it here
89
# by popping the last item written off the stack, striping
90
# the whitespace and then pushing it back on the stack.
91
last_write = self.doc.pop_write().rstrip(' ')
92
93
# Sometimes, for whatever reason, a tag like <b/> is present. This
94
# is problematic because if we simply translate that directly then
95
# we end up with something like ****, which rst will assume is a
96
# heading instead of an empty bold.
97
if last_write == markup:
98
return
99
100
self.doc.push_write(last_write)
101
self.doc.write(markup + ' ')
102
103
def start_bold(self, attrs=None):
104
self._start_inline('**')
105
106
def end_bold(self):
107
self._end_inline('**')
108
109
def start_b(self, attrs=None):
110
self.doc.do_translation = True
111
self.start_bold(attrs)
112
113
def end_b(self):
114
self.doc.do_translation = False
115
self.end_bold()
116
117
def bold(self, s):
118
if s:
119
self.start_bold()
120
self.doc.write(s)
121
self.end_bold()
122
123
def ref(self, title, link=None):
124
if link is None:
125
link = title
126
self.doc.write(':doc:`%s <%s>`' % (title, link))
127
128
def _heading(self, s, border_char):
129
border = border_char * len(s)
130
self.new_paragraph()
131
self.doc.write('%s\n%s\n%s' % (border, s, border))
132
self.new_paragraph()
133
134
def h1(self, s):
135
self._heading(s, '*')
136
137
def h2(self, s):
138
self._heading(s, '=')
139
140
def h3(self, s):
141
self._heading(s, '-')
142
143
def start_italics(self, attrs=None):
144
self._start_inline('*')
145
146
def end_italics(self):
147
self._end_inline('*')
148
149
def italics(self, s):
150
if s:
151
self.start_italics()
152
self.doc.write(s)
153
self.end_italics()
154
155
def start_p(self, attrs=None):
156
if self.do_p:
157
self.doc.write('\n\n%s' % self.spaces())
158
159
def end_p(self):
160
if self.do_p:
161
self.doc.write('\n\n%s' % self.spaces())
162
163
def start_code(self, attrs=None):
164
self.doc.do_translation = True
165
self._start_inline('``')
166
167
def end_code(self):
168
self.doc.do_translation = False
169
self._end_inline('``')
170
171
def code(self, s):
172
if s:
173
self.start_code()
174
self.doc.write(s)
175
self.end_code()
176
177
def start_note(self, attrs=None):
178
self.new_paragraph()
179
self.doc.write('.. note::')
180
self.indent()
181
self.new_paragraph()
182
183
def end_note(self):
184
self.dedent()
185
self.new_paragraph()
186
187
def start_important(self, attrs=None):
188
self.new_paragraph()
189
self.doc.write('.. warning::')
190
self.indent()
191
self.new_paragraph()
192
193
def end_important(self):
194
self.dedent()
195
self.new_paragraph()
196
197
def start_danger(self, attrs=None):
198
self.new_paragraph()
199
self.doc.write('.. danger::')
200
self.indent()
201
self.new_paragraph()
202
203
def end_danger(self):
204
self.dedent()
205
self.new_paragraph()
206
207
def start_a(self, attrs=None):
208
if attrs:
209
for attr_key, attr_value in attrs:
210
if attr_key == 'href':
211
self.a_href = attr_value
212
self.doc.write('`')
213
else:
214
# There are some model documentation that
215
# looks like this: <a>DescribeInstances</a>.
216
# In this case we just write out an empty
217
# string.
218
self.doc.write(' ')
219
self.doc.do_translation = True
220
221
def link_target_definition(self, refname, link):
222
self.doc.writeln('.. _%s: %s' % (refname, link))
223
224
def sphinx_reference_label(self, label, text=None):
225
if text is None:
226
text = label
227
if self.doc.target == 'html':
228
self.doc.write(':ref:`%s <%s>`' % (text, label))
229
else:
230
self.doc.write(text)
231
232
def end_a(self):
233
self.doc.do_translation = False
234
if self.a_href:
235
last_write = self.doc.pop_write()
236
last_write = last_write.rstrip(' ')
237
if last_write and last_write != '`':
238
if ':' in last_write:
239
last_write = last_write.replace(':', r'\:')
240
self.doc.push_write(last_write)
241
self.doc.push_write(' <%s>`__' % self.a_href)
242
elif last_write == '`':
243
# Look at start_a(). It will do a self.doc.write('`')
244
# which is the start of the link title. If that is the
245
# case then there was no link text. We should just
246
# use an inline link. The syntax of this is
247
# `<http://url>`_
248
self.doc.push_write('`<%s>`__' % self.a_href)
249
else:
250
self.doc.push_write(self.a_href)
251
self.doc.hrefs[self.a_href] = self.a_href
252
self.doc.write('`__')
253
self.a_href = None
254
self.doc.write(' ')
255
256
def start_i(self, attrs=None):
257
self.doc.do_translation = True
258
self.start_italics()
259
260
def end_i(self):
261
self.doc.do_translation = False
262
self.end_italics()
263
264
def start_li(self, attrs=None):
265
self.new_line()
266
self.do_p = False
267
self.doc.write('* ')
268
269
def end_li(self):
270
self.do_p = True
271
self.new_line()
272
273
def li(self, s):
274
if s:
275
self.start_li()
276
self.doc.writeln(s)
277
self.end_li()
278
279
def start_ul(self, attrs=None):
280
if self.list_depth != 0:
281
self.indent()
282
self.list_depth += 1
283
self.new_paragraph()
284
285
def end_ul(self):
286
self.list_depth -= 1
287
if self.list_depth != 0:
288
self.dedent()
289
self.new_paragraph()
290
291
def start_ol(self, attrs=None):
292
# TODO: Need to control the bullets used for LI items
293
if self.list_depth != 0:
294
self.indent()
295
self.list_depth += 1
296
self.new_paragraph()
297
298
def end_ol(self):
299
self.list_depth -= 1
300
if self.list_depth != 0:
301
self.dedent()
302
self.new_paragraph()
303
304
def start_examples(self, attrs=None):
305
self.doc.keep_data = False
306
307
def end_examples(self):
308
self.doc.keep_data = True
309
310
def start_fullname(self, attrs=None):
311
self.doc.keep_data = False
312
313
def end_fullname(self):
314
self.doc.keep_data = True
315
316
def start_codeblock(self, attrs=None):
317
self.doc.write('::')
318
self.indent()
319
self.new_paragraph()
320
321
def end_codeblock(self):
322
self.dedent()
323
self.new_paragraph()
324
325
def codeblock(self, code):
326
"""
327
Literal code blocks are introduced by ending a paragraph with
328
the special marker ::. The literal block must be indented
329
(and, like all paragraphs, separated from the surrounding
330
ones by blank lines).
331
"""
332
self.start_codeblock()
333
self.doc.writeln(code)
334
self.end_codeblock()
335
336
def toctree(self):
337
if self.doc.target == 'html':
338
self.doc.write('\n.. toctree::\n')
339
self.doc.write(' :maxdepth: 1\n')
340
self.doc.write(' :titlesonly:\n\n')
341
else:
342
self.start_ul()
343
344
def tocitem(self, item, file_name=None):
345
if self.doc.target == 'man':
346
self.li(item)
347
else:
348
if file_name:
349
self.doc.writeln(' %s' % file_name)
350
else:
351
self.doc.writeln(' %s' % item)
352
353
def hidden_toctree(self):
354
if self.doc.target == 'html':
355
self.doc.write('\n.. toctree::\n')
356
self.doc.write(' :maxdepth: 1\n')
357
self.doc.write(' :hidden:\n\n')
358
359
def hidden_tocitem(self, item):
360
if self.doc.target == 'html':
361
self.tocitem(item)
362
363
def table_of_contents(self, title=None, depth=None):
364
self.doc.write('.. contents:: ')
365
if title is not None:
366
self.doc.writeln(title)
367
if depth is not None:
368
self.doc.writeln(' :depth: %s' % depth)
369
370
def start_sphinx_py_class(self, class_name):
371
self.new_paragraph()
372
self.doc.write('.. py:class:: %s' % class_name)
373
self.indent()
374
self.new_paragraph()
375
376
def end_sphinx_py_class(self):
377
self.dedent()
378
self.new_paragraph()
379
380
def start_sphinx_py_method(self, method_name, parameters=None):
381
self.new_paragraph()
382
content = '.. py:method:: %s' % method_name
383
if parameters is not None:
384
content += '(%s)' % parameters
385
self.doc.write(content)
386
self.indent()
387
self.new_paragraph()
388
389
def end_sphinx_py_method(self):
390
self.dedent()
391
self.new_paragraph()
392
393
def start_sphinx_py_attr(self, attr_name):
394
self.new_paragraph()
395
self.doc.write('.. py:attribute:: %s' % attr_name)
396
self.indent()
397
self.new_paragraph()
398
399
def end_sphinx_py_attr(self):
400
self.dedent()
401
self.new_paragraph()
402
403
def write_py_doc_string(self, docstring):
404
docstring_lines = docstring.splitlines()
405
for docstring_line in docstring_lines:
406
self.doc.writeln(docstring_line)
407
408
def external_link(self, title, link):
409
if self.doc.target == 'html':
410
self.doc.write('`%s <%s>`_' % (title, link))
411
else:
412
self.doc.write(title)
413
414
def internal_link(self, title, page):
415
if self.doc.target == 'html':
416
self.doc.write(':doc:`%s <%s>`' % (title, page))
417
else:
418
self.doc.write(title)
419
420