Blog Read

Blog Refreshed Part Two

This a shorter post, and maybe more of just a Mango Blog plugin post.

So in part one I went of the tweaks that I made to help me for entering and editing blog posts. Now I will show you what I did to fixed the code formatting. Again I created a plugin, the plugin that I created using coldFish(coldfish.riaforge.org) to format the code in my blog entries. Here is my plugin xml.

view plain print about
1<?xml version="1.0" encoding="UTF-8"?>
2<plugin id="com.asfusion.mango.plugins.coidFish" name="Cold Fish" version="0.1" provider-name="Think-lab" class="coldfish.Handler">
3<description><![CDATA[
4This plugin is used to format code within code tags using coldfish. url - coldfish.riaforge.org
5]]>

6</description>
7
8 <requiresVersion match="greaterOrEqual">1.5</requiresVersion>
9 <listens>
10     <event name="postGetContent" type="synch" priority="1" />
11 </listens>
12</plugin>

This plugin listening for the "postGetContent" event.

Now time for the handler code. In the init function we are setting "variables.coldfish" equal to the coldFish component.

view plain print about
1<cffunction name="init" access="public" output="false" returntype="any">
2        
3        <cfargument name="mainManager" type="any" required="true" />
4        <cfargument name="preferences" type="any" required="true" />
5        
6            <cfset setManager(arguments.mainManager) />
7            <cfset setPreferencesManager(arguments.preferences) />            
8            
9            <cfset variables.coldfish = createObject("component","coldfish").init()/>
10        <cfreturn this/>
11        
12</cffunction>

In the processEvent function I am using some code from BlogCFC from the blog component renderEntry function, thanks Ray. The only thing that I really want to point out in the function below is how I am overriding the content variable which is where the blog post is stored "arguments.event.accessObject.content". Also in the code below added spaces within the < code > tags for this post only.

view plain print about
1<cffunction name="processEvent" hint="Synchronous event handling" access="public" output="false" returntype="any">
2        <cfargument name="event" type="any" required="true" />
3
4        <cfset var eventName     = arguments.event.getName() />        
5        <cfset var data         = "" />
6        <cfset var newBody        = "" />
7        <cfset var tmpContent    = "" />
8        <cfset var newContent    = "" />
9        <cfset var counter        = 0 />
10        <cfset var codeblock    = "" />
11        <cfset var codeportion    = "" />
12        <cfset var result        = "" />
13        
14        
15        <cfif eventName EQ "postGetContent" OR eventName EQ "postGetExcerpt">
16            <cfset data         = arguments.event.accessObject />
17            <cfset tmpContent     = data.content />
18            
19            <!--- Check for code blocks --->
20            <cfif findNoCase("start_code",tmpContent) and findNoCase("< / end_code >",tmpContent)>
21                <cfset counter = findNoCase("< start_code >",tmpContent)>

22                <cfloop condition="counter gte 1">
23     <cfset codeblock = reFindNoCase("(?s)(.*)(start_code)(.*)(</ end_code >)(.*)",tmpContent,1,1)>
24                    <cfif arrayLen(codeblock.len) gte 6>
25     <cfset codeportion = mid(tmpContent, codeblock.pos[4], codeblock.len[4])>
26     <cfif len(trim(codeportion))>
27                            <cfset result = variables.coldfish.formatString(trim(codeportion))>
28                            <cfset result = "&lt; div class='code' &gt;#result#&lt; /div &gt;">
29                        <cfelse>
30                            <cfset result = "">
31                        </cfif>
32                        <cfset newbody = mid(tmpContent, 1, codeblock.len[2]) & result & mid(tmpContent,codeblock.pos[6],codeblock.len[6])>
33    
34     <cfset tmpContent = newbody>
35                        <cfset counter = findNoCase("start_code",tmpContent,counter)>
36                    <cfelse>
37                        <!--- bad crap, maybe < start_code > and no ender, or maybe < / end_code >< start_code > --->
38                        <cfset counter = 0>
39                    </cfif>
40                </cfloop>
41                <cfset data.content = tmpContent />
42                
43            </cfif>
44        </cfif>
45
46        <cfreturn arguments.event />
47        
48    </cffunction>
blog comments powered by Disqus