Skip to content
3 changes: 3 additions & 0 deletions Doc/library/idle.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ IDLE
single: Python Editor
single: Integrated Development Environment

..
Remember to update Lib/idlelib/help.html with idlelib.help.copy_source() when modifying this file.

--------------

IDLE is Python's Integrated Development and Learning Environment.
Expand Down
45 changes: 21 additions & 24 deletions Lib/idlelib/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@

HelpWindow - Display HelpFrame in a standalone window.

copy_strip - Copy idle.html to help.html, rstripping each line.
copy_strip - Copy idle.html to help.html, between certain tags while rstripping each line.

show_idlehelp - Create HelpWindow. Called in EditorWindow.help_dialog.
"""
import os
from html.parser import HTMLParser
from os.path import abspath, dirname, isfile, join
from platform import python_version
Expand Down Expand Up @@ -77,11 +78,7 @@ def handle_starttag(self, tag, attrs):
if a == 'class':
class_ = v
s = ''
if tag == 'section' and attrs == [('id', 'idle')]:
self.show = True # Start main content.
elif tag == 'div' and class_ == 'clearer':
self.show = False # End main content.
elif tag == 'p' and self.prevtag and not self.prevtag[0]:
if tag == 'p' and self.prevtag and not self.prevtag[0]:
# Begin a new block for <p> tags after a closed tag.
# Avoid extra lines, e.g. after <pre> tags.
lastline = self.text.get('end-1c linestart', 'end-1c')
Expand Down Expand Up @@ -112,31 +109,27 @@ def handle_starttag(self, tag, attrs):
s = '\n'
elif tag == 'pre':
self.pre = True
if self.show:
self.text.insert('end', '\n\n')
self.text.insert('end', '\n\n')
self.tags = 'preblock'
elif tag == 'a' and class_ == 'headerlink':
self.hdrlink = True
elif tag == 'h1':
self.tags = tag
elif tag in ['h2', 'h3']:
if self.show:
self.header = ''
self.text.insert('end', '\n\n')
self.header = ''
self.text.insert('end', '\n\n')
self.tags = tag
if self.show:
self.text.insert('end', s, (self.tags, self.chartags))
self.text.insert('end', s, (self.tags, self.chartags))
self.prevtag = (True, tag)

def handle_endtag(self, tag):
"Handle endtags in help.html."
if tag in ['h1', 'h2', 'h3']:
assert self.level == 0
if self.show:
indent = (' ' if tag == 'h3' else
' ' if tag == 'h2' else
'')
self.toc.append((indent+self.header, self.text.index('insert')))
indent = (' ' if tag == 'h3' else
' ' if tag == 'h2' else
'')
self.toc.append((indent+self.header, self.text.index('insert')))
self.tags = ''
elif tag in ['span', 'em']:
self.chartags = ''
Expand All @@ -151,7 +144,7 @@ def handle_endtag(self, tag):

def handle_data(self, data):
"Handle date segments in help.html."
if self.show and not self.hdrlink:
if not self.hdrlink:
d = data if self.pre else data.replace('\n', ' ')
if self.tags == 'h1':
try:
Expand Down Expand Up @@ -251,7 +244,7 @@ def __init__(self, parent, filename, title):


def copy_strip(): # pragma: no cover
"""Copy idle.html to idlelib/help.html, stripping trailing whitespace.
"""Copy idle.html to idlelib/help.html, between certain tags while stripping trailing whitespace.

Files with trailing whitespace cannot be pushed to the git cpython
repository. For 3.x (on Windows), help.html is generated, after
Expand All @@ -263,7 +256,7 @@ def copy_strip(): # pragma: no cover

It can be worthwhile to occasionally generate help.html without
touching idle.rst. Changes to the master version and to the doc
build system may result in changes that should not changed
build system may result in changes that should not change
the displayed text, but might break HelpParser.

As long as master and maintenance versions of idle.rst remain the
Expand All @@ -276,10 +269,14 @@ def copy_strip(): # pragma: no cover
src = join(abspath(dirname(dirname(dirname(__file__)))),
'Doc', 'build', 'html', 'library', 'idle.html')
dst = join(abspath(dirname(__file__)), 'help.html')
with open(src, 'rb') as inn,\
open(dst, 'wb') as out:

with open(src, 'r', encoding="utf-8") as inn, open(dst, 'w', encoding="utf-8") as out:
copy = False
for line in inn:
out.write(line.rstrip() + b'\n')
if '<section id="idle">' in line: copy = True
if copy: out.write(line.rstrip() + '\n')
if '<div class="clearer">' in line: break

print(f'{src} copied to {dst}')


Expand Down
Loading