有没有一种方法可以生成单元格中没有段落的html表(使用docutils rst2html)?

2024-09-28 22:19:24 发布

您现在位置:Python中文网/ 问答频道 /正文

如果我有这样的rst:

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

如您所见,单元格内容放在段落标记内(在<p></p>内),因此被格式化为段落,而不像我的表格在css文件中配置的那样(例如,如果我在css中向文本段落添加第一行缩进,单元格内容也会接收该缩进)

有没有一种方法可以生成单元格中没有段落的html表(使用docutils rst2html)?你知道吗

注意事项:

  • 实际上,我正在转换一个非常长的rst文件,因此最好让它正常工作,而不是侵入/替换html。你知道吗
  • 同样的事情也发生在我的列表项中,它们是在<p>标记中创建的(例如<li><p>Something</p></li>)。你知道吗

Tags: a2htmla1tablersth1headtr
1条回答
网友
1楼 · 发布于 2024-09-28 22:19:24

您可以使用自定义CSS覆盖<p>标记的可视间距。像这样的。你知道吗

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

然而,我觉得奇怪的是,您的输出在表中生成了<p>标记,因为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>

相关问题 更多 >