首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Python docutils创建简单的html标记

如何使用Python docutils创建简单的html标记
EN

Stack Overflow用户
提问于 2019-12-06 20:31:12
回答 1查看 476关注 0票数 5

我正在为鹈鹕开发一个库,目前正在使用reStructuredText和docutils。

我要做的是创建一个应该输出html的自定义指令。

我要输出的html是:

代码语言:javascript
复制
<div class="row">
  <div class="col-md-6">
    <div class="card">
      <div class="card-header">
        <h4 class="card-title">Regular header</h4>
        <p class="category">Category subtitle</p>
      </div>
      <div class="card-body">
        The place is close to Barceloneta Beach and bus stop just 2 min by walk and near to "Naviglio" where you can enjoy the main night life in Barcelona...
      </div>
    </div>
  </div>
</div>

到目前为止,我创建了这个Python代码

代码语言:javascript
复制
from docutils import nodes
from docutils.parsers.rst import Directive
from pelican.rstdirectives import directives

class Row(Directive):
    required_arguments = 0
    optional_arguments = 0
    final_argument_whitespace = True
    option_spec = {}
    has_content = True

    def run(self):
        # Raise an error if the directive does not have contents.
        self.assert_has_content()

        node = nodes.Element()
        node.document = self.state.document
        self.state.nested_parse(self.content, self.content_offset, node)

        # entries = []
        resultnode = nodes.container()
        for i, child in enumerate(node):
            para = nodes.paragraph()
            para += [child]
            resultnode.append(para)
            # entry = nodes.container()
            # entry.append(para)
            # entries.append(entry)

        resultnode.update_basic_atts({
            "classes": ["row"]
        })
        # resultnode.append(nodes.container('', *entries))

        return [resultnode]

class Card(Directive):
    required_arguments = 0
    optional_arguments = 0
    final_argument_whitespace = True
    option_spec = {
        "header-title": str,
        "header-category": str
    }
    has_content = True

    def run(self):
        # Raise an error if the directive does not have contents.
        self.assert_has_content()
        text = '\n'.join(self.content)

        col = nodes.container()
        col["classes"] = ["col-md-6"]

        card = nodes.container()
        card["classes"] = ["card"]
        col.append(card)

        card_header = nodes.container()
        card_header["classes"] = ["card-header"]
        card.append(card_header)

        header_title = nodes.paragraph(text=self.options.get("header-title"))
        header_title["classes"] = ["card-title"]
        card_header.append(header_title)

        header_category = nodes.paragraph(text="Category subtitle")
        header_category["classes"] = ["category"]
        card_header.append(header_category)

        card_body = nodes.paragraph(text=text)
        card_body["classes"] = ["card-body"]
        card.append(card_body)

        return [col]

def register():
    directives.register_directive("card", Card)
    directives.register_directive("row", Row)

它创建了这个html:

代码语言:javascript
复制
<div class="row docutils container">
  <div class="col-md-6 docutils container">
    <div class="card docutils container">
      <div class="card-header docutils container">
        <p class="card-title">Regular header</p>
        <p class="category">Category subtitle</p>
      </div>
      <p class="card-body">
        Hello, world
      </p>
    </div>
  </div>
</div>

所以有几件事我想知道:

  1. 如何从class属性中删除docutils containercontainer与主题container css类相冲突。
  2. 如何创建简单的html元素,如
EN

回答 1

Stack Overflow用户

发布于 2021-03-04 03:20:31

与其重用container,不如创建一个自定义节点类型。然后,创建HTML编写器的子类,以输出所需的标记。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59219964

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档