Automatic more/ with BlogCFC

April 12, 2007 6:39 PM
Related Categories: Web Dev

I wanted to have BlogCFC automatically add the more/ tag into my blog posts.  I first thought that I should count the chars of the post, and if over a certain size add the tag somewhere in the middle.  However that really didn't offer much control or predictability.

Next I thought it would be fun to check if there were three paragraphs or more.  If so to place the more/ tag between the first and second paragraph.  Seemed easy enough however in my wine drinking furry I ran into many stumbling blocks.  I rewrote the code many times looking for the most elegant solution.

This is what I came up with.  It is just entered into the entry.cfm file right after the cfif for form.submission.

First use this handy user defined function I got off of CFLib.org.

 

<cfscript>
/**
 * Replaces oldSubString with newSubString from a specified starting position while ignoring case.
 *
 * @param theString      The string to modify. (Required)
 * @param oldSubString       The substring to replace. (Required)
 * @param newSubString      The substring to use as a replacement. (Required)
 * @param startIndex      Where to start replacing in the string. (Required)
 * @param theScope       Number of replacements to make. Default is "ONE". Value can be "ONE" or "ALL." (Optional)
 * @return Returns a string.
 * @author Shawn Seley (shawnse@aol.com)
 * @version 1, June 26, 2002
 */

function ReplaceAtNoCase(theString, oldSubString, newSubString, startIndex){
    var targetString  = "";
    var preString     = "";

    var theScope      = "ONE";
    if(ArrayLen(Arguments) GTE 5) theScope    = Arguments[5];

    if (startIndex LTE Len(theString)) {
        targetString = Right(theString, Len(theString)-startIndex+1);
        if (startIndex GT 1) preString = Left(theString, startIndex-1);
        return preString & ReplaceNoCase(targetString, oldSubString, newSubString, theScope);
    } else {
        return theString;
    }
}
</cfscript>

 

Next right after rays code replaces the smart quotes (see his funny comments).

 

<cfset origbody = form.body>

<cfset myposition1 = REFindNoCase('<p>(.*)</p>', form.body)>
<cfset myposition2 = REFindNoCase('<p>(.*)</p>', form.body, evaluate(myposition1 + 1))>
<cfset myposition3 = REFindNoCase('<p>(.*)</p>', form.body, evaluate(myposition2 + 1))>
<cfset findmore = REFindNoCase('<more/>', form.body)>

<cfif myposition2 GT 0 AND myposition3 GT 0 AND findmore eq 0>
    <cfset form.body = "#ReplaceAtNoCase(form.body,  '</p>','</p><more/>', myposition1)#">

</cfif>

 

So, will it work?  Does my new FCKeditor put in the .code style correctly?  Lets all hope this post proves yes!

Update:  I forgot to check if the post was being edited and thus didn't need to have it added.  I ammended the code above to reflect that.

 


Like this entry? Subscribe to my blog.

Comments (moderation on)

Oh, here is another fun idea. For those that want to add google adsense half way through a blog post you can do so using the same set of code. Just put the adsense code right after the more tag.
# Posted By Joshua | 4/12/07 7:00 PM
Doh, ok, editing still doesn't work. The reason is that the editor I am using, FCKeditor, fixes the tag and changes it. I don't have time tonight but soon I need to find the code to add new tags to the editors allowed list.
# Posted By Joshua | 4/12/07 7:04 PM

Sponsors


Savvy Content Manager