mercurial/minirst.py
changeset 39333 9fe97e676250
parent 39332 200ad3e85a97
child 39334 92e9aa38a578
equal deleted inserted replaced
39332:200ad3e85a97 39333:9fe97e676250
   678         return text, pruned
   678         return text, pruned
   679 
   679 
   680 def filtersections(blocks, section):
   680 def filtersections(blocks, section):
   681     """Select parsed blocks under the specified section"""
   681     """Select parsed blocks under the specified section"""
   682     parents = []
   682     parents = []
   683     if True:
   683     sections = getsections(blocks)
   684         sections = getsections(blocks)
   684     blocks = []
   685         blocks = []
   685     i = 0
   686         i = 0
   686     lastparents = []
   687         lastparents = []
   687     synthetic = []
   688         synthetic = []
   688     collapse = True
   689         collapse = True
   689     while i < len(sections):
   690         while i < len(sections):
   690         name, nest, b = sections[i]
   691             name, nest, b = sections[i]
   691         del parents[nest:]
   692             del parents[nest:]
   692         parents.append(i)
   693             parents.append(i)
   693         if name == section:
   694             if name == section:
   694             if lastparents != parents:
   695                 if lastparents != parents:
   695                 llen = len(lastparents)
   696                     llen = len(lastparents)
   696                 plen = len(parents)
   697                     plen = len(parents)
   697                 if llen and llen != plen:
   698                     if llen and llen != plen:
   698                     collapse = False
   699                         collapse = False
   699                 s = []
   700                     s = []
   700                 for j in pycompat.xrange(3, plen - 1):
   701                     for j in pycompat.xrange(3, plen - 1):
   701                     parent = parents[j]
   702                         parent = parents[j]
   702                     if (j >= llen or
   703                         if (j >= llen or
   703                         lastparents[j] != parent):
   704                             lastparents[j] != parent):
   704                         s.append(len(blocks))
   705                             s.append(len(blocks))
   705                         sec = sections[parent][2]
   706                             sec = sections[parent][2]
   706                         blocks.append(sec[0])
   707                             blocks.append(sec[0])
   707                         blocks.append(sec[-1])
   708                             blocks.append(sec[-1])
   708                 if s:
   709                     if s:
   709                     synthetic.append(s)
   710                         synthetic.append(s)
   710 
   711 
   711             lastparents = parents[:]
   712                 lastparents = parents[:]
   712             blocks.extend(b)
   713                 blocks.extend(b)
   713 
   714 
   714             ## Also show all subnested sections
   715                 ## Also show all subnested sections
   715             while i + 1 < len(sections) and sections[i + 1][1] > nest:
   716                 while i + 1 < len(sections) and sections[i + 1][1] > nest:
   716                 i += 1
   717                     i += 1
   717                 blocks.extend(sections[i][2])
   718                     blocks.extend(sections[i][2])
   718         i += 1
   719             i += 1
   719     if collapse:
   720         if collapse:
   720         synthetic.reverse()
   721             synthetic.reverse()
   721         for s in synthetic:
   722             for s in synthetic:
   722             path = [blocks[syn]['lines'][0] for syn in s]
   723                 path = [blocks[syn]['lines'][0] for syn in s]
   723             real = s[-1] + 2
   724                 real = s[-1] + 2
   724             realline = blocks[real]['lines']
   725                 realline = blocks[real]['lines']
   725             realline[0] = ('"%s"' %
   726                 realline[0] = ('"%s"' %
   726                            '.'.join(path + [realline[0]]).replace('"', ''))
   727                                '.'.join(path + [realline[0]]).replace('"', ''))
   727             del blocks[s[0]:real]
   728                 del blocks[s[0]:real]
       
   729 
   728 
   730     return blocks
   729     return blocks
   731 
   730 
   732 def getsections(blocks):
   731 def getsections(blocks):
   733     '''return a list of (section name, nesting level, blocks) tuples'''
   732     '''return a list of (section name, nesting level, blocks) tuples'''