def unknown_entityref(self, entname, terminator): if self.suppress_output: return img = self.load_dingbat(entname) if img: if type(img) is TupleType: s, tag = img if tag: if tag != "_ding": tag = (self.formatter.writer.fonttag or '') + tag self.viewer.configure_fonttag(tag) self.formatter.push_style(tag) self.viewer.text.tag_raise(tag) self.handle_data(s) self.formatter.pop_style() else: self.handle_data(s) else: bgcolor = self.viewer.text['background'] label = Label(self.viewer.text, image=img, background=bgcolor, borderwidth=0) self.add_subwindow(label) # this needs to be done *after* the add_subwindow() # call to get the right <Button-3> bindings. if self.anchor: IconicEntityLinker(self.viewer, self.anchor, self.target, label) else: # Could not load dingbat, allow parent class to handle: HTMLParser.unknown_entityref(self, entname, terminator)
def __init__(self, viewer, reload=0): global _inited self.viewer = viewer self.reload = reload self.context = self.viewer.context self.app = self.context.app self.load_dingbat = self.app.load_dingbat self.loaded = [] self.current_map = None self.target = None self.formatter_stack = [] fmt = formatter.AbstractFormatter(self.viewer) HTMLParser.__init__(self, fmt) self.push_formatter(fmt) if not _inited: _inited = 1 init_module(self.app.prefs) self._ids = {} # Hackery so reload status can be reset when all applets are loaded import AppletLoader self.reload1 = self.reload and AppletLoader.set_reload(self.context) if self.reload1: self.reload1.attach(self) if self.app.prefs.GetBoolean('parsing-html', 'strict'): self.sgml_parser.restrict(0) # Information from <META ... CONTENT="..."> is collected here. # Entries are KEY --> [(NAME, HTTP-EQUIV, CONTENT), ...], where # KEY is (NAME or HTTP-EQUIV). self._metadata = {}
def __init__(self, writer, settings, context): if not self._inited: for k, v in self.fontdingbats.items(): self.dingbats[(k, 'grey')] = v self.dingbats[(k, 'color')] = v import Greek for k, v in Greek.entitydefs.items(): tup = (v, 'Symbol') self.dingbats[(k, 'grey')] = tup self.dingbats[(k, 'color')] = tup PrintingHTMLParser._inited = 1 HTMLParser.__init__(self, AbstractFormatter(writer)) if settings.strict_parsing: self.sgml_parser.restrict(0) self._baseurl = context.get_baseurl() self.context = context self.settings = settings if settings.imageflag: self._image_loader = utils.image_loader self._image_cache = {} self._anchors = {None: None} self._anchor_sequence = [] self._anchor_xforms = [] if not settings.footnoteflag: self.add_anchor_transform(disallow_anchor_footnotes) else: self.add_anchor_transform( disallow_self_reference(context.get_url())) self.__fontsize = [3]
def do_hr(self, attrs): if attrs.has_key('src') and self.app.load_images: align = extract_keyword('align', attrs, default='center', conv=lambda s,gu=grailutil: gu.conv_enumeration( gu.conv_normstring(s), ['left', 'center', 'right'])) self.implied_end_p() self.formatter.push_alignment(align) self.do_img({'border': '0', 'src': attrs['src']}) self.formatter.pop_alignment() self.formatter.add_line_break() return HTMLParser.do_hr(self, attrs) color = extract_keyword('color', attrs) rule = self.viewer.rules[-1] if attrs.has_key('noshade') and self.viewer.rules: if color: if not self.configcolor('background', color, widget=rule): self.configcolor('background', self.viewer.text['foreground'], widget=rule) else: # this color is known to work already rule.config(background=self.viewer.text['foreground']) rule.config(relief=FLAT) size = extract_keyword('size', attrs, 2, conv=grailutil.conv_integer) if size == 1: # could not actually set it to 1 unless it was flat; do it now: width = string.atoi(rule.cget('width')) rule.config(borderwidth=0, height=1, width=width+2) elif color: self.configcolor('background', color, widget=rule)
def make_format(self, format, default='disc', listtype = None): fmt = format or default if fmt in ('disc', 'circle', 'square') and listtype == 'ul': img = self.load_dingbat(fmt) return img or HTMLParser.make_format(self, format, default) else: return HTMLParser.make_format(self, format, default, listtype = listtype)
def header_bgn(self, tag, level, attrs): HTMLParser.header_bgn(self, tag, level, attrs) dingbat = extract_keyword('dingbat', attrs) if dingbat: self.unknown_entityref(dingbat, '') self.formatter.add_flowing_data(' ') elif attrs.has_key('src'): self.do_img(attrs) self.formatter.add_flowing_data(' ')
def close(self): HTMLParser.close(self) if self.reload1: self.reload1.detach(self) self.reload1 = None refresh = None if self._metadata.has_key("refresh"): name, http_equiv, refresh = self._metadata["refresh"][0] elif self.context.get_headers().has_key("refresh"): refresh = self.context.get_headers()["refresh"] if refresh: DynamicReloader(self.context, refresh)
def unknown_entityref(self, entname, terminator): dingbat = self.load_dingbat(entname) if type(dingbat) is types.TupleType: apply(self.formatter.writer.ps.push_font_string, dingbat) self.formatter.assert_line_data() elif dingbat: dingbat.restrict(0.9 * self.formatter.writer.ps.get_fontsize(), self.formatter.writer.ps.get_pagewidth()) self.formatter.writer.send_eps_data(dingbat, 'absmiddle') self.formatter.assert_line_data() else: HTMLParser.unknown_entityref(self, entname, terminator)
def start_pre(self, attrs): HTMLParser.start_pre(self, attrs) new_size = AS_IS width = extract_keyword('width', attrs, 0, conv=conv_integer) if width > 0: ps = self.formatter.writer.ps space_width = ps._font.text_width(' ') pagewidth = ps.get_pagewidth() required = space_width * width if required > pagewidth: factor = pagewidth / required new_size = ps.get_fontsize() * factor self.formatter.push_font((new_size, AS_IS, AS_IS, AS_IS))
def make_format(self, format, default='disc', listtype=None): fmt = format or default if type(fmt) is StringType: fmt = string.lower(fmt) if fmt in ('disc', 'circle', 'square'): if listtype == 'ul': img = self.load_dingbat(fmt) return img or HTMLParser.make_format(self, format, default, listtype = listtype) else: return '1.' else: return HTMLParser.make_format(self, format, default, listtype = listtype)
def do_li(self, attrs): if attrs.has_key('dingbat'): if self.list_stack: if self.list_stack[-1][0] == 'ul': self.list_handle_dingbat(attrs) else: self.list_handle_dingbat(attrs) elif attrs.has_key('src'): if self.list_stack: if self.list_stack[-1][0] == 'ul': self.list_handle_src(attrs) else: self.list_handle_src(attrs) HTMLParser.do_li(self, attrs)
def start_body(self, attrs): HTMLParser.start_body(self, attrs) if not self.app.prefs.GetBoolean('parsing-html', 'honor-colors'): return from grailutil import conv_normstring bgcolor = extract_keyword('bgcolor', attrs, conv=conv_normstring) if bgcolor: clr = self.configcolor('background', bgcolor) if clr: # Normally not important, but ISINDEX would cause # these to be non-empty, as would all sorts of illegal stuff: for hr in self.viewer.rules + self.viewer.subwindows: hr.config(highlightbackground = clr) self.configcolor('foreground', extract_keyword('text', attrs, conv=conv_normstring)) self.configcolor('foreground', extract_keyword('link', attrs, conv=conv_normstring), 'a') self.configcolor('foreground', extract_keyword('vlink', attrs, conv=conv_normstring), 'ahist') self.configcolor('foreground', extract_keyword('alink', attrs, conv=conv_normstring), 'atemp')
def start_body(self, attrs): HTMLParser.start_body(self, attrs) if not self.app.prefs.GetBoolean('parsing-html', 'honor-colors'): return from grailutil import conv_normstring bgcolor = extract_keyword('bgcolor', attrs, conv=conv_normstring) if bgcolor: clr = self.configcolor('background', bgcolor) if clr: # Normally not important, but ISINDEX would cause # these to be non-empty, as would all sorts of illegal stuff: for hr in self.viewer.rules + self.viewer.subwindows: hr.config(highlightbackground=clr) self.configcolor('foreground', extract_keyword('text', attrs, conv=conv_normstring)) self.configcolor('foreground', extract_keyword('link', attrs, conv=conv_normstring), 'a') self.configcolor('foreground', extract_keyword('vlink', attrs, conv=conv_normstring), 'ahist') self.configcolor('foreground', extract_keyword('alink', attrs, conv=conv_normstring), 'atemp')
def start_ol(self, attrs): HTMLParser.start_ol(self, attrs) self.formatter.writer.suppress_indentation()
def do_dt(self, attrs): HTMLParser.do_dt(self, attrs) self.formatter.writer.suppress_indentation()
def do_li(self, attrs): self.list_check_dingbat(attrs) HTMLParser.do_li(self, attrs) self.formatter.writer.suppress_indentation()
def end_ol(self): HTMLParser.end_ol(self) self.formatter.writer.suppress_indentation(0)
def start_ul(self, attrs, tag='ul'): if attrs.has_key('dingbat'): self.list_handle_dingbat(attrs) elif attrs.has_key('src'): self.list_handle_src(attrs) HTMLParser.start_ul(self, attrs, tag=tag)
def end_title(self): HTMLParser.end_title(self) self.context.set_title(self.title) if not self.inhead: self.badhtml = 1
def end_pre(self): self.formatter.pop_font() HTMLParser.end_pre(self)
def end_title(self): HTMLParser.end_title(self) self.formatter.writer.ps.set_title(self.title) self.formatter.writer.ps.prune_titles()
def do_base(self, attrs): HTMLParser.do_base(self, attrs) if self.base: self.context.set_baseurl(self.base)
def header_number(self, tag, level, attrs): # make sure we have at least 3*fontsize vertical space available: self.require_vspace(3) # now call the base class: HTMLParser.header_number(self, tag, level, attrs)
def close(self): if self._anchor_sequence: self.write_footnotes() HTMLParser.close(self)
def header_end(self, tag, level): HTMLParser.header_end(self, tag, level) self.formatter.writer.suppress_indentation()