【问题标题】:ReST strikethroughReST 删除线
【发布时间】:2011-09-25 00:23:47
【问题描述】:

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

例如在转换为 HTML 时呈现为 <strike> 标记的内容,例如: 重构文本

【问题讨论】:

标签: restructuredtext strikethrough docutils


【解决方案1】:

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

.. role:: strike
    :class: strike

在文档中,可以这样应用:

:strike:`This text is crossed out`

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

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

【讨论】:

  • 这种方法的问题在于,您必须在要使用删除线的每个 reST 文件中重复此定义,并且它不会在 HTML 端产生有意义的标记(它会最好生成<del><s> 标签)。我已经在下面发布了代码(stackoverflow.com/a/62493316/695591
【解决方案2】:

至少有三种方法:

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

只是用小字表示 footnote of Sphinx reSt Primer.

【讨论】:

  • 您确定该链接。我看起来不像你想要的那样。
  • @Tshepang,我检查了,链接最终在脚注上声明:当默认域包含类指令时,该指令将被隐藏。因此,Sphinx 将其重新导出为第一类。
【解决方案3】:

According to the official spec ReST 中没有没有删除线的指令标记。

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

【讨论】:

【解决方案4】:

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

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)

当然,您可以对其进行微调以生成 &lt;s&gt;

【讨论】:

    【解决方案5】:

    我发现其他答案非常有帮助。 我对 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 经验对我来说并不明显要编辑哪些文件。

    【讨论】:

      【解决方案6】:

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

      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 创建一个文档,但是 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 进行渲染是正确的。

      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
      • _静态/
        • css/
          • user.define.css

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

      而且我建议你去看docutils.parsers.rst.roles.py

      【讨论】:

      • 我不知道为什么我的回答会让人不高兴然后给我投反对票,希望你能告诉我为什么,无论如何,我更新了我的解决方案希望你能理解。
      • 这是目前的最佳答案。第一个“只有一个文件”。解决方案真的是最优的。这是唯一真正适用于托管在您直接控制的第三方站点(例如,GitHub 或 GitLab 托管 README.rst)上的 reST 文件的常见情况的解决方案。讨厌的人会讨厌,@Carson。我不会亲自接受反对票。他们只是果冻。
      【解决方案7】:

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

      :strike:`text` 
      

      :del:`text` 
      

      显示罢工文本。

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

      【讨论】:

        【解决方案8】:

        从 Docutils 0.17 开始,如果在 inlineliteralcontainer 元素中找到匹配的类值,则 HTML5 编写器使用 &lt;del&gt;

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

        【讨论】:

          猜你喜欢
          • 2018-09-22
          • 2015-12-07
          • 2021-04-19
          • 1970-01-01
          • 1970-01-01
          • 2022-01-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多