在 Restructured Text 中是否有办法划掉文字?
例如,当转换为 HTML 时,可以呈现为 <strike>
标签的内容,如:ReSTructuredText
在 Restructured Text 中是否有办法划掉文字?
例如,当转换为 HTML 时,可以呈现为 <strike>
标签的内容,如:ReSTructuredText
我仔细查看了文档,就像Ville Säävuori建议的那样,我决定这样添加删除线:
.. role:: strike
:class: strike
在文档中,可以按照如下方式应用::strike:`This text is crossed out`
然后在我的 css
文件中有以下条目:
.strike {
text-decoration: line-through;
}
<del>
或<s>
标记)。我已经发布了该代码(https://dev59.com/iGw15IYBdhLWcg3wmMx9#62493316) - Clément至少有三种方法可以做到这一点:
.. 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
指令是唯一可以将样式应用于段落部分的指令。
像Gozzilli建议的那样,给一个名为strike
的指令添加一个strike
类是多余的,因为指令名称是html输出的默认类。
我已经使用rest2html
和Sphinx检查了这些语法。但是,虽然rest2html
一切正常,但class
指令在Sphinx中失败了。您必须将其替换为
.. rst-class:: strike
This paragraph too is is striked through.
这只在Sphinx reSt Primer的一个小脚注中提到。
根据官方规范,在ReST中不存在删除线标记的指令。
但是,如果环境允许使用:raw:角色或您能够编写自己的角色,则可以编写自定义插件来添加删除线标记。
del
角色的定义,如果你想在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)
<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>
。
_templates
目录中的layout.html
,按上述描述扩展了基本的HTML模板。此文件的内容如下:{% extends "!layout.html" %}
{% set css_files = css_files + ["_static/myStyle.css"] %}
这基本上是将自定义的CSS文件应用到所有默认的HTML文档中。
最后,在我的源目录中的 _static 目录中,我包含了一个名为 myStyle.css
的文件,其中包含以下内容:
.strike {
text-decoration: line-through;
}
考虑到用户可能具有不同的背景,因此没有一种适用于所有人的解决方案。
如果您只在一个文件上使用它。例如,您将一个简单的项目发布到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
,可以在每个rst文件中自动添加角色,但是如果您更改内容(该文件包含您定义的格式),则必须重新生成以使渲染正确。
您可以创建一个扩展来实现这一功能。
# 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 ...`
docutils.parsers.rst.roles.py
。README.rst
)的常见情况的解决方案,而这些站点并不在您的直接控制之下。无论怎样,@Carson,讨厌者总是会讨厌的。我不会把这些踩票当成个人攻击。他们只是嫉妒罢了。 - Cecil Currypip install sphinxnotes-strike
,然后使用以下命令::strike:`text`
或者
:del:`text`
展示删除线文本的方法。
<del>
。.. role:: del
:del:`This text has been deleted`, here is the rest of the paragraph.
.. container:: del
This paragraph has been deleted.