Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#13 2017-12-13 12:30:03

etc
Developer
Registered: 2010-11-11
Posts: 5,192
Website GitHub

Re: Poll: useful 'escape' transforms

Speaking of <txp:evaluate />, we can extend the list of XPath 1.0 functions to any PHP string function judged safe to use, like str_replace. In this case using arguments would be straightforward:

<txp:evaluate query='str_replace("from", "to", <txp:body escape="quote" />)' />

Howzat?

Offline

#14 2017-12-13 12:34:01

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,448
Website GitHub

Re: Poll: useful 'escape' transforms

etc wrote #308290:

Howzat?

Magic!

EDIT: escape="quote" isn’t in your list in the OP. Can it be added, as it has value?

Last edited by Bloke (2017-12-13 12:36:52)


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#15 2017-12-13 14:21:31

etc
Developer
Registered: 2010-11-11
Posts: 5,192
Website GitHub

Re: Poll: useful 'escape' transforms

Bloke wrote #308291:

escape="quote" isn’t in your list in the OP. Can it be added, as it has value?

Yes, but I’m not sure for the name. What it currently does is transform a string in a XPath-readable format:

Hello, world! => 'Hello, world!' // ok, but
Hi, "m'dam"! => concat('Hi, "m', "'", 'dam"!')

I doubt it could be useful elsewhere than <txp:evaluate /> and have not found a more robust solution to escape quotes in XPath functions without transforming them into HTML entities (which we don’t want). Still investigating.

And while we are here, which functions to add to the pool?

Offline

#16 2017-12-13 15:22:36

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,448
Website GitHub

Re: Poll: useful 'escape' transforms

etc wrote #308295:

Yes, but I’m not sure for the name.

I was wondering that too. Tricky. ‘quote’ is the most logical choice, I suppose.

have not found a more robust solution to escape quotes in XPath functions

Can our txpspecialchars() help? That handles both double-quotes and apostrophes, but I dunno how clever it is if the string contains both.

which functions to add to the pool?

Can’t think of any others that don’t require parameters. Possibly some way to remove newlines but preserve spaces? Is that helpful, as distinct from trim? Probably not…

EDIT: or do you mean functions for use in <txp:evaluate>?

Last edited by Bloke (2017-12-13 15:23:49)


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#17 2017-12-13 15:53:42

etc
Developer
Registered: 2010-11-11
Posts: 5,192
Website GitHub

Re: Poll: useful 'escape' transforms

Bloke wrote #308299:

Can our txpspecialchars() help? That handles both double-quotes and apostrophes, but I dunno how clever it is if the string contains both.

No, it just replaces them with &#039; and &quot;, but this is not always desired. And escape="html" is already alias of txpspecialchars($thing).

do you mean functions for use in <txp:evaluate>?

Yes. We could even make it a user pref (and caveat utilitor), but then code copy-pasting would not always work (though I don’t care).

Offline

#18 2017-12-13 16:36:59

uli
Moderator
From: Cologne
Registered: 2006-08-15
Posts: 4,306

Re: Poll: useful 'escape' transforms

Bloke wrote #308289:

We do. :-)

Sigh! :]


In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links

Offline

#19 2017-12-14 00:42:34

phiw13
Plugin Author
From: Japan
Registered: 2004-02-27
Posts: 3,192
Website

Re: Poll: useful 'escape' transforms

escape="textile"

Do I understand correctly?

I have this in the caption field of an image: a _test_ image - something *bold*.

In the form used to insert images in the body of an article, I have this:

<figcaption itemprop="caption"><txp:image_info type="caption" escape="textile" /></figcaption>

but no transform happens, the string is outputted verbatim, with underscores and asterisks.

Edit: simple example, inserted in a page template:

<txp:images …… >
  <figure>
    <a href="<txp:image_url />" class="image-gallery-link">
      <txp:image />
    </a>
    <txp:image_info type="caption" escape="textile" wraptag="figcaption" />
  </figure>
</txp:images>

Last edited by phiw13 (2017-12-14 05:00:02)


Where is that emoji for a solar powered submarine when you need it ?
Sand space – admin theme for Textpattern

Offline

#20 2017-12-14 09:17:28

etc
Developer
Registered: 2010-11-11
Posts: 5,192
Website GitHub

Re: Poll: useful 'escape' transforms

phiw13 wrote #308304:

escape="textile"

Do I understand correctly?

Short answer: tags like <txp:image_info /> that already had escape attribute are not affected by the global escape, to avoid escaping twice. They must be modified individually to comply with globals.

Longer answer: I did modify some tags, but not this one, since it escapes only some parts of its output, unlike the global escape applied to the whole output, including the eventual wrap tag. But it shouldn’t be difficult to plug the global escape in, leave it with me. And thanks for testing! :-)

Offline

#21 2017-12-14 11:39:24

etc
Developer
Registered: 2010-11-11
Posts: 5,192
Website GitHub

Re: Poll: useful 'escape' transforms

Escape in images is done now, please test.

Also, if someone needs to extend the available functions list in <txp:evaluate query />, here is a tiny Easter eggs plugin. You can also enable CSS parsing and use css forms à la rah_external_output. I don’t think we will enable these “virtual” prefs in 4.7 yet, but feel free to test (at your own risk).

# Name: etc_easter_eggs v0.1 
# Type: Admin/Public plugin
# 
# Author: 
# URL: 
# Recommended load order: 5

# .....................................................................
# This is a plugin for Textpattern CMS - http://textpattern.com/
# To install: textpattern > admin > plugins
# Paste the following text into the 'Install plugin' box:
# .....................................................................

YToxMTp7czo0OiJuYW1lIjtzOjE1OiJldGNfZWFzdGVyX2VnZ3MiO3M6NjoiYXV0aG9yIjtz
OjA6IiI7czoxMDoiYXV0aG9yX3VyaSI7czowOiIiO3M6NzoidmVyc2lvbiI7czozOiIwLjEi
O3M6MTE6ImRlc2NyaXB0aW9uIjtzOjA6IiI7czo0OiJjb2RlIjtzOjMxODoicmVnaXN0ZXJf
Y2FsbGJhY2soJ2V0Y19wcmVmcycsICdwcmV0ZXh0X2VuZCcpOwoKZnVuY3Rpb24gZXRjX3By
ZWZzKCRldmVudCwgJHN0ZXApIHsKICAgIGdsb2JhbCAkcHJlZnM7CgogICAgLy8gUEhQIGZ1
bmN0aW9ucyB1c2VkIGluIDx0eHA6ZXZhbHVhdGUgLz46IGNvbW1hLXNlcGFyYXRlZCBsaXN0
IG9mICduYW1lJyBvciAnYWxpYXM6bmFtZScKICAgICRwcmVmc1sndHhwX2V2YWx1YXRlX2Z1
bmN0aW9ucyddID0gJ21kNSwgcmVwbGFjZTpwcmVnX3JlcGxhY2UnOwogICAgLy8gUGFyc2Ug
Y3NzPwogICAgJHByZWZzWydwYXJzZV9jc3MnXSA9IGZhbHNlOwp9IjtzOjQ6InR5cGUiO3M6
MToiMSI7czo1OiJvcmRlciI7czoxOiI1IjtzOjU6ImZsYWdzIjtzOjE6IjAiO3M6NDoiaGVs
cCI7YjowO3M6MzoibWQ1IjtzOjMyOiI3YzMwN2RkZTI2ZGM0YzZjMDk0YzNhZWJmYmQwMTEw
YyI7fQ==

Offline

#22 2017-12-14 12:17:03

phiw13
Plugin Author
From: Japan
Registered: 2004-02-27
Posts: 3,192
Website

Re: Poll: useful 'escape' transforms

etc wrote #308307:

Escape in images is done now, please test.

Will do so tomorrow morning :-)


Where is that emoji for a solar powered submarine when you need it ?
Sand space – admin theme for Textpattern

Offline

#23 2017-12-15 12:43:45

phiw13
Plugin Author
From: Japan
Registered: 2004-02-27
Posts: 3,192
Website

Re: Poll: useful 'escape' transforms

etc wrote #308307:

Escape in images is done now, please test.

It works nicely. Of course (and this goes for any place where escape="textile" is used), now we have that nice side effect that the string to which it is applied is wrapped in a <p />.
so for my example above, the output is:

<figcaption><p>a <em>test</em> image - something <strong>bold<strong></p></figcaption>

Nothing annoying for my use case, though.

—-
BTW – speaking of evaluate, I spotted this in the templates for the Textpattern main website:

width="<txp:evaluate query='ceiling(<txp:image_info type="w" />div2)' />"

Amazingly, I even manage to understand what it does… A question though, does the number in the div2 need to be an integer? Or can it be something like div1.5 ? It did not immediately work in a quick test (the output was blank), but then I had some other issues with what I was trying and I did not have time to investigate that part further. Populating the srcset attribute with multiple sizes can be giant pain.


Where is that emoji for a solar powered submarine when you need it ?
Sand space – admin theme for Textpattern

Offline

#24 2017-12-15 13:17:03

etc
Developer
Registered: 2010-11-11
Posts: 5,192
Website GitHub

Re: Poll: useful 'escape' transforms

phiw13 wrote #308313:

It works nicely. Of course (and this goes for any place where escape="textile" is used), now we have that nice side effect that the string to which it is applied is wrapped in a <p />.

Dunno what to do with this, prepend a space, perhaps?

<txp:evaluate escape="textile" wraptag="figcaption"> <txp:image_info type="caption" /></txp:evaluate>

BTW – speaking of evaluate, … does the number in the div2 need to be an integer?

No, <txp:evaluate query='ceiling(2.718 div 1.618)' /> is fine.

Offline

Board footer

Powered by FluxBB