diff options
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> | 2017-02-10 21:15:12 +0000 |
---|---|---|
committer | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> | 2017-02-10 21:15:12 +0000 |
commit | baf9c34aa202602fb402e16009781599539414c8 (patch) | |
tree | 770e3b333884ba1d74e23410637102443ce133a8 | |
parent | dd12a8ddd86518089194989de39f555cb03d04ff (diff) | |
download | poezio-baf9c34aa202602fb402e16009781599539414c8.tar.gz poezio-baf9c34aa202602fb402e16009781599539414c8.tar.bz2 poezio-baf9c34aa202602fb402e16009781599539414c8.tar.xz poezio-baf9c34aa202602fb402e16009781599539414c8.zip |
Add a code plugin, for sending highlighted snippets of code.
-rw-r--r-- | plugins/code.py | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/plugins/code.py b/plugins/code.py new file mode 100644 index 00000000..44afd3d8 --- /dev/null +++ b/plugins/code.py @@ -0,0 +1,44 @@ +""" +This plugin adds a :term:`/code` command, to send syntax highlighted snippets +of code using pygments and XHTML-IM (XEP-0071). + +Install +------- + +Either use your distribution tools to install python3-pygments or equivalent, +or run: + +.. code-block:: shell + + pip install --user pygments + +Usage +----- + +.. glossary:: + + /code <language> <snippet> + + Run this command to send the <snippet> of code, syntax highlighted + using pygments’s <language> lexer. +""" + +from plugin import BasePlugin + +from pygments import highlight +from pygments.lexers import get_lexer_by_name +from pygments.formatters import HtmlFormatter +FORMATTER = HtmlFormatter(noclasses=True) + +class Plugin(BasePlugin): + def init(self): + self.api.add_command('code', self.command_code, + usage='<message>', + short='Sends syntax-highlighted code', + help='Sends syntax-highlighted code in the current tab') + + def command_code(self, args): + language, code = args.split(None, 1) + lexer = get_lexer_by_name(language) + room = self.api.current_tab() + room.command_xhtml(highlight(code, lexer, FORMATTER).strip().replace('\n</pre>', '</pre>')) |