2017-02-10 21:15:12 +00:00
|
|
|
|
"""
|
|
|
|
|
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.
|
|
|
|
|
"""
|
|
|
|
|
|
2018-08-08 22:56:28 +00:00
|
|
|
|
from poezio.plugin import BasePlugin
|
2017-02-10 21:15:12 +00:00
|
|
|
|
|
|
|
|
|
from pygments import highlight
|
|
|
|
|
from pygments.lexers import get_lexer_by_name
|
2018-08-15 11:24:11 +00:00
|
|
|
|
from pygments.formatters import HtmlFormatter #pylint: disable=no-name-in-module
|
2017-02-24 17:35:08 +00:00
|
|
|
|
FORMATTER = HtmlFormatter(nowrap=True, noclasses=True)
|
2017-02-10 21:15:12 +00:00
|
|
|
|
|
2018-08-15 11:13:17 +00:00
|
|
|
|
|
2017-02-10 21:15:12 +00:00
|
|
|
|
class Plugin(BasePlugin):
|
|
|
|
|
def init(self):
|
2018-08-15 11:13:17 +00:00
|
|
|
|
self.api.add_command(
|
|
|
|
|
'code',
|
|
|
|
|
self.command_code,
|
|
|
|
|
usage='<language> <code>',
|
|
|
|
|
short='Sends syntax-highlighted code',
|
|
|
|
|
help='Sends syntax-highlighted code in the current tab')
|
2017-02-10 21:15:12 +00:00
|
|
|
|
|
|
|
|
|
def command_code(self, args):
|
|
|
|
|
language, code = args.split(None, 1)
|
|
|
|
|
lexer = get_lexer_by_name(language)
|
2019-02-28 21:05:57 +00:00
|
|
|
|
tab = self.api.current_tab()
|
2017-02-24 17:35:08 +00:00
|
|
|
|
code = highlight(code, lexer, FORMATTER)
|
2019-02-28 21:05:57 +00:00
|
|
|
|
tab.command_xhtml('<pre><code class="language-%s">%s</code></pre>' % (language, code.rstrip('\n')))
|