From 1cd0b4d6ea1f231c7786a01ebf3852419a4b762a Mon Sep 17 00:00:00 2001 From: mathieui Date: Sun, 7 Dec 2014 20:50:24 +0100 Subject: Fix #2570 (add /filter_jid to XMLTab, and syntax highlighting) Also add /filter_from and /filter_to, and allow chaining filters. --- src/core/commands.py | 2 +- src/core/core.py | 2 +- src/core/handlers.py | 38 +++++++++++++++++++++++++++++++++++--- 3 files changed, 37 insertions(+), 5 deletions(-) (limited to 'src/core') diff --git a/src/core/commands.py b/src/core/commands.py index 715e213e..2777833b 100644 --- a/src/core/commands.py +++ b/src/core/commands.py @@ -994,11 +994,11 @@ def command_message(self, args): @command_args_parser.ignored def command_xml_tab(self): """/xml_tab""" - self.xml_tab = True xml_tab = self.focus_tab_named('XMLTab', tabs.XMLTab) if not xml_tab: tab = tabs.XMLTab() self.add_tab(tab, True) + self.xml_tab = tab @command_args_parser.quoted(1) def command_adhoc(self, args): diff --git a/src/core/core.py b/src/core/core.py index 54d48aa4..79d19087 100644 --- a/src/core/core.py +++ b/src/core/core.py @@ -90,7 +90,7 @@ class Core(object): self.tab_win = windows.GlobalInfoBar() # Whether the XML tab is opened - self.xml_tab = False + self.xml_tab = None self.xml_buffer = TextBuffer() self.tabs = [] diff --git a/src/core/handlers.py b/src/core/handlers.py index dc6238d5..e0c89abf 100644 --- a/src/core/handlers.py +++ b/src/core/handlers.py @@ -17,7 +17,8 @@ from os import path from slixmpp import InvalidJID from slixmpp.stanza import Message -from slixmpp.xmlstream.stanzabase import StanzaBase +from slixmpp.xmlstream.stanzabase import StanzaBase, ElementBase +from xml.etree import ElementTree as ET import bookmark import common @@ -37,6 +38,18 @@ from theming import dump_tuple, get_theme from . commands import dumb_callback +try: + from pygments import highlight + from pygments.lexers import get_lexer_by_name + from pygments.formatters import HtmlFormatter + LEXER = get_lexer_by_name('xml') + FORMATTER = HtmlFormatter(noclasses=True) +except ImportError: + def highlight(text, *args, **kwargs): + return text + LEXER = None + FORMATTER = None + def on_session_start_features(self, _): """ Enable carbons & blocking on session start if wanted and possible @@ -1104,7 +1117,17 @@ def outgoing_stanza(self, stanza): We are sending a new stanza, write it in the xml buffer if needed. """ if self.xml_tab: - self.add_message_to_text_buffer(self.xml_buffer, '\x191}<--\x19o %s' % stanza) + xhtml_text = highlight('%s' % stanza, LEXER, FORMATTER) + poezio_colored = xhtml.xhtml_to_poezio_colors(xhtml_text, force=True) + self.add_message_to_text_buffer(self.xml_buffer, poezio_colored, + nickname=get_theme().CHAR_XML_OUT) + try: + if self.xml_tab.match_stanza(ElementBase(ET.fromstring(stanza))): + self.add_message_to_text_buffer(self.xml_tab.filtered_buffer, poezio_colored, + nickname=get_theme().CHAR_XML_OUT) + except: + log.debug('', exc_info=True) + if isinstance(self.current_tab(), tabs.XMLTab): self.current_tab().refresh() self.doupdate() @@ -1114,7 +1137,16 @@ def incoming_stanza(self, stanza): We are receiving a new stanza, write it in the xml buffer if needed. """ if self.xml_tab: - self.add_message_to_text_buffer(self.xml_buffer, '\x192}-->\x19o %s' % stanza) + xhtml_text = highlight('%s' % stanza, LEXER, FORMATTER) + poezio_colored = xhtml.xhtml_to_poezio_colors(xhtml_text, force=True) + self.add_message_to_text_buffer(self.xml_buffer, poezio_colored, + nickname=get_theme().CHAR_XML_IN) + try: + if self.xml_tab.match_stanza(stanza): + self.add_message_to_text_buffer(self.xml_tab.filtered_buffer, poezio_colored, + nickname=get_theme().CHAR_XML_IN) + except: + log.debug('', exc_info=True) if isinstance(self.current_tab(), tabs.XMLTab): self.current_tab().refresh() self.doupdate() -- cgit v1.2.3