休息删除线

发布于 2024-11-17 19:29:05 字数 140 浏览 4 评论 0原文

是否可以在重组文本中删除文本?

例如,转换为 HTML 时呈现为 标记的内容,例如: ReSTructuredText

Is it possible to strike text through in Restructured Text?

Something that for example renders as a <strike> tag when converted to HTML, like:
ReSTructuredText

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

牵强ㄟ 2024-11-24 19:29:05

按照 Ville Säävuori 的建议,我更好地检查了文档,并决定添加删除线,如下所示:

.. role:: strike
    :class: strike

在文档中,可以按如下方式应用此操作:

:strike:`This text is crossed out`

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

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

I checked the docs better, as suggested by Ville Säävuori, and I decided to add the strikethrough like this:

.. role:: strike
    :class: strike

In the document, this can be applied as follows:

:strike:`This text is crossed out`

Then in my css file I have an entry:

.strike {
  text-decoration: line-through;
}
骄傲 2024-11-24 19:29:05

至少有三种方法可以做到这一点:

.. 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 输出的类。

我已经使用 rest2htmlSphinx 检查了这些语法。但
rest2htmlclass 一切都按预期工作
指令因 Sphinx 失败。您必须将其替换为

.. rst-class:: strike

This paragraph too is is striked through.

This is only
Sphinx Rest Primer 的脚注

There is at least three ways of doing it:

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

After applying rst2html you get:

<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>

You use them with a style

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

Here I have taken the admonition directive as example but any
directive that allow the :class: option would do.

As it generates a span the role directive is the only one that
allow to apply your style to a part of a paragraph.

It is redundant to add a class strike to a directive also named
strike, as suggest Gozzilli, because the directive name is the default
class for the html output.

I have checked these syntax both with rest2html and Sphinx. But
while everything works as expected with rest2html the class
directive fail with Sphinx. You have to replace it with

.. rst-class:: strike

This paragraph too is is striked through.

This is only stated in a small
footnote of Sphinx reSt Primer.

属性 2024-11-24 19:29:05

根据官方规范没有删除线指令< ReST 中的 /strong> 标记。

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

According to the official spec there is no directive for strikethrough markup in ReST.

However, if the environment allows for :raw: role or you are able to write your own roles, then you can write a custom plugin for it.

山川志 2024-11-24 19:29:05

我发现其他答案非常有帮助。
我对 Sphinx 不是很熟悉,但我正在将它用于一个项目。我也想要删除线功能,并根据之前的答案让它发挥作用。
需要明确的是,我添加了 gozzilli 提到的删除线角色,但我使用 rst_prolog 变量将其保存在我的 conf.py 中,如堆栈溢出线程 此处。这意味着该角色可用于您的所有其余文件。

然后,我通过在源目录内的 _templates 中创建 layout.html 来扩展基本 html 模板,如上所述。该文件的内容是:

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

这基本上包括所有构建的默认 html 文档的自定义 css 文件。

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

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

其他答案已经提供了。

我只是写这个答案,因为以我有限的 Sphinx 经验,要编辑哪些文件对我来说并不明显。

I found the other answers very helpful.
I am not very familiar with Sphinx but I am using it for a project. I too wanted the strike-through ability and have got it working based on the previous answers.
To be clear, I added my strikethrough role as gozzilli mentioned but I saved it inside my conf.py using the rst_prolog variable as discussed in the stack overflow thread here. This means that this role is available to all of your rest files.

I then extended the base html template as described above by creating layout.htmlwithin _templatesinside my source directory. The contents of this file are:

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

This basically includes a custom css file to all your built default html docs.

Finally, in my _static directory within my source directory I included the file myStyle.css which contains:

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

Which the other answers have already provided.

I am merely writing this answer as it wasn't obvious to me with my limited Sphinx experience which files to edit.

虫児飞 2024-11-24 19:29:05

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

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/

,然后会看到图片如下:

“输入图片描述在这里

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


下面是为Sphinx的用户写的

2.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,它可以在每个第一个文件上自动添加角色,但是如果您更改内容(该文件,它包含您定义的格式),那么您必须重建 以使渲染正确。

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/
      • 用户.define.css

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

我建议您查看 docutils.parsers.rst.roles.py

Consider the user may have a different background, so here is no one solution that can be suitable for everyone.

1.Only one file

If you only use it only on one file. For example, you published a simple project to PyPI, and you may probably just only one README.rst file. The following may you want.

.. |ss| raw:: html

    <strike>

.. |se| raw:: html

    </strike>

single line
=============

|ss| abc\ |se|\defg

multiple line
=============

|ss|  
line 1

line 2
|se|

789

you can copy and paste it on this website: https://livesphinx.herokuapp.com/

and will see the picture as the following:

enter image description here

It's simple, and you can on directly see the preview on some IDE, for example, PyCharm.


bellow is writing for the users of Sphinx

2.beginner of Sphinx

If you are a beginner of Sphinx. ( I mean maybe you want to use Sphinx to create a document, but Python is not familiar for you ) then try as following:

# 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;}

With the rst_prolog, It can auto-add the role on each rst files, but if you change the content( that file, it contains a format that you define), then you must rebuild to make the render is correct.

3.Create roles

You can create an extension to achieve it.

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

The full architecture:

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

about the rules, you can reference this link rst-roles

And I vary recommended you to see the docutils.parsers.rst.roles.py .

半边脸i 2024-11-24 19:29:05

这是 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 编写器生成正确的 标记,如下所示:

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)

当然,您可以简单地调整它以生成

Here's a Python definition of a del role, which works better than the accepted answer if you want to use the role in multiple pages of a Pelican blog or a Sphinx documentation project:

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)

Even better would be to extend the HTML writer to produce a proper <del> tag, like this:

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)

You can trivially adjust it to produce an <s>, of course.

贪恋 2024-11-24 19:29:05

我为此编写了一个扩展。
只需 pip install sphinxnotes-strike 并使用:

:strike:`text` 

:del:`text` 

显示删除文本。

欲了解更多信息:https://sphinx-notes.github.io/strike/

I wrote an extension for this.
Just pip install sphinxnotes-strike and use:

:strike:`text` 

or

:del:`text` 

to show strike text.

For more info: https://sphinx-notes.github.io/strike/

怎樣才叫好 2024-11-24 19:29:05

从 Docutils 0.17 开始,如果在 inlineliteral中找到匹配的类值,HTML5 编写器将使用 容器元素:

.. role:: del

:del:`This text has been deleted`, here is the rest of the paragraph.

.. container:: del

  This paragraph has been deleted.

Spinx用户可以考虑将角色定义添加到他们的rst_prolog

Since Docutils 0.17, the HTML5-writer uses <del> if a matching class value is found in inline, literal, or container elements:

.. role:: del

:del:`This text has been deleted`, here is the rest of the paragraph.

.. container:: del

  This paragraph has been deleted.

Spinx users may consider to add the role definition to their rst_prolog.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文