Auto Generated Code Download Fixed

Posted By : todd sharp Posted At : October 10, 2006 9:06 PM Posted In: Blogging, ColdFusion

2

I finally got a chance to fix my BlogCFC mod to add auto generated code downloads to entries that use the 'more' tag. If you haven't read about the mod yet, read this post first and then come back here and get this mod to replace step 5 with the code attached here.

<cffunction name="hasCodeSamples" access="remote" returntype="boolean" hint="does the entry contain any code samples">
    <cfargument name="entryId" type="uuid" required="true">
    <cfargument name="dontlog" type="boolean" required="true" hint="we don't want to log this request as a view so pass true">
    <cfset var hasCode = false>
    <cfset var theEntry = getEntry(arguments.entryId, arguments.dontlog)>
    <cfset var theBody = theEntry.body & theEntry.morebody>
    <cfif ArrayLen(utils.getTagContentAll("code", theBody))>
        <cfset hasCode = true>
    </cfif>
    <cfreturn hasCode/>
</cffunction>

<cffunction name="getCodeSamples" access="remote" returntype="string" hint="gets the code samples for download">
    <cfargument name="entryId" type="uuid" required="true">
    <cfargument name="dontlog" type="boolean" required="true" hint="we don't want to log this request as a view so pass true">
    <cfset var renderedCode = "">
    <cfset var theHeader = "">
    <cfset var theEntry = getEntry(arguments.entryId, arguments.dontlog)>
    <cfset var theBody = theEntry.body & theEntry.morebody>
    <cfset var theCode = utils.getTagContentAll("code", theBody)>

    <cfsavecontent variable="theHeader">
    <cfoutput>
<!---
** BlogCFC Auto Generated code download**
Generated from blog post: #makeLink(entryId)#
Author: #theEntry.name# (#instance.ownerEmail#)
Generated On: #dateFormat(now(), "full")# #timeFormat(now(), "hh:mm tt")#
--->

    </cfoutput>
    </cfsavecontent>
    <cfsavecontent variable="theBody">
    <cfoutput>
        <cfloop from="1" to="#ArrayLen(theCode)#" index="i">
<!--- Code Sample ## #i# --->
#theCode[i]#
        </cfloop>
    </cfoutput>
    </cfsavecontent>
    <cfset renderedCode = theHeader & theBody>
    <cfset renderedCode = replace(renderedCode, "<", "<", "all")>
    <cfset renderedCode = replace(renderedCode, ">", ">", "all")>

    <cfreturn renderedCode/>
</cffunction>

Related Blog Entries

Comments (2)

Rob's Gravatar I needed to add the id to the link to get it to work.
a href="#application.rooturl#/downloadCode.cfm#"
to this
a href="#application.rooturl#/downloadCode.cfm?id=#id#"

Thank you, very nice.

todd's Gravatar Rob: Good catch, thank you. I usually am copy/pasting the code samples into a text file while I do these type of posts and I obviously forgot to update my file as I was working.

Did you implement this somewhere I can see? I'd like to see it in action if you can share.