0

我尝试在 Sphinx 中进行扩展,其想法是扩展检测部分的标题,然后将它们转换为折叠部分,即 sphinx 呈现带有前缀按钮的标题,该按钮有助于显示或不显示的内容部分。

test_fold
=============

.. fold:: 

    * First Section 
        Hae duae provinciae bello quondam piratico catervis mixtae praedonum 
        a Servilio pro consule missae sub iugum factae sunt vectigales. 
        et hae quidem regiones velut in prominenti terrarum lingua positae 
        ob orbe eoo monte Amano disparantur.

    * Second Section
        Itaque tum Scaevola cum in eam ipsam mentionem incidisset, exposuit 
        nobis sermonem Laeli de amicitia habitum ab illo secum et cum altero 
        genero, C. Fannio Marci filio, paucis diebus post mortem Africani. 
        Eius disputationis sententias memoriae mandavi, quas hoc libro 
        exposui arbitratu meo; quasi enim ipsos induxi loquentes, ne 
        'inquam' et 'inquit' saepius interponeretur, atque ut tamquam 
        a praesentibus coram haberi sermo videretur.

    * Third Section
        Et interdum acciderat, ut siquid in penetrali secreto nullo citerioris 
        vitae ministro praesente paterfamilias uxori susurrasset in aurem, 
        velut Amphiarao referente aut Marcio, quondam vatibus inclitis, 
        postridie disceret imperator. ideoque etiam parietes arcanorum 
        soli conscii timebantur.

我开始写这样的扩展:

from docutils.nodes import raw
from docutils.parsers.rst import Directive

FOLD = "*"
foldSize = len(FOLD)

def removeSpace(_str):
    if _str[0]==" ":
        return removeSpace(_str[1:])
    else:
        return _str

class FoldDirective(Directive):

   # this enables content in the directive
   has_content = True

   def run(self):

    titles = []
    mains = []
    newMain = ""

    #loop
    for indexInt in xrange(len(self.content)):

        #get
        string = self.content[indexInt]
        if indexInt>0:
            previousString = self.content[indexInt-1]
        else:
            previousString = ""

        #Check
        if len(string)>0:
            if string[0] == FOLD:
                titles.append(string[foldSize:])
                if indexInt>0:
                    mains.append(newMain)
                newMain = ""
            elif previousString[0] == FOLD:
                newMain = string
            else:
                newMain += string

    #append the last
    mains.append(newMain)

    #return
    return [raw('',"<table><tr>",format='html')]+map(
        lambda title,main:
        raw('',"<td><button/></td><td><div>"+str(title)+"</div><td>     <tr><td></td><td><div>"+str(main)+"</div></td></tr>",format='html'),
        titles,mains
    )+[raw('',"</tr></table>",format='html')]

def setup(app):
    app.add_directive('fold', FoldDirective)

但这里的问题是: - 我不知道将按钮与 onClick 函数绑定,这会使点击时的主要消失。- 主要内容只是放入原始 html,这不是最佳的,因为如果它们还包含子指令,它们将不会被处理。- 如果我必须选择,那么狮身人面像的文档真的很难理解,因此在这种情况下会调用一些节点。一般实例,但我不知道具体是什么。

谢谢

4

2 回答 2

1

您可以了解它在云主题中的表现如何:http: //pythonhosted.org/cloud_sptheme/cloud_theme_test.html#toggleable-section

于 2015-10-06T07:10:09.817 回答
0

我建议阅读Sphinx 扩展的文档以及查看这个扩展,它使用 Disqus 注释的指令更改 HTML 标记。

于 2015-10-05T17:23:10.297 回答