Tinymce editor gallery plugin Regexp problem?

I am using tinymce editor for inserting contents to mysql.
I have changed wordpress gallery editor plugin according to my system.

If there is gallery code in content. I convert this code to a symbolic photo, so that user understand there is a gallery , in stead of seeing a code. Like wordpress does.

Read More

If there is only 1 gallery in content, i convert this code to image successfully, but if there is more than 1 gallery it fails.

How can i convert all {gallery} code into a symbolic image before saving to db and convert these photos back to {gallery} code again while inserting or updating into mysql.

I am so bad on regular expression.
I think do_gallery RegExp has mistake. How should i change this.

initalising editor like:

ed.onBeforeSetContent.add(function(ed, o) {
            ed.dom.loadCSS(url + "/css/gallery.css");
            o.content = t._do_gallery(o.content);
        });

        ed.onPostProcess.add(function(ed, o) {
            if (o.get)
                o.content = t._get_gallery(o.content);
        });

My “do and get gallery” codes like that:

_do_gallery : function(co) {
        return co.replace(/{gallery([^]]*)}/g, function(a,b){
            var image = '<img src="gallery.gif" class="wpGallery mceItem" title="gallery'+tinymce.DOM.encode(b)+'" />';
            console.log(image);
            return image;

        });
    },

    _get_gallery : function(co) {

        function getAttr(s, n) {
            n = new RegExp(n + '="([^"]+)"', 'g').exec(s);
            return n ? tinymce.DOM.decode(n[1]) : '';
        };

        return co.replace(/(?:<p{^>}*>)*(<img[^>]+>)(?:</p>)*/g, function(a,im) {
            var cls = getAttr(im, 'class');

            if ( cls.indexOf('wpGallery') != -1 )
                return '<p>{'+tinymce.trim(getAttr(im, 'title'))+'}</p>';

            return a;
        });
    }

If Content is:

<p>Blah</p>
<p>{gallery Name="gallery1" id="82" galeryID="15"  sizeId="6" galery_type="list"}</p>
<p>test</p>

this is ok

<img src="gallery.gif" class="wpGallery mceItem" title="gallery Name=&quot;tekne1&quot; id=&quot;82&quot; galeryID=&quot;15&quot; sizeId=&quot;6&quot; galery_type=&quot;liste&quot;" />

But, if content is:

<p>Blah</p>
<p>{gallery Name="gallery1" id="82" galeryID="15"  sizeId="6" galery_type="list"}</p>
<p>test</p>
<p>{gallery Name="gallery2" id="88" galeryID="11"  sizeId="1" galery_type="slide"}</p>
<p>test2</p>

it logs

<img src="gallery.gif" class="wpGallery mceItem" title="gallery Name=&quot;gallery1&quot; id=&quot;82&quot; galeryID=&quot;15&quot; sizeId=&quot;6&quot; galery_type=&quot;list&quot;}&lt;/p&gt; &lt;p&gt;test&lt;/p&gt; &lt;p&gt;{gallery Name=&quot;gallery2&quot; id=&quot;88&quot; galeryID=&quot;11&quot; sizeId=&quot;1&quot; galery_type=&quot;slide&quot;" />

I hope i could explain my problem
Thank you.

Related posts

Leave a Reply

1 comment

  1. I suspect that your original regex is a typo, looks like a missing Shift when you hit the ]. Try this:

    /{gallery([^}]*)}/g
    

    Then the ([^}]*) part will (greedily) eat up any sequence of characters that aren’t }; your original one would consume any sequence of character that didn’t include a ] and the result is that you’d grab everything between the first { and the last } rather than just grabbing the text between pairs of braces.