diff -r 4419e2941205 Lib/pydoc.py --- a/Lib/pydoc.py Sun Sep 22 11:46:51 2013 +0200 +++ b/Lib/pydoc.py Sun Oct 20 20:50:33 2013 +0530 @@ -968,6 +968,116 @@ """Produce html documentation for a data descriptor.""" return self._docdescriptor(name, object, mod) + def dockeywordlist(self, server = False): + """Produce HTML documentation for list of keywords. + Returns tuple of the form (title, contents) + """ + heading = html.heading( + 'INDEX', + '#ffffff', '#7799ee') + names = sorted(Helper.keywords.keys()) + + def bltinlink(name): + if server: + href = '"topic?key=%s"' % (name) + else: + href = '"%s.html"' % (name) + return '%s' % (href, name) + + contents = html.multicolumn(names, bltinlink) + contents = heading + html.bigsection( + 'Keywords', '#ffffff', '#ee77aa', contents) + return 'Keywords', contents + + def doctopiclist(self, server = False): + """Produce HTML documentation for list of topics. + Returns tuple of the form (title, contents) + """ + def bltinlink(name): + if server: + href = '"topic?key=%s"' % (name) + else: + href = '"%s.html"' % (name) + return '%s' % (href, name) + + heading = html.heading( + 'INDEX', + '#ffffff', '#7799ee') + names = sorted(Helper.topics.keys()) + + contents = html.multicolumn(names, bltinlink) + contents = heading + html.bigsection( + 'Topics', '#ffffff', '#ee77aa', contents) + return 'Topics', contents + + def docsymbollist(self, server = False): + """Produce HTML documentation for list of topics. + Returns tuple of the form (title, contents) + """ + def bltinlink(name): + if server: + href = '"topic?key=%s"' % (name) + else: + href = '"%s.html"' % (name) + return '%s' % (href, name) + + heading = html.heading( + 'INDEX', + '#ffffff', '#7799ee') + names = sorted(Helper.symbols.keys()) + + contents = html.multicolumn(names, bltinlink) + contents = heading + html.bigsection( + 'Symbols', '#ffffff', '#ee77aa', contents) + return 'Symbols', contents + + def doctopic(self, topic, server = False): + """Produce HTML documentation for topics, keyword or symbol. + Returns tuple of the form (title, contents) + """ + buf = io.StringIO() + htmlhelp = Helper(buf, buf) + topic = htmlhelp.symbols.get(topic, topic) + topic, _, xrefs = topic.partition(' ') + contents, xrefs = htmlhelp._gettopic(topic, xrefs) + if topic in htmlhelp.keywords: + title = 'KEYWORD' + else: + title = 'TOPIC' + heading = html.heading( + '%s' % title, + '#ffffff', '#7799ee') + contents = '
%s
' % html.markup(contents) + contents = html.bigsection(topic , '#ffffff','#ee77aa', contents) + if xrefs: + xrefs = sorted(xrefs.split()) + + def bltinlink(name): + if server: + href = '"topic?key=%s"' % (name) + else: + href = '"%s.html"' % (name) + return '%s' % (href, name) + + xrefs = html.multicolumn(xrefs, bltinlink) + xrefs = html.section('Related help topics: ', + '#ffffff', '#ee77aa', xrefs) + return ('%s %s' % (title, topic), + ''.join((heading, contents, xrefs))) + + def docspecial(self, topic): + """Produce HTML documentation for an individual topic/keyword/symbol + or list of topics/keywords/symbols. + Returns tuple of the form (title, contents) + """ + if topic == 'keywords': + return self.dockeywordlist() + if topic == 'topics': + return self.doctopiclist() + if topic == 'symbols': + return self.docsymbollist() + return self.doctopic(topic) + def index(self, dir, shadowed=None): """Generate an HTML index for a directory of modules.""" modpkgs = [] @@ -1581,12 +1691,17 @@ try: object, name = resolve(thing, forceload) page = html.page(describe(object), html.document(object, name)) - file = open(name + '.html', 'w', encoding='utf-8') - file.write(page) - file.close() - print('wrote', name + '.html') except (ImportError, ErrorDuringImport) as value: - print(value) + try: + page = html.page(*html.docspecial(thing)) + name = thing + except ValueError: + print(value) + return None + file = open(name + '.html', 'w', encoding='utf-8') + file.write(page) + file.close() + print('wrote', name + '.html') def writedocs(dir, pkgpath='', done=None): """Write out HTML documentation for all modules in a directory tree.""" @@ -2329,60 +2444,15 @@ def html_topics(): """Index of topic texts available.""" - - def bltinlink(name): - return '%s' % (name, name) - - heading = html.heading( - 'INDEX', - '#ffffff', '#7799ee') - names = sorted(Helper.topics.keys()) - - contents = html.multicolumn(names, bltinlink) - contents = heading + html.bigsection( - 'Topics', '#ffffff', '#ee77aa', contents) - return 'Topics', contents + return html.doctopiclist(server = True) def html_keywords(): """Index of keywords.""" - heading = html.heading( - 'INDEX', - '#ffffff', '#7799ee') - names = sorted(Helper.keywords.keys()) - - def bltinlink(name): - return '%s' % (name, name) - - contents = html.multicolumn(names, bltinlink) - contents = heading + html.bigsection( - 'Keywords', '#ffffff', '#ee77aa', contents) - return 'Keywords', contents + return html.dockeywordlist(server = True) def html_topicpage(topic): """Topic or keyword help page.""" - buf = io.StringIO() - htmlhelp = Helper(buf, buf) - contents, xrefs = htmlhelp._gettopic(topic) - if topic in htmlhelp.keywords: - title = 'KEYWORD' - else: - title = 'TOPIC' - heading = html.heading( - '%s' % title, - '#ffffff', '#7799ee') - contents = '
%s
' % html.markup(contents) - contents = html.bigsection(topic , '#ffffff','#ee77aa', contents) - if xrefs: - xrefs = sorted(xrefs.split()) - - def bltinlink(name): - return '%s' % (name, name) - - xrefs = html.multicolumn(xrefs, bltinlink) - xrefs = html.section('Related help topics: ', - '#ffffff', '#ee77aa', xrefs) - return ('%s %s' % (title, topic), - ''.join((heading, contents, xrefs))) + return html.doctopic(topic, server = True) def html_getobj(url): obj = locate(url, forceload=1) diff -r 4419e2941205 Lib/test/test_pydoc.py --- a/Lib/test/test_pydoc.py Sun Sep 22 11:46:51 2013 +0200 +++ b/Lib/test/test_pydoc.py Sun Oct 20 20:50:33 2013 +0530 @@ -466,6 +466,16 @@ methods = pydoc.allmethods(TestClass) self.assertDictEqual(methods, expected) + def test_writedoc(self): + # Issue 14680: pydoc with -w option does not work for a + # lot of help topics + test_words = ['EXPRESSIONS', 'keywords', '_'] + for word in test_words: + with captured_stdout() as out: + filename = word + '.html' + pydoc.writedoc(word) + self.assertEqual(out.getvalue(), 'wrote %s\n' % (filename)) + unlink(filename) class PydocImportTest(PydocBaseTest):