From c852422d880388644df6f8680da0ae2fc82c9fe8 Mon Sep 17 00:00:00 2001
From: milde
Date: Fri, 19 Mar 2021 00:23:33 +0000
Subject: [PATCH] HTML5: Place code-block line numbers in pseudo-elements.
This way, they are skipped when copying text from code blocks.
Solves feature request #32.
git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk@8636 929543f6-e4f2-0310-98a6-ba3bd3dd1d04
---
docutils/HISTORY.txt | 4 ++++
docutils/docutils/writers/html5_polyglot/__init__.py | 9 ++++++++-
docutils/docutils/writers/html5_polyglot/minimal.css | 12 +++++++++---
docutils/docutils/writers/html5_polyglot/plain.css | 1 -
docutils/docutils/writers/html5_polyglot/responsive.css | 1 -
docutils/docutils/writers/html5_polyglot/tuftig.css | 1 -
docutils/test/functional/expected/standalone_rst_html5.html | 10 +++++-----
7 files changed, 26 insertions(+), 12 deletions(-)
diff --git a/docutils/HISTORY.txt b/docutils/HISTORY.txt
index b861ef1da..de481c307 100644
--- a/docutils/HISTORY.txt
+++ b/docutils/HISTORY.txt
@@ -109,6 +109,10 @@ Version 0.17b1
- Add a `viewport meta tag`__ to fix rendering in mobile browsers.
__ https://developer.mozilla.org/en-US/docs/Web/HTML/Viewport_meta_tag
+
+ - Add code line numbers as "data-*" arguments.
+ This way, they may be skipped when copying text from code blocks
+ (see minimal.css). Solves feature request #32.
* docutils/writers/html5_polyglot/minimal.css
diff --git a/docutils/docutils/writers/html5_polyglot/__init__.py b/docutils/docutils/writers/html5_polyglot/__init__.py
index 42c411b43..3b9ff7503 100644
--- a/docutils/docutils/writers/html5_polyglot/__init__.py
+++ b/docutils/docutils/writers/html5_polyglot/__init__.py
@@ -322,6 +322,13 @@ class HTMLTranslator(writers._html_base.HTMLTranslator):
classes.remove(tags[0])
else:
node.html5tagname = 'span'
+ if (classes == ['ln'] and isinstance(node.parent, nodes.literal_block)
+ and 'code' in node.parent.get('classes')):
+ if self.body[-1] == '':
+ self.body[-1] = ''%node.astext()
+ else:
+ self.body.append(''
+ % node.astext())
self.body.append(self.starttag(node, node.html5tagname, ''))
def depart_inline(self, node):
@@ -413,7 +420,7 @@ class HTMLTranslator(writers._html_base.HTMLTranslator):
# (see responsive.css how this is used for sidebar navigation).
# TODO: use the new HTML5 element
print 'This is Python code.'
The :number-lines: option (with optional start value) generates line
numbers:
-
8 # print integers from 0 to 9:
- 9 for i in range(10):
-10 print i
+
8 # print integers from 0 to 9:
+ 9 for i in range(10):
+10 print i
For inline code snippets, there is the code role, which can be used
directly (the code will not be parsed/tagged, as the language is not known)
or as base for special code roles, e.g. the LaTeX code in the next
@@ -1016,8 +1016,8 @@ paragraph.
\alpha = f(x) prints α = f(x).
The :code: option of the include directive sets the included content
as a code block, here the rst file header_footer.txt with line numbers: