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