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?
我正在使用Django的标记包将restructuredText转换为html。有没有办法自定义HTML编写器为每个
标签添加一个类属性?
I could use the class directive for each paragraph, but I'd like to automate this process.
我可以为每个段落使用class指令,但我想自动化这个过程。
For example, I want this restructured text:
例如,我想要这个重组的文本:
hello
=====
A paragraph of text.
To be converted to this html.
要转换为这个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.
我想插入类的原因是因为我正在使用连字符库,它通过将连字符添加到带有“连字符”类的所有标记来工作。我可以将连字符类添加到容器标记中,但随后所有子项都将继承连字符类。我可以使用javascript动态添加类,但我认为可能有一种简单的方法来使用restructuredText。
Thanks for the help,
谢谢您的帮助,
Joe
2 个解决方案
#1
4
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技术来选择输出中的所有段落:
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.js,我建议使用其selectorfunction设置以不同方式选择元素:
Hyphenator.config({
selectorfunction: function () {
/* Use jQuery to find all the REST p tags. */
return $('div.resttext p');
}
});
Hyphenator.run();
#2
5
Subclass the built-in html4css1
writer, using this as a reference..
子类内置的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')
Then use it like this:
然后像这样使用它:
from docutils.core import publish_string
print publish_string("*This* is the input text", writer=MyHTMLWriter())
#1
4
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技术来选择输出中的所有段落:
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.js,我建议使用其selectorfunction设置以不同方式选择元素:
Hyphenator.config({
selectorfunction: function () {
/* Use jQuery to find all the REST p tags. */
return $('div.resttext p');
}
});
Hyphenator.run();
#2
5
Subclass the built-in html4css1
writer, using this as a reference..
子类内置的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')
Then use it like this:
然后像这样使用它:
from docutils.core import publish_string
print publish_string("*This* is the input text", writer=MyHTMLWriter())