FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
ProdigiousPython/docs/_static/copybutton_funcs.js at develop · ProdigiousPython/ProdigiousPython · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
ProdigiousPython
/
ProdigiousPython
Public
Notifications
You must be signed in to change notification settings
Fork
6
Star
9
Code
Issues
3
Pull requests
5
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
ProdigiousPython
/
docs
/
_static
/
copybutton_funcs.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
73 lines (66 loc) · 2.63 KB
Breadcrumbs
ProdigiousPython
/
docs
/
_static
/
copybutton_funcs.js
Copy path
File metadata and controls
73 lines (66 loc) · 2.63 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
function
escapeRegExp
(
string
)
{
return
string
.
replace
(
/
[
.
*
+
?
^
$
{
}
(
)
|
[
\]
\\
]
/
g
,
'\\$&'
)
;
// $& means the whole matched string
}
/**
* Removes excluded text from a Node.
*
*
@param
{
Node
} target Node to filter.
*
@param
{
string
} exclude CSS selector of nodes to exclude.
*
@returns
{
DOMString
} Text from `target` with text removed.
*/
export
function
filterText
(
target
,
exclude
)
{
const
clone
=
target
.
cloneNode
(
true
)
;
// clone as to not modify the live DOM
if
(
exclude
)
{
// remove excluded nodes
clone
.
querySelectorAll
(
exclude
)
.
forEach
(
node
=>
node
.
remove
(
)
)
;
}
return
clone
.
innerText
;
}
// Callback when a copy button is clicked. Will be passed the node that was clicked
// should then grab the text and replace pieces of text that shouldn't be used in output
export
function
formatCopyText
(
textContent
,
copybuttonPromptText
,
isRegexp
=
false
,
onlyCopyPromptLines
=
true
,
removePrompts
=
true
,
copyEmptyLines
=
true
,
lineContinuationChar
=
""
,
hereDocDelim
=
""
)
{
var
regexp
;
var
match
;
// Do we check for line continuation characters and "HERE-documents"?
var
useLineCont
=
!
!
lineContinuationChar
var
useHereDoc
=
!
!
hereDocDelim
// create regexp to capture prompt and remaining line
if
(
isRegexp
)
{
regexp
=
new
RegExp
(
'^('
+
copybuttonPromptText
+
')(.*)'
)
}
else
{
regexp
=
new
RegExp
(
'^('
+
escapeRegExp
(
copybuttonPromptText
)
+
')(.*)'
)
}
const
outputLines
=
[
]
;
var
promptFound
=
false
;
var
gotLineCont
=
false
;
var
gotHereDoc
=
false
;
const
lineGotPrompt
=
[
]
;
for
(
const
line
of
textContent
.
split
(
'\n'
)
)
{
match
=
line
.
match
(
regexp
)
if
(
match
||
gotLineCont
||
gotHereDoc
)
{
promptFound
=
regexp
.
test
(
line
)
lineGotPrompt
.
push
(
promptFound
)
if
(
removePrompts
&&
promptFound
)
{
outputLines
.
push
(
match
[
2
]
)
}
else
{
outputLines
.
push
(
line
)
}
gotLineCont
=
line
.
endsWith
(
lineContinuationChar
)
&
useLineCont
if
(
line
.
includes
(
hereDocDelim
)
&
useHereDoc
)
gotHereDoc
=
!
gotHereDoc
}
else
if
(
!
onlyCopyPromptLines
)
{
outputLines
.
push
(
line
)
}
else
if
(
copyEmptyLines
&&
line
.
trim
(
)
===
''
)
{
outputLines
.
push
(
line
)
}
}
// If no lines with the prompt were found then just use original lines
if
(
lineGotPrompt
.
some
(
v
=>
v
===
true
)
)
{
textContent
=
outputLines
.
join
(
'\n'
)
;
}
// Remove a trailing newline to avoid auto-running when pasting
if
(
textContent
.
endsWith
(
"\n"
)
)
{
textContent
=
textContent
.
slice
(
0
,
-
1
)
}
return
textContent
}
Back
|
FazBrowse Home
|
New Git URL