How to manipulate text on-the-fly?

Hi,


I'm wondering if there's a way to manipulate text on the fly, kind of like a filtering system. For example, I'd like to have commonly used acronyms automatically marked up with the appropriate terms in an <acronym> tag, without intervention from our content editors. Similarly, I'd love to be able to add a different class to <a> elements that do not point to our site, so that we can give them a different colour, or whathaveyou.



However, I don't want these changes to be committed to the database – I'd rather have it happen at parse/cache time. That way, we can add and remove a given filter on a whim without making any kind of permanent change. Some of these things can be performed with JavaScript, but that's not really ideal. I'd be delighted with PHP, mostly since I already have a heap of PHP text parsing functions from other projects, stashed away for a rainy day.



I don't know if it's necessarily a good job for a custom asset, since it'd be nice to just apply these filters to text in all kinds of assets.



Is there something appropriate that we could chuck in a parse file to make this happen? If not, what's another option here?

The easiest way is to create your own design area that inherits from the current BODY design area. The current BODY design area currently does some text transformation of its own, and it does it on-the-fly. It does things like remove images and spacing from content for text-only versions.


It wouldn't be too hard to try hacking up the current BODY design area to see if you can get it working.



This works a little differently depending on what version of Matrix you are using. You're going to want to edit the file /path/to/matrix/core/assets/designs/design_areas/design_area_body/design_area_body.inc



If you are using 3.6, edit the method printArea(). If you are using 3.7, edit _printPaintingCode(). In this method, you will see some code like this (this code is from 3.6):

    switch ($format) {
        case 'text':
        case 'low_bandwidth':
            // text only design - strip images, embeds and objects
            echo '
                $body = preg_replace(\'|\]*)\>|Ui\',\'\',$body);
                $body = preg_replace(\'|\\(.*)|i\',\'\',$body);
                $body = preg_replace(\'|\\(.*)|i\',\'\',$body);
                ';
            if ($format == 'low_bandwidth') {
                // low bandwidth design - strip images, embeds, objects, and additional whitespace
                ...
            }
        break;
    }


Here, the BODY design area does some manipulation of the body content for two conditions; "text" and "low_bandwidth". To set one of these conditions, you edit the parse file and change the BODY tag:
    
        
    


Here, I am setting the format to "text" and the BODY design area will then strip IMG, EMBED and OBJECT tags for me.

You can define your own format in here. Lets call it "raena":
    
        
    


Then we need to change the code:
    switch ($format) {
        case 'raena' :
          // $body contains the string we are going to print
          // so manipulate the HTML in here as you like
          $body = ...
        break;
 &nbsp; &nbsp;case 'text':
 &nbsp; &nbsp;case 'low_bandwidth':
 &nbsp; &nbsp; &nbsp; &nbsp;// text only design - strip images, embeds and objects
 &nbsp; &nbsp; &nbsp; &nbsp;...
 &nbsp; &nbsp; &nbsp; &nbsp;if ($format == 'low_bandwidth') {
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// low bandwidth design - strip images, embeds, objects, and additional whitespace
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;...
 &nbsp; &nbsp; &nbsp; &nbsp;}
 &nbsp; &nbsp;break;
}</pre><br />

Make sure you reparse your design after making these changes.

Ooh, I don't know if we can get away with doing that ourselves cause we're on a managed install with you guys. It's great to know where to do it, though.


Thanks!

[quote]Ooh, I don’t know if we can get away with doing that ourselves cause we’re on a managed install with you guys.  It’s great to know where to do it, though.
[right][post=“8098”]<{POST_SNAPBACK}>[/post][/right][/quote]



To maintain your SLA, you’ll need to get Squiz to code this modification for you. :slight_smile:

I find it hard to believe we'd be the only ones who would have some use for customised text manipulations. Is there no room for some sort of backend-editable way of doing that?

[quote]I find it hard to believe we’d be the only ones who would have some use for customised text manipulations.  Is there no room for some sort of backend-editable way of doing that?
[right][post=“8125”]<{POST_SNAPBACK}>[/post][/right][/quote]



I’m sure there is. :slight_smile: Depending on your needs, you could subnit this as a feature request (lowest priority), or contact your account manager to arrange a formal quote for development (highest priority).



Essentially, submitting a feature request is regarded more as a suggestion to the developers. If you want/need a particular feature within a specific timeframe, obviously its best to speak to Squiz formally about it. :slight_smile:

For sure; I'm just curious as to whether that'd be useful for anyone other than control freaks like myself. :wink: