我使用Django的标记包reStructuredText的转换成HTML。有没有一种方法可以自定义的HTML编写的类属性添加到每个<p>标签?

我可以使用类指令获得每个段落,但我想使该过程自动化。

例如,我想此重组文本:

hello
=====

A paragraph of text.

要被转换为这个网站。

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

我要插入类的原因是因为我使用的连字符库这通过添加连字符所有标签与“断字”类的工作。我可以在断字类添加到容器的标签,但后来所有的孩子将继承断字的类。我可以使用JavaScript来动态添加类,但我想可能有一个简单的方法用reStructuredText的做到这一点。

感谢您的帮助,

有帮助吗?

解决方案

你不说你为什么要添加一个类每一个段落,但它可能是更容易采取不同的方法。例如,如果你想样式的段落,你可以使用不同的CSS技术来选择输出的所有段落:

CSS:

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

HTML:

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

更新:既然你要使用hyphenator.js,我会建议使用其selectorfunction设置来选择不同的元素:

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

其他提示

子类中的内置html4css1作家,使用作为参考..

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())
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top