Automatic more/ with BlogCFC
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.
/**
* 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 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)