import re, collections 

#



class Node:
  def  __init__(self, beg, end) :
     self.beg=beg
     self.end=end
     self.nodes=collections.OrderedDict() 

        
def addSection(text, title, newsection) :
# language, section name hard-coded to Navajo / STEM

 m=re.compile( "(=*) *([^=\n]*) *(=*)")
    
 language="" 
 section=""
 lastsection=None
 lastlanguage=None
 tree=collections.OrderedDict() 


 for p in re.finditer(m, text):

   balise=p.group(1)
   if balise == "==" :
        
     if lastsection:
        lastsection.end=p.span()[0]-1
     if lastlanguage :
        lastlanguage.end=p.span()[0]-1
         

          
     
     language=p.group(2).strip()
     tree[language] =Node(p.span()[0], len(text )) 
     lastlanguage=tree[language] 
     section=""
     lastsection =None
    
   elif balise=="===":
     if lastsection:
       lastsection.end =p.span()[0]-1
    
     section=p.group(2).strip()
     tree[language].nodes[section] =Node(p.span()[0], len(text )) 

     lastsection=tree[language].nodes[section]

 #print (tree)
 #for lang in tree:
 #  print ( lang, tree[lang].beg, tree[lang].end)
 #  for section in tree[lang].nodes:
 #    print ("  ", section, tree[lang].nodes[section].beg, tree[lang].nodes[section]. end) 

 beg=0
 end=0
 needtitle=False
 if "Navajo" in tree:
  lang=tree["Navajo"]
  if "Stem" in lang.nodes:
    beg=lang.nodes["Stem"].beg
    end=lang.nodes["Stem"].end
    print("found Navajo stems ") 
  else:
    for sect in lang.nodes:
      if sect < "Stem" :
        beg=end=lang.nodes[sect].end
        print("found Navajo", sect)
 else:
  needtitle=True
  for lang in tree:
     if lang < "Navajo" :
        beg=end=tree[lang] .end
        print("found " , lang) 

#i f be==-1:


    
 #print("@@",beg, end) 
 if needtitle :
    newsection=title+newsection
 new =text[0:beg].strip()+"\n"+ newsection.strip()+"\n"+text[end:] 

 return new

text="==B==\n"
text+="===Section1===\nsone te t goes here\n"
text+="===Section2===\n another text\n"
#text+="==Navajo ==\n"
#text+="===Section1===\n"
#text+="===Section2===\n"
#text+="Something  \n"
#text+="===Noun==\n with a lot of things \n"
#text+="===Suffix==\n"
#text+="Something  \n"
#print (addSection(text,"==Navajo==\n","===Stem===\n")) 
found  B
==B==
===Section1===
sone te t goes here
===Section2===
 another text
==Navajo==
===Stem===

 
sit= pywikibot.Site('en','wiktionary')   
2 +4
  File "<ipython-input-5-ae4b33676789>", line 1
    2 +4
     ^
SyntaxError: invalid character in identifier
2 +4
  File "<ipython-input-6-24cecce99df6>", line 1
    2 +4
     ^
SyntaxError: invalid character in identifier
2+4
6
sit
 
page=pywikibot.Category(sit,'Category:Navajo irregular verbs')
page.exists()
True
from pywikibot import pagegenerators
gen = pagegenerators.CategorizedPageGenerator(page)
text=""
for page in gen:
    text+=page.text
text
'==Navajo==\n\n===Etymology===\n{{nv-prefixes|a|di-fire|yíní|3s|L}} {{nv-link-to-stem |-díín}} {{nv-link-to-root  |-DÍÍN|light|light shines}}\n\n===Verb===\n{{head|nv|verb}}\n\n# he/she/it is [[direct#Verb|directing]], [[cast#Verb|casting]] or [[reflect#Verb|reflect]]ing light\n\n====Usage notes====\nThis is a neuter verb that uses only the imperfective mode.\n\n====Conjugation====\n{{nv-paradigm|neuter imperfective||irregular}}\n{{nv-verbtable|IMPERFECTIVE\n|adííníshdíín|adííníildíín|daʼdííníildíín|adííníłdíín|adíínółdíín|daʼdíínółdíín|adiníłdíín|daʼdiníłdíín|adizhníłdíín|dazhdiʼníłdíín}}\n\n====Related terms====\n* {{l|nv|sháńdíín}}\n* {{l|nv|adinídíín}} \n\n[[Category:nv:Light]]==Navajo==\n===Etymology===\n{{nv-prefixes|á-comp|ni-them|l}} {{nv-link-to-stem|-tso}} {{nv-link-to-root|-TSOH|big|to be big}}\n\n===Verb===\n{{head|nv|verb}}\n\n# all [[completed]], [[finished]]\n# all of it\n# [[entirely]]\n\n====Conjugation====\n{{nv-paradigm|neuter imperfective|ni|irr}}\n[[Category:Navajo verbs : plural only]] \n{{nv-verbtable|PERFECTIVE\n|-|tʼáá áníiltso|? |-|tʼáá ánółtso|? |tʼáá ałtso|? |tʼáá ájíłtso|?}}\nall of us (3+), all of you (3+), all of them (3+), all of it, everyone, everybody.==Navajo==\n\n===Etymology===\n{{nv-prefixes |b|ho|ní-comp|ni-term|3s|d}} {{nv-link-to-stem |-dząąh}} {{nv-link-to-root |-DZĄĄD|possible|to be possible}}.\n\nThe stem may actually be [[-ząąh]], potentially ultimately deriving from the root {{m|nv|-YĄĄD}} ("to be wise", see {{m|nv|honisą}}/{{m|nv|hóyą}}). Compare Chipewyan {{l|chp|hurélyą||good weather}} and Sarcee {{l|srs|-dhzoh|nanidhzoh|get well}}.\n\nOther difficulties are the origin of the high tone on {{m|nv|ho-}} and the pre-stem long \'\'-née-\'\' (possibly from {{m|nv|ní-}} + {{m|nv|ni-}} ?), only found in few other bases (see {{m|nv|ánéeltʼeʼ}}).\n\n===Verb===\n{{head|nv|verb}}\n\n# it becomes [[possible]]\n# it becomes [[practical]], [[feasible]]\n\n====Usage notes====\nThis verb is semantically limited to expression in the third person.\n\n====Conjugation====\n[[Category:Navajo irregular verbs]] \n{| style="margin-bottom:.1em; margin-right:1em;width:24.5em; border:1px solid #AAAAAA; border-collapse:collapse;" cellpadding="4" rules="all"\n!bgcolor=#EFEAAA align=left style="font-size:90%;"|IMPERFECTIVE\n!bgcolor=#EFEFFF align=center WIDTH="166" style="font-size:90%;"|singular\n|-\n!bgcolor=#EFEFFF align=left style="font-size:90%;"|3rd person\n|align=center|bihónéedząąh\n|}\n{| style="margin-bottom:.1em; margin-right:1em;width:24.5em; border:1px solid #AAAAAA; border-collapse:collapse;" cellpadding="4" rules="all"\n!bgcolor=#EFEAAA align=left style="font-size:90%;"|NEUTER \n!bgcolor=#EFEFFF align=center WIDTH="166" style="font-size:90%;"|singular\n|-\n!bgcolor=#EFEFFF align=left style="font-size:90%;"|3rd person\n|align=center|[[bihónéedzą́]]\n|}\n{| style="margin-bottom:1em; margin-right:1em;width:24.5em; border:1px solid #AAAAAA; border-collapse:collapse;" cellpadding="4" rules="all"\n!bgcolor=#EFEAAA align=left style="font-size:90%;"|PERFECTIVE\n!bgcolor=#EFEFFF align=center WIDTH="166" style="font-size:90%;"|singular\n|-\n!bgcolor=#EFEFFF align=left style="font-size:90%;"|3rd person\n|align=center|[[bihónéesdząąd]]\n|}==Navajo==\n===Etymology===\n{{nv-prefixes|chʼí|ni|3s}} + {{nv-prefixes |-}} {{nv-link-to-stem|-ááh}} {{nv-link-to-root |-YÁ|go|to go, to walk  —1 actor}}.\n\nPrefix {{m|nv|chʼí-}} becomes \'\'chʼé\'\'- when in pre-stem position. \n\n===Verb===\n{{head|nv|verb}}\n# he/she [[go]]es out horizontally (as through a door), [[exit]]s, [[leave]]s.\n\n====Usage notes====\nThis stem is used for going / walking individually. For movement in pairs, see {{m|nv|chʼéʼaash}}. For movement in groups of three or more, see {{m|nv|chʼékááh}}.\n\nPlural forms of this verb refer to several people going independently one from another: \n:*\'\'\'Chʼíniidzá\'\'\'  — \'\'each of us went out\'\'.\n:*Nihí łaʼ \'\'\'chʼínooyá\'\'\'  — \'\'one of you went out\'\'.\n:*\'\'\'Chʼídahaadzá\'\'\' (seriative aspect) — \'\'we went out one at a time, one after another\'\'.\n\n====Conjugation====\n{{nv-paradigm |momentaneous |ni/ni|irr}} \n{{nv-verbtable|IMPERFECTIVE\n|chʼíníshááh|chʼíniidááh|chʼídaniidááh|chʼínínááh|chʼínóhhááh|chʼídanohhááh|chʼéghááh|chʼídaaghááh|chʼíjíghááh|chʼídajighááh}}\n{{nv-verbtable|PERFECTIVE\n|chʼíníyá|chʼíniidzá|chʼídaniidzá|chʼííníyá|chʼínooyá|chʼídanooyá|chʼíníyá||chʼízhníyá|}}\n{{nv-verbtable|FUTURE\n|chʼídeeshááł|chʼídiidááł|chʼídadiidááł|chʼídíínááł|chʼídoodááł|chʼídadoodááł|chʼídoogááł|chʼídadoogááł|chʼízhdoogááł|chʼídazhdoogááł}}==Navajo==\n\n===Etymology===\n{{nv-prefixes|di-legs|3s|d}} {{nv-link-to-stem|-dzééh}} {{nv-link-to-root|-DZAʼ|stagger}}.\n\nThe stem and root may actually be [[:Category:Navajo roots sharing the stem -zééh |\'\'-zééh\'\']] / {{m|nv|-ZAʼ}}.\n\n===Verb===\n{{head|nv|verb}}\n\n# he/she is [[stagger]]ing somewhere, [[stumble#Verb|stumbl]]ing somewhere\n\n====Usage notes====\nThis verb calls for an adverb or postpositional phrase describing the destination of the action.\n\n====Conjugation====\n{{nv-paradigm|momentaneous|Ø/yi|irr}} (shift to si-perfective in the duoplural et plural forms). \n{{nv-verbtable|IMPERFECTIVE\n|disdzééh|diidzééh|dadiidzééh|dídzééh|dohdzééh|dadohdzééh|didzééh|dadidzééh|jididzééh|dazhdidzééh}}\n{{nv-verbtable|PERFECTIVE\n|deesdzaʼ|deedzaʼ|dadeedzaʼ|díínídzaʼ|disoohdzaʼ|dadisoohdzaʼ|doodzaʼ|dadeesdzaʼ|jidoodzaʼ|dazhdeesdzaʼ}}==Navajo==\n\n===Etymology===\n{{nv-prefixes|di-oral|ni-adj|si|3s|ł}} {{nv-link-to-stem|-yéél |neuter perfective}} {{nv-link-to-root |-YÉÉL |calm|to be calm}}. \n\nThe root onset, originally *\'\'gh\'\' (> \'\'y\'\'), devoices after \'\' ł\'\'-classifier: \'\'ł\'\' + \'\'ghéél\'\' > \'\'ł\'\' + \'\'héél\'\'. \n\nFor the high tone on the prefix complex, see also {{m|nv |díní-}}. \n[[Category:Navajo verbs with prefix díní-]] \n\n===Verb===\n{{head|nv|verb}}\n\n# it/he/she is [[quiet]], [[noiseless]], [[still]], [[calm]]\n\n====Usage notes====\nThis is a neuter verb that uses only the perfective mode.\n\n====Conjugation====\n{{nv-paradigm|neuter perfective|si|irr}} (see also [[díní-#Conjugation|\'\'díní-\'\']]). \n{{nv-verbtable|PERFECTIVE\n|díníshhéél|díníilyéél|dadíníilyéél|díníłhéél|dínóołhéél|dadínóołhéél|déesxéél|dadéesxéél|jidéesxéél|dazdéesxéél}}\n\n====Related terms====\n* {{l|nv|diiłheeł}}\n* {{l|nv|hodéezyéél}}==Navajo==\n===Etymology===\n{{nv-prefixes|á-do|ná-rev|3o|3s|d}} {{nv-link-to-stem|-lééh}} {{nv-link-to-root|-LAA|make}}, ultimately from the root {{m|nv|-YAA}}.\n[[Category:Navajo terms belonging to the root -YAA (make)]]\n\n===Verb===\n{{head|nv|verb}}\n\n# he/she is [[repair]]ing it, [[fix]]ing it\n\n====Conjugation====\n{{nv-paradigm|momentaneous|Ø/yi|irr}} \n{{nv-verbtable|IMPERFECTIVE\n|ánáshdlééh|ánéiilnééh|ánádeiilnééh|ánánídlééh|ánáhdlééh|ánádaahdlééh|ánéídlééh|ánádeidlééh|áńjídlééh|ánádajidlééh}}\n{{nv-verbtable|PERFECTIVE\n|ánáshdlaa|ánéiilyaa|ánádeiilyaa|ánéinidlaa|ánáoodlaa|ánádaahdlaa|ánáyiidlaa|ánádayiidlaa|áńjiidlaa|ánádajiidlaa}}\n{{nv-verbtable|FUTURE\n|áńdeeshdlííł|áńdiilnííł|áńdadiilnííł|áńdíídlííł|áńdoohdlííł|áńdadoohdlííł|ánéidoodlííł|áńdeidoodlííł|ánízhdoodlííł|áńdazhdoodlííł}}==Navajo==\n\n===Etymology===\n{{nv-prefixes|á-comp|ní-comp|si|L}} {{nv-link-to-stem|zólí}} {{nv-link-to-root|-ZÓLĺ|light|to be light}}.\n\n===Verb===\n{{head|nv|verb}}\n\n# it is [[light]] (weighing little)\n\n====Usage notes====\nThis is a neuter verb. As such, it has only the imperfective stem.\nThird person is irregular and is reminiscent of a si-perfective.\n\n====Conjugation====\n{{nv-paradigm|neuter imperfective||irr}}\n{{nv-verbtable|IMPERFECTIVE\n|áníszólí|ániilzólí|ádaniilzólí|áníníłzólí|ánółzólí|ádanołzólí|ászólí|ádaaszólí|ájíszólí|ádajízólí}}\n\n====Derived terms====\n* {{l|nv|béésh ászólí}} (aluminum)\n* {{l|nv|níłchʼi ászólí}} (helium){{also|ili|iḻi|-ili|ili-}}\n==Navajo==\n\n===Etymology===\n{{m|nv|ʼa-||pos=thematic}} + {{m|nv|-ni-||pos=adjectival}} + {{m|nv|-Ø-||pos=3rd person subject prefix}} + {{m|nv|-Ø-||pos=classifier}} + {{m|nv|-LĮ́Į́ʼ|-lį́|pos=neuter verb stem|to be}}, from the root {{m|nv|-LĮ́Į́ʼ}}.\n[[Category:Navajo terms belonging to the root -LĮ́Į́ʼ (be)]]\n\n===Verb===\n{{head|nv|verb}}\n# he/she/it has a value, is worth\n#: {{m|nv|bą́ą́h ílį́}} - it is worth, it costs\n\n====Conjugation====\n<u>Paradigm:</u> Neuter ni-imperfective, with some irregularities.\n{{nv-verbtable|IMPERFECTIVE\n|anishłį́|?|?|aníłį́?|?|?|íłį́|?|?|?}}\n{| style="margin-bottom:.1em; margin-right:1em;width:22.5em; border:1px solid #AAAAAA; border-collapse:collapse;" cellpadding="4" rules="all"\n!bgcolor=#EFEAAA align=left style="font-size:90%;"|PERFECTIVE\n!bgcolor=#EFEFFF align=center WIDTH="166" style="font-size:90%;"|singular\n|-\n!bgcolor=#EFEFFF align=left style="font-size:90%;"|3rd person\n|align=center|{{l|nv|azlį́į́ʼ}}\n|}\n{| style="margin-bottom:1em; margin-right:1em;width:22.5em; border:1px solid #AAAAAA; border-collapse:collapse;" cellpadding="4" rules="all"\n!bgcolor=#EFEAAA align=left style="font-size:90%;"|FUTURE\n!bgcolor=#EFEFFF align=center WIDTH="166" style="font-size:90%;"|singular\n|-\n!bgcolor=#EFEFFF align=left style="font-size:90%;"|3rd person\n|align=center|{{l|nv|adooleeł}}\n|}\n\n====Related terms====\n* {{l|nv|ílį́įgo}}\n* {{l|nv|ílį́įnii}}\n\n[[Category:Navajo verbs neuter ni-imperfective]] \n[[Category:Navajo irregular verbs]]'