summaryrefslogtreecommitdiffstats
path: root/Doc
diff options
context:
space:
mode:
authorEzio Melotti <ezio.melotti@gmail.com>2011-10-30 07:19:33 (GMT)
committerEzio Melotti <ezio.melotti@gmail.com>2011-10-30 07:19:33 (GMT)
commit59dba38166160163ce4ac473933ab1c74b6222de (patch)
tree9267f2dd9aa5cd8d7c205528056f717a545ecf2f /Doc
parentef4e2fa1714cbc9d0dfda648769ba5c98235ec4d (diff)
downloadcpython-59dba38166160163ce4ac473933ab1c74b6222de.zip
cpython-59dba38166160163ce4ac473933ab1c74b6222de.tar.gz
cpython-59dba38166160163ce4ac473933ab1c74b6222de.tar.bz2
Add a button to the code examples in the doc to show/hide the prompts and output.
Diffstat (limited to 'Doc')
-rw-r--r--Doc/tools/sphinxext/layout.html1
-rw-r--r--Doc/tools/sphinxext/static/copybutton.js56
2 files changed, 57 insertions, 0 deletions
diff --git a/Doc/tools/sphinxext/layout.html b/Doc/tools/sphinxext/layout.html
index a256181..8659ef0 100644
--- a/Doc/tools/sphinxext/layout.html
+++ b/Doc/tools/sphinxext/layout.html
@@ -6,6 +6,7 @@
{% endblock %}
{% block extrahead %}
<link rel="shortcut icon" type="image/png" href="{{ pathto('_static/py.png', 1) }}" />
+ <script type="text/javascript" src="{{ pathto('_static/copybutton.js', 1) }}"></script>
{{ super() }}
{% endblock %}
{% block footer %}
diff --git a/Doc/tools/sphinxext/static/copybutton.js b/Doc/tools/sphinxext/static/copybutton.js
new file mode 100644
index 0000000..91f8763
--- /dev/null
+++ b/Doc/tools/sphinxext/static/copybutton.js
@@ -0,0 +1,56 @@
+$(document).ready(function() {
+ /* Add a [>>>] button on the top-right corner of code samples to hide
+ * the >>> and ... prompts and the output and thus make the code
+ * copyable. */
+ var div = $('.highlight-python .highlight,' +
+ '.highlight-python3 .highlight')
+ var pre = div.find('pre');
+
+ // get the styles from the current theme
+ pre.parent().parent().css('position', 'relative');
+ var hide_text = 'Hide the prompts and ouput';
+ var show_text = 'Show the prompts and ouput';
+ var border_width = pre.css('border-top-width');
+ var border_style = pre.css('border-top-style');
+ var border_color = pre.css('border-top-color');
+ var button_styles = {
+ 'cursor':'pointer', 'position': 'absolute', 'top': '0', 'right': '0',
+ 'border-color': border_color, 'border-style': border_style,
+ 'border-width': border_width, 'color': border_color, 'text-size': '75%',
+ 'font-family': 'monospace', 'padding-left': '0.2em', 'padding-right': '0.2em'
+ }
+
+ // create and add the button to all the code blocks that contain >>>
+ div.each(function(index) {
+ var jthis = $(this);
+ if (jthis.find('.gp').length > 0) {
+ var button = $('<span class="copybutton">&gt;&gt;&gt;</span>');
+ button.css(button_styles)
+ button.attr('title', hide_text);
+ jthis.prepend(button);
+ }
+ // tracebacks (.gt) contain bare text elements that need to be
+ // wrapped in a span to work with .nextUntil() (see later)
+ jthis.find('pre:has(.gt)').contents().filter(function() {
+ return ((this.nodeType == 3) && (this.data.trim().length > 0));
+ }).wrap('<span>');
+ });
+
+ // define the behavior of the button when it's clicked
+ $('.copybutton').toggle(
+ function() {
+ var button = $(this);
+ button.parent().find('.go, .gp, .gt').hide();
+ button.next('pre').find('.gt').nextUntil('.gp, .go').css('visibility', 'hidden');
+ button.css('text-decoration', 'line-through');
+ button.attr('title', show_text);
+ },
+ function() {
+ var button = $(this);
+ button.parent().find('.go, .gp, .gt').show();
+ button.next('pre').find('.gt').nextUntil('.gp, .go').css('visibility', 'visible');
+ button.css('text-decoration', 'none');
+ button.attr('title', hide_text);
+ });
+});
+