how to ignore system prompt symbols in codeblock on copy?

Post here questions and problems related to editing and publishing DITA content.
galanohan
Posts: 115
Joined: Mon Jul 10, 2023 11:49 am

how to ignore system prompt symbols in codeblock on copy?

Post by galanohan »

hi,

some of the readers asked us if it's possible to set a rule in the publishing template or include some js script to ignore system prompt symbols like $ > or # in code block on copy.

lots of sample scripts were provided by coders without splitting code and execution result. we hope the readers could read the result right after the script so they get to know what the expected results are. but when they copy the code sample, and paste them to client, they need to remove the result before clicking the run button.

so I used a js script for such purpose:

document.addEventListener('copy', function(e) {
const selection = window.getSelection();
const text = selection.toString().replace(/[$#>]+/g, ''); // Remove $, #, > symbols

// Oxygen XML Editor WebHelp requires using the 'copy' event on document.body
const copyEvent = new ClipboardEvent('copy', { dataType: 'text/plain', data: text });
document.body.dispatchEvent(copyEvent);

e.preventDefault();
});


and call this js file from an xml file which was specified for webhelp.fragment.after.search.input parameter in the opt file.
<script src='./oncopy.js' defer></script>

and test the script, nothing happens.

is it possible to have a parameter for the webhelp publishing template so that when copying codeblock from pages system prompt symbols can be ignored?
cosmin_andrei
Posts: 138
Joined: Mon Jun 12, 2017 10:50 am

Re: how to ignore system prompt symbols in codeblock on copy?

Post by cosmin_andrei »

Hi,
I have added your feedback as a feature request to our issue tracking tool to be analyzed by our development team.
For now, your feedback it is the first use case reported by our users. In case it will be requested by other users, we are thinking about of adding a parameter.
Regards,
Cosmin
--
Cosmin Andrei
oXygen XML Editor and Author Support
galanohan
Posts: 115
Joined: Mon Jul 10, 2023 11:49 am

Re: how to ignore system prompt symbols in codeblock on copy?

Post by galanohan »

Hi Cosmin,

Copying and pasting sample code for a test run might be an important step for my software users, from SQL learners to other programming language beginners. Specifically, in topics like quickstart, deployment, installing, configuration, etc.

The very first time somebody told me about this feature was a programmer in my company who was developing a plugin that has some dependencies on NumPy (a package for scientific computing in Python). He said NumPy's documentation allows users to copy script only, those output was displayed in the codeblock. For example, https://numpy.org/doc/stable/user/quick ... an-example .

He was right. From a reader's perpective, when learning interpreted programming languages like Python of which the compiler interprets each line and execute each line afterwards, lots of in-line output are generated. For writers, of course it's possible to split the codeblock for each script and separate the output in another paragraph or codeblock, but that could break the entire script and decrease the readability of code, even if in-line comments are given.

Their documentation was created over Sphinx 5.3.0 or earlier. With Sphinx, certain rules can be set for the copy event with javascript script. That's why I asked if it's possible to implement certain js script, specify the js script in the value of certain html fragment in opt, and get it running for every html page where there's a code block.
Costin
Posts: 829
Joined: Mon Dec 05, 2011 6:04 pm

Re: how to ignore system prompt symbols in codeblock on copy?

Post by Costin »

Hi galanohan,

This could be achieved by my manually editing of the DITA sources. Please follow the steps I described below:
1 - surround the fragment you want to be skipped from selection with a <ph> element
2 - add the @outputclass="ignoreSelection" class to the <ph> element which you just inserted at the previous point
3 - contribute the following CSS rule in you template

Code: Select all

.ignoreSelection {
  user-select: none;
}
If you need to automate this and you have XSLT knowledge, you could also do that by developing an XSLT customization which would use an XSLT extension.

Regards,
Costin
Costin Sandoi
oXygen XML Editor and Author Support
galanohan
Posts: 115
Joined: Mon Jul 10, 2023 11:49 am

Re: how to ignore system prompt symbols in codeblock on copy?

Post by galanohan »

Thank you!

I'll give it a try.

Regards,
Galano
Post Reply