如何让 restructedText 添加一个类到每个 html

标签?

发布于 2024-08-13 10:05:33 字数 724 浏览 6 评论 0原文

我正在使用 Django 的标记包将 restructedText 转换为 html。有没有办法自定义 HTML 编写器以向每个

标记添加类属性?

我可以为每个段落使用 class 指令,但我'我想自动化这个过程。

例如,我想要将这个重组文本:

hello
=====

A paragraph of text.

转换为这个html。

<h1>hello</h1>
<p class="specialClass">A paragraph of text.</p>

我想插入类的原因是因为我正在使用 连字符库,它的工作原理是添加具有“hyphenate”类的所有标签的连字符。我可以将连字符类添加到容器标记,但随后所有子级都会继承连字符类。我可以使用 javascript 动态添加类,但我认为可能有一种简单的方法可以使用 restructedText 来完成此操作。

谢谢你的帮助,

I'm using Django's markup package to transform restructuredText into html. Is there a way to customize the HTML writer to add a class attribute to each <p> tag?

I could use the class directive for each paragraph, but I'd like to automate this process.

For example, I want this restructured text:

hello
=====

A paragraph of text.

To be converted to this html.

<h1>hello</h1>
<p class="specialClass">A paragraph of text.</p>

The reason I want to insert classes is because I'm using the hyphenator library which works by adding hyphens to all tags with a "hyphenate" class. I could add the hyphenate class to the container tag, but then all the children would inherit the hyphenate class. I could use javascript to dynamically add the class, but I thought there might be a simple way to do it with restructuredText.

Thanks for the help,

Joe

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

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

发布评论

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

评论(2

橘虞初梦 2024-08-20 10:05:33

使用 this 作为参考..

from docutils.writers import html4css1

class MyHTMLWriter(html4css1.Writer):
  """
  This docutils writer will use the MyHTMLTranslator class below.
  """
  def __init__(self):
      html4css1.Writer.__init__(self)
      self.translator_class = MyHTMLTranslator

class MyHTMLTranslator(html4css1.HTMLTranslator):
  def visit_paragraph(self, node):
      self.section_level += 1
      self.body.append(self.starttag(node, 'p', CLASS='specialClass'))
  def depart_paragraph(self, node):
      self.section_level -= 1
      self.body.append('</p>\n')

然后像这样使用它:

from docutils.core import publish_string
print publish_string("*This* is the input text", writer=MyHTMLWriter())

Subclass the built-in html4css1 writer, using this as a reference..

from docutils.writers import html4css1

class MyHTMLWriter(html4css1.Writer):
  """
  This docutils writer will use the MyHTMLTranslator class below.
  """
  def __init__(self):
      html4css1.Writer.__init__(self)
      self.translator_class = MyHTMLTranslator

class MyHTMLTranslator(html4css1.HTMLTranslator):
  def visit_paragraph(self, node):
      self.section_level += 1
      self.body.append(self.starttag(node, 'p', CLASS='specialClass'))
  def depart_paragraph(self, node):
      self.section_level -= 1
      self.body.append('</p>\n')

Then use it like this:

from docutils.core import publish_string
print publish_string("*This* is the input text", writer=MyHTMLWriter())
帅气称霸 2024-08-20 10:05:33

您没有说明为什么要向每个段落添加一个类,但采用不同的方法可能会更容易。例如,如果您尝试设置段落样式,则可以使用不同的 CSS 技术来选择输出中的所有段落:

CSS:

div.resttext p {
    /* all the styling you want... */
}

HTML:

<div class='resttext'>
<p>Blah</p>
<p>Bloo</p>
</div>

Update: 由于您尝试使用 hyphenator.js,我建议使用其 < code>selectorfunction 设置以不同方式选择元素:

Hyphenator.config({
    selectorfunction: function () {
        /* Use jQuery to find all the REST p tags. */
        return $('div.resttext p');
        }
    });
Hyphenator.run();

You don't say why you want to add a class to every paragraph, but it might be easier to take a different approach. For example, if you are trying to style the paragraphs, you can use a different CSS technique to select all the paragraphs in the output:

CSS:

div.resttext p {
    /* all the styling you want... */
}

HTML:

<div class='resttext'>
<p>Blah</p>
<p>Bloo</p>
</div>

Update: since you are trying to use hyphenator.js, I would suggest using its selectorfunction setting to select the elements differently:

Hyphenator.config({
    selectorfunction: function () {
        /* Use jQuery to find all the REST p tags. */
        return $('div.resttext p');
        }
    });
Hyphenator.run();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文