【问题标题】:Is there a way to generate html tables without paragraphs inside the cells (using docutils rst2html)?有没有办法在单元格内生成没有段落的html表格(使用docutils rst2html)?
【发布时间】:2018-06-13 09:18:21
【问题描述】:

如果我有这样的第一个:

+--------------------------------+
| Table H1                       |
+-------------+------------------+
| Table H2a   | Table H2b        |
+=============+==================+
| a1          | b1               |
+-------------+------------------+
| a2          | b2               |
+-------------+------------------+
| a3          | b3               |
+-------------+------------------+

并像这样将其转换为html:

python -u %PYTHONPATH3%\\Scripts\\rst2html5.py input.rst output.html

生成的html表格如下:

<body>
    <div class="document"><blockquote><table>
        <colgroup>
            <col style="width: 42%" />
            <col style="width: 58%" />
        </colgroup>
        <thead>
            <tr><th class="head" colspan="2"><p>Table H1</p></th>
            </tr>
            <tr><th class="head"><p>Table H2a</p></th>
            <th class="head"><p>Table H2b</p></th>
            </tr>
        </thead>
        <tbody>
            <tr><td><p>a1</p></td>
            <td><p>b1</p></td>
            </tr>
            <tr><td><p>a2</p></td>
            <td><p>b2</p></td>
            </tr>
            <tr><td><p>a3</p></td>
            <td><p>b3</p></td>
            </tr>
        </tbody>
    </table></blockquote></div>
</body>

如您所见,单元格内容放置在段落标签内(&lt;p&gt;&lt;/p&gt; 内),因此被格式化为段落,而不像我的表格在 css 文件中配置(例如,如果我添加第一个 - css中文本段落的行缩进,单元格内容也接收该缩进)

有没有办法在单元格内生成没有段落的html表格(使用docutils rst2html)?

注意事项:

  • 我实际上是在转换一个非常长的 rst 文件,因此最好让它正常工作,而不是修改/替换 html。
  • 我的列表项也会发生同样的事情,它们是在&lt;p&gt; 标签内创建的(例如&lt;li&gt;&lt;p&gt;Something&lt;/p&gt;&lt;/li&gt;)。

【问题讨论】:

    标签: python html css restructuredtext


    【解决方案1】:

    您可以使用自定义 CSS 覆盖 &lt;p&gt; 标记的视觉间距。像这样。

    td > p, li > p {
        margin: 0;
    }
    

    但是,我认为奇怪的是,您的输出会在表格中生成 &lt;p&gt; 标记,因为这不是 docutils 所做的,假设这是 rst2html5 所基于的。我建议您联系该库的作者。

    编辑:这是我使用 Sphinx 和您的 reST 示例的 HTML 输出:

    <table border="1" class="docutils">
    <colgroup>
    <col width="42%">
    <col width="58%">
    </colgroup>
    <thead valign="bottom">
    <tr class="row-odd"><th class="head" colspan="2">Table H1</th>
    </tr>
    <tr class="row-even"><th class="head">Table H2a</th>
    <th class="head">Table H2b</th>
    </tr>
    </thead>
    <tbody valign="top">
    <tr class="row-odd"><td>a1</td>
    <td>b1</td>
    </tr>
    <tr class="row-even"><td>a2</td>
    <td>b2</td>
    </tr>
    <tr class="row-odd"><td>a3</td>
    <td>b3</td>
    </tr>
    </tbody>
    </table>
    

    【讨论】:

    • 它解决了real 问题(渲染),即使段落仍然存在。也许我应该坚持这一点,并在 docutils 的页面上提交错误/问题。感谢您的 CSS 解决方案!
    • 问题应该提交给rst2html5,而不是docutils。或者改用 rst2html、docutils 或 Sphinx。
    • 我使用的 rst2html5 脚本随 docutils 一起提供(Author: Günter Milde &lt;milde@users.sf.net&gt;Maintainer: docutils-develop@lists.sourceforge.net),由一个 .py 文件(docutils.writers.html5_polyglot.__init__.py 加上一些 css 文件)组成。rst2html5 只是调用它的脚本/前端的名称。
    • 我无法使用 Sphinx 复制无关的 &lt;p&gt; 标签,它也使用 docutils。我已经相应地编辑了我的答案。我没有安装 rst2html5,但我会指责那里,因为它在其 description on PyPI 中声明“这是对 docutils 的 rst2html 的完全重写,并使用了新的 HTML5 结构,例如 &lt;section&gt;&lt;aside&gt;。”
    • 对,使用 Sphinx 也解决了这个问题(正确生成 html / 清除不必要的段落标签),所以正如你之前所说,具体的 rst2html5 脚本是罪魁祸首。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-05
    • 1970-01-01
    • 2019-11-06
    • 1970-01-01
    相关资源
    最近更新 更多