From fd0b4a8af5b97ee24951735e8e821ca7ad586b29 Mon Sep 17 00:00:00 2001 From: Liz Date: Tue, 6 Dec 2022 00:04:53 -0700 Subject: [PATCH 1/2] Ensure default `.highlight` is used in CSS styles --- lektor_markdown_highlighter.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lektor_markdown_highlighter.py b/lektor_markdown_highlighter.py index 3876b84..22e6528 100644 --- a/lektor_markdown_highlighter.py +++ b/lektor_markdown_highlighter.py @@ -13,7 +13,7 @@ class MarkdownHighlighterPlugin(Plugin): description = 'Lektor plugin that adds syntax highlighting for markdown blocks with Pygments.' def get_formatter(self): - return HtmlFormatter(style=self.get_style(), cssclass="hll") + return HtmlFormatter(style=self.get_style()) def get_style(self): return self.get_config().get('pygments.style', 'default') @@ -38,7 +38,7 @@ def get_pygments_stylesheet(artifact_name='/static/pygments.css'): self.config_filename]) def build_stylesheet(artifact): with artifact.open('w') as f: - f.write(self.get_formatter().get_style_defs()) + f.write(self.get_formatter().get_style_defs(".highlight")) return artifact_name def pygmentize(text, lang): From 80c1f84b66f0caa93aeebb2879d86b1f7c2ee0ff Mon Sep 17 00:00:00 2001 From: Liz Date: Tue, 6 Dec 2022 00:22:36 -0700 Subject: [PATCH 2/2] Add `cssclass` config option and documentation --- README.md | 17 ++++++++++++++--- lektor_markdown_highlighter.py | 7 +++++-- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 601a464..cbe96c9 100644 --- a/README.md +++ b/README.md @@ -15,15 +15,26 @@ lektor plugins add markdown-highlighter The plugin has a config file that is used to configure a few things for Pygments. Just create a file named `markdown-highlighter.ini` into your -`configs/` folder. Currently only `pygments.style` is used: +`configs/` folder. + +You can use `pygments.style` to select any of the built-in Pygments styles: ```ini [pygments] style = tango ``` -You can use this to select any of the built-in Pygments styles. Support for -custom styles will come in the future. +Support for custom styles will come in the future. + +You can also use `pygments.cssclass` to apply a custom CSS class +to the generated html and CSS: + +```ini +[pygments] +cssclass = mycode +``` + +By default the plugin will use `highlight` for the CSS class. The config file is considered the "source" for the Pygments stylesheet, so you must create the configuration file (it can be empty) or Lektor's build will prune `pygments.css`. diff --git a/lektor_markdown_highlighter.py b/lektor_markdown_highlighter.py index 22e6528..3fdeee6 100644 --- a/lektor_markdown_highlighter.py +++ b/lektor_markdown_highlighter.py @@ -12,8 +12,11 @@ class MarkdownHighlighterPlugin(Plugin): name = 'Markdown Highlighter' description = 'Lektor plugin that adds syntax highlighting for markdown blocks with Pygments.' + def get_cssclass(self): + return self.get_config().get('pygments.cssclass', 'highlight') + def get_formatter(self): - return HtmlFormatter(style=self.get_style()) + return HtmlFormatter(style=self.get_style(), cssclass=self.get_cssclass()) def get_style(self): return self.get_config().get('pygments.style', 'default') @@ -38,7 +41,7 @@ def get_pygments_stylesheet(artifact_name='/static/pygments.css'): self.config_filename]) def build_stylesheet(artifact): with artifact.open('w') as f: - f.write(self.get_formatter().get_style_defs(".highlight")) + f.write(self.get_formatter().get_style_defs(f'.{self.get_cssclass()}')) return artifact_name def pygmentize(text, lang):