2015-10-05 70 views
0

我嘗試在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,這是不是最佳的,因爲如果他們的內容也子指令,他們不會被對待。 - 如果我必須選擇sphinx的文檔,那麼很難理解,因此在這種情況下可以調用一些節點。通用實例,但我不知道具體是什麼。

感謝

+0

歡迎來到StackOverflow。請參閱stackoverflow.com/help/how-to-ask和stackoverflow.com/help/mcve。我們可以幫助您編寫指令,但我們通常不會從頭開始編寫代碼。 – Prune

回答