blob: e19d3615016f2f1d9c7b734c549caf896f6aaee4 [file] [log] [blame]
Terry Jan Reedycba1a1a2015-09-21 22:36:42 -04001""" help.py: Implement the Idle help menu.
2Contents are subject to revision at any time, without notice.
Terry Jan Reedy5d46ab12015-09-20 19:57:13 -04003
Terry Jan Reedy5d46ab12015-09-20 19:57:13 -04004
penguindustin96466302019-05-06 14:57:17 -04005Help => About IDLE: display About Idle dialog
Terry Jan Reedy5d46ab12015-09-20 19:57:13 -04006
Terry Jan Reedy6fa5bdc2016-05-28 13:22:31 -04007<to be moved here from help_about.py>
Terry Jan Reedy5d46ab12015-09-20 19:57:13 -04008
Terry Jan Reedy5d46ab12015-09-20 19:57:13 -04009
Terry Jan Reedycba1a1a2015-09-21 22:36:42 -040010Help => IDLE Help: Display help.html with proper formatting.
11Doc/library/idle.rst (Sphinx)=> Doc/build/html/library/idle.html
12(help.copy_strip)=> Lib/idlelib/help.html
Terry Jan Reedy5d46ab12015-09-20 19:57:13 -040013
Martin Panter96a4f072016-02-10 01:17:51 +000014HelpParser - Parse help.html and render to tk Text.
Terry Jan Reedy5d46ab12015-09-20 19:57:13 -040015
Terry Jan Reedycba1a1a2015-09-21 22:36:42 -040016HelpText - Display formatted help.html.
17
18HelpFrame - Contain text, scrollbar, and table-of-contents.
Terry Jan Reedy5d46ab12015-09-20 19:57:13 -040019(This will be needed for display in a future tabbed window.)
20
Terry Jan Reedycba1a1a2015-09-21 22:36:42 -040021HelpWindow - Display HelpFrame in a standalone window.
22
23copy_strip - Copy idle.html to help.html, rstripping each line.
Terry Jan Reedy5d46ab12015-09-20 19:57:13 -040024
25show_idlehelp - Create HelpWindow. Called in EditorWindow.help_dialog.
26"""
27from html.parser import HTMLParser
Serhiy Storchakaccd047e2016-04-25 00:12:32 +030028from os.path import abspath, dirname, isfile, join
Terry Jan Reedy39e9af62016-08-25 20:04:14 -040029from platform import python_version
Terry Jan Reedybfbaa6b2016-08-31 00:50:55 -040030
Terry Jan Reedy01e35752016-06-10 18:19:21 -040031from tkinter import Toplevel, Frame, Text, Menu
32from tkinter.ttk import Menubutton, Scrollbar
Terry Jan Reedy5d46ab12015-09-20 19:57:13 -040033from tkinter import font as tkfont
Terry Jan Reedybfbaa6b2016-08-31 00:50:55 -040034
Terry Jan Reedy6fa5bdc2016-05-28 13:22:31 -040035from idlelib.config import idleConf
Terry Jan Reedy5d46ab12015-09-20 19:57:13 -040036
Terry Jan Reedy5d46ab12015-09-20 19:57:13 -040037## About IDLE ##
38
39
40## IDLE Help ##
41
42class HelpParser(HTMLParser):
Terry Jan Reedycba1a1a2015-09-21 22:36:42 -040043 """Render help.html into a text widget.
Terry Jan Reedy5d46ab12015-09-20 19:57:13 -040044
45 The overridden handle_xyz methods handle a subset of html tags.
46 The supplied text should have the needed tag configurations.
47 The behavior for unsupported tags, such as table, is undefined.
Terry Jan Reedy7811a9c2016-03-01 01:13:07 -050048 If the tags generated by Sphinx change, this class, especially
49 the handle_starttag and handle_endtags methods, might have to also.
Terry Jan Reedy5d46ab12015-09-20 19:57:13 -040050 """
51 def __init__(self, text):
52 HTMLParser.__init__(self, convert_charrefs=True)
53 self.text = text # text widget we're rendering into
Terry Jan Reedy974a2712015-09-24 17:32:01 -040054 self.tags = '' # current block level text tags to apply
55 self.chartags = '' # current character level text tags
Terry Jan Reedy5d46ab12015-09-20 19:57:13 -040056 self.show = False # used so we exclude page navigation
57 self.hdrlink = False # used so we don't show header links
58 self.level = 0 # indentation level
59 self.pre = False # displaying preformatted text
Terry Jan Reedy28670d12015-09-27 04:40:08 -040060 self.hprefix = '' # prefix such as '25.5' to strip from headings
Terry Jan Reedy5d46ab12015-09-20 19:57:13 -040061 self.nested_dl = False # if we're in a nested <dl>
62 self.simplelist = False # simple list (no double spacing)
Terry Jan Reedy28670d12015-09-27 04:40:08 -040063 self.toc = [] # pair headers with text indexes for toc
64 self.header = '' # text within header tags for toc
Terry Jan Reedy5d46ab12015-09-20 19:57:13 -040065
66 def indent(self, amt=1):
67 self.level += amt
68 self.tags = '' if self.level == 0 else 'l'+str(self.level)
69
70 def handle_starttag(self, tag, attrs):
Terry Jan Reedycba1a1a2015-09-21 22:36:42 -040071 "Handle starttags in help.html."
Terry Jan Reedy5d46ab12015-09-20 19:57:13 -040072 class_ = ''
73 for a, v in attrs:
74 if a == 'class':
75 class_ = v
76 s = ''
77 if tag == 'div' and class_ == 'section':
78 self.show = True # start of main content
79 elif tag == 'div' and class_ == 'sphinxsidebar':
80 self.show = False # end of main content
81 elif tag == 'p' and class_ != 'first':
82 s = '\n\n'
83 elif tag == 'span' and class_ == 'pre':
Terry Jan Reedy974a2712015-09-24 17:32:01 -040084 self.chartags = 'pre'
Terry Jan Reedy5d46ab12015-09-20 19:57:13 -040085 elif tag == 'span' and class_ == 'versionmodified':
Terry Jan Reedy974a2712015-09-24 17:32:01 -040086 self.chartags = 'em'
Terry Jan Reedy5d46ab12015-09-20 19:57:13 -040087 elif tag == 'em':
Terry Jan Reedy974a2712015-09-24 17:32:01 -040088 self.chartags = 'em'
Terry Jan Reedy5d46ab12015-09-20 19:57:13 -040089 elif tag in ['ul', 'ol']:
90 if class_.find('simple') != -1:
91 s = '\n'
92 self.simplelist = True
93 else:
94 self.simplelist = False
95 self.indent()
96 elif tag == 'dl':
97 if self.level > 0:
98 self.nested_dl = True
99 elif tag == 'li':
100 s = '\n* ' if self.simplelist else '\n\n* '
101 elif tag == 'dt':
102 s = '\n\n' if not self.nested_dl else '\n' # avoid extra line
103 self.nested_dl = False
104 elif tag == 'dd':
105 self.indent()
106 s = '\n'
107 elif tag == 'pre':
108 self.pre = True
109 if self.show:
110 self.text.insert('end', '\n\n')
111 self.tags = 'preblock'
112 elif tag == 'a' and class_ == 'headerlink':
113 self.hdrlink = True
114 elif tag == 'h1':
Terry Jan Reedy5d46ab12015-09-20 19:57:13 -0400115 self.tags = tag
116 elif tag in ['h2', 'h3']:
117 if self.show:
Terry Jan Reedy28670d12015-09-27 04:40:08 -0400118 self.header = ''
Terry Jan Reedy5d46ab12015-09-20 19:57:13 -0400119 self.text.insert('end', '\n\n')
120 self.tags = tag
121 if self.show:
Terry Jan Reedy974a2712015-09-24 17:32:01 -0400122 self.text.insert('end', s, (self.tags, self.chartags))
Terry Jan Reedy5d46ab12015-09-20 19:57:13 -0400123
124 def handle_endtag(self, tag):
Terry Jan Reedycba1a1a2015-09-21 22:36:42 -0400125 "Handle endtags in help.html."
Terry Jan Reedy974a2712015-09-24 17:32:01 -0400126 if tag in ['h1', 'h2', 'h3']:
Terry Jan Reedy5d46ab12015-09-20 19:57:13 -0400127 self.indent(0) # clear tag, reset indent
Terry Jan Reedy28670d12015-09-27 04:40:08 -0400128 if self.show:
Terry Jan Reedydb40cb52018-10-28 01:21:36 -0400129 indent = (' ' if tag == 'h3' else
130 ' ' if tag == 'h2' else
131 '')
132 self.toc.append((indent+self.header, self.text.index('insert')))
Terry Jan Reedy974a2712015-09-24 17:32:01 -0400133 elif tag in ['span', 'em']:
134 self.chartags = ''
Terry Jan Reedy5d46ab12015-09-20 19:57:13 -0400135 elif tag == 'a':
136 self.hdrlink = False
137 elif tag == 'pre':
138 self.pre = False
139 self.tags = ''
140 elif tag in ['ul', 'dd', 'ol']:
141 self.indent(amt=-1)
142
143 def handle_data(self, data):
Terry Jan Reedycba1a1a2015-09-21 22:36:42 -0400144 "Handle date segments in help.html."
Terry Jan Reedy5d46ab12015-09-20 19:57:13 -0400145 if self.show and not self.hdrlink:
146 d = data if self.pre else data.replace('\n', ' ')
147 if self.tags == 'h1':
Terry Jan Reedydb40cb52018-10-28 01:21:36 -0400148 try:
149 self.hprefix = d[0:d.index(' ')]
150 except ValueError:
151 self.hprefix = ''
152 if self.tags in ['h1', 'h2', 'h3']:
153 if (self.hprefix != '' and
154 d[0:len(self.hprefix)] == self.hprefix):
155 d = d[len(self.hprefix):]
156 self.header += d.strip()
Terry Jan Reedy974a2712015-09-24 17:32:01 -0400157 self.text.insert('end', d, (self.tags, self.chartags))
Terry Jan Reedy5d46ab12015-09-20 19:57:13 -0400158
159
160class HelpText(Text):
Terry Jan Reedycba1a1a2015-09-21 22:36:42 -0400161 "Display help.html."
Terry Jan Reedy5d46ab12015-09-20 19:57:13 -0400162 def __init__(self, parent, filename):
163 "Configure tags and feed file to parser."
Terry Jan Reedy52736dd2015-09-25 00:49:18 -0400164 uwide = idleConf.GetOption('main', 'EditorWindow', 'width', type='int')
165 uhigh = idleConf.GetOption('main', 'EditorWindow', 'height', type='int')
166 uhigh = 3 * uhigh // 4 # lines average 4/3 of editor line height
Terry Jan Reedy5d46ab12015-09-20 19:57:13 -0400167 Text.__init__(self, parent, wrap='word', highlightthickness=0,
Terry Jan Reedy52736dd2015-09-25 00:49:18 -0400168 padx=5, borderwidth=0, width=uwide, height=uhigh)
Terry Jan Reedy5d46ab12015-09-20 19:57:13 -0400169
170 normalfont = self.findfont(['TkDefaultFont', 'arial', 'helvetica'])
171 fixedfont = self.findfont(['TkFixedFont', 'monaco', 'courier'])
172 self['font'] = (normalfont, 12)
173 self.tag_configure('em', font=(normalfont, 12, 'italic'))
174 self.tag_configure('h1', font=(normalfont, 20, 'bold'))
175 self.tag_configure('h2', font=(normalfont, 18, 'bold'))
176 self.tag_configure('h3', font=(normalfont, 15, 'bold'))
Terry Jan Reedy974a2712015-09-24 17:32:01 -0400177 self.tag_configure('pre', font=(fixedfont, 12), background='#f6f6ff')
Terry Jan Reedy5d46ab12015-09-20 19:57:13 -0400178 self.tag_configure('preblock', font=(fixedfont, 10), lmargin1=25,
179 borderwidth=1, relief='solid', background='#eeffcc')
180 self.tag_configure('l1', lmargin1=25, lmargin2=25)
181 self.tag_configure('l2', lmargin1=50, lmargin2=50)
182 self.tag_configure('l3', lmargin1=75, lmargin2=75)
183 self.tag_configure('l4', lmargin1=100, lmargin2=100)
184
185 self.parser = HelpParser(self)
186 with open(filename, encoding='utf-8') as f:
187 contents = f.read()
188 self.parser.feed(contents)
189 self['state'] = 'disabled'
190
191 def findfont(self, names):
192 "Return name of first font family derived from names."
193 for name in names:
194 if name.lower() in (x.lower() for x in tkfont.names(root=self)):
195 font = tkfont.Font(name=name, exists=True, root=self)
196 return font.actual()['family']
197 elif name.lower() in (x.lower()
198 for x in tkfont.families(root=self)):
199 return name
200
201
202class HelpFrame(Frame):
Terry Jan Reedycba1a1a2015-09-21 22:36:42 -0400203 "Display html text, scrollbar, and toc."
Terry Jan Reedy5d46ab12015-09-20 19:57:13 -0400204 def __init__(self, parent, filename):
205 Frame.__init__(self, parent)
Terry Jan Reedy01e35752016-06-10 18:19:21 -0400206 # keep references to widgets for test access.
207 self.text = text = HelpText(self, filename)
Terry Jan Reedy5d46ab12015-09-20 19:57:13 -0400208 self['background'] = text['background']
Terry Jan Reedy01e35752016-06-10 18:19:21 -0400209 self.toc = toc = self.toc_menu(text)
210 self.scroll = scroll = Scrollbar(self, command=text.yview)
Terry Jan Reedy5d46ab12015-09-20 19:57:13 -0400211 text['yscrollcommand'] = scroll.set
Terry Jan Reedy01e35752016-06-10 18:19:21 -0400212
Terry Jan Reedy28670d12015-09-27 04:40:08 -0400213 self.rowconfigure(0, weight=1)
214 self.columnconfigure(1, weight=1) # text
Terry Jan Reedy01e35752016-06-10 18:19:21 -0400215 toc.grid(row=0, column=0, sticky='nw')
216 text.grid(row=0, column=1, sticky='nsew')
217 scroll.grid(row=0, column=2, sticky='ns')
Terry Jan Reedy5d46ab12015-09-20 19:57:13 -0400218
Terry Jan Reedy28670d12015-09-27 04:40:08 -0400219 def toc_menu(self, text):
220 "Create table of contents as drop-down menu."
Terry Jan Reedy5d46ab12015-09-20 19:57:13 -0400221 toc = Menubutton(self, text='TOC')
222 drop = Menu(toc, tearoff=False)
Terry Jan Reedy28670d12015-09-27 04:40:08 -0400223 for lbl, dex in text.parser.toc:
224 drop.add_command(label=lbl, command=lambda dex=dex:text.yview(dex))
Terry Jan Reedy5d46ab12015-09-20 19:57:13 -0400225 toc['menu'] = drop
226 return toc
227
228
229class HelpWindow(Toplevel):
Terry Jan Reedycba1a1a2015-09-21 22:36:42 -0400230 "Display frame with rendered html."
Terry Jan Reedy5d46ab12015-09-20 19:57:13 -0400231 def __init__(self, parent, filename, title):
232 Toplevel.__init__(self, parent)
233 self.wm_title(title)
234 self.protocol("WM_DELETE_WINDOW", self.destroy)
235 HelpFrame(self, filename).grid(column=0, row=0, sticky='nsew')
236 self.grid_columnconfigure(0, weight=1)
237 self.grid_rowconfigure(0, weight=1)
238
239
Terry Jan Reedycba1a1a2015-09-21 22:36:42 -0400240def copy_strip():
Terry Jan Reedy7811a9c2016-03-01 01:13:07 -0500241 """Copy idle.html to idlelib/help.html, stripping trailing whitespace.
Terry Jan Reedy5f582bd2016-03-01 01:18:47 -0500242
Terry Jan Reedy2b555fc2018-10-28 01:29:00 -0400243 Files with trailing whitespace cannot be pushed to the git cpython
Terry Jan Reedy7811a9c2016-03-01 01:13:07 -0500244 repository. For 3.x (on Windows), help.html is generated, after
Terry Jan Reedy2b555fc2018-10-28 01:29:00 -0400245 editing idle.rst on the master branch, with
Terry Jan Reedy7811a9c2016-03-01 01:13:07 -0500246 sphinx-build -bhtml . build/html
247 python_d.exe -c "from idlelib.help import copy_strip; copy_strip()"
Terry Jan Reedy2b555fc2018-10-28 01:29:00 -0400248 Check build/html/library/idle.html, the help.html diff, and the text
249 displayed by Help => IDLE Help. Add a blurb and create a PR.
Terry Jan Reedy5f582bd2016-03-01 01:18:47 -0500250
Terry Jan Reedy2b555fc2018-10-28 01:29:00 -0400251 It can be worthwhile to occasionally generate help.html without
252 touching idle.rst. Changes to the master version and to the doc
253 build system may result in changes that should not changed
254 the displayed text, but might break HelpParser.
Terry Jan Reedy5f582bd2016-03-01 01:18:47 -0500255
Terry Jan Reedy2b555fc2018-10-28 01:29:00 -0400256 As long as master and maintenance versions of idle.rst remain the
257 same, help.html can be backported. The internal Python version
258 number is not displayed. If maintenance idle.rst diverges from
259 the master version, then instead of backporting help.html from
Terry Jan Reedy8e3a7382019-07-21 15:24:45 -0400260 master, repeat the procedure above to generate a maintenance
Terry Jan Reedy2b555fc2018-10-28 01:29:00 -0400261 version.
Terry Jan Reedy7811a9c2016-03-01 01:13:07 -0500262 """
Terry Jan Reedycba1a1a2015-09-21 22:36:42 -0400263 src = join(abspath(dirname(dirname(dirname(__file__)))),
Terry Jan Reedy2b555fc2018-10-28 01:29:00 -0400264 'Doc', 'build', 'html', 'library', 'idle.html')
Terry Jan Reedycba1a1a2015-09-21 22:36:42 -0400265 dst = join(abspath(dirname(__file__)), 'help.html')
266 with open(src, 'rb') as inn,\
267 open(dst, 'wb') as out:
268 for line in inn:
Terry Jan Reedy6f5cdfe2015-09-23 03:45:13 -0400269 out.write(line.rstrip() + b'\n')
terryjreedy188aedf2017-06-13 21:32:16 -0400270 print(f'{src} copied to {dst}')
Terry Jan Reedycba1a1a2015-09-21 22:36:42 -0400271
Terry Jan Reedy5d46ab12015-09-20 19:57:13 -0400272def show_idlehelp(parent):
Terry Jan Reedycba1a1a2015-09-21 22:36:42 -0400273 "Create HelpWindow; called from Idle Help event handler."
274 filename = join(abspath(dirname(__file__)), 'help.html')
Terry Jan Reedy5d46ab12015-09-20 19:57:13 -0400275 if not isfile(filename):
Terry Jan Reedycba1a1a2015-09-21 22:36:42 -0400276 # try copy_strip, present message
Terry Jan Reedy364d6e12015-09-21 22:42:32 -0400277 return
Terry Jan Reedy39e9af62016-08-25 20:04:14 -0400278 HelpWindow(parent, filename, 'IDLE Help (%s)' % python_version())
Terry Jan Reedy5d46ab12015-09-20 19:57:13 -0400279
280if __name__ == '__main__':
Terry Jan Reedyee5ef302018-06-15 18:20:55 -0400281 from unittest import main
282 main('idlelib.idle_test.test_help', verbosity=2, exit=False)
283
Terry Jan Reedy5d46ab12015-09-20 19:57:13 -0400284 from idlelib.idle_test.htest import run
285 run(show_idlehelp)