Page 1 of 1
Replacing Code
Posted: Sun Aug 20, 2006 7:26 pm
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:
with
Code: Select all
<div class="...">
<div>
<img src="..." alt="..." />
</div>
</div>
Thanks for any suggestions you might have on accomplishing this...

Re: Replacing Code
Posted: Mon Aug 21, 2006 12:47 pm
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
Posted: Mon Aug 21, 2006 2:50 pm
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.
Posted: Mon Aug 21, 2006 2:54 pm
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
Posted: Mon Aug 21, 2006 8:43 pm
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?
Posted: Mon Aug 21, 2006 8:49 pm
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
Posted: Mon Aug 21, 2006 9:08 pm
by dolbex
Tried that with no luck although I can't quite find what's wrong...
Ugh... my brain is in pain.

Posted: Mon Aug 21, 2006 9:22 pm
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