aboutsummaryrefslogtreecommitdiffstats
path: root/nikola/plugins/compile/rest/listing.py
diff options
context:
space:
mode:
authorLibravatarUnit 193 <unit193@unit193.net>2022-08-05 01:00:44 -0400
committerLibravatarUnit 193 <unit193@unit193.net>2022-08-05 01:00:44 -0400
commitc61750def3e047296a3633247aec4637bd216633 (patch)
treecd7175c2f172e20255adb7d0ccf9a5fa405df175 /nikola/plugins/compile/rest/listing.py
parent9132854aa950a4c85c89cf7617d3240bd3fe1cbd (diff)
parent393aa58f2c5afd51f92fd9bd4b6dfd0dc90cea41 (diff)
Update upstream source from tag 'upstream/8.2.3'
Update to upstream version '8.2.3' with Debian dir 572430fed4befec44daff4153d2f83da363024d0
Diffstat (limited to 'nikola/plugins/compile/rest/listing.py')
-rw-r--r--nikola/plugins/compile/rest/listing.py33
1 files changed, 30 insertions, 3 deletions
diff --git a/nikola/plugins/compile/rest/listing.py b/nikola/plugins/compile/rest/listing.py
index f669b16..48dbe4c 100644
--- a/nikola/plugins/compile/rest/listing.py
+++ b/nikola/plugins/compile/rest/listing.py
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
-# Copyright © 2012-2021 Roberto Alsina and others.
+# Copyright © 2012-2022 Roberto Alsina and others.
# Permission is hereby granted, free of charge, to any
# person obtaining a copy of this software and associated
@@ -57,7 +57,8 @@ class CodeBlock(Directive):
'name': directives.unchanged,
'number-lines': directives.unchanged, # integer or None
'linenos': directives.unchanged,
- 'tab-width': directives.nonnegative_int}
+ 'tab-width': directives.nonnegative_int,
+ 'emphasize-lines': directives.unchanged_required}
has_content = True
def run(self):
@@ -103,7 +104,33 @@ class CodeBlock(Directive):
else:
anchor_ref = 'rest_code_' + uuid.uuid4().hex
- formatter = utils.NikolaPygmentsHTML(anchor_ref=anchor_ref, classes=classes, linenos=linenos, linenostart=linenostart)
+ linespec = self.options.get('emphasize-lines')
+ if linespec:
+ try:
+ nlines = len(self.content)
+ hl_lines = utils.parselinenos(linespec, nlines)
+ if any(i >= nlines for i in hl_lines):
+ raise self.error(
+ 'line number spec is out of range(1-%d): %r' %
+ (nlines, self.options['emphasize-lines'])
+ )
+ hl_lines = [x + 1 for x in hl_lines if x < nlines]
+ except ValueError as err:
+ raise self.error(err)
+ else:
+ hl_lines = None
+
+ extra_kwargs = {}
+ if hl_lines is not None:
+ extra_kwargs['hl_lines'] = hl_lines
+
+ formatter = utils.NikolaPygmentsHTML(
+ anchor_ref=anchor_ref,
+ classes=classes,
+ linenos=linenos,
+ linenostart=linenostart,
+ **extra_kwargs
+ )
out = pygments.highlight(code, lexer, formatter)
node = nodes.raw('', out, format='html')