Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 28670d1

Browse files
committed
Issue #25198: When using the Idle dov TOC menu, put the section title at the
top of the window, unless it is too near the bottom to do do.
1 parent c402d8d commit 28670d1

1 file changed

Lines changed: 14 additions & 22 deletions

File tree

Lib/idlelib/help.py

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,11 @@ def __init__(self, text):
5555
self.hdrlink = False # used so we don't show header links
5656
self.level = 0 # indentation level
5757
self.pre = False # displaying preformatted text
58-
self.hprefix = '' # strip e.g. '25.5' from headings
58+
self.hprefix = '' # prefix such as '25.5' to strip from headings
5959
self.nested_dl = False # if we're in a nested <dl>
6060
self.simplelist = False # simple list (no double spacing)
61-
self.tocid = 1 # id for table of contents entries
62-
self.contents = [] # map toc ids to section titles
63-
self.data = '' # to record data within header tags for toc
61+
self.toc = [] # pair headers with text indexes for toc
62+
self.header = '' # text within header tags for toc
6463

6564
def indent(self, amt=1):
6665
self.level += amt
@@ -111,14 +110,10 @@ def handle_starttag(self, tag, attrs):
111110
elif tag == 'a' and class_ == 'headerlink':
112111
self.hdrlink = True
113112
elif tag == 'h1':
114-
self.text.mark_set('toc'+str(self.tocid),
115-
self.text.index('end-1line'))
116113
self.tags = tag
117114
elif tag in ['h2', 'h3']:
118115
if self.show:
119-
self.data = ''
120-
self.text.mark_set('toc'+str(self.tocid),
121-
self.text.index('end-1line'))
116+
self.header = ''
122117
self.text.insert('end', '\n\n')
123118
self.tags = tag
124119
if self.show:
@@ -128,10 +123,8 @@ def handle_endtag(self, tag):
128123
"Handle endtags in help.html."
129124
if tag in ['h1', 'h2', 'h3']:
130125
self.indent(0) # clear tag, reset indent
131-
if self.show and tag in ['h1', 'h2', 'h3']:
132-
title = self.data
133-
self.contents.append(('toc'+str(self.tocid), title))
134-
self.tocid += 1
126+
if self.show:
127+
self.toc.append((self.header, self.text.index('insert')))
135128
elif tag in ['span', 'em']:
136129
self.chartags = ''
137130
elif tag == 'a':
@@ -151,7 +144,7 @@ def handle_data(self, data):
151144
if self.tags in ['h1', 'h2', 'h3'] and self.hprefix != '':
152145
if d[0:len(self.hprefix)] == self.hprefix:
153146
d = d[len(self.hprefix):].strip()
154-
self.data += d
147+
self.header += d
155148
self.text.insert('end', d, (self.tags, self.chartags))
156149

157150

@@ -205,19 +198,18 @@ def __init__(self, parent, filename):
205198
self['background'] = text['background']
206199
scroll = Scrollbar(self, command=text.yview)
207200
text['yscrollcommand'] = scroll.set
201+
self.rowconfigure(0, weight=1)
202+
self.columnconfigure(1, weight=1) # text
203+
self.toc_menu(text).grid(column=0, row=0, sticky='nw')
208204
text.grid(column=1, row=0, sticky='nsew')
209205
scroll.grid(column=2, row=0, sticky='ns')
210-
self.grid_columnconfigure(1, weight=1)
211-
self.grid_rowconfigure(0, weight=1)
212-
toc = self.contents_widget(text)
213-
toc.grid(column=0, row=0, sticky='nw')
214206

215-
def contents_widget(self, text):
216-
"Create table of contents."
207+
def toc_menu(self, text):
208+
"Create table of contents as drop-down menu."
217209
toc = Menubutton(self, text='TOC')
218210
drop = Menu(toc, tearoff=False)
219-
for tag, lbl in text.parser.contents:
220-
drop.add_command(label=lbl, command=lambda mark=tag:text.see(mark))
211+
for lbl, dex in text.parser.toc:
212+
drop.add_command(label=lbl, command=lambda dex=dex:text.yview(dex))
221213
toc['menu'] = drop
222214
return toc
223215

0 commit comments

Comments
 (0)