Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds possibility to configure lexer via YAML config file. #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions rstblog/modules/pygments.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,26 @@ class CodeBlock(Directive):
required_arguments = 1
optional_arguments = 0
final_argument_whitespace = False
lexers_config = {}

def run(self):
try:
lexer = get_lexer_by_name(self.arguments[0])
self.configure_lexer(lexer)
except ValueError:
lexer = TextLexer()
code = u'\n'.join(self.content)
formatted = highlight(code, lexer, html_formatter)
return [nodes.raw('', formatted, format='html')]

def configure_lexer(self, lexer):
if lexer.name not in self.lexers_config:
return

config = self.lexers_config[lexer.name]
for kv in config:
property = kv.keys()[0]
setattr(lexer, property, kv[property])

def inject_stylesheet(context, **kwargs):
context.add_stylesheet('_pygments.css')
Expand All @@ -51,6 +61,12 @@ def write_stylesheet(builder, **kwargs):

def setup(builder):
global html_formatter

lexers_config = builder.config.root_get('modules.pygments.lexers', {})
for config in lexers_config:
language = config.keys()[0];
CodeBlock.lexers_config[language] = config[language]

style = get_style_by_name(builder.config.root_get('modules.pygments.style'))
html_formatter = HtmlFormatter(style=style)
directives.register_directive('code-block', CodeBlock)
Expand Down