def lex(xmlstring): """ LEXs XML string. Allows broken CDATA (pygments does not) :param xmlstring: :return: [XmlInterval] """ lexer = XmlLexer() lokens = [] try: tokens = lexer.get_tokens_unprocessed(xmlstring) return list(_rejoin(tokens)) except _RejoinError as re: # probably CDATA, will check in depth; hope no such pieces in attributes lc = xmlstring.find("]]>") lo = xmlstring.find("<![CDATA[") rc = xmlstring.rfind("]]>") ro = xmlstring.rfind("<![CDATA[") if lc >= 0 and (lo == -1 or lo > lc): # missing open CDATA return lex("<![CDATA[" + xmlstring)[1:] elif ro >= 0 and (rc == -1 or rc < ro): # missing close CDATA return lex(xmlstring + "]]>")[:-1] except Exception as e: logging.fatal("XML lexing:" + repr(e))
def highlightRML(rml): lexer = XmlLexer() styledRml = '' for ttype, token in lexer.get_tokens(rml): start, end = STYLES_FORMATTING.get(ttype, ('', '')) styledRml += start + saxutils.escape(token) + end return styledRml
def highlight(src, syntax="python"): # type: (t.Text, t.Text) -> t.Text """Apply syntax highlighting. Only available if pygments is installed.""" if syntax == "xml": src = " " * 80 + src src = textwrap.dedent(src).strip() if not config.color: return src try: from pygments import highlight as pyg_highlight from pygments.lexers import PythonLexer, XmlLexer, RstLexer from pygments.formatters.terminal import TerminalFormatter except ImportError: return src else: if syntax == "python": lexer = PythonLexer() elif syntax == "xml": lexer = XmlLexer() elif syntax == "rst": lexer = RstLexer() else: raise ValueError("Unknown syntax {!r}".format(syntax)) return pyg_highlight(src, lexer, TerminalFormatter()).strip() # type: ignore
def get_revision(request, entity_id, rev): entity = get_object_or_404(Entity, id=entity_id) md = entity.metadata.get_revision(rev) formatter = HtmlFormatter(linenos=True) html = HTML_WRAPPER % (entity_id, rev, highlight(md, XmlLexer(), formatter)) return HttpResponse(html.encode(settings.DEFAULT_CHARSET))
def xml_highlight(xml_string): if xml_string: formated_xml = format_xml(xml_string) highlight_xml = highlight(formated_xml, XmlLexer(), HtmlFormatter(linenos=True)) return highlight_xml else: return ''
def display_xml_nice(xml): formatter = HtmlFormatter() IPython.display.display( HTML('<style type="text/css">{}</style> {}'.format( formatter.get_style_defs(".highlight"), highlight(xml, XmlLexer(), formatter), )))
def pretty_format_data(data, content_type=False): if data: if content_type: return highlight(data, XmlLexer(), HtmlFormatter()) else: return highlight(json.dumps(data, indent=4), JsonLexer(), HtmlFormatter())
def main(): """ Main function to excecute everything in order """ parser = argparse.ArgumentParser( description= "Command-line tool to validate and pretty-print JSON and XML") parser.add_argument("-x", action="store_true", help="Data is XML") parser.add_argument("-b", action="store_true", help="Read data in chunks") args = parser.parse_args() if args.x and args.b: sys.stderr.write("-x and -b cannot be used simultaneously\n") exit(1) elif args.b: for line in sys.stdin: print(color_yo_shit(format_code(line), JSONLexer())) else: data = sys.stdin.read() if sys.stdout.isatty(): try: data = color_yo_shit(format_code(data, args.x), XmlLexer() if args.x else JSONLexer()) except ValueError as e: print e print(data)
def xml(xml_element: ET.Element) -> DisplayHandle: formatter = HtmlFormatter() xml_indented = ET.tostring( xml_element, pretty_print=True, encoding='unicode') style = formatter.get_style_defs('.highlight') code = highlight(xml_indented, XmlLexer(), formatter) return display(HTML(f'<style type="text/css">{style}</style> {code}'))
def renderXML(context, xml): hformat = HtmlFormatter() body = highlight(prettify_xml(xml), XmlLexer(), hformat) env = Environment(loader=FileSystemLoader(context.confDirPath)) template_file = 'xml_render.html' template = env.get_template(template_file) return template.render(body=body)
def item_description(self, item): if item.metadata: formatter = HtmlFormatter(linenos=False, outencoding=settings.DEFAULT_CHARSET) if item.has_metadata(): xml = item.metadata.read() return highlight(xml, XmlLexer(), formatter) else: return ugettext(u'No metadata yet')
def do_schema(self, line): """schema prints the schema of the current index """ schema = self.solr.schema() print highlight(schema, formatter=TerminalFormatter(), lexer=XmlLexer()).rstrip()
def do_fields(self, line): """fields prints the fields of the current schema """ schema = self.solr.schema() search = re.search(r'<fields>.*?</fields>', schema, re.DOTALL) print highlight(search.group(0), formatter=TerminalFormatter(), lexer=XmlLexer()).rstrip()
def pprint_xml(xml_str, color=False): proc = subprocess.Popen(shlex.split('xmllint --format -'), stdin=subprocess.PIPE, stdout=subprocess.PIPE) (stdout, stderr) = proc.communicate(xml_str) if color: text = highlight(stdout, XmlLexer(), TerminalFormatter()) else: text = stdout print(text)
def highlight_xml(context, xml): """render XML as highlighted HTML""" hformat = HtmlFormatter() css = hformat.get_style_defs('.highlight') body = highlight(prettify_xml(xml), XmlLexer(), hformat) env = Environment(loader=FileSystemLoader(context.ppath)) template_file = 'resources/templates/xml_highlight.html' template = env.get_template(template_file) return template.render(css=css, body=body)
def pygmentize(text, format): """Returns respective HTML snippet of a source code aimed to be highlighted.""" if format == "n3" or format == "turtle" or format == "nt" or format == "nquads": lexer = Notation3Lexer() elif format == "rdfa" or format == "microdata": lexer = HtmlLexer() elif format == "pretty-xml" or format == "xml" or format == "trix": lexer = XmlLexer() elif format == "rdf-json" or format == "rdf-json-pretty" or format == "json-ld": lexer = JsonLexer() else: lexer = guess_lexer(text) return highlight(text, lexer, HtmlFormatter())
def item_description(self, item): entity = item['entity'] current = item['revision']['versionid'] formatter = HtmlFormatter(linenos=True, outencoding=settings.DEFAULT_CHARSET) if 'previous' in item['revision']: previous = item['revision']['previous'] diff = entity.metadata.get_diff(previous, current) html = highlight(diff, DiffLexer(), formatter) else: xml = entity.metadata.get_revision(current) html = highlight(xml, XmlLexer(), formatter) return html
def print_proc_entitlements(self): """ Get the plist embedded inside the process' __LINKEDIT section. """ linkedit_section = self.target.modules[0].FindSection('__LINKEDIT') linkedit_data = self.symbol( linkedit_section.GetLoadAddress(self.target)).peek( linkedit_section.size) # just look for the xml start inside the __LINKEDIT section. should be good enough since wer'e not # expecting any other XML there entitlements = str( linkedit_data[linkedit_data.find(b'<?xml'):].split(b'\xfa', 1)[0], 'utf8') print(highlight(entitlements, XmlLexer(), TerminalTrueColorFormatter()))
def highlight_content(context, content, mimetype): """render content as highlighted HTML""" hformat = HtmlFormatter() css = hformat.get_style_defs('.highlight') if mimetype == 'json': body = highlight(json.dumps(content, indent=4), JsonLexer(), hformat) elif mimetype == 'xml': body = highlight(prettify_xml(content), XmlLexer(), hformat) env = Environment(loader=FileSystemLoader(context.ppath)) template_file = 'resources/templates/api_highlight.html' template = env.get_template(template_file) return template.render(css=css, body=body)
def main(): """ Main function to execute everything in order """ import argparse from pygments.lexers import JsonLexer, XmlLexer from pjson.core import format_code, color_yo_shit parser = argparse.ArgumentParser(description="Command-line tool to " "validate and pretty-print JSON and XML") parser.add_argument("-x", action="store_true", help="Data is XML") parser.add_argument("-b", action="store_true", help="Read data in chunks") parser.add_argument("-t", action="store_true", help="Colorize regardless " "if output is a terminal") args = parser.parse_args() if args.x and args.b: sys.stderr.write("-x and -b cannot be used simultaneously\n") parser.print_usage(sys.stderr) exit(1) colorize = args.t or sys.stdout.isatty() if args.b: for line in sys.stdin: text = format_code(line) if colorize: text = color_yo_shit(text, JsonLexer()) print(text) else: data = sys.stdin.read() try: text = format_code(data, args.x) if colorize: text = color_yo_shit( text, XmlLexer() if args.x else JsonLexer()).rstrip('\r\n') print(text) except (ValueError, ET.ParseError) as e: message = str(e) if colorize: red = '\x1b[31;m' reset_colors = '\x1b[0;m' message = ''.join([red, message, reset_colors]) sys.stderr.write(message + '\n') exit(1)
def view_xml(self, m=None): if not self.dock_window: self.dock_window = TextEditor(self) self.iface.mainWindow().addDockWidget(Qt.BottomDockWidgetArea, self.dock_window) if not self.using_mapnik: # http://trac.osgeo.org/qgis/changeset/12955 - render starting signal QObject.connect(self.canvas, SIGNAL("renderComplete(QPainter *)"), self.checkLayers) self.dock_window.show() if self.loaded_mapfile: # if we have loaded a map xml or mml # so lets just display the active file xml = open(self.loaded_mapfile, 'rb').read() else: if not m: # regenerate from qgis objects e_c = sync.EasyCanvas(self, self.canvas) m = e_c.to_mapnik() if hasattr(mapnik, 'save_map_to_string'): xml = mapnik.save_map_to_string(m) else: (handle, mapfile) = tempfile.mkstemp('.xml', 'quantumnik-map-') os.close(handle) mapnik.save_map(m, str(mapfile)) xml = open(mapfile, 'rb').read() e = self.canvas.extent() bbox = '%s %s %s %s' % (e.xMinimum(), e.yMinimum(), e.xMaximum(), e.yMaximum()) cmd = '\n<!-- nik2img.py mapnik.xml out.png -d %s %s -e %s -->\n' % ( self.canvas.width(), self.canvas.height(), bbox) try: if self.mapnik_map: cmd += '<!-- <MinScaleDenominator>%s</MinScaleDenominator> -->\n' % ( self.mapnik_map.scale_denominator()) except: pass code = xml + cmd if HIGHLIGHTING: highlighted = highlight( code, XmlLexer(), HtmlFormatter(linenos=False, nowrap=False, full=True)) self.dock_window.textEdit.setHtml(highlighted) else: self.dock_window.textEdit.setText(xml + cmd)
def view_source(self, req, resp, url): """ View the highlighted source (from `action_view`). """ content_type = resp.content_type if content_type.startswith('application/xml'): lexer = XmlLexer() elif content_type == 'text/html': lexer = HtmlLexer() else: ## FIXME: what then? lexer = HtmlLexer() text = pygments_highlight( resp.body, lexer, HtmlFormatter(full=True, linenos=True, lineanchors='code')) return Response(text)
def print_config(output, config_spec): tree = output.simulation(0).config() if config_spec: expr = '//*[local-name() = $name]' snippets = tree.xpath(expr, name=config_spec) else: snippets = [tree] for i, what in enumerate(snippets): if i > 0: print() text = etree.tostring(what, encoding='unicode') if sys.stdout.isatty(): formatter = Terminal256Formatter() print(highlight(text, XmlLexer(), formatter)) else: print(text)
def xml(self, code): code = code.strip('\n') + '\n' self.snippet(code, line_numbers=False) return self with self._lock: self.update_terminal_width() if not self.terminal_colors: self(code).nl() else: try: from pygments import highlight from pygments.lexers import XmlLexer from pygments.formatters import TerminalFormatter except ImportError: self(xml).nl() else: hcode = highlight(code, XmlLexer(), TerminalFormatter()) self(hcode) return self
def __update_window(self, width, height, message_no, page_no): """ Update the window with the menu and the new text """ file_exists_label = '-F-ILE' if not os.path.exists(self.__create_file_name(message_no)): file_exists_label = '(f)ile' # Clear the screen if PLATFORM == 'win32': # Ugly hack until someone figures out a better way for Windows # probably something with a cls command, but I cannot test it for _ in range(50): print else: sys.stdout.write('\x1b[2J\x1b[H') # Clear screen # Content content = self.messages[message_no].output.rstrip('\n') out = content if self.args.color: out = pygments.highlight(content, XmlLexer(), TerminalFormatter()) # Paging functionality if message_no not in self.pages: self._form_pages(message_no, content, out, height, width) # Coerce in range page_no = max(min(len(self.pages[message_no]) - 1, page_no), 0) page_content = self.pages[message_no][page_no] # Menu max_message = str(len(self.messages) - 1) position_string = u'{{0: >{0}}}/{{1: <{0}}}'.format(len(max_message)) position_string = position_string.format(message_no, max_message) # Assume less than 100 pages current_max_page = len(self.pages[message_no]) - 1 pages_string = u'{0: >2}/{1: <2}'.format(page_no, current_max_page) menu = (u'(b)rowser | {0} | Message {1} \u2193 (s)\u2191 (w) | ' u'Page {2} \u2190 (a)\u2192 (d) | (q)uit\n{3}').\ format(file_exists_label, position_string, pages_string, '-' * width) print menu print page_content return page_no
def info(heritrix_conf, jobname, raw): """Show information about all jobs or a single job. If given a jobname as argument then only display information about this job. Tries to use ``pygments`` to colorize the output.""" import json api = heritrix_conf.api try: if raw: info = api.info(job_name=jobname, raw=True).text else: info = api.info(job_name=jobname, raw=False) info = json.dumps(info, indent=2) except HeritrixAPIError as ex: click.echo(f"Error: {ex}", err=True) raise click.Abort() if raw: try: from pygments import highlight from pygments.formatters import TerminalFormatter from pygments.lexers import XmlLexer info = highlight(info, XmlLexer(), TerminalFormatter()) except ImportError: pass else: try: from pygments import highlight from pygments.formatters import TerminalFormatter from pygments.lexers import JsonLexer info = highlight(info, JsonLexer(), TerminalFormatter()) except ImportError: pass click.echo(info)
import sys, time, os, lxml.etree as etree, re from parse_bill import parse from watchdog.observers import Observer from watchdog.events import FileSystemEventHandler, FileCreatedEvent, DirCreatedEvent from pygments.lexers import XmlLexer from pygments.formatters import HtmlFormatter from pygments import highlight from shutil import rmtree def is_docx(path): base = os.path.basename(path) return path.endswith('.docx') and not base.startswith('~') xml_lexer = XmlLexer() html_formatter = HtmlFormatter(style='igor') error_re = re.compile(r'errors="(\d+)"') class EventHandler(FileSystemEventHandler): def __init__(self, in_path, out_path): self.in_path = in_path self.out_path = out_path def make_index(self, src_path): pyg_path = self.pygments_path(src_path) children = os.listdir(pyg_path) html = '<ul>' for child in children:
def print_xml(xml_str: str): formatter = HtmlFormatter() IPython.display.display( HTML('<style type="text/css">{}</style> {}'.format( formatter.get_style_defs('.highlight'), highlight(xml_str, XmlLexer(), formatter))))
idx += 1 else: # inside comment if i.int_type == IntervalType.general and not i.name and i.srepr == '-->': current_comment += i.srepr inside_comment = False yield XmlInterval(IntervalType.comment, current_comment_offs, current_comment, None, idx) idx += 1 current_comment_offs = 0 else: current_comment += i.srepr # just for testing purposes, TODO: remove it completely if __name__ == '__main__': lexer = XmlLexer() s = '' # with open("tmpFile4CloneFinding.txt", encoding='utf-8') as f: s = f.read() # s = '<tag><![CDATA[cdata with <tag></tag> here]]></tag>' # s = '<![CDATA[<t cdata with <tag></tag> here]]></tag>' # s = '''very broken]> ]]><![CDATA[<CDATA example''' s = '''<programlisting language="xml"><![CDATA[<bean class="org.springframework.amqp.rabbit.core.RabbitTemplate"> <property name="connectionFactory" ref="rabbitConnectionFactory"/> <property name="messageConverter"> <bean class="org.springframework.amqp.support.''' ints = lex(s) with open("out.txt", 'w+', encoding='utf-8') as outf: for t in ints:
def highlight_xml(xml_str): # Highlights a string containing XML, using terminal color codes return highlight(xml_str, XmlLexer(), TerminalFormatter())