| 1 | <cfcomponent displayName="Render - Core Rendering CFC" output="false"> |
|---|
| 2 | |
|---|
| 3 | <cffunction name="renderDisplay" output="false"> |
|---|
| 4 | <cfargument name="string" required="false" default=""> |
|---|
| 5 | <cfset var md = getMetaData(this)> |
|---|
| 6 | <cfset var x = ""> |
|---|
| 7 | |
|---|
| 8 | <cfloop index="x" from="1" to="#arrayLen(md.functions)#"> |
|---|
| 9 | <cfif md.functions[x].name is "display"> |
|---|
| 10 | <cfset arguments.string = processTags(arguments.string, listLast(md.name,"."), "display")> |
|---|
| 11 | <cfreturn arguments.string> |
|---|
| 12 | </cfif> |
|---|
| 13 | </cfloop> |
|---|
| 14 | <!--- do nothing ---> |
|---|
| 15 | <cfreturn arguments.string> |
|---|
| 16 | </cffunction> |
|---|
| 17 | |
|---|
| 18 | <!--- |
|---|
| 19 | I am passed a string, a tag, and a method to call. |
|---|
| 20 | I find all instances of your tag <xxx>, I get the args, |
|---|
| 21 | and I call your method. What you return I replace in the string. |
|---|
| 22 | ---> |
|---|
| 23 | <cffunction name="processTags"> |
|---|
| 24 | <cfargument name="string" required="true"> |
|---|
| 25 | <cfargument name="tag" required="true"> |
|---|
| 26 | <cfargument name="method" required="true"> |
|---|
| 27 | <cfset var result = ""> |
|---|
| 28 | <cfset var tags = getTags(arguments.string, arguments.tag)> |
|---|
| 29 | <cfset var x = ""> |
|---|
| 30 | <cfset var key = ""> |
|---|
| 31 | |
|---|
| 32 | <cfloop index="x" from="1" to="#arrayLen(tags)#"> |
|---|
| 33 | <!--- first, call method with args ---> |
|---|
| 34 | <cfinvoke method="#arguments.method#" returnVariable="result"> |
|---|
| 35 | <cfloop item="key" collection="#tags[x].args#"> |
|---|
| 36 | <cfinvokeargument name="#key#" value="#tags[x].args[key]#"> |
|---|
| 37 | </cfloop> |
|---|
| 38 | </cfinvoke> |
|---|
| 39 | <cfset arguments.string = replace(arguments.string, tags[x].match, trim(result))> |
|---|
| 40 | </cfloop> |
|---|
| 41 | |
|---|
| 42 | <cfreturn arguments.string> |
|---|
| 43 | </cffunction> |
|---|
| 44 | |
|---|
| 45 | <!--- |
|---|
| 46 | The purpose of this function is to look in a string for all |
|---|
| 47 | cases of <XXX. It will return an array of structs such that each |
|---|
| 48 | struct has: |
|---|
| 49 | POS: position of match |
|---|
| 50 | LEN: len of match |
|---|
| 51 | ARGS: A structure of arguments and values |
|---|
| 52 | |
|---|
| 53 | So, I can say, getTags("amazon") and it could return |
|---|
| 54 | |
|---|
| 55 | matches[1].pos=100 |
|---|
| 56 | matches[1].len=221 |
|---|
| 57 | matches[1].match=the match |
|---|
| 58 | matches[1].args[asin] = .... |
|---|
| 59 | matches[1].args[affiliate] = ... |
|---|
| 60 | ---> |
|---|
| 61 | <cffunction name="getTags" output="false"> |
|---|
| 62 | <cfargument name="string" required="true"> |
|---|
| 63 | <cfargument name="tag" required="true"> |
|---|
| 64 | <cfset var result = arrayNew(1)> |
|---|
| 65 | <cfset var matches = reFindAll("<#arguments.tag#.*?>", arguments.string)> |
|---|
| 66 | <cfset var x = ""> |
|---|
| 67 | <cfset var argString = ""> |
|---|
| 68 | <cfset var argPair = ""> |
|---|
| 69 | <cfset var arg = ""> |
|---|
| 70 | <cfset var value = ""> |
|---|
| 71 | |
|---|
| 72 | <cfif matches.pos[1] is not 0> |
|---|
| 73 | |
|---|
| 74 | <cfloop index="x" from="1" to="#arrayLen(matches.pos)#"> |
|---|
| 75 | <cfset result[arrayLen(result)+1] = structNew()> |
|---|
| 76 | <cfset result[arrayLen(result)].pos = matches.pos[x]> |
|---|
| 77 | <cfset result[arrayLen(result)].len = matches.len[x]> |
|---|
| 78 | <cfset result[arrayLen(result)].match = mid(arguments.string, matches.pos[x], matches.len[x])> |
|---|
| 79 | <cfset result[arrayLen(result)].args = structNew()> |
|---|
| 80 | |
|---|
| 81 | <cfset argString = replace(result[arrayLen(result)].match, "<#arguments.tag#","")> |
|---|
| 82 | <cfset argString = replace(argString, "/>","")> |
|---|
| 83 | <cfset argString = replace(argString, ">","")> |
|---|
| 84 | <cfset argString = trim(argString)> |
|---|
| 85 | |
|---|
| 86 | <cfif len(argString)> |
|---|
| 87 | <cfloop index="argPair" list="#argString#" delimiters=" "> |
|---|
| 88 | <cfif listLen(argPair, "=") is 2> |
|---|
| 89 | <cfset arg = listFirst(argPair, "=")> |
|---|
| 90 | <cfset value = listLast(argPair, "=")> |
|---|
| 91 | <cflog file="blogrender" text="arg=#arg#, value=#value#"> |
|---|
| 92 | <cfif value is """"""> |
|---|
| 93 | <cfset value = ""> |
|---|
| 94 | <cfelse> |
|---|
| 95 | <cfif left(value, 1) is """"> |
|---|
| 96 | <cfset value = right(value, len(value)-1)> |
|---|
| 97 | </cfif> |
|---|
| 98 | <cfif right(value, 1) is """"> |
|---|
| 99 | <cfset value = left(value, len(value)-1)> |
|---|
| 100 | </cfif> |
|---|
| 101 | </cfif> |
|---|
| 102 | <cfset result[arrayLen(result)].args[arg] = value> |
|---|
| 103 | </cfif> |
|---|
| 104 | </cfloop> |
|---|
| 105 | </cfif> |
|---|
| 106 | <cfset result[arrayLen(result)].argString = argString> |
|---|
| 107 | </cfloop> |
|---|
| 108 | |
|---|
| 109 | </cfif> |
|---|
| 110 | |
|---|
| 111 | <cfreturn result> |
|---|
| 112 | </cffunction> |
|---|
| 113 | |
|---|
| 114 | <!--- |
|---|
| 115 | Returns all the matches of a regular expression within a string. |
|---|
| 116 | |
|---|
| 117 | @param regex Regular expression. (Required) |
|---|
| 118 | @param text String to search. (Required) |
|---|
| 119 | @return Returns a structure. |
|---|
| 120 | @author Ben Forta (ben@forta.com) |
|---|
| 121 | @version 1, July 15, 2005 |
|---|
| 122 | ---> |
|---|
| 123 | <cffunction name="reFindAll" output="true" returnType="struct"> |
|---|
| 124 | <cfargument name="regex" type="string" required="yes"> |
|---|
| 125 | <cfargument name="text" type="string" required="yes"> |
|---|
| 126 | |
|---|
| 127 | <!--- Define local variables ---> |
|---|
| 128 | <cfset var results=structNew()> |
|---|
| 129 | <cfset var pos=1> |
|---|
| 130 | <cfset var subex=""> |
|---|
| 131 | <cfset var done=false> |
|---|
| 132 | |
|---|
| 133 | <!--- Initialize results structure ---> |
|---|
| 134 | <cfset results.len=arraynew(1)> |
|---|
| 135 | <cfset results.pos=arraynew(1)> |
|---|
| 136 | |
|---|
| 137 | <!--- Loop through text ---> |
|---|
| 138 | <cfloop condition="not done"> |
|---|
| 139 | |
|---|
| 140 | <!--- Perform search ---> |
|---|
| 141 | <cfset subex=reFind(arguments.regex, arguments.text, pos, true)> |
|---|
| 142 | <!--- Anything matched? ---> |
|---|
| 143 | <cfif subex.len[1] is 0> |
|---|
| 144 | <!--- Nothing found, outta here ---> |
|---|
| 145 | <cfset done=true> |
|---|
| 146 | <cfelse> |
|---|
| 147 | <!--- Got one, add to arrays ---> |
|---|
| 148 | <cfset arrayappend(results.len, subex.len[1])> |
|---|
| 149 | <cfset arrayappend(results.pos, subex.pos[1])> |
|---|
| 150 | <!--- Reposition start point ---> |
|---|
| 151 | <cfset pos=subex.pos[1]+subex.len[1]> |
|---|
| 152 | </cfif> |
|---|
| 153 | </cfloop> |
|---|
| 154 | |
|---|
| 155 | <!--- If no matches, add 0 to both arrays ---> |
|---|
| 156 | <cfif arraylen(results.len) is 0> |
|---|
| 157 | <cfset arrayappend(results.len, 0)> |
|---|
| 158 | <cfset arrayappend(results.pos, 0)> |
|---|
| 159 | </cfif> |
|---|
| 160 | |
|---|
| 161 | <!--- and return results ---> |
|---|
| 162 | <cfreturn results> |
|---|
| 163 | </cffunction> |
|---|
| 164 | |
|---|
| 165 | </cfcomponent> |
|---|