我们从Python开源项目中,提取了以下3个代码示例,用于说明如何使用docutils.core.publish_doctree()。
def render_rst(blob, path): doc = publish_doctree(blob.as_raw_string()) for node in doc.traverse(nodes.reference): uri = urlparse.urlparse(node['refuri']) if not uri.netloc and os.path.basename(uri.path) == "README.rst": node['refuri'] = urlparse.urlunparse( (uri.scheme, uri.netloc, uri.path[:-10] or "./", uri.params, uri.query, uri.fragment)) output = publish_from_doctree( doc, destination_path=path, writer=MyWriter(), settings_overrides = { 'embed_stylesheet': False, 'xml_declaration': False, 'math_output': 'mathjax'}) new_blob = Blob.from_string(output) store.add_object(new_blob) return new_blob.id
def run(self): module_parts = self.content[0].split(".") module = ".".join(module_parts[0:len(module_parts) - 1]) member = module_parts[len(module_parts) - 1] api_module = __import__(module, fromlist = ['a']) api = api_module.__dict__[member] #parser = Parser() #publisher = Publisher() request = HttpRequest() top_level_response = api.top_level(request, None) top_level_doc = json.loads(top_level_response.content) for name in sorted(api._registry.keys()): resource_dict = top_level_doc[name] resource = api._registry[name] resource_dict['schema'] = resource.build_schema() resource_dict['schema']['field_list'] = [{'name': field, 'meta': meta} for field, meta in resource_dict['schema']['fields'].items()] for field, field_meta in resource_dict['schema']['fields'].items(): if field == 'id': field_meta['help_text'] = "Integer record identifier, unique for objects of this type" elif field == 'content_type_id': field_meta['help_text'] = "Integer type identifier" elif field == 'state' and field_meta['help_text'] == tastypie.fields.CharField.help_text: field_meta['help_text'] = "Unicode string, may be set based on ``available_transitions`` field" elif field == 'immutable_state' and field_meta['help_text'] == tastypie.fields.BooleanField.help_text: field_meta['help_text'] = "If ``true``, this object may not have its state modified by the user (monitoring only)" elif field == 'resource_uri': field_meta['help_text'] = "URL for this object" elif field == 'available_transitions': field_meta['help_text'] = "List of {'verb':"", 'state':""} for possible states (for use with POST)" elif field == 'available_jobs': field_meta['help_text'] = "List of {'args':{}, 'class_name':"", 'confirmation':"", verb: ""} for possible " \ "non-state-change jobs (for use with the ``command`` resource)" elif field == 'label': field_meta['help_text'] = "Non-unique human readable name for presentation" resource_dict['list_allowed_methods'] = [m.upper() for m in resource._meta.list_allowed_methods] resource_dict['detail_allowed_methods'] = [m.upper() for m in resource._meta.detail_allowed_methods] resource_dict['ordering'] = resource._meta.ordering resource_dict['filtering'] = resource._meta.filtering for field, methods in resource_dict['filtering'].items(): if methods == tastypie.constants.ALL_WITH_RELATIONS: resource_dict['filtering'][field] = ["including dereferenced attributes"] if methods == tastypie.constants.ALL: resource_dict['filtering'][field] = ["any filter type"] resource_dict['doc'] = resource.__doc__ path = os.path.dirname(__file__) rst_template = open(path + "/tasty-endpoint-template.rst").read() template_vars = { 'endpoints': top_level_doc, } django_template = Template(rst_template) output_rst = django_template.render(Context(template_vars)) #open('dump.rst', 'w').write(output_rst) doctree = publish_doctree(output_rst) return doctree.children
def parse(doc_string): # TODO: rewrite (really ugly) if not doc_string: return {} def _paragraph(node_): return str(node_.children[0]) def _is_node(node_, name): return node_ and node_.tagname == name def _is_paragraph(node_, value): return _is_node(node_, "paragraph") and _paragraph(node_) == value def _fields(node_): fields = [] returns = None for field in node_: name, body = None, None for child in field: if _is_node(child, "field_name"): name = str(child.children[0]) elif _is_node(child, "field_body"): body = _paragraph(child.children[0]) name_pieces = name.split(" ") if len(name_pieces) == 3: fields.append(Parameter(name_pieces[2], name_pieces[1], body)) elif len(name_pieces) == 2: fields.append(Parameter(name_pieces[1], None, body)) elif name_pieces[0] == "return": returns = body return fields, returns result = {} text = [] root = publish_doctree(doc_string).children[0] if len(root.children) == 1 and root.children[0].tagname == "#text": text.append(_paragraph(root)) else: for index, (node, next_node) in enumerate(zip(root.children, root.children[1:] + [None])): if index == 0 and _is_node(node, "paragraph"): text.append(str(node.children[0])) else: if _is_node(next_node, "literal_block") and _is_paragraph(node, ":example:"): result["example"] = str(next_node.children[0]) elif _is_node(next_node, "literal_block") and _is_paragraph(node, ":rexample:"): result["result_example"] = str(next_node.children[0]) elif _is_node(node, "field_list"): result["params"], result["return"] = _fields(node) result["text"] = "".join(text) return result