【发布时间】:2011-09-25 00:23:47
【问题描述】:
是否可以在重组文本中删除文本?
例如在转换为 HTML 时呈现为 <strike> 标记的内容,例如:
重构文本
【问题讨论】:
-
当您使用 Sphinx 时,这可能会有所帮助:stackoverflow.com/a/24932178/2923406
标签: restructuredtext strikethrough docutils
是否可以在重组文本中删除文本?
例如在转换为 HTML 时呈现为 <strike> 标记的内容,例如:
重构文本
【问题讨论】:
标签: restructuredtext strikethrough docutils
按照 Ville Säävuori 的建议,我更好地检查了文档,我决定像这样添加删除线:
.. role:: strike
:class: strike
在文档中,可以这样应用:
:strike:`This text is crossed out`
然后在我的css 文件中我有一个条目:
.strike {
text-decoration: line-through;
}
【讨论】:
<del> 或<s> 标签)。我已经在下面发布了代码(stackoverflow.com/a/62493316/695591)
至少有三种方法:
.. role:: strike
An example of :strike:`strike through text`.
.. container:: strike
Here the full block of test is striked through.
An undecorated paragraph.
.. class:: strike
This paragraph too is is striked through.
.. admonition:: cancelled
:class: strike
I strike through cancelled text.
申请rst2html后,您将获得:
<p>An example of <span class="strike">strike through text</span>.</p>
<div class="strike container">
Here the full block of test is striked through.</div>
<p>An undecorated paragraph.</p>
<p class="strike">This paragraph too is is striked through.</p>
<div class="strike admonition">
<p class="first admonition-title">cancelled</p>
<p class="last">I strike through cancelled text.</p>
您将它们与样式一起使用
.strike {
text-decoration: line-through;
}
这里我以admonition 指令为例,但任何
允许:class: 选项的指令就可以了。
当它生成span 时,role 指令是唯一一个
允许将您的样式应用于段落的一部分。
将strike 类添加到同样名为的指令中是多余的
strike,建议 Gozzilli,因为指令名称是默认的
html 输出的类。
我已经用rest2html 和 Sphinx 检查了这些语法。但
虽然 rest2html 和 class 的一切都按预期工作
Sphinx 指令失败。您必须将其替换为
.. rst-class:: strike
This paragraph too is is striked through.
这只是用小字表示 footnote of Sphinx reSt Primer.
【讨论】:
According to the official spec ReST 中没有没有删除线的指令标记。
但是,如果环境允许 :raw: 角色或者您能够编写自己的角色,那么您可以为其编写自定义插件。
【讨论】:
这是del 角色的 Python 定义,如果您想在 Pelican 博客或 Sphinx 文档项目的多个页面中使用该角色,它比公认的答案效果更好:
from docutils import nodes
from docutils.parsers.rst import roles
def deleted_role(_role, rawtext, text, _lineno, _inliner, options={}, _content=[]):
roles.set_classes(options)
options.setdefault('classes', []).append("del")
return [nodes.inline(rawtext, text, **options)], []
roles.register_canonical_role('del', deleted_role)
更好的办法是扩展 HTML 编写器以生成正确的 <del> 标记,如下所示:
from docutils import nodes
from docutils.parsers.rst import roles
from docutils.writers._html_base import HTMLTranslator
class delnode(nodes.inline):
pass
def visit_delnode(self, node):
self.body.append(self.starttag(node, 'del', ''))
def depart_delnode(self, node):
self.body.append('</del>')
HTMLTranslator.visit_delnode = visit_delnode
HTMLTranslator.depart_delnode = depart_delnode
def deleted_role(_role, rawtext, text, _lineno, _inliner, options={}, _content=[]):
roles.set_classes(options)
return [delnode(rawtext, text, **options)], []
roles.register_canonical_role('del', deleted_role)
当然,您可以对其进行微调以生成 <s>。
【讨论】:
我发现其他答案非常有帮助。 我对 Sphinx 不是很熟悉,但我正在将它用于一个项目。我也想要删除能力,并根据之前的答案让它工作。 为了清楚起见,我添加了 gozzilli 提到的删除线角色,但我使用堆栈溢出线程 here 中讨论的 rst_prolog 变量将其保存在我的 conf.py 中。这意味着该角色可用于您的所有 REST 文件。
然后我通过在我的源目录中创建layout.htmlwithin _templates来扩展基本 html 模板。这个文件的内容是:
{% extends "!layout.html" %}
{% set css_files = css_files + ["_static/myStyle.css"] %}
这基本上包括一个自定义 css 文件到您构建的所有默认 html 文档。
最后,在我的源目录中的 _static 目录中,我包含了文件 myStyle.css,其中包含:
.strike {
text-decoration: line-through;
}
其他答案已经提供了。
我只是在写这个答案,因为我有限的 Sphinx 经验对我来说并不明显要编辑哪些文件。
【讨论】:
考虑到用户可能有不同的背景,所以这里没有一种适合所有人的解决方案。
如果您只在一个文件上使用它。例如,您向 PyPI 发布了一个简单的项目,您可能只有一个 README.rst 文件。您可能想要以下内容。
.. |ss| raw:: html
<strike>
.. |se| raw:: html
</strike>
single line
=============
|ss| abc\ |se|\defg
multiple line
=============
|ss|
line 1
line 2
|se|
789
你可以复制粘贴到这个网站:https://livesphinx.herokuapp.com/
会看到如下图片:
很简单,你可以在某些IDE上直接看到预览,例如PyCharm。
下面是为 Sphinx 的用户写的
如果您是 Sphinx 的初学者。 (我的意思是也许你想使用 Sphinx 创建一个文档,但是 Python 对你来说并不熟悉)然后尝试如下:
# conf.py
from pathlib import Path
html_static_path = ['_static', ]
html_css_files = ['css/user.define.css'] # If you want to control which HTML should contain it, you can put it on the HTML, which is very like the answer by @Gregory Kuhn.
with open(Path(__file__).parent / Path('_static/css/user.define.rst'), 'r') as f:
user_define_role = f.read()
rst_prolog = '\n'.join([ user_define_role + '\n',]) # will be included at the beginning of every source file that is read.
# rst_epilog = '\n'.join([ user_define_role + '\n',]) # it's ok if you put it on the end.
user.define.rst
.. role:: strike
user.define.css
.strike {text-decoration: line-through;}
使用rst_prolog,它可以在每个第一个文件上自动添加角色,但是如果您更改内容(该文件,它包含您定义的格式),那么您必须rebuild 进行渲染是正确的。
您可以创建一个扩展来实现它。
# conf.py
extensions = ['_ext.rst_roles', ]
html_static_path = ['_static', ]
html_css_files = ['css/user.define.css']
# rst_roles.py
from sphinx.application import Sphinx
from docutils.parsers.rst import roles
from docutils import nodes
from docutils.parsers.rst.states import Inliner
def strike_role(role, rawtext, text, lineno, inliner: Inliner, options={}, content=[]):
your_css_strike_name = 'strike'
return nodes.inline(rawtext, text, **dict(classes=[your_css_strike_name])), []
def setup(app: Sphinx):
roles.register_canonical_role('my-strike', strike_role) # usage: :my-strike:`content ...`
完整架构:
关于规则,可以参考这个链接rst-roles
而且我建议你去看docutils.parsers.rst.roles.py。
【讨论】:
README.rst)上的 reST 文件的常见情况的解决方案。讨厌的人会讨厌,@Carson。我不会亲自接受反对票。他们只是果冻。
我为此写了一个扩展。
只需pip install sphinxnotes-strike 并使用:
:strike:`text`
或
:del:`text`
显示罢工文本。
【讨论】:
从 Docutils 0.17 开始,如果在 inline、literal 或 container 元素中找到匹配的类值,则 HTML5 编写器使用 <del>:
.. role:: del
:del:`This text has been deleted`, here is the rest of the paragraph.
.. container:: del
This paragraph has been deleted.
【讨论】: