view piecrust/processing/pygments_style.py @ 1195:ae9387338db1 draft default tip

admin: add option to publish immediately
author Ludovic Chabant <ludovic@chabant.com>
date Fri, 30 Dec 2022 16:48:04 -0800
parents 727110ea112a
children
line wrap: on
line source

import yaml
from pygments.formatters import HtmlFormatter
from piecrust.processing.base import SimpleFileProcessor


class PygmentsStyleProcessor(SimpleFileProcessor):
    PROCESSOR_NAME = 'pygments_style'

    def __init__(self):
        super(PygmentsStyleProcessor, self).__init__({'pygstyle': 'css'})

    def _doProcess(self, in_path, out_path):
        with open(in_path, 'r') as fp:
            config = yaml.safe_load(fp)

        style_name = config.get('style', 'default')
        class_name = config.get('class', '.highlight')
        fmt = HtmlFormatter(style=style_name).get_style_defs(class_name)

        with open(out_path, 'w') as fp:
            fp.write(fmt)

        return True