|
From: <mi...@us...> - 2021-03-19 00:23:36
|
Revision: 8636
http://sourceforge.net/p/docutils/code/8636
Author: milde
Date: 2021-03-19 00:23:33 +0000 (Fri, 19 Mar 2021)
Log Message:
-----------
HTML5: Place code-block line numbers in pseudo-elements.
This way, they are skipped when copying text from code blocks.
Solves feature request #32.
Modified Paths:
--------------
trunk/docutils/HISTORY.txt
trunk/docutils/docutils/writers/html5_polyglot/__init__.py
trunk/docutils/docutils/writers/html5_polyglot/minimal.css
trunk/docutils/docutils/writers/html5_polyglot/plain.css
trunk/docutils/docutils/writers/html5_polyglot/responsive.css
trunk/docutils/docutils/writers/html5_polyglot/tuftig.css
trunk/docutils/test/functional/expected/standalone_rst_html5.html
Modified: trunk/docutils/HISTORY.txt
===================================================================
--- trunk/docutils/HISTORY.txt 2021-03-19 00:23:19 UTC (rev 8635)
+++ trunk/docutils/HISTORY.txt 2021-03-19 00:23:33 UTC (rev 8636)
@@ -109,6 +109,10 @@
- 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
Modified: trunk/docutils/docutils/writers/html5_polyglot/__init__.py
===================================================================
--- trunk/docutils/docutils/writers/html5_polyglot/__init__.py 2021-03-19 00:23:19 UTC (rev 8635)
+++ trunk/docutils/docutils/writers/html5_polyglot/__init__.py 2021-03-19 00:23:33 UTC (rev 8636)
@@ -322,6 +322,13 @@
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] == '<code>':
+ self.body[-1] = '<code data-lineno="%s">'%node.astext()
+ else:
+ self.body.append('</code><code data-lineno="%s">'
+ % node.astext())
self.body.append(self.starttag(node, node.html5tagname, ''))
def depart_inline(self, node):
@@ -413,7 +420,7 @@
# (see responsive.css how this is used for sidebar navigation).
# TODO: use the new HTML5 element <aside>?
def visit_topic(self, node):
- if ('contents' in node['classes']
+ if ('contents' in node['classes']
and isinstance(node.parent, nodes.document)):
self.body_prefix[0] = '</head>\n<body class="with-toc">\n'
self.body.append(self.starttag(node, 'div', CLASS='topic'))
Modified: trunk/docutils/docutils/writers/html5_polyglot/minimal.css
===================================================================
--- trunk/docutils/docutils/writers/html5_polyglot/minimal.css 2021-03-19 00:23:19 UTC (rev 8635)
+++ trunk/docutils/docutils/writers/html5_polyglot/minimal.css 2021-03-19 00:23:33 UTC (rev 8636)
@@ -145,8 +145,8 @@
span.option { white-space: nowrap; }
/* Footnotes and Citations */
-dl.footnote.superscript > dd {margin-left: 1em; }
-dl.footnote.brackets > dd {margin-left: 2em; }
+dl.footnote.superscript > dd { margin-left: 1em; }
+dl.footnote.brackets > dd { margin-left: 2em; }
dl.footnote > dt { font-weight: normal; }
a.footnote-reference.brackets:before,
dt.label > span.brackets:before { content: "["; }
@@ -209,8 +209,14 @@
}
div.line-block { display: block; }
div.line-block div.line-block, pre { margin-left: 2em; }
-pre.code .ln { color: gray; } /* line numbers */
+/* Code line numbers: dropped when copying text from the page */
+pre.code .ln { display: none; }
+pre.code code:before {
+ content: attr(data-lineno); /* …, none) fallback not supported by any browser */
+ color: gray;
+}
+
/* Tables */
table {
border-collapse: collapse;
Modified: trunk/docutils/docutils/writers/html5_polyglot/plain.css
===================================================================
--- trunk/docutils/docutils/writers/html5_polyglot/plain.css 2021-03-19 00:23:19 UTC (rev 8635)
+++ trunk/docutils/docutils/writers/html5_polyglot/plain.css 2021-03-19 00:23:33 UTC (rev 8636)
@@ -241,7 +241,6 @@
/* Code */
pre.code { padding: 0.7ex }
pre.code, code { background-color: #eeeeee }
-pre.code .ln { color: gray; } /* line numbers */
/* basic highlighting: for a complete scheme, see */
/* http://docutils.sourceforge.net/sandbox/stylesheets/ */
pre.code .comment, code .comment { color: #5C6576 }
Modified: trunk/docutils/docutils/writers/html5_polyglot/responsive.css
===================================================================
--- trunk/docutils/docutils/writers/html5_polyglot/responsive.css 2021-03-19 00:23:19 UTC (rev 8635)
+++ trunk/docutils/docutils/writers/html5_polyglot/responsive.css 2021-03-19 00:23:33 UTC (rev 8636)
@@ -225,7 +225,6 @@
}
/* Literal and Code */
-pre.code .ln { color: gray; } /* line numbers */
/* basic highlighting: for a complete scheme, see */
/* http://docutils.sourceforge.net/sandbox/stylesheets/ */
pre.code .comment, code .comment { color: #5C6576 }
Modified: trunk/docutils/docutils/writers/html5_polyglot/tuftig.css
===================================================================
--- trunk/docutils/docutils/writers/html5_polyglot/tuftig.css 2021-03-19 00:23:19 UTC (rev 8635)
+++ trunk/docutils/docutils/writers/html5_polyglot/tuftig.css 2021-03-19 00:23:33 UTC (rev 8636)
@@ -299,7 +299,6 @@
/* font-size: 0.9em; */
overflow: auto;
}
-pre.code .ln { color: gray; } /* line numbers */
/* basic highlighting: for a complete scheme, see */
/* http://docutils.sourceforge.net/sandbox/stylesheets/ */
pre.code .comment, code .comment { color: #5C6576 }
Modified: trunk/docutils/test/functional/expected/standalone_rst_html5.html
===================================================================
--- trunk/docutils/test/functional/expected/standalone_rst_html5.html 2021-03-19 00:23:19 UTC (rev 8635)
+++ trunk/docutils/test/functional/expected/standalone_rst_html5.html 2021-03-19 00:23:33 UTC (rev 8636)
@@ -1005,9 +1005,9 @@
<pre class="code python literal-block"><code>print 'This is Python code.'</code></pre>
<p>The <span class="docutils literal"><span class="pre">:number-lines:</span></span> option (with optional start value) generates line
numbers:</p>
-<pre class="code python literal-block"><code><span class="ln"> 8 </span># print integers from 0 to 9:
-<span class="ln"> 9 </span>for i in range(10):
-<span class="ln">10 </span> print i</code></pre>
+<pre class="code python literal-block"><code data-lineno=" 8 "><span class="ln"> 8 </span># print integers from 0 to 9:
+</code><code data-lineno=" 9 "><span class="ln"> 9 </span>for i in range(10):
+</code><code data-lineno="10 "><span class="ln">10 </span> print i</code></pre>
<p>For inline code snippets, there is the <cite>code</cite> 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 @@
<code class="tex">\alpha = f(x)</code> prints <span class="formula"><i>α</i> = <i>f</i>(<i>x</i>)</span>.</p>
<p>The <span class="docutils literal">:code:</span> option of the <cite>include</cite> directive sets the included content
as a code block, here the rst file <span class="docutils literal">header_footer.txt</span> with line numbers:</p>
-<pre class="code rst literal-block"><code><span class="ln">1 </span>.. header:: Document header
-<span class="ln">2 </span>.. footer:: Document footer</code></pre>
+<pre class="code rst literal-block"><code data-lineno="1 "><span class="ln">1 </span>.. header:: Document header
+</code><code data-lineno="2 "><span class="ln">2 </span>.. footer:: Document footer</code></pre>
</section>
<section id="meta">
<h4><a class="toc-backref" href="#toc-entry-63"><span class="sectnum">2.14.11</span> Meta</a></h4>
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|