Skip to content
This repository was archived by the owner on Oct 22, 2024. It is now read-only.

Commit 313419b

Browse files
Updates to docsite:
1. Enable copy button in code blocks. 2. Add "ShellSession" to lexer that extends "console" lexer. Signed-off-by: Kevin Putnam <[email protected]>
1 parent cb9b36b commit 313419b

File tree

8 files changed

+174
-10
lines changed

8 files changed

+174
-10
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ GEN_DOCS = $(SPHINXBUILD) -M html "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O
241241
( ! [ "$$GITHUB_SHA" ] || ! [ "$$GITHUB_REPOSITORY" ] || \
242242
find $(BUILDDIR)/html/ -name '*.html' | \
243243
xargs sed -i -e "s;github.com/intel/pmem-csi/\\(deploy/\\S*\\);github.com/$$GITHUB_REPOSITORY/\\1?ref=$$GITHUB_SHA;g" ) && \
244-
cp docs/html/index.html $(BUILDDIR)/html/index.html
244+
cp docs/html/index.html $(BUILDDIR)/html/index.html && cp docs/js/copybutton.js $(BUILDDIR)/html/_static/copybutton.js
245245
vhtml: _work/venv/.stamp
246246
. _work/venv/bin/activate && $(GEN_DOCS)
247247
html:

conf.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
],
1515
"extensions": [
1616
"recommonmark",
17-
"sphinx_markdown_tables"
17+
"sphinx_markdown_tables",
18+
"sphinx_copybutton"
1819
],
20+
"copybutton_prompt_text": "$ ",
1921
"html_theme": "sphinx_rtd_theme",
2022
"project": "PMEM-CSI",
2123
"templates_path": [

conf.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@
44
from os.path import isdir, isfile, join, basename, dirname
55
from os import makedirs, getenv
66
from shutil import copyfile
7+
#support for modified code block
8+
from pygments.lexers.shell import BashSessionLexer
9+
from sphinx.highlighting import lexers
10+
11+
#############
12+
#
13+
# Add a special lexer to add a class to console lexer
14+
#
15+
#############
16+
17+
class copyAllConsole (BashSessionLexer):
18+
name = 'ShellSession'
19+
20+
lexers['ShellSession'] = copyAllConsole(startinLine=True)
721

822
##############################################################################
923
#

docs/js/copybutton.js

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
// introduces special behavior for ShellSession
2+
3+
// Localization support
4+
const messages = {
5+
'en': {
6+
'copy': 'Copy',
7+
'copy_to_clipboard': 'Copy to clipboard',
8+
'copy_success': 'Copied!',
9+
'copy_failure': 'Failed to copy',
10+
},
11+
'es' : {
12+
'copy': 'Copiar',
13+
'copy_to_clipboard': 'Copiar al portapapeles',
14+
'copy_success': '¡Copiado!',
15+
'copy_failure': 'Error al copiar',
16+
},
17+
'de' : {
18+
'copy': 'Kopieren',
19+
'copy_to_clipboard': 'In die Zwischenablage kopieren',
20+
'copy_success': 'Kopiert!',
21+
'copy_failure': 'Fehler beim Kopieren',
22+
}
23+
}
24+
25+
let locale = 'en'
26+
if( document.documentElement.lang !== undefined
27+
&& messages[document.documentElement.lang] !== undefined ) {
28+
locale = document.documentElement.lang
29+
}
30+
31+
/**
32+
* Set up copy/paste for code blocks
33+
*/
34+
35+
const runWhenDOMLoaded = cb => {
36+
if (document.readyState != 'loading') {
37+
cb()
38+
} else if (document.addEventListener) {
39+
document.addEventListener('DOMContentLoaded', cb)
40+
} else {
41+
document.attachEvent('onreadystatechange', function() {
42+
if (document.readyState == 'complete') cb()
43+
})
44+
}
45+
}
46+
47+
const codeCellId = index => `codecell${index}`
48+
49+
// Clears selected text since ClipboardJS will select the text when copying
50+
const clearSelection = () => {
51+
if (window.getSelection) {
52+
window.getSelection().removeAllRanges()
53+
} else if (document.selection) {
54+
document.selection.empty()
55+
}
56+
}
57+
58+
// Changes tooltip text for two seconds, then changes it back
59+
const temporarilyChangeTooltip = (el, newText) => {
60+
const oldText = el.getAttribute('data-tooltip')
61+
el.setAttribute('data-tooltip', newText)
62+
setTimeout(() => el.setAttribute('data-tooltip', oldText), 2000)
63+
}
64+
65+
// Callback when a copy button is clicked. Will be passed the node that was clicked
66+
// should then grab the text and replace pieces of text that shouldn't be used in output
67+
var copyTargetText = (trigger) => {
68+
var target = document.querySelector(trigger.attributes['data-clipboard-target'].value);
69+
var textContent = target.innerText.split('\n');
70+
var copybuttonPromptText = '$ '; // Inserted from config
71+
var onlyCopyPromptLines = true; // Inserted from config
72+
var removePrompts = true; // Inserted from config
73+
74+
grandParent = target.parentElement.parentElement;
75+
blockType = grandParent.classList;
76+
if (blockType[0].includes("ShellSession")) {
77+
onlyCopyPromptLines = false;
78+
}
79+
80+
// Text content line filtering based on prompts (if a prompt text is given)
81+
if (copybuttonPromptText.length > 0) {
82+
// If only copying prompt lines, remove all lines that don't start w/ prompt
83+
if (onlyCopyPromptLines) {
84+
linesWithPrompt = textContent.filter((line) => {
85+
return line.startsWith(copybuttonPromptText) || (line.length == 0); // Keep newlines
86+
});
87+
// Check to make sure we have at least one non-empty line
88+
var nonEmptyLines = linesWithPrompt.filter((line) => {return line.length > 0});
89+
// If we detected lines w/ prompt, then overwrite textContent w/ those lines
90+
if ((linesWithPrompt.length > 0) && (nonEmptyLines.length > 0)) {
91+
textContent = linesWithPrompt;
92+
}
93+
}
94+
// Remove the starting prompt from any remaining lines
95+
if (removePrompts) {
96+
textContent.forEach((line, index) => {
97+
if (line.startsWith(copybuttonPromptText)) {
98+
textContent[index] = line.slice(copybuttonPromptText.length);
99+
}
100+
});
101+
}
102+
}
103+
textContent = textContent.join('\n');
104+
// Remove a trailing newline to avoid auto-running when pasting
105+
if (textContent.endsWith("\n")) {
106+
textContent = textContent.slice(0, -1)
107+
}
108+
return textContent
109+
}
110+
111+
const addCopyButtonToCodeCells = () => {
112+
// If ClipboardJS hasn't loaded, wait a bit and try again. This
113+
// happens because we load ClipboardJS asynchronously.
114+
if (window.ClipboardJS === undefined) {
115+
setTimeout(addCopyButtonToCodeCells, 250)
116+
return
117+
}
118+
119+
// Add copybuttons to all of our code cells
120+
const codeCells = document.querySelectorAll('div.highlight pre')
121+
codeCells.forEach((codeCell, index) => {
122+
const id = codeCellId(index)
123+
codeCell.setAttribute('id', id)
124+
const pre_bg = getComputedStyle(codeCell).backgroundColor;
125+
126+
const clipboardButton = id =>
127+
`<a class="copybtn o-tooltip--left" style="background-color: ${pre_bg}" data-tooltip="${messages[locale]['copy']}" data-clipboard-target="#${id}">
128+
<img src="${DOCUMENTATION_OPTIONS.URL_ROOT}_static/copy-button.svg" alt="${messages[locale]['copy_to_clipboard']}">
129+
</a>`
130+
codeCell.insertAdjacentHTML('afterend', clipboardButton(id))
131+
})
132+
133+
// Initialize with a callback so we can modify the text before copy
134+
const clipboard = new ClipboardJS('.copybtn', {text: copyTargetText})
135+
136+
// Update UI with error/success messages
137+
clipboard.on('success', event => {
138+
clearSelection()
139+
temporarilyChangeTooltip(event.trigger, messages[locale]['copy_success'])
140+
})
141+
142+
clipboard.on('error', event => {
143+
temporarilyChangeTooltip(event.trigger, messages[locale]['copy_failure'])
144+
})
145+
}
146+
147+
runWhenDOMLoaded(addCopyButtonToCodeCells)

docs/requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
sphinx
22
sphinx_rtd_theme
33
recommonmark
4-
sphinx-markdown-tables
4+
sphinx-markdown-tables
5+
sphinx-copybutton

examples/gce.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ and check out the source code with `repo sync`.
3939

4040
Before proceeding, apply the following patch:
4141

42-
```sh
43-
cd src/overlays
44-
patch -p1 <<EOF
42+
```ShellSession
43+
$ cd src/overlays
44+
$ patch -p1 <<EOF
4545
commit 148e1095ba56fcf626d184fd2c427bd192e53a28
4646
Author: Patrick Ohly <[email protected]>
4747
Date: Fri Aug 2 10:46:30 2019 +0200
@@ -269,7 +269,7 @@ kubectl create -f https://raw.githubusercontent.com/intel/pmem-csi/v0.5.0/deploy
269269
It is expected that `my-csi-app-2` will never start because the COS
270270
kernel lacks support for xfs. But `my-csi-app-1` comes up:
271271

272-
```sh
272+
```console
273273
$ kubectl get pv
274274
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
275275
pvc-0c2ebc68-cd77-4c08-9fbb-f8a5d33440b9 4Gi RWO Delete Bound default/pmem-csi-pvc-ext4 pmem-csi-sc-ext4 2m52s

make.bat

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ goto end
3232

3333
:html
3434
%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS%
35-
python.exe .\fix-refs.py
36-
copy index.html %BUILDDIR%\html\index.html
35+
copy docs\html\index.html %BUILDDIR%\html\index.html
36+
copy docs\js\copybutton.js %BUILDDIR%\html\_static\copybutton.js
3737
goto end
3838

3939
:help

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ platform = mylinux: linux
66
mywindows: win32
77
whitelist_externals = make.bat
88
/usr/bin/make
9-
deps = -rrequirements.txt
9+
deps = -rdocs/requirements.txt
1010
commands =
1111
mylinux: make {posargs}
1212
mywindows: make.bat {posargs}

0 commit comments

Comments
 (0)