我想弄清楚是否有办法嵌套 Sphinx 扩展指令。我有一些很好用的自定义节点类型,但我希望生成的 HTML 具有更大的灵活性。
这是我要完成的示例:
假设我有一个指令来创建一个文本框和按钮来触发一些 JavaScript。这很好用 - 我可以把它放在我的 reST 文件中:
.. myDirective:: name
:opt1: val
content
Run Code Online (Sandbox Code Playgroud)
现在,我希望能够在输出中使用 jQuery 创建选项卡式界面。我有一个节点类型,它创建发生这种情况所需的原始 HTML/JavaScript。
我希望能够做的是为选项卡节点提供 1 个或多个 myDirective(或其他指令)的实例,并将每个实例放入另一个选项卡中。像这样的东西:
..tabbedInterface::
.. myDirective:: name1
:op1: val1
content
.. myDirective:: name2
:op1: val1
content
Run Code Online (Sandbox Code Playgroud)
显然这是一个非常具体的情况,但这通常是我想要完成的 - 嵌套指令。
我正在尝试将我们的API文档及其专有文档生成器架构迁移到reStructuredText.给出最难的时间的是,我们有一个API细节的表格表示,直接用HTML编码,a:
--------+------------+--------+--------------------------------+
Param | Required | Type | Description
----------------------------------------------------------------
id | Yes | int | This is the ID of the record...
content | No | string | Optional string contents...
Run Code Online (Sandbox Code Playgroud)
(即目前编码为<tr><td class='param'>id</td><td class='required'>Yes</td>...)
我想在RST中执行此操作,但是在语义上执行此操作,而不是仅使用RST表格式.但我找不到任何自定义指令的好例子来处理我想要的方式,这就像是
:.. parameter-table:: My Parameter Table
.. item::
:param: "id"
:required: true
:type: "int"
:desc: "This is the ID of the record..."
Run Code Online (Sandbox Code Playgroud)
如何在reStructuredText中完成此操作?
documentation restructuredtext documentation-generation docutils python-sphinx
这是一个简单的reST片段:
deleting this line causes all subheadings to be rendered as h1 tags
I should be an h1
=================
I should be an h2
-----------------
foo
I should also be an h2
----------------------
foo
Run Code Online (Sandbox Code Playgroud)
这是一个演示它的演示:
初始行:http
://rst.ninjs.org/?n = f = 66780d732a33c7844f350c240804d0没有初始行:http://rst.ninjs.org/?n = 550ea2c1b4233affdce1d158c5dc4d99
我正在使用以下Python渲染reST:
from docutils.core import publish_parts
parts = publish_parts(rest_content, writer_name="html")
html_snippet = parts['html_body']
Run Code Online (Sandbox Code Playgroud)
如何在<h2>没有初始线的情况下获得副标题(特别是标签)?是否在子标题上方提供了两级层次结构?天真地提供页眉没有帮助:http://rst.ninjs.org/?n = e874f6eaad17c8ae7fd565f9ecb2212b
是否有针对docutil.nodes树的reST Writer?我找不到一个,但也许我错过了一些明显的东西.或者自己写一个是微不足道的?我想实现reST-to-reST转换.
我正在使用Sphinx,非常喜欢它,但它不会拿起模块概要.没有错误或任何事情,只是简单......没有.这是我试图自动记录的模块:
# vim: set fileencoding=utf-8 :
"""
.. module:: CONF
:synopsis: Configuration module, tells where data files are.
.. moduleauthor:: Mr Anderson <mr@matrix.com>
"""
Run Code Online (Sandbox Code Playgroud)
这是ReST索引文件中的Sphinx指令:
.. automodule:: CONF
:synopsis:
Run Code Online (Sandbox Code Playgroud)
我从狮身人面像那里得到了各种各样美妙的东西,所以对我来说一般都没有.我得到的唯一可疑的事情是:SEVERE: Duplicate ID: "module-CONF".一些谷歌搜索让我相信虽然这个错误很"正常"?
我开始使用带有reStructuredText rst页面格式的pelican .我有自定义javascript(jQuery)的东西,我想用div属性控制,比如data-default-tpl="basename"嵌套内容.
什么延伸和什么.我看Directives和nodes,但我不能换我围绕如何做到这一点的头.
.. rstdiv:: class1 class2
:name: namessid
:extra: thisIsMyextra
.. rstdiv:: nested class3
:name: nestedid
:extra: data-default-tpl="basename"
some text
.. container:: This is normal rst container
:name: contid
text
Run Code Online (Sandbox Code Playgroud)
从rst到html与鹈鹕.
<div id="nameisid" class="class1 class2" thisIsMyextra>
<div id="nestedid" class="nested class3" data-default-tpl="basename">
some text
</div>
</div>
<div id="contid" class="container This is normal rst container">
text
</div>
Run Code Online (Sandbox Code Playgroud) 我想从重组结构化文本字符串中的代码指令中逐字提取源代码。
接下来是我第一次尝试这样做,但是我想知道是否有更好的方法(即更健壮或更通用或更直接)来做到这一点。
假设我在Python中将以下第一个文本作为字符串:
s = '''
My title
========
Use this to square a number.
.. code:: python
def square(x):
return x**2
and here is some javascript too.
.. code:: javascript
foo = function() {
console.log('foo');
}
'''
Run Code Online (Sandbox Code Playgroud)
要获得两个代码块,我可以做
from docutils.core import publish_doctree
doctree = publish_doctree(s)
source_code = [child.astext() for child in doctree.children
if 'code' in child.attributes['classes']]
Run Code Online (Sandbox Code Playgroud)
现在,source_code是仅包含两个代码块中逐字记录源代码的列表。如果需要,我也可以使用child的attribute属性来找出代码类型。
它可以完成工作,但是有更好的方法吗?
我正在尝试使用 docutils 包将 ReST 转换为 HTML。这个答案简洁地使用了 docutils 的publish_*便利功能来一步实现这一点。我想转换的 ReST 文档有多个部分,我想在生成的 HTML 中将它们分开。因此,我想分解这个过程:
这是我正在努力解决的第三步。以下是我如何执行第一步和第二步:
from docutils import utils
from docutils.frontend import OptionParser
from docutils.parsers.rst import Parser
# preamble
rst = '*NB:* just an example.' # will actually have many sections
path = 'some.url.com'
settings = OptionParser(components=(Parser,)).get_default_values()
# step 1
document = utils.new_document(path, settings)
Parser().parse(rst, document)
# step 2
for node in document:
do_something_with(node)
# step 3: Help!
for node in filtered(document): …Run Code Online (Sandbox Code Playgroud) 如何在 Sphinx 中遍历文档的每个部分名称?
(docutils 的文档在哪里?除了Sphinx 应用程序 API之外,很难找到任何有用的东西;即使查看docutils/nodes.py的源代码也没有增加太多帮助。)
我正在为 Pelican 开发一个库,目前我正在使用 reStructuredText 和 docutils。
我想做的是创建一个应该输出 html 的自定义指令。
我要输出的html是:
<div class="row">
<div class="col-md-6">
<div class="card">
<div class="card-header">
<h4 class="card-title">Regular header</h4>
<p class="category">Category subtitle</p>
</div>
<div class="card-body">
The place is close to Barceloneta Beach and bus stop just 2 min by walk and near to "Naviglio" where you can enjoy the main night life in Barcelona...
</div>
</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
到目前为止我创建了这个Python代码
from docutils import nodes
from docutils.parsers.rst import Directive
from pelican.rstdirectives import directives
class Row(Directive):
required_arguments = 0
optional_arguments = …Run Code Online (Sandbox Code Playgroud)