From 045c4cb82ff84d46f0f5f76f8cbfd01ed777ec85 Mon Sep 17 00:00:00 2001 From: anjali411 Date: Sun, 15 Aug 2021 06:22:53 -0700 Subject: [PATCH] Add copy button to code snippets in docs (#63149) Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/63149 Test Plan: Imported from OSS Reviewed By: navahgar, albanD Differential Revision: D30308891 Pulled By: anjali411 fbshipit-source-id: ad51180ab2f27c4525682b2603bbf753bb8f1ce9 --- docs/cpp/source/conf.py | 2 +- docs/requirements.txt | 1 + docs/source/_static/css/copybutton.css | 81 ++++++++++++++++++++++++++++++++++ docs/source/conf.py | 6 ++- 4 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 docs/source/_static/css/copybutton.css diff --git a/docs/cpp/source/conf.py b/docs/cpp/source/conf.py index 094a6f8..3bc56ed 100644 --- a/docs/cpp/source/conf.py +++ b/docs/cpp/source/conf.py @@ -25,7 +25,7 @@ import textwrap # If your documentation needs a minimal Sphinx version, state it here. # -needs_sphinx = '1.6' +needs_sphinx = '3.1.2' # Add any Sphinx extension module names here, as strings. They can be # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom diff --git a/docs/requirements.txt b/docs/requirements.txt index 70951ca..34ec607 100644 --- a/docs/requirements.txt +++ b/docs/requirements.txt @@ -6,3 +6,4 @@ matplotlib tensorboard # required to build torch.distributed.elastic.rendezvous.etcd* docs python-etcd>=0.4.5 +sphinx_copybutton diff --git a/docs/source/_static/css/copybutton.css b/docs/source/_static/css/copybutton.css new file mode 100644 index 0000000..bc084b0 --- /dev/null +++ b/docs/source/_static/css/copybutton.css @@ -0,0 +1,81 @@ +/* Copy buttons */ +button.copybtn { + position: absolute; + display: flex; + top: .3em; + right: .5em; + width: 1.7em; + height: 1.7em; + opacity: 1; + transition: opacity 0.3s, border .3s, background-color .3s; + user-select: none; + padding: 0; + border: none; + outline: none; + border-radius: 0.4em; + border: #e1e1e1 1px solid; + background-color: rgb(245, 245, 245); +} + +button.copybtn.success { + border-color: #22863a; +} + +button.copybtn img { + width: 100%; + padding: .2em; +} + +div.highlight { + position: relative; +} + +.highlight button.copybtn { + opacity: 1; +} + +.highlight button.copybtn:hover { + background-color: rgb(235, 235, 235); +} + +.highlight button.copybtn:active { + background-color: rgb(187, 187, 187); +} + +/** + * A minimal CSS-only tooltip copied from: + * https://codepen.io/mildrenben/pen/rVBrpK + * + * To use, write HTML like the following: + * + *

Short

+ */ + .o-tooltip--left { + position: relative; + } + + .o-tooltip--left:after { + opacity: 1; + visibility: visible; + position: absolute; + content: attr(data-tooltip); + padding: .2em; + font-size: .8em; + left: -.2em; + background: grey; + color: white; + white-space: nowrap; + z-index: 2; + border-radius: 2px; + transform: translateX(-102%) translateY(0); + transition: opacity 0.2s cubic-bezier(0.64, 0.09, 0.08, 1), transform 0.2s cubic-bezier(0.64, 0.09, 0.08, 1); +} + +.o-tooltip--left:hover:after { + display: block; + opacity: 1; + visibility: visible; + transform: translateX(-100%) translateY(0); + transition: opacity 0.2s cubic-bezier(0.64, 0.09, 0.08, 1), transform 0.2s cubic-bezier(0.64, 0.09, 0.08, 1); + transition-delay: .5s; +} diff --git a/docs/source/conf.py b/docs/source/conf.py index 9fe249a..f65c87f 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -38,7 +38,7 @@ import pytorch_sphinx_theme # If your documentation needs a minimal Sphinx version, state it here. # -needs_sphinx = '1.6' +needs_sphinx = '3.1.2' # Add any Sphinx extension module names here, as strings. They can be # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom @@ -54,6 +54,7 @@ extensions = [ 'sphinx.ext.viewcode', 'sphinxcontrib.katex', 'sphinx.ext.autosectionlabel', + 'sphinx_copybutton', ] # build the templated autosummary files @@ -421,3 +422,6 @@ def patched_make_field(self, types, domain, items, **kw): return nodes.field('', fieldname, fieldbody) TypedField.make_field = patched_make_field + +copybutton_prompt_text = r'>>> |\.\.\. ' +copybutton_prompt_is_regexp = True -- 2.7.4