ReST strikethrough

问题描述 投票:47回答:5

能否在重组后的文本中打通文本?

例如,有些东西会被渲染成 <strike> 标签,当转换为HTML时,就像。ReSTructuredText

restructuredtext strikethrough docutils
5个回答
44
投票

我按照Ville Säävuori的建议,更好地检查了文档,我决定添加这样的删除线。

.. role:: strike
    :class: strike

在文档中,它可以被应用于以下方面:

:strike:`This text is crossed out`

然后在我的 css 文件中我有一个条目。

.strike {
  text-decoration: line-through;
}

15
投票

至少有三种方法。

.. 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: 选项就可以了。

因为它产生了一个 spanrole 指令是唯一一个允许将你的样式应用到段落的一部分的指令。

在一个段落中添加一个类是多余的。strike 的指令,也称为strike正如Gozzilli所建议的那样,因为指令名是html输出的默认class。

我已经检查了这些语法,无论是用 rest2html斯芬克斯. 但是,虽然一切都能像预期的那样使用 rest2htmlclass指令失败 斯芬克斯. 你得换成

.. rst-class:: strike

This paragraph too is is striked through.

这就是 只是 小声说狮身人面像初级读物注解.


12
投票

根据官方规格没有删除线的指令 的标记。

但是,如果环境允许:raw:角色,或者你能够编写自己的角色,那么你可以为它编写一个自定义插件。


4
投票

我发现其他的答案非常有帮助。我对Sphinx不是很熟悉,但我正在为一个项目使用它。我也希望有击穿的能力,并且已经根据前面的答案让它工作了。要说明的是,我添加了gozzilli提到的删除角色,但我使用了堆栈溢出线程中讨论的rst_prolog变量将其保存在conf.py中。此处. 这意味着这个角色对你所有的休息文件都是可用的。

然后,我扩展了上述的基本html模板,创建了 layout.html_templates在我的源目录下。这个文件的内容是。

{% extends "!layout.html" %}
{% set css_files = css_files + ["_static/myStyle.css"] %}

基本上包含了一个自定义的css文件到你所有内置的默认html docs。

最后,在我的源代码目录下的_static目录中,我包含了以下文件。myStyle.css 其中包含:

.strike {
  text-decoration: line-through;
}

其他答案已经提供了

我只是写了这个答案,因为以我有限的 Sphinx 经验,我并不清楚要编辑哪些文件。


2
投票

这里有一个Python的定义 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)

更好的办法是扩展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>,当然。


0
投票

考虑到用户可能有不同的背景,所以这里没有一个解决方案可以适合所有人。

1.只有一个文件

如果你只在一个文件上使用它。例如,你在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。

然后会看到如下图。

enter image description here

很简单,你可以在一些IDE上直接看到预览,比如PyCharm。


下面是写给Sphinx用户的。

2.斯芬克斯的初学者

如果你是一个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文件上,但如果你改变了内容(该文件包含你定义的格式),那么你必须 改造 以使渲染正确。

3、创建角色

你可以创建一个扩展来实现它。

# 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 ...`

完整的架构。

  • conf.py
  • _ext
    • rst_roles.py
  • _static
    • css
      • user.define.css

关于规则,你可以参考这个链接 rst-roles

而我不同推荐你去看 docutils.parsers.rst.roles.py .

© www.soinside.com 2019 - 2024. All rights reserved.