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.

 

<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.

 

TweetBacks
Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Joshua's Gravatar 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
Joshua's Gravatar 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

NAVIGATION

Home
About Me

RSS


Search

Subscribe

Enter your email address to subscribe to this blog.

Recent Entries

Google TV Review
Playbook - Developers It's Time To Get Started
cf.Objective 2011 - Speak Your Mind
Timesheets, Project Management, and Invoicing - FreshBooks Review
A New Phase of My Life

Recent Comments

OTA OK?
AnoraDD said: I get 18 of these exact sms's everyday! How do I get it to STOP?!? [More]

Coldfusion Hosting with Network Solutions
LIzm said: Ugh. I have a client who insists on hosting with them and two weeks after first contact, a very simp... [More]

IE nested list item whitespace solution: vertical-align:bottom
Lauren said: Thought I'd add to the thank yous... Thank you! [More]

OTA OK?
Rita said: Thank you, this was very helpful. [More]

OTA OK?
diggs said: Just got it today, T-Mobile. Thanks for the articale about it. [More]

Calendar

Sun Mon Tue Wed Thu Fri Sat
   1234
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29    

Archives By Subject

blogs (31) [RSS]
books (4) [RSS]
Consulting (2) [RSS]
Crazy (39) [RSS]
DIY (8) [RSS]
Flex (3) [RSS]
games (10) [RSS]
GRRR (13) [RSS]
Ideas (11) [RSS]
Local (15) [RSS]
LOLpics (2) [RSS]
money (9) [RSS]
music (3) [RSS]
Personal (28) [RSS]
Photos (8) [RSS]
Politics (8) [RSS]
Projects (22) [RSS]
Review (20) [RSS]
RPM (9) [RSS]
Spam (16) [RSS]
Technology (69) [RSS]
Testing (3) [RSS]
TV (15) [RSS]
video (32) [RSS]
Web Dev (230) [RSS]
World of Warcraft (16) [RSS]