def handle_entityref(self, name): # Unescape special chars in code -- pygments will re-escape them if self.in_code(): if name == 'quot': self.data.append('"') elif name == 'gt': self.data.append('>') elif name == 'lt': self.data.append('<') else: self.data.append('&%s;' % name) else: # Default, pass through as-is HTMLPassThrough.handle_entityref(self, name)
def handle_endtag(self, tag): if self.data: # We've ended a <code> tag within <pre>. options, hl_lines = {}, [] # Parts of self.data will have \n in them. data = ''.join(self.data) lines = data.split('\n') if ':::' in self.data[0]: firstline, lines = lines[0], lines[1:] options = self.parse_code_header(firstline) if options.get('highlight'): # Highlighted lines within the code example. hl_lines = options['highlight'].split(',') code = '\n'.join(lines) self.emit(self.highlight(code, options.get('lang'), hl_lines)) self.data = [] HTMLPassThrough.handle_endtag(self, tag)
def handle_endtag(self, tag): if self.data: options, hl_lines = {}, [] # parts of self.data will have \n in them data = ''.join(self.data) # TODO: document the format we're parsing in README if ':::' in self.data[0]: lines = data.split('\n') firstline, lines = lines[0], lines[1:] options = self.parse_code_header(firstline) if options.get('highlight'): # Highlighted lines within the code example hl_lines = options['highlight'].split(',') code = '\n'.join(lines) self.emit(self.highlight(code, options.get('lang'), hl_lines)) else: self.emit(self.plain(data, hl_lines)) self.data = [] HTMLPassThrough.handle_endtag(self, tag)
def reset(self): HTMLPassThrough.reset(self) self.data = []
def handle_data(self, data): if self.in_code(): self.data.append(data) else: HTMLPassThrough.handle_data(self, data)