| 1 | n/a | """ help.py: Implement the Idle help menu. |
|---|
| 2 | n/a | Contents are subject to revision at any time, without notice. |
|---|
| 3 | n/a | |
|---|
| 4 | n/a | |
|---|
| 5 | n/a | Help => About IDLE: diplay About Idle dialog |
|---|
| 6 | n/a | |
|---|
| 7 | n/a | <to be moved here from help_about.py> |
|---|
| 8 | n/a | |
|---|
| 9 | n/a | |
|---|
| 10 | n/a | Help => IDLE Help: Display help.html with proper formatting. |
|---|
| 11 | n/a | Doc/library/idle.rst (Sphinx)=> Doc/build/html/library/idle.html |
|---|
| 12 | n/a | (help.copy_strip)=> Lib/idlelib/help.html |
|---|
| 13 | n/a | |
|---|
| 14 | n/a | HelpParser - Parse help.html and render to tk Text. |
|---|
| 15 | n/a | |
|---|
| 16 | n/a | HelpText - Display formatted help.html. |
|---|
| 17 | n/a | |
|---|
| 18 | n/a | HelpFrame - Contain text, scrollbar, and table-of-contents. |
|---|
| 19 | n/a | (This will be needed for display in a future tabbed window.) |
|---|
| 20 | n/a | |
|---|
| 21 | n/a | HelpWindow - Display HelpFrame in a standalone window. |
|---|
| 22 | n/a | |
|---|
| 23 | n/a | copy_strip - Copy idle.html to help.html, rstripping each line. |
|---|
| 24 | n/a | |
|---|
| 25 | n/a | show_idlehelp - Create HelpWindow. Called in EditorWindow.help_dialog. |
|---|
| 26 | n/a | """ |
|---|
| 27 | n/a | from html.parser import HTMLParser |
|---|
| 28 | n/a | from os.path import abspath, dirname, isfile, join |
|---|
| 29 | n/a | from platform import python_version |
|---|
| 30 | n/a | |
|---|
| 31 | n/a | from tkinter import Toplevel, Frame, Text, Menu |
|---|
| 32 | n/a | from tkinter.ttk import Menubutton, Scrollbar |
|---|
| 33 | n/a | from tkinter import font as tkfont |
|---|
| 34 | n/a | |
|---|
| 35 | n/a | from idlelib.config import idleConf |
|---|
| 36 | n/a | |
|---|
| 37 | n/a | ## About IDLE ## |
|---|
| 38 | n/a | |
|---|
| 39 | n/a | |
|---|
| 40 | n/a | ## IDLE Help ## |
|---|
| 41 | n/a | |
|---|
| 42 | n/a | class HelpParser(HTMLParser): |
|---|
| 43 | n/a | """Render help.html into a text widget. |
|---|
| 44 | n/a | |
|---|
| 45 | n/a | The overridden handle_xyz methods handle a subset of html tags. |
|---|
| 46 | n/a | The supplied text should have the needed tag configurations. |
|---|
| 47 | n/a | The behavior for unsupported tags, such as table, is undefined. |
|---|
| 48 | n/a | If the tags generated by Sphinx change, this class, especially |
|---|
| 49 | n/a | the handle_starttag and handle_endtags methods, might have to also. |
|---|
| 50 | n/a | """ |
|---|
| 51 | n/a | def __init__(self, text): |
|---|
| 52 | n/a | HTMLParser.__init__(self, convert_charrefs=True) |
|---|
| 53 | n/a | self.text = text # text widget we're rendering into |
|---|
| 54 | n/a | self.tags = '' # current block level text tags to apply |
|---|
| 55 | n/a | self.chartags = '' # current character level text tags |
|---|
| 56 | n/a | self.show = False # used so we exclude page navigation |
|---|
| 57 | n/a | self.hdrlink = False # used so we don't show header links |
|---|
| 58 | n/a | self.level = 0 # indentation level |
|---|
| 59 | n/a | self.pre = False # displaying preformatted text |
|---|
| 60 | n/a | self.hprefix = '' # prefix such as '25.5' to strip from headings |
|---|
| 61 | n/a | self.nested_dl = False # if we're in a nested <dl> |
|---|
| 62 | n/a | self.simplelist = False # simple list (no double spacing) |
|---|
| 63 | n/a | self.toc = [] # pair headers with text indexes for toc |
|---|
| 64 | n/a | self.header = '' # text within header tags for toc |
|---|
| 65 | n/a | |
|---|
| 66 | n/a | def indent(self, amt=1): |
|---|
| 67 | n/a | self.level += amt |
|---|
| 68 | n/a | self.tags = '' if self.level == 0 else 'l'+str(self.level) |
|---|
| 69 | n/a | |
|---|
| 70 | n/a | def handle_starttag(self, tag, attrs): |
|---|
| 71 | n/a | "Handle starttags in help.html." |
|---|
| 72 | n/a | class_ = '' |
|---|
| 73 | n/a | for a, v in attrs: |
|---|
| 74 | n/a | if a == 'class': |
|---|
| 75 | n/a | class_ = v |
|---|
| 76 | n/a | s = '' |
|---|
| 77 | n/a | if tag == 'div' and class_ == 'section': |
|---|
| 78 | n/a | self.show = True # start of main content |
|---|
| 79 | n/a | elif tag == 'div' and class_ == 'sphinxsidebar': |
|---|
| 80 | n/a | self.show = False # end of main content |
|---|
| 81 | n/a | elif tag == 'p' and class_ != 'first': |
|---|
| 82 | n/a | s = '\n\n' |
|---|
| 83 | n/a | elif tag == 'span' and class_ == 'pre': |
|---|
| 84 | n/a | self.chartags = 'pre' |
|---|
| 85 | n/a | elif tag == 'span' and class_ == 'versionmodified': |
|---|
| 86 | n/a | self.chartags = 'em' |
|---|
| 87 | n/a | elif tag == 'em': |
|---|
| 88 | n/a | self.chartags = 'em' |
|---|
| 89 | n/a | elif tag in ['ul', 'ol']: |
|---|
| 90 | n/a | if class_.find('simple') != -1: |
|---|
| 91 | n/a | s = '\n' |
|---|
| 92 | n/a | self.simplelist = True |
|---|
| 93 | n/a | else: |
|---|
| 94 | n/a | self.simplelist = False |
|---|
| 95 | n/a | self.indent() |
|---|
| 96 | n/a | elif tag == 'dl': |
|---|
| 97 | n/a | if self.level > 0: |
|---|
| 98 | n/a | self.nested_dl = True |
|---|
| 99 | n/a | elif tag == 'li': |
|---|
| 100 | n/a | s = '\n* ' if self.simplelist else '\n\n* ' |
|---|
| 101 | n/a | elif tag == 'dt': |
|---|
| 102 | n/a | s = '\n\n' if not self.nested_dl else '\n' # avoid extra line |
|---|
| 103 | n/a | self.nested_dl = False |
|---|
| 104 | n/a | elif tag == 'dd': |
|---|
| 105 | n/a | self.indent() |
|---|
| 106 | n/a | s = '\n' |
|---|
| 107 | n/a | elif tag == 'pre': |
|---|
| 108 | n/a | self.pre = True |
|---|
| 109 | n/a | if self.show: |
|---|
| 110 | n/a | self.text.insert('end', '\n\n') |
|---|
| 111 | n/a | self.tags = 'preblock' |
|---|
| 112 | n/a | elif tag == 'a' and class_ == 'headerlink': |
|---|
| 113 | n/a | self.hdrlink = True |
|---|
| 114 | n/a | elif tag == 'h1': |
|---|
| 115 | n/a | self.tags = tag |
|---|
| 116 | n/a | elif tag in ['h2', 'h3']: |
|---|
| 117 | n/a | if self.show: |
|---|
| 118 | n/a | self.header = '' |
|---|
| 119 | n/a | self.text.insert('end', '\n\n') |
|---|
| 120 | n/a | self.tags = tag |
|---|
| 121 | n/a | if self.show: |
|---|
| 122 | n/a | self.text.insert('end', s, (self.tags, self.chartags)) |
|---|
| 123 | n/a | |
|---|
| 124 | n/a | def handle_endtag(self, tag): |
|---|
| 125 | n/a | "Handle endtags in help.html." |
|---|
| 126 | n/a | if tag in ['h1', 'h2', 'h3']: |
|---|
| 127 | n/a | self.indent(0) # clear tag, reset indent |
|---|
| 128 | n/a | if self.show: |
|---|
| 129 | n/a | self.toc.append((self.header, self.text.index('insert'))) |
|---|
| 130 | n/a | elif tag in ['span', 'em']: |
|---|
| 131 | n/a | self.chartags = '' |
|---|
| 132 | n/a | elif tag == 'a': |
|---|
| 133 | n/a | self.hdrlink = False |
|---|
| 134 | n/a | elif tag == 'pre': |
|---|
| 135 | n/a | self.pre = False |
|---|
| 136 | n/a | self.tags = '' |
|---|
| 137 | n/a | elif tag in ['ul', 'dd', 'ol']: |
|---|
| 138 | n/a | self.indent(amt=-1) |
|---|
| 139 | n/a | |
|---|
| 140 | n/a | def handle_data(self, data): |
|---|
| 141 | n/a | "Handle date segments in help.html." |
|---|
| 142 | n/a | if self.show and not self.hdrlink: |
|---|
| 143 | n/a | d = data if self.pre else data.replace('\n', ' ') |
|---|
| 144 | n/a | if self.tags == 'h1': |
|---|
| 145 | n/a | self.hprefix = d[0:d.index(' ')] |
|---|
| 146 | n/a | if self.tags in ['h1', 'h2', 'h3'] and self.hprefix != '': |
|---|
| 147 | n/a | if d[0:len(self.hprefix)] == self.hprefix: |
|---|
| 148 | n/a | d = d[len(self.hprefix):].strip() |
|---|
| 149 | n/a | self.header += d |
|---|
| 150 | n/a | self.text.insert('end', d, (self.tags, self.chartags)) |
|---|
| 151 | n/a | |
|---|
| 152 | n/a | |
|---|
| 153 | n/a | class HelpText(Text): |
|---|
| 154 | n/a | "Display help.html." |
|---|
| 155 | n/a | def __init__(self, parent, filename): |
|---|
| 156 | n/a | "Configure tags and feed file to parser." |
|---|
| 157 | n/a | uwide = idleConf.GetOption('main', 'EditorWindow', 'width', type='int') |
|---|
| 158 | n/a | uhigh = idleConf.GetOption('main', 'EditorWindow', 'height', type='int') |
|---|
| 159 | n/a | uhigh = 3 * uhigh // 4 # lines average 4/3 of editor line height |
|---|
| 160 | n/a | Text.__init__(self, parent, wrap='word', highlightthickness=0, |
|---|
| 161 | n/a | padx=5, borderwidth=0, width=uwide, height=uhigh) |
|---|
| 162 | n/a | |
|---|
| 163 | n/a | normalfont = self.findfont(['TkDefaultFont', 'arial', 'helvetica']) |
|---|
| 164 | n/a | fixedfont = self.findfont(['TkFixedFont', 'monaco', 'courier']) |
|---|
| 165 | n/a | self['font'] = (normalfont, 12) |
|---|
| 166 | n/a | self.tag_configure('em', font=(normalfont, 12, 'italic')) |
|---|
| 167 | n/a | self.tag_configure('h1', font=(normalfont, 20, 'bold')) |
|---|
| 168 | n/a | self.tag_configure('h2', font=(normalfont, 18, 'bold')) |
|---|
| 169 | n/a | self.tag_configure('h3', font=(normalfont, 15, 'bold')) |
|---|
| 170 | n/a | self.tag_configure('pre', font=(fixedfont, 12), background='#f6f6ff') |
|---|
| 171 | n/a | self.tag_configure('preblock', font=(fixedfont, 10), lmargin1=25, |
|---|
| 172 | n/a | borderwidth=1, relief='solid', background='#eeffcc') |
|---|
| 173 | n/a | self.tag_configure('l1', lmargin1=25, lmargin2=25) |
|---|
| 174 | n/a | self.tag_configure('l2', lmargin1=50, lmargin2=50) |
|---|
| 175 | n/a | self.tag_configure('l3', lmargin1=75, lmargin2=75) |
|---|
| 176 | n/a | self.tag_configure('l4', lmargin1=100, lmargin2=100) |
|---|
| 177 | n/a | |
|---|
| 178 | n/a | self.parser = HelpParser(self) |
|---|
| 179 | n/a | with open(filename, encoding='utf-8') as f: |
|---|
| 180 | n/a | contents = f.read() |
|---|
| 181 | n/a | self.parser.feed(contents) |
|---|
| 182 | n/a | self['state'] = 'disabled' |
|---|
| 183 | n/a | |
|---|
| 184 | n/a | def findfont(self, names): |
|---|
| 185 | n/a | "Return name of first font family derived from names." |
|---|
| 186 | n/a | for name in names: |
|---|
| 187 | n/a | if name.lower() in (x.lower() for x in tkfont.names(root=self)): |
|---|
| 188 | n/a | font = tkfont.Font(name=name, exists=True, root=self) |
|---|
| 189 | n/a | return font.actual()['family'] |
|---|
| 190 | n/a | elif name.lower() in (x.lower() |
|---|
| 191 | n/a | for x in tkfont.families(root=self)): |
|---|
| 192 | n/a | return name |
|---|
| 193 | n/a | |
|---|
| 194 | n/a | |
|---|
| 195 | n/a | class HelpFrame(Frame): |
|---|
| 196 | n/a | "Display html text, scrollbar, and toc." |
|---|
| 197 | n/a | def __init__(self, parent, filename): |
|---|
| 198 | n/a | Frame.__init__(self, parent) |
|---|
| 199 | n/a | # keep references to widgets for test access. |
|---|
| 200 | n/a | self.text = text = HelpText(self, filename) |
|---|
| 201 | n/a | self['background'] = text['background'] |
|---|
| 202 | n/a | self.toc = toc = self.toc_menu(text) |
|---|
| 203 | n/a | self.scroll = scroll = Scrollbar(self, command=text.yview) |
|---|
| 204 | n/a | text['yscrollcommand'] = scroll.set |
|---|
| 205 | n/a | |
|---|
| 206 | n/a | self.rowconfigure(0, weight=1) |
|---|
| 207 | n/a | self.columnconfigure(1, weight=1) # text |
|---|
| 208 | n/a | toc.grid(row=0, column=0, sticky='nw') |
|---|
| 209 | n/a | text.grid(row=0, column=1, sticky='nsew') |
|---|
| 210 | n/a | scroll.grid(row=0, column=2, sticky='ns') |
|---|
| 211 | n/a | |
|---|
| 212 | n/a | def toc_menu(self, text): |
|---|
| 213 | n/a | "Create table of contents as drop-down menu." |
|---|
| 214 | n/a | toc = Menubutton(self, text='TOC') |
|---|
| 215 | n/a | drop = Menu(toc, tearoff=False) |
|---|
| 216 | n/a | for lbl, dex in text.parser.toc: |
|---|
| 217 | n/a | drop.add_command(label=lbl, command=lambda dex=dex:text.yview(dex)) |
|---|
| 218 | n/a | toc['menu'] = drop |
|---|
| 219 | n/a | return toc |
|---|
| 220 | n/a | |
|---|
| 221 | n/a | |
|---|
| 222 | n/a | class HelpWindow(Toplevel): |
|---|
| 223 | n/a | "Display frame with rendered html." |
|---|
| 224 | n/a | def __init__(self, parent, filename, title): |
|---|
| 225 | n/a | Toplevel.__init__(self, parent) |
|---|
| 226 | n/a | self.wm_title(title) |
|---|
| 227 | n/a | self.protocol("WM_DELETE_WINDOW", self.destroy) |
|---|
| 228 | n/a | HelpFrame(self, filename).grid(column=0, row=0, sticky='nsew') |
|---|
| 229 | n/a | self.grid_columnconfigure(0, weight=1) |
|---|
| 230 | n/a | self.grid_rowconfigure(0, weight=1) |
|---|
| 231 | n/a | |
|---|
| 232 | n/a | |
|---|
| 233 | n/a | def copy_strip(): |
|---|
| 234 | n/a | """Copy idle.html to idlelib/help.html, stripping trailing whitespace. |
|---|
| 235 | n/a | |
|---|
| 236 | n/a | Files with trailing whitespace cannot be pushed to the hg cpython |
|---|
| 237 | n/a | repository. For 3.x (on Windows), help.html is generated, after |
|---|
| 238 | n/a | editing idle.rst in the earliest maintenance version, with |
|---|
| 239 | n/a | sphinx-build -bhtml . build/html |
|---|
| 240 | n/a | python_d.exe -c "from idlelib.help import copy_strip; copy_strip()" |
|---|
| 241 | n/a | After refreshing TortoiseHG workshop to generate a diff, |
|---|
| 242 | n/a | check both the diff and displayed text. Push the diff along with |
|---|
| 243 | n/a | the idle.rst change and merge both into default (or an intermediate |
|---|
| 244 | n/a | maintenance version). |
|---|
| 245 | n/a | |
|---|
| 246 | n/a | When the 'earlist' version gets its final maintenance release, |
|---|
| 247 | n/a | do an update as described above, without editing idle.rst, to |
|---|
| 248 | n/a | rebase help.html on the next version of idle.rst. Do not worry |
|---|
| 249 | n/a | about version changes as version is not displayed. Examine other |
|---|
| 250 | n/a | changes and the result of Help -> IDLE Help. |
|---|
| 251 | n/a | |
|---|
| 252 | n/a | If maintenance and default versions of idle.rst diverge, and |
|---|
| 253 | n/a | merging does not go smoothly, then consider generating |
|---|
| 254 | n/a | separate help.html files from separate idle.htmls. |
|---|
| 255 | n/a | """ |
|---|
| 256 | n/a | src = join(abspath(dirname(dirname(dirname(__file__)))), |
|---|
| 257 | n/a | 'Doc', 'build', 'html', 'library', 'idle.html') |
|---|
| 258 | n/a | dst = join(abspath(dirname(__file__)), 'help.html') |
|---|
| 259 | n/a | with open(src, 'rb') as inn,\ |
|---|
| 260 | n/a | open(dst, 'wb') as out: |
|---|
| 261 | n/a | for line in inn: |
|---|
| 262 | n/a | out.write(line.rstrip() + b'\n') |
|---|
| 263 | n/a | print('idle.html copied to help.html') |
|---|
| 264 | n/a | |
|---|
| 265 | n/a | def show_idlehelp(parent): |
|---|
| 266 | n/a | "Create HelpWindow; called from Idle Help event handler." |
|---|
| 267 | n/a | filename = join(abspath(dirname(__file__)), 'help.html') |
|---|
| 268 | n/a | if not isfile(filename): |
|---|
| 269 | n/a | # try copy_strip, present message |
|---|
| 270 | n/a | return |
|---|
| 271 | n/a | HelpWindow(parent, filename, 'IDLE Help (%s)' % python_version()) |
|---|
| 272 | n/a | |
|---|
| 273 | n/a | if __name__ == '__main__': |
|---|
| 274 | n/a | from idlelib.idle_test.htest import run |
|---|
| 275 | n/a | run(show_idlehelp) |
|---|