Skip to content

Commit b63b555

Browse files
committed
Apply pygments CSS styles to rendered preview.
1 parent cec6120 commit b63b555

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

tkintermd/frame.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from markdown import Markdown
1313
from pygments import lex
1414
from pygments.lexers.markup import MarkdownLexer
15+
from pygments.formatters.html import HtmlFormatter
1516
from pygments.token import Generic
1617
from pygments.lexer import bygroups
1718
from pygments.styles import get_style_by_name, get_all_styles
@@ -389,10 +390,15 @@ def load_style(self, stylename):
389390
)
390391
# self.export_options_text_area.tag_configure(str(Generic.StrongEmph), font=('Monospace', 10, 'bold', 'italic'))
391392
self.syntax_highlighting_tags.append(str(Generic.StrongEmph))
392-
self.css = 'body {background-color: %s; color: %s}' % (
393-
self.style.background_color or 'white',
394-
self.text_area.tag_cget("Token.Text", "foreground") or 'black',
395-
)# used string%interpolation here because f'string' interpolation is too annoying with embeded { and }
393+
self.formatter = HtmlFormatter()
394+
self.pygments = self.formatter.get_style_defs(".highlight")
395+
# Previous version.
396+
self.css = 'body {background-color: %s; color: %s }\nbody .highlight{ background-color: %s; }\n%s' % (
397+
self.style.background_color,
398+
self.text_area.tag_cget("Token.Text", "foreground"),
399+
self.style.background_color,
400+
self.pygments
401+
)#used string%interpolation here because f'string' interpolation is too annoying with embeded { and }
396402
self.preview_document.add_css(self.css)
397403
return self.syntax_highlighting_tags
398404

0 commit comments

Comments
 (0)