1818from docutils .parsers .rst import directives
1919
2020import sphinx
21- import sphinx .directives .code
2221from sphinx .domains import Domain , ObjType
2322from sphinx .locale import l_ , _
2423from sphinx .directives import ObjectDescription
3029
3130from mongodb_conf import conf
3231
33-
3432def make_index_entry (* args ):
3533 """Sphinx 1.4 makes a breaking change in index format, so return a valid
3634 index entry whichever version we're running."""
3735 return args + (None ,) if sphinx .version_info >= (1 , 4 ) else args
3836
39-
4037def basename (path ):
4138 return os .path .splitext (os .path .basename (path ))[0 ]
4239
43-
4440class MongoDBObject (ObjectDescription ):
4541 """
4642 Description of a MongoDB object.
@@ -161,6 +157,7 @@ def add_target_and_index(self, name_obj, sig, signode):
161157 else :
162158 objects [fullname ] = self .env .docname , self .objtype
163159
160+
164161 indextext = self .get_index_text (objectname , name_obj )
165162 if indextext :
166163 self .indexnode ['entries' ].append (
@@ -213,11 +210,9 @@ def run(self):
213210 names = ('rtype' ,)),
214211 ]
215212
216-
217213class MongoDBMethod (MongoDBObject ):
218214 has_arguments = True
219215
220-
221216class MongoDBXRefRole (XRefRole ):
222217 def process_link (self , env , refnode , has_explicit_title , title , target ):
223218 # basically what sphinx.domains.python.PyXRefRole does
@@ -235,11 +230,10 @@ def process_link(self, env, refnode, has_explicit_title, title, target):
235230 refnode ['refspecific' ] = True
236231 return title , target
237232
238-
239233def render_domain_data (mongodb_directives ):
240- directives = {}
241- roles = {}
242- object_types = {}
234+ directives = { }
235+ roles = { }
236+ object_types = { }
243237
244238 for directive in mongodb_directives :
245239 reftype = directive ['name' ]
@@ -254,7 +248,6 @@ def render_domain_data(mongodb_directives):
254248
255249 return directives , roles , object_types
256250
257-
258251class MongoDBDomain (Domain ):
259252 """MongoDB Documentation domain."""
260253 name = 'mongodb'
@@ -264,7 +257,7 @@ class MongoDBDomain(Domain):
264257 directives , roles , object_types = render_domain_data (conf ['directives' ])
265258
266259 initial_data = {
267- 'objects' : {}, # fullname -> docname, objtype
260+ 'objects' : {}, # fullname -> docname, objtype
268261 }
269262
270263 def find_obj (self , env , obj , name , typ , searchorder = 0 ):
@@ -343,114 +336,11 @@ def run(self):
343336 return result
344337
345338
346- class code_button_row (nodes .Element ):
347- pass
348-
349-
350- def visit_code_button_row (self , node ):
351- start_tag = self .starttag (node , 'div' , CLASS = 'button-row' )
352- self .body .append (start_tag )
353-
354-
355- def depart_code_button_row (self , node ):
356- self .body .append ('</div>\n ' )
357-
358-
359- class code_button (nodes .Element ):
360- pass
361-
362-
363- def visit_code_button (self , node ):
364- href = node .get ('href' , False )
365- css_class = ' ' .join (['code-button' ] + node .get ('classes' , []))
366-
367- if href :
368- start_tag = self .starttag (node , 'a' , CLASS = css_class , role = 'button' , href = href , target = '_blank' )
369- else :
370- start_tag = self .starttag (node , 'a' , CLASS = css_class , role = 'button' )
371-
372- self .body .append (start_tag )
373-
374-
375- def depart_code_button (self , node ):
376- self .body .append (node ['text' ][0 ] + '</a>\n ' )
377-
378-
379- class code_container (nodes .Element ):
380- pass
381-
382-
383- def visit_code_container (self , node ):
384- start_tag = self .starttag (node , 'div' , CLASS = 'button-code-block' )
385- self .body .append (start_tag )
386-
387-
388- def depart_code_container (self , node ):
389- self .body .append ('</div>\n ' )
390-
391-
392- def create_button (button_type , link , classes = []):
393- """Create a button inside of a code block with the given label and link."""
394- button = code_button ('' )
395- button ['text' ] = [button_type ]
396- button ['classes' ] = classes
397-
398- if link :
399- button ['href' ] = [link ]
400-
401- return button
402-
403-
404- class CodeBlock (sphinx .directives .code .CodeBlock ):
405- """
406- Add copy, show in stitch, and github buttons to code block.
407- """
408- option_spec = sphinx .directives .code .CodeBlock .option_spec .copy ()
409- option_spec .update ({
410- 'button-github' : directives .uri ,
411- 'button-stitch' : directives .uri ,
412- 'copyable' : lambda argument : directives .choice (argument ,
413- ('true' , 'false' , None )),
414- })
415-
416- def run (self ):
417- options = self .options
418-
419- container = code_container ('' )
420- codeblock = sphinx .directives .code .CodeBlock .run (self )
421- br = code_button_row ('' )
422-
423- if options .get ('copyable' , 'true' ) != 'false' :
424- codeblock [0 ]['classes' ] += ['copyable-code-block' ]
425- br += create_button ('copy' , False , ['code-button--copy' ])
426-
427- if options .get ('button-github' ):
428- br += create_button ('github' , options ['button-github' ])
429-
430- if options .get ('button-stitch' ):
431- br += create_button ('stitch' , options ['button-stitch' ])
432-
433- container += br
434- container += codeblock
435- return [container ]
436-
437-
438339def setup (app ):
439340 app .add_domain (MongoDBDomain )
440341 directives .register_directive ('figure' , ExtendedFigure )
441342
442- app .add_node (code_button_row , html = (
443- visit_code_button_row , depart_code_button_row
444- ))
445- app .add_node (code_button , html = (
446- visit_code_button , depart_code_button
447- ))
448- app .add_node (code_container , html = (
449- visit_code_container , depart_code_container
450- ))
451- directives .register_directive ('code-block' , CodeBlock )
452-
453343 # Do NOT turn on parallel reads until we know what's causing massive
454344 # (2+ GB per worker) memory bloat and thrashing.
455- return {'parallel_read_safe' : False ,
456- 'parallel_write_safe' : True }
345+ return { 'parallel_read_safe' : False ,
346+ 'parallel_write_safe' : True }
0 commit comments