2023-10-10 00:06:31 +02:00
|
|
|
#!/usr/bin/python
|
2023-10-10 11:50:20 +02:00
|
|
|
import os
|
2023-10-10 12:33:45 +02:00
|
|
|
import tempfile
|
2023-10-10 11:50:20 +02:00
|
|
|
import pyperclip
|
2023-10-10 00:06:31 +02:00
|
|
|
from pygments import highlight
|
|
|
|
from pygments.lexers import get_lexer_by_name
|
|
|
|
from pygments.formatters import ImageFormatter
|
|
|
|
from pygments.formatters.img import PilNotAvailable
|
|
|
|
|
2023-10-10 11:50:20 +02:00
|
|
|
# Definir lenguaje (sintaxis)
|
2023-10-10 00:06:31 +02:00
|
|
|
lenguaje = get_lexer_by_name("python", stripall=True)
|
|
|
|
|
2023-10-10 11:50:20 +02:00
|
|
|
# Recoger el codigo del Portapapeles
|
|
|
|
codigo = pyperclip.paste()
|
2023-10-10 00:06:31 +02:00
|
|
|
|
2023-10-10 12:33:45 +02:00
|
|
|
# fichero temporal para nombre unico y no sobreescribir
|
|
|
|
fichero = tempfile.NamedTemporaryFile()
|
|
|
|
fichero.name += '.png'
|
|
|
|
|
2023-10-10 11:50:20 +02:00
|
|
|
# Generar la imagen
|
2023-10-10 12:33:45 +02:00
|
|
|
img = highlight(codigo, lenguaje, ImageFormatter(style='native'), outfile=fichero.name)
|
2023-10-10 00:06:31 +02:00
|
|
|
|
2023-10-10 11:50:20 +02:00
|
|
|
# Abrir la imagen generada
|
2023-10-10 12:33:45 +02:00
|
|
|
os.system('open ' + fichero.name)
|