diff --git a/sphinx_copybutton/_static/copybutton_funcs.js b/sphinx_copybutton/_static/copybutton_funcs.js index dbe1aaa..7be1b1a 100644 --- a/sphinx_copybutton/_static/copybutton_funcs.js +++ b/sphinx_copybutton/_static/copybutton_funcs.js @@ -42,8 +42,15 @@ export function formatCopyText(textContent, copybuttonPromptText, isRegexp = fal const lineGotPrompt = []; for (const line of textContent.split('\n')) { match = line.match(regexp) + if (!match) { + // Blank REPL lines are often ">>>" without the trailing space in ">>> ". + const padded = (line + ' ').match(regexp) + if (padded && padded[2] === '') { + match = padded + } + } if (match || gotLineCont || gotHereDoc) { - promptFound = regexp.test(line) + promptFound = !!match lineGotPrompt.push(promptFound) if (removePrompts && promptFound) { outputLines.push(match[2]) diff --git a/sphinx_copybutton/_static/test.js b/sphinx_copybutton/_static/test.js index f2158d5..a94e5b9 100644 --- a/sphinx_copybutton/_static/test.js +++ b/sphinx_copybutton/_static/test.js @@ -32,6 +32,31 @@ output removePrompts: true, expected: '\nfirst\nsecond' }, + { + description: 'with non-regexp python prompt and blank REPL line', + text: ` +>>> first +>>> +>>> second`, + prompt: '>>> ', + isRegexp: false, + onlyCopyPromptLines: true, + removePrompts: true, + expected: '\nfirst\n\nsecond' + }, + { + description: 'with regexp python prompt and blank REPL line', + text: ` +>>> first +>>> +>>> second`, + prompt: '>>> ', + isRegexp: true, + onlyCopyPromptLines: true, + removePrompts: true, + copyEmptyLines: true, + expected: '\nfirst\n\nsecond' + }, { description: 'with non-regexp console prompt', text: `