Terry Jan Reedy | 13b21f8 | 2015-09-21 22:36:36 -0400 | [diff] [blame^] | 1 | """ help.py: Implement the Idle help menu. |
| 2 | Contents are subject to revision at any time, without notice. |
Terry Jan Reedy | a2f257b | 2015-09-20 19:56:54 -0400 | [diff] [blame] | 3 | |
Terry Jan Reedy | a2f257b | 2015-09-20 19:56:54 -0400 | [diff] [blame] | 4 | |
| 5 | Help => About IDLE: diplay About Idle dialog |
| 6 | |
| 7 | <to be moved here from aboutDialog.py> |
| 8 | |
Terry Jan Reedy | a2f257b | 2015-09-20 19:56:54 -0400 | [diff] [blame] | 9 | |
Terry Jan Reedy | 13b21f8 | 2015-09-21 22:36:36 -0400 | [diff] [blame^] | 10 | Help => IDLE Help: Display help.html with proper formatting. |
| 11 | Doc/library/idle.rst (Sphinx)=> Doc/build/html/library/idle.html |
| 12 | (help.copy_strip)=> Lib/idlelib/help.html |
Terry Jan Reedy | a2f257b | 2015-09-20 19:56:54 -0400 | [diff] [blame] | 13 | |
Terry Jan Reedy | 13b21f8 | 2015-09-21 22:36:36 -0400 | [diff] [blame^] | 14 | HelpParser - Parse help.html and and render to tk Text. |
Terry Jan Reedy | a2f257b | 2015-09-20 19:56:54 -0400 | [diff] [blame] | 15 | |
Terry Jan Reedy | 13b21f8 | 2015-09-21 22:36:36 -0400 | [diff] [blame^] | 16 | HelpText - Display formatted help.html. |
| 17 | |
| 18 | HelpFrame - Contain text, scrollbar, and table-of-contents. |
Terry Jan Reedy | a2f257b | 2015-09-20 19:56:54 -0400 | [diff] [blame] | 19 | (This will be needed for display in a future tabbed window.) |
| 20 | |
Terry Jan Reedy | 13b21f8 | 2015-09-21 22:36:36 -0400 | [diff] [blame^] | 21 | HelpWindow - Display HelpFrame in a standalone window. |
| 22 | |
| 23 | copy_strip - Copy idle.html to help.html, rstripping each line. |
Terry Jan Reedy | a2f257b | 2015-09-20 19:56:54 -0400 | [diff] [blame] | 24 | |
| 25 | show_idlehelp - Create HelpWindow. Called in EditorWindow.help_dialog. |
| 26 | """ |
| 27 | from HTMLParser import HTMLParser |
| 28 | from os.path import abspath, dirname, isdir, isfile, join |
| 29 | from Tkinter import Tk, Toplevel, Frame, Text, Scrollbar, Menu, Menubutton |
| 30 | import tkFont as tkfont |
| 31 | |
| 32 | use_ttk = False # until available to import |
| 33 | if use_ttk: |
| 34 | from tkinter.ttk import Menubutton |
| 35 | |
| 36 | ## About IDLE ## |
| 37 | |
| 38 | |
| 39 | ## IDLE Help ## |
| 40 | |
| 41 | class HelpParser(HTMLParser): |
Terry Jan Reedy | 13b21f8 | 2015-09-21 22:36:36 -0400 | [diff] [blame^] | 42 | """Render help.html into a text widget. |
Terry Jan Reedy | a2f257b | 2015-09-20 19:56:54 -0400 | [diff] [blame] | 43 | |
| 44 | The overridden handle_xyz methods handle a subset of html tags. |
| 45 | The supplied text should have the needed tag configurations. |
| 46 | The behavior for unsupported tags, such as table, is undefined. |
| 47 | """ |
| 48 | def __init__(self, text): |
| 49 | HTMLParser.__init__(self) |
| 50 | self.text = text # text widget we're rendering into |
| 51 | self.tags = '' # current text tags to apply |
| 52 | self.show = False # used so we exclude page navigation |
| 53 | self.hdrlink = False # used so we don't show header links |
| 54 | self.level = 0 # indentation level |
| 55 | self.pre = False # displaying preformatted text |
| 56 | self.hprefix = '' # strip e.g. '25.5' from headings |
| 57 | self.nested_dl = False # if we're in a nested <dl> |
| 58 | self.simplelist = False # simple list (no double spacing) |
| 59 | self.tocid = 1 # id for table of contents entries |
| 60 | self.contents = [] # map toc ids to section titles |
| 61 | self.data = '' # to record data within header tags for toc |
| 62 | |
| 63 | def indent(self, amt=1): |
| 64 | self.level += amt |
| 65 | self.tags = '' if self.level == 0 else 'l'+str(self.level) |
| 66 | |
| 67 | def handle_starttag(self, tag, attrs): |
Terry Jan Reedy | 13b21f8 | 2015-09-21 22:36:36 -0400 | [diff] [blame^] | 68 | "Handle starttags in help.html." |
Terry Jan Reedy | a2f257b | 2015-09-20 19:56:54 -0400 | [diff] [blame] | 69 | class_ = '' |
| 70 | for a, v in attrs: |
| 71 | if a == 'class': |
| 72 | class_ = v |
| 73 | s = '' |
| 74 | if tag == 'div' and class_ == 'section': |
| 75 | self.show = True # start of main content |
| 76 | elif tag == 'div' and class_ == 'sphinxsidebar': |
| 77 | self.show = False # end of main content |
| 78 | elif tag == 'p' and class_ != 'first': |
| 79 | s = '\n\n' |
| 80 | elif tag == 'span' and class_ == 'pre': |
| 81 | self.tags = 'pre' |
| 82 | elif tag == 'span' and class_ == 'versionmodified': |
| 83 | self.tags = 'em' |
| 84 | elif tag == 'em': |
| 85 | self.tags = 'em' |
| 86 | elif tag in ['ul', 'ol']: |
| 87 | if class_.find('simple') != -1: |
| 88 | s = '\n' |
| 89 | self.simplelist = True |
| 90 | else: |
| 91 | self.simplelist = False |
| 92 | self.indent() |
| 93 | elif tag == 'dl': |
| 94 | if self.level > 0: |
| 95 | self.nested_dl = True |
| 96 | elif tag == 'li': |
| 97 | s = '\n* ' if self.simplelist else '\n\n* ' |
| 98 | elif tag == 'dt': |
| 99 | s = '\n\n' if not self.nested_dl else '\n' # avoid extra line |
| 100 | self.nested_dl = False |
| 101 | elif tag == 'dd': |
| 102 | self.indent() |
| 103 | s = '\n' |
| 104 | elif tag == 'pre': |
| 105 | self.pre = True |
| 106 | if self.show: |
| 107 | self.text.insert('end', '\n\n') |
| 108 | self.tags = 'preblock' |
| 109 | elif tag == 'a' and class_ == 'headerlink': |
| 110 | self.hdrlink = True |
| 111 | elif tag == 'h1': |
| 112 | self.text.mark_set('toc'+str(self.tocid), |
| 113 | self.text.index('end-1line')) |
| 114 | self.tags = tag |
| 115 | elif tag in ['h2', 'h3']: |
| 116 | if self.show: |
| 117 | self.data = '' |
| 118 | self.text.mark_set('toc'+str(self.tocid), |
| 119 | self.text.index('end-1line')) |
| 120 | self.text.insert('end', '\n\n') |
| 121 | self.tags = tag |
| 122 | if self.show: |
| 123 | self.text.insert('end', s, self.tags) |
| 124 | |
| 125 | def handle_endtag(self, tag): |
Terry Jan Reedy | 13b21f8 | 2015-09-21 22:36:36 -0400 | [diff] [blame^] | 126 | "Handle endtags in help.html." |
Terry Jan Reedy | a2f257b | 2015-09-20 19:56:54 -0400 | [diff] [blame] | 127 | if tag in ['h1', 'h2', 'h3', 'span', 'em']: |
| 128 | self.indent(0) # clear tag, reset indent |
| 129 | if self.show and tag in ['h1', 'h2', 'h3']: |
| 130 | title = self.data |
| 131 | self.contents.append(('toc'+str(self.tocid), title)) |
| 132 | self.tocid += 1 |
| 133 | elif tag == 'a': |
| 134 | self.hdrlink = False |
| 135 | elif tag == 'pre': |
| 136 | self.pre = False |
| 137 | self.tags = '' |
| 138 | elif tag in ['ul', 'dd', 'ol']: |
| 139 | self.indent(amt=-1) |
| 140 | |
| 141 | def handle_data(self, data): |
Terry Jan Reedy | 13b21f8 | 2015-09-21 22:36:36 -0400 | [diff] [blame^] | 142 | "Handle date segments in help.html." |
Terry Jan Reedy | a2f257b | 2015-09-20 19:56:54 -0400 | [diff] [blame] | 143 | if self.show and not self.hdrlink: |
| 144 | d = data if self.pre else data.replace('\n', ' ') |
| 145 | if self.tags == 'h1': |
| 146 | self.hprefix = d[0:d.index(' ')] |
| 147 | if self.tags in ['h1', 'h2', 'h3'] and self.hprefix != '': |
| 148 | if d[0:len(self.hprefix)] == self.hprefix: |
| 149 | d = d[len(self.hprefix):].strip() |
| 150 | self.data += d |
| 151 | self.text.insert('end', d, self.tags) |
| 152 | |
| 153 | def handle_charref(self, name): |
| 154 | self.text.insert('end', unichr(int(name))) |
| 155 | |
| 156 | |
| 157 | class HelpText(Text): |
Terry Jan Reedy | 13b21f8 | 2015-09-21 22:36:36 -0400 | [diff] [blame^] | 158 | "Display help.html." |
Terry Jan Reedy | a2f257b | 2015-09-20 19:56:54 -0400 | [diff] [blame] | 159 | def __init__(self, parent, filename): |
| 160 | "Configure tags and feed file to parser." |
| 161 | Text.__init__(self, parent, wrap='word', highlightthickness=0, |
| 162 | padx=5, borderwidth=0) |
Terry Jan Reedy | 37ad796 | 2015-09-20 20:05:51 -0400 | [diff] [blame] | 163 | |
Terry Jan Reedy | a2f257b | 2015-09-20 19:56:54 -0400 | [diff] [blame] | 164 | normalfont = self.findfont(['TkDefaultFont', 'arial', 'helvetica']) |
| 165 | fixedfont = self.findfont(['TkFixedFont', 'monaco', 'courier']) |
| 166 | self['font'] = (normalfont, 12) |
| 167 | self.tag_configure('em', font=(normalfont, 12, 'italic')) |
| 168 | self.tag_configure('h1', font=(normalfont, 20, 'bold')) |
| 169 | self.tag_configure('h2', font=(normalfont, 18, 'bold')) |
| 170 | self.tag_configure('h3', font=(normalfont, 15, 'bold')) |
| 171 | self.tag_configure('pre', font=(fixedfont, 12)) |
| 172 | self.tag_configure('preblock', font=(fixedfont, 10), lmargin1=25, |
| 173 | borderwidth=1, relief='solid', background='#eeffcc') |
| 174 | self.tag_configure('l1', lmargin1=25, lmargin2=25) |
| 175 | self.tag_configure('l2', lmargin1=50, lmargin2=50) |
| 176 | self.tag_configure('l3', lmargin1=75, lmargin2=75) |
| 177 | self.tag_configure('l4', lmargin1=100, lmargin2=100) |
| 178 | |
Terry Jan Reedy | 37ad796 | 2015-09-20 20:05:51 -0400 | [diff] [blame] | 179 | self.parser = HelpParser(self) |
Terry Jan Reedy | a2f257b | 2015-09-20 19:56:54 -0400 | [diff] [blame] | 180 | with open(filename) as f: |
| 181 | contents = f.read().decode(encoding='utf-8') |
| 182 | self.parser.feed(contents) |
| 183 | self['state'] = 'disabled' |
| 184 | |
| 185 | def findfont(self, names): |
| 186 | "Return name of first font family derived from names." |
| 187 | for name in names: |
| 188 | if name.lower() in (x.lower() for x in tkfont.names(root=self)): |
| 189 | font = tkfont.Font(name=name, exists=True, root=self) |
| 190 | return font.actual()['family'] |
| 191 | elif name.lower() in (x.lower() |
| 192 | for x in tkfont.families(root=self)): |
| 193 | return name |
| 194 | |
| 195 | |
| 196 | class HelpFrame(Frame): |
Terry Jan Reedy | 13b21f8 | 2015-09-21 22:36:36 -0400 | [diff] [blame^] | 197 | "Display html text, scrollbar, and toc." |
Terry Jan Reedy | a2f257b | 2015-09-20 19:56:54 -0400 | [diff] [blame] | 198 | def __init__(self, parent, filename): |
| 199 | Frame.__init__(self, parent) |
| 200 | text = HelpText(self, filename) |
| 201 | self['background'] = text['background'] |
| 202 | scroll = Scrollbar(self, command=text.yview) |
| 203 | text['yscrollcommand'] = scroll.set |
| 204 | text.grid(column=1, row=0, sticky='nsew') |
| 205 | scroll.grid(column=2, row=0, sticky='ns') |
| 206 | self.grid_columnconfigure(1, weight=1) |
| 207 | self.grid_rowconfigure(0, weight=1) |
| 208 | toc = self.contents_widget(text) |
| 209 | toc.grid(column=0, row=0, sticky='nw') |
Terry Jan Reedy | 37ad796 | 2015-09-20 20:05:51 -0400 | [diff] [blame] | 210 | |
Terry Jan Reedy | a2f257b | 2015-09-20 19:56:54 -0400 | [diff] [blame] | 211 | def contents_widget(self, text): |
Terry Jan Reedy | 13b21f8 | 2015-09-21 22:36:36 -0400 | [diff] [blame^] | 212 | "Create table of contents." |
Terry Jan Reedy | a2f257b | 2015-09-20 19:56:54 -0400 | [diff] [blame] | 213 | toc = Menubutton(self, text='TOC') |
| 214 | drop = Menu(toc, tearoff=False) |
| 215 | for tag, lbl in text.parser.contents: |
| 216 | drop.add_command(label=lbl, command=lambda mark=tag:text.see(mark)) |
| 217 | toc['menu'] = drop |
| 218 | return toc |
| 219 | |
| 220 | |
| 221 | class HelpWindow(Toplevel): |
Terry Jan Reedy | 13b21f8 | 2015-09-21 22:36:36 -0400 | [diff] [blame^] | 222 | "Display frame with rendered html." |
Terry Jan Reedy | a2f257b | 2015-09-20 19:56:54 -0400 | [diff] [blame] | 223 | def __init__(self, parent, filename, title): |
| 224 | Toplevel.__init__(self, parent) |
| 225 | self.wm_title(title) |
| 226 | self.protocol("WM_DELETE_WINDOW", self.destroy) |
| 227 | HelpFrame(self, filename).grid(column=0, row=0, sticky='nsew') |
| 228 | self.grid_columnconfigure(0, weight=1) |
| 229 | self.grid_rowconfigure(0, weight=1) |
| 230 | |
| 231 | |
Terry Jan Reedy | 13b21f8 | 2015-09-21 22:36:36 -0400 | [diff] [blame^] | 232 | def copy_strip(): |
| 233 | "Copy idle.html to idlelib/help.html, stripping trailing whitespace." |
| 234 | src = join(abspath(dirname(dirname(dirname(__file__)))), |
| 235 | 'Doc', 'build', 'html', 'library', 'idle.html') |
| 236 | dst = join(abspath(dirname(__file__)), 'help.html') |
| 237 | with open(src, 'r') as inn,\ |
| 238 | open(dst, 'w') as out: |
| 239 | for line in inn: |
| 240 | out.write(line.rstrip() + '\n') |
| 241 | print('idle.html copied to help.html') |
| 242 | |
Terry Jan Reedy | a2f257b | 2015-09-20 19:56:54 -0400 | [diff] [blame] | 243 | def show_idlehelp(parent): |
Terry Jan Reedy | 13b21f8 | 2015-09-21 22:36:36 -0400 | [diff] [blame^] | 244 | "Create HelpWindow; called from Idle Help event handler." |
| 245 | filename = join(abspath(dirname(__file__)), 'help.html') |
Terry Jan Reedy | a2f257b | 2015-09-20 19:56:54 -0400 | [diff] [blame] | 246 | if not isfile(filename): |
Terry Jan Reedy | 13b21f8 | 2015-09-21 22:36:36 -0400 | [diff] [blame^] | 247 | # try copy_strip, present message |
| 248 | return |
Terry Jan Reedy | a2f257b | 2015-09-20 19:56:54 -0400 | [diff] [blame] | 249 | HelpWindow(parent, filename, 'IDLE Help') |
| 250 | |
| 251 | if __name__ == '__main__': |
| 252 | from idlelib.idle_test.htest import run |
| 253 | run(show_idlehelp) |