diff options
author | Julian Taylor <jtaylor.debian@googlemail.com> | 2014-01-25 19:40:09 +0100 |
---|---|---|
committer | Julian Taylor <jtaylor.debian@googlemail.com> | 2014-01-25 19:52:48 +0100 |
commit | e70bdcfe4eca9b0b3a0bc4ea867b31c02f87cd02 (patch) | |
tree | f28ee3c44158a787497e2a98a40b39db09819125 | |
parent | 65c59fe6a516e23db50ecb8ca160b9f9de12dc11 (diff) | |
download | scipy-sphinx-theme-e70bdcfe4eca9b0b3a0bc4ea867b31c02f87cd02.tar.gz |
add copybutton from cpython sources
allows toggling the input markers in source examples to allow
copy & pasting them.
-rw-r--r-- | _theme/scipy/layout.html | 5 | ||||
-rw-r--r-- | _theme/scipy/static/js/copybutton.js | 60 |
2 files changed, 63 insertions, 2 deletions
diff --git a/_theme/scipy/layout.html b/_theme/scipy/layout.html index 39fc75b..b449c2e 100644 --- a/_theme/scipy/layout.html +++ b/_theme/scipy/layout.html @@ -96,11 +96,12 @@ HAS_SOURCE: {{ has_source|lower }} }; </script> - <script type="text/javascript" src="{{ pathto('_static/js/jquery.min.js', 1) }}"></script> - <script type="text/javascript" src="{{ pathto('_static/js/bootstrap.min.js', 1) }}"></script> {%- for scriptfile in script_files %} <script type="text/javascript" src="{{ pathto(scriptfile, 1) }}"></script> {%- endfor %} + <script type="text/javascript" src="{{ pathto('_static/js/jquery.min.js', 1) }}"></script> + <script type="text/javascript" src="{{ pathto('_static/js/bootstrap.min.js', 1) }}"></script> + <script type="text/javascript" src="{{ pathto('_static/js/copybutton.js', 1) }}"></script> {%- endmacro %} {%- macro css() %} diff --git a/_theme/scipy/static/js/copybutton.js b/_theme/scipy/static/js/copybutton.js new file mode 100644 index 0000000..ace6922 --- /dev/null +++ b/_theme/scipy/static/js/copybutton.js @@ -0,0 +1,60 @@ +// Copyright 2014 PSF. Licensed under the PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2 +// File originates from the cpython source found in Doc/tools/sphinxext/static/copybutton.js + +$(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 output'; + var show_text = 'Show the prompts and output'; + 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', + 'border-radius': '0 3px 0 0' + } + + // 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">>>></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); + }); +}); + |