Joe*_*Joe 6 python django restructuredtext
我正在使用Django的标记包将restructuredText转换为html.有没有办法自定义HTML编写器以向每个<p>标记添加类属性?
我可以为每个段落使用class指令,但我想自动执行此过程.
例如,我想要这个重组的文本:
hello
=====
A paragraph of text.
Run Code Online (Sandbox Code Playgroud)
要转换为这个HTML.
<h1>hello</h1>
<p class="specialClass">A paragraph of text.</p>
Run Code Online (Sandbox Code Playgroud)
我想插入类的原因是因为我正在使用连字符库,它通过将连字符添加到带有"连字符"类的所有标记来工作.我可以将连字符类添加到容器标记中,但随后所有子项都将继承连字符类.我可以使用javascript动态添加类,但我认为可能有一种简单的方法来使用restructuredText.
谢谢您的帮助,
乔
子类内置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')
Run Code Online (Sandbox Code Playgroud)
然后像这样使用它:
from docutils.core import publish_string
print publish_string("*This* is the input text", writer=MyHTMLWriter())
Run Code Online (Sandbox Code Playgroud)
您没有说明为什么要向每个段落添加一个类,但采用不同的方法可能会更容易。例如,如果您尝试设置段落样式,则可以使用不同的 CSS 技术来选择输出中的所有段落:
CSS:
div.resttext p {
/* all the styling you want... */
}
Run Code Online (Sandbox Code Playgroud)
HTML:
<div class='resttext'>
<p>Blah</p>
<p>Bloo</p>
</div>
Run Code Online (Sandbox Code Playgroud)
更新:由于您尝试使用 hyphenator.js,我建议使用其selectorfunction设置以不同方式选择元素:
Hyphenator.config({
selectorfunction: function () {
/* Use jQuery to find all the REST p tags. */
return $('div.resttext p');
}
});
Hyphenator.run();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1093 次 |
| 最近记录: |