Convert to CDATA/XML?

28 views
Skip to first unread message

@lbutlr

unread,
Jan 25, 2022, 8:42:25 PM1/25/22
to BBEdit Talk
Is there a way to easily convert a block of HTML text with entities in, like & into CDATA XML-compatible codes like &?

The only thing I see in the manual for CDATA is

> • Escape <CDATA>: This option controls whether BBEdit should convert <![CDATA[]]> sections to normal text.

--
If you have any young friends who aspire to become writers, the
second-greatest favor you can do them is to present them with
copies of The Elements of Style. The first-greatest, of course,
is to shoot them now, while they're happy. -Dorothy Parker

jj

unread,
Jan 26, 2022, 12:11:18 PM1/26/22
to BBEdit Talk
Hi @lbutlr,

One possible approach is with node.js and the node module html-entities.

Create a BBEdit Text Filter with this snippet into ~/Library/Application Support/BBEdit/Text\ Filters/html_entities_to_xml_entities.js

        #!/usr/bin/env node
        const html_entities = require('html-entities');
        process.stdin.setEncoding('utf8');
        process.stdin.on('data', function(text) {
          let decoded = html_entities.decode(text);
          let reencoded = html_entities.encode(decoded, {mode: 'nonAsciiPrintable', level: 'xml'});
          process.stdout.write(reencoded);
        })

In case node.js is not already installed on your system:

        % brew install node
       
In case the node html-entities module in not already installed on your system:

        % npm install -g html-entities

HTH

Jean Jourdain

@lbutlr

unread,
Jan 27, 2022, 4:13:30 PM1/27/22
to BBEdit Talk
On 2022 Jan 26, at 10:11, jj <goo...@mixio.com> wrote:
> One possible approach is with node.js and the node module html-entities.

Thanks, I was hoping for a simpler solution that didn't rely on something else to install and I will probably use htmlspecialchars() from php instead since that is currently in macOS (for now), and I am more comfortable with php anyway.

Thought there might be something in BBEdit (though I can use the text factory to call out to php).

--
"I've had a perfectly wonderful evening. But this wasn't it." -
Groucho Marx

Reply all
Reply to author
Forward
0 new messages