def insert_docs(buf, iter, obj, bold_tag): """Insert documentation about obj into a gtk.TextBuffer @param buf: the buffer to insert the documentation into @param iter: the location to insert the documentation @param obj: the object to get documentation about @param bold_tag: the tag to use for bold text, such as headings """ # If the routine is an instance, we get help on the type instead if is_data_object(obj): obj = type(obj) name = getattr(obj, '__name__', None) document = pydoc.text.document(obj, name) # pydoc.text.document represents boldface with overstrikes, we need to # reverse engineer this and find the spans of bold text pos = 0 while True: m = BOLD_RE.search(document, pos) if m is None: # Strip the trailing newline; this isn't very justifiable in general terms, # but matches what we need in Reinteract if document.endswith("\n"): buf.insert(iter, document[pos:-1]) else: buf.insert(iter, document[pos:]) break buf.insert(iter, document[pos:m.start()]) insert_with_tag(buf, iter, STRIP_BOLD_RE.sub(lambda m: m.group(1), m.group()), bold_tag) pos = m.end()
def callback(text, bold): if bold: insert_with_tag(buf, iter, text, bold_tag) else: buf.insert(iter, text)