mirror of
				https://github.com/python/cpython.git
				synced 2025-11-04 07:31:38 +00:00 
			
		
		
		
	Handle Windows paths and don't double up on HTML header sections in new pydoc URL handler
This commit is contained in:
		
							parent
							
								
									1eb40bc945
								
							
						
					
					
						commit
						ecace28ef4
					
				
					 2 changed files with 19 additions and 17 deletions
				
			
		
							
								
								
									
										26
									
								
								Lib/pydoc.py
									
										
									
									
									
								
							
							
						
						
									
										26
									
								
								Lib/pydoc.py
									
										
									
									
									
								
							| 
						 | 
					@ -2540,7 +2540,7 @@ def bltinlink(name):
 | 
				
			||||||
            '<p align=right><font color="#909090" face="helvetica,'
 | 
					            '<p align=right><font color="#909090" face="helvetica,'
 | 
				
			||||||
            'arial"><strong>pydoc</strong> by Ka-Ping Yee'
 | 
					            'arial"><strong>pydoc</strong> by Ka-Ping Yee'
 | 
				
			||||||
            '<ping@lfw.org></font>')
 | 
					            '<ping@lfw.org></font>')
 | 
				
			||||||
        return html.page('Index of Modules', ''.join(contents))
 | 
					        return 'Index of Modules', ''.join(contents)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def html_search(key):
 | 
					    def html_search(key):
 | 
				
			||||||
        """Search results page."""
 | 
					        """Search results page."""
 | 
				
			||||||
| 
						 | 
					@ -2568,11 +2568,11 @@ def bltinlink(name):
 | 
				
			||||||
            results.append(bltinlink(name) + desc)
 | 
					            results.append(bltinlink(name) + desc)
 | 
				
			||||||
        contents = heading + html.bigsection(
 | 
					        contents = heading + html.bigsection(
 | 
				
			||||||
            'key = %s' % key, '#ffffff', '#ee77aa', '<br>'.join(results))
 | 
					            'key = %s' % key, '#ffffff', '#ee77aa', '<br>'.join(results))
 | 
				
			||||||
        return html.page('Search Results', contents)
 | 
					        return 'Search Results', contents
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def html_getfile(path):
 | 
					    def html_getfile(path):
 | 
				
			||||||
        """Get and display a source file listing safely."""
 | 
					        """Get and display a source file listing safely."""
 | 
				
			||||||
        path = os.sep + path.replace('%20', ' ')
 | 
					        path = path.replace('%20', ' ')
 | 
				
			||||||
        with open(path, 'r') as fp:
 | 
					        with open(path, 'r') as fp:
 | 
				
			||||||
            lines = html.escape(fp.read())
 | 
					            lines = html.escape(fp.read())
 | 
				
			||||||
        body = '<pre>%s</pre>' % lines
 | 
					        body = '<pre>%s</pre>' % lines
 | 
				
			||||||
| 
						 | 
					@ -2581,7 +2581,7 @@ def html_getfile(path):
 | 
				
			||||||
            '#ffffff', '#7799ee')
 | 
					            '#ffffff', '#7799ee')
 | 
				
			||||||
        contents = heading + html.bigsection(
 | 
					        contents = heading + html.bigsection(
 | 
				
			||||||
            'File: %s' % path, '#ffffff', '#ee77aa', body)
 | 
					            'File: %s' % path, '#ffffff', '#ee77aa', body)
 | 
				
			||||||
        return html.page('getfile %s' % path, contents)
 | 
					        return 'getfile %s' % path, contents
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def html_topics():
 | 
					    def html_topics():
 | 
				
			||||||
        """Index of topic texts available."""
 | 
					        """Index of topic texts available."""
 | 
				
			||||||
| 
						 | 
					@ -2597,7 +2597,7 @@ def bltinlink(name):
 | 
				
			||||||
        contents = html.multicolumn(names, bltinlink)
 | 
					        contents = html.multicolumn(names, bltinlink)
 | 
				
			||||||
        contents = heading + html.bigsection(
 | 
					        contents = heading + html.bigsection(
 | 
				
			||||||
            'Topics', '#ffffff', '#ee77aa', contents)
 | 
					            'Topics', '#ffffff', '#ee77aa', contents)
 | 
				
			||||||
        return html.page('Topics', contents)
 | 
					        return 'Topics', contents
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def html_keywords():
 | 
					    def html_keywords():
 | 
				
			||||||
        """Index of keywords."""
 | 
					        """Index of keywords."""
 | 
				
			||||||
| 
						 | 
					@ -2612,7 +2612,7 @@ def bltinlink(name):
 | 
				
			||||||
        contents = html.multicolumn(names, bltinlink)
 | 
					        contents = html.multicolumn(names, bltinlink)
 | 
				
			||||||
        contents = heading + html.bigsection(
 | 
					        contents = heading + html.bigsection(
 | 
				
			||||||
            'Keywords', '#ffffff', '#ee77aa', contents)
 | 
					            'Keywords', '#ffffff', '#ee77aa', contents)
 | 
				
			||||||
        return html.page('Keywords', contents)
 | 
					        return 'Keywords', contents
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def html_topicpage(topic):
 | 
					    def html_topicpage(topic):
 | 
				
			||||||
        """Topic or keyword help page."""
 | 
					        """Topic or keyword help page."""
 | 
				
			||||||
| 
						 | 
					@ -2636,7 +2636,7 @@ def bltinlink(name):
 | 
				
			||||||
        xrefs = html.multicolumn(xrefs, bltinlink)
 | 
					        xrefs = html.multicolumn(xrefs, bltinlink)
 | 
				
			||||||
        xrefs = html.section('Related help topics: ',
 | 
					        xrefs = html.section('Related help topics: ',
 | 
				
			||||||
                             '#ffffff', '#ee77aa', xrefs)
 | 
					                             '#ffffff', '#ee77aa', xrefs)
 | 
				
			||||||
        return html.page('%s %s' % (title, topic),
 | 
					        return ('%s %s' % (title, topic),
 | 
				
			||||||
                ''.join((heading, contents, xrefs)))
 | 
					                ''.join((heading, contents, xrefs)))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def html_error(url):
 | 
					    def html_error(url):
 | 
				
			||||||
| 
						 | 
					@ -2656,17 +2656,17 @@ def get_html_page(url):
 | 
				
			||||||
        title = url
 | 
					        title = url
 | 
				
			||||||
        contents = ''
 | 
					        contents = ''
 | 
				
			||||||
        if url in ("", ".", "index"):
 | 
					        if url in ("", ".", "index"):
 | 
				
			||||||
            contents = html_index()
 | 
					            title, contents = html_index()
 | 
				
			||||||
        elif url == "topics":
 | 
					        elif url == "topics":
 | 
				
			||||||
            contents = html_topics()
 | 
					            title, contents = html_topics()
 | 
				
			||||||
        elif url == "keywords":
 | 
					        elif url == "keywords":
 | 
				
			||||||
            contents = html_keywords()
 | 
					            title, contents = html_keywords()
 | 
				
			||||||
        elif url.startswith("search?key="):
 | 
					        elif url.startswith("search?key="):
 | 
				
			||||||
            contents = html_search(url[11:])
 | 
					            title, contents = html_search(url[11:])
 | 
				
			||||||
        elif url.startswith("getfile?key="):
 | 
					        elif url.startswith("getfile?key="):
 | 
				
			||||||
            url = url[12:]
 | 
					            url = url[12:]
 | 
				
			||||||
            try:
 | 
					            try:
 | 
				
			||||||
                contents = html_getfile(url)
 | 
					                title, contents = html_getfile(url)
 | 
				
			||||||
            except IOError:
 | 
					            except IOError:
 | 
				
			||||||
                contents = html_error('could not read file %r' % url)
 | 
					                contents = html_error('could not read file %r' % url)
 | 
				
			||||||
                title = 'Read Error'
 | 
					                title = 'Read Error'
 | 
				
			||||||
| 
						 | 
					@ -2680,7 +2680,7 @@ def get_html_page(url):
 | 
				
			||||||
                title = describe(obj)
 | 
					                title = describe(obj)
 | 
				
			||||||
                contents = html.document(obj, url)
 | 
					                contents = html.document(obj, url)
 | 
				
			||||||
            elif url in Helper.keywords or url in Helper.topics:
 | 
					            elif url in Helper.keywords or url in Helper.topics:
 | 
				
			||||||
                contents = html_topicpage(url)
 | 
					                title, contents = html_topicpage(url)
 | 
				
			||||||
            else:
 | 
					            else:
 | 
				
			||||||
                contents = html_error(
 | 
					                contents = html_error(
 | 
				
			||||||
                    'no Python documentation found for %r' % url)
 | 
					                    'no Python documentation found for %r' % url)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -237,8 +237,10 @@ def print_diffs(text1, text2):
 | 
				
			||||||
    print('\n' + ''.join(diffs))
 | 
					    print('\n' + ''.join(diffs))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def get_html_title(text):
 | 
					def get_html_title(text):
 | 
				
			||||||
    _, _, text = text.rpartition("<title>")
 | 
					    # Bit of hack, but good enough for test purposes
 | 
				
			||||||
    title, _, _ = text.rpartition("</title>")
 | 
					    header, _, _ = text.partition("</head>")
 | 
				
			||||||
 | 
					    _, _, title = header.partition("<title>")
 | 
				
			||||||
 | 
					    title, _, _ = title.partition("</title>")
 | 
				
			||||||
    return title
 | 
					    return title
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -449,7 +451,7 @@ def test_url_requests(self):
 | 
				
			||||||
            self.assertEqual(result, title)
 | 
					            self.assertEqual(result, title)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        path = string.__file__
 | 
					        path = string.__file__
 | 
				
			||||||
        title = "Python: getfile /" + path
 | 
					        title = "Python: getfile " + path
 | 
				
			||||||
        url = "getfile?key=" + path
 | 
					        url = "getfile?key=" + path
 | 
				
			||||||
        text = pydoc._url_handler(url, "text/html")
 | 
					        text = pydoc._url_handler(url, "text/html")
 | 
				
			||||||
        result = get_html_title(text)
 | 
					        result = get_html_title(text)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue