Replacing Code

Discussion corner for Developers of Serendipity.
Post Reply
dolbex
Regular
Posts: 22
Joined: Fri Aug 18, 2006 9:17 pm

Replacing Code

Post by dolbex »

Howdy Folks,

I am just getting my Serendipity installation off the ground (went very smooth btw) and I am running into a bit of trouble. I would like to have some automation with how images are handled in a post. When generating a post I would like for the engine to replace:

Code: Select all

<img src=...>
with

Code: Select all

	
<div class="...">
  <div>
    <img src="..." alt="..." />
  </div>
</div>
Thanks for any suggestions you might have on accomplishing this... :wink:
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: Replacing Code

Post by garvinhicking »

Hi!

There are actually multiple ways to get to rome here :)

First one, maybe the easiest for you, is to install the "Regular Expressions" Markup plugin. There you can add a "from" and "to" rule for that replacement.

Second option is to use the "Content Rewrite" plugin, which acts a bit like regular expressions, but is more menu-guided and does not create custom file creation.

Third option is to modify a plugin like bbcode or textile to include a short tag for your [preimage] and [postimage] thingies?

Fourth option would be to use a plugin like the "Typeset/Extended Buttons" plugin to add a button for inserting your code into the entry immediately.

Last but not least: You could create your own little plugin that hooks on the required events (like the markup plugins all do) and push it so that your replacement is done there? This would have the upside of full PHP complexity. :)

Best regards and HTH,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
dolbex
Regular
Posts: 22
Joined: Fri Aug 18, 2006 9:17 pm

Post by dolbex »

GH,

Thanks so much for your help and your hard work on such a great CMS. (I will be sure to donate once the site is up and running)

I decided to go with option one but am stumbling a little bit with the Regular expression

Code: Select all

$regexpArray = array(
    'SearchArray'=>array(
      '/<img ([^"]+)>/U'
    ),
    'ReplaceArray'=>array(
      '<div class="alpha-shadow"><div><img $1 /></div></div>'     
    )
); 
Is what I have now, but it doesn't seem to be working. I want to find every image in a post and replace it with the code in the replace array ($1 being 'part 1'). I'm such a RegEx nub... :(

Also, on the editor (I've installed the FKE WYSIWYG editor) on the backend I am getting this error when I edit although the edits do seem to still save...

Code: Select all

Warning: preg_replace() [function.preg-replace]: Unknown modifier '>' in .../plugins/serendipity_event_regexpmarkup/serendipity_event_regexpmarkup.php on line 150
Thanks for any help anyone can provide.
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post by garvinhicking »

Hi!

You're welcome! :)

You could try this:

Code: Select all

$regexpArray = array(
    'SearchArray'=>array(
      '/<img([^>]+)>/U'
    ),
    'ReplaceArray'=>array(
      '<div class="alpha-shadow"><div><img \1></div></div>'     
    )
); 
And see if that works? I changed two minor things which might get it on track?

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
dolbex
Regular
Posts: 22
Joined: Fri Aug 18, 2006 9:17 pm

Post by dolbex »

That works perfect. The only problem that I am running into that I didn't forsee was if the image is a link. If it is a link the link ends up around the DIV's blowing the whole thing up.

Any clue how to look for the link and include that inside the divs?
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post by garvinhicking »

Hi!

Uh, that is a larger regular expression that I can't think of right now. You would need to match "(<a[^>]>)?" as an optional patter and include the reference to that via \1, \2 etc.?

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
dolbex
Regular
Posts: 22
Joined: Fri Aug 18, 2006 9:17 pm

Post by dolbex »

Code: Select all

<a([^>]+)><img([^>]+)></a>
Tried that with no luck although I can't quite find what's wrong...

Ugh... my brain is in pain. :)
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post by garvinhicking »

Hi!

If you use "</a>" in your pattern, you need to escape the "/", so that it reads: "<\/a>". Maybe that'S the problem...?

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
Post Reply