capybara/ext-syntax/syntax-render.py

18 lines
521 B
Python

import sys
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.util import ClassNotFound
from pygments.formatters import HtmlFormatter
formatter = HtmlFormatter(linenos="table", encoding="utf-8")
try:
lexer = get_lexer_by_name(sys.argv[1], encoding="utf-8")
except IndexError:
sys.stderr.write("must provide lexer name")
except:
sys.stderr.write(f"couldn't find lexer {sys.argv[1]}\n")
sys.stdout.write(highlight(sys.stdin.read(), lexer, formatter).decode("utf-8"))