PageRenderTime 35ms CodeModel.GetById 18ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 1ms

/wheels/view/text.cfm

http://raihan.googlecode.com/
ColdFusion | 280 lines | 273 code | 7 blank | 0 comment | 23 complexity | 368f1906c88570c2f70a389e219aea0a MD5 | raw file
  1<cffunction name="autoLink" returntype="string" access="public" output="false" hint="Turns all URLs and email addresses into hyperlinks."
  2	examples=
  3	'
  4		##autoLink("Download Wheels from http://cfwheels.org/download")##
  5		-> Download Wheels from <a href="http://cfwheels.org/download">http://cfwheels.org/download</a>
  6
  7		##autoLink("Email us at info@cfwheels.org")##
  8		-> Email us at <a href="mailto:info@cfwheels.org">info@cfwheels.org</a>
  9	'
 10	categories="view-helper,text" functions="excerpt,highlight,simpleFormat,titleize,truncate">
 11	<cfargument name="text" type="string" required="true" hint="The text to create links in.">
 12	<cfargument name="link" type="string" required="false" default="all" hint="Whether to link URLs, email addresses or both. Possible values are: `all` (default), `URLs` and `emailAddresses`.">
 13	<cfargument name="relative" type="boolean" required="false" default="true" hint="Should we autolink relative urls">
 14	<cfscript>
 15		var loc = {};
 16		if (arguments.link != "emailAddresses")
 17		{
 18			if(arguments.relative)
 19			{
 20				arguments.regex = "(?:(?:<a\s[^>]+)?(?:https?://|www\.|\/)[^\s\b]+)";
 21			}
 22			else
 23			{
 24				arguments.regex = "(?:(?:<a\s[^>]+)?(?:https?://|www\.)[^\s\b]+)";
 25			}
 26			arguments.text = $autoLinkLoop(argumentCollection=arguments);
 27		}
 28		if (arguments.link != "URLs")
 29		{
 30			arguments.regex = "(?:(?:<a\s[^>]+)?(?:[^@\s]+)@(?:(?:[-a-z0-9]+\.)+[a-z]{2,}))";
 31			arguments.protocol = "mailto:";
 32			arguments.text = $autoLinkLoop(argumentCollection=arguments);
 33		}
 34	</cfscript>
 35	<cfreturn arguments.text>
 36</cffunction>
 37
 38<cffunction name="$autoLinkLoop" access="public" returntype="string" output="false">
 39	<cfargument name="text" type="string" required="true">
 40	<cfargument name="regex" type="string" required="true">
 41	<cfargument name="protocol" type="string" required="false" default="">
 42	<cfscript>
 43	var loc = {};
 44	loc.PunctuationRegEx = "([^\w\/-]+)$";
 45	loc.startPosition = 1;
 46	loc.match = ReFindNoCase(arguments.regex, arguments.text, loc.startPosition, true);
 47	while(loc.match.pos[1] gt 0)
 48	{
 49		loc.startPosition = loc.match.pos[1] + loc.match.len[1];
 50		loc.str = Mid(arguments.text, loc.match.pos[1], loc.match.len[1]);
 51		if (Left(loc.str, 2) neq "<a")
 52		{
 53			arguments.text = RemoveChars(arguments.text, loc.match.pos[1], loc.match.len[1]);
 54			// remove any sort of trailing puncuation
 55			loc.punctuation = ArrayToList(ReMatchNoCase(loc.PunctuationRegEx, loc.str));
 56			loc.str = REReplaceNoCase(loc.str, loc.PunctuationRegEx, "", "all");
 57			arguments.href = arguments.protocol & loc.str;
 58			loc.element = $element("a", arguments, loc.str, "text,regex,link,domains,protocol,relative") & loc.punctuation;
 59			arguments.text = Insert(loc.element, arguments.text, loc.match.pos[1]-1);
 60			loc.startPosition = loc.match.pos[1] + len(loc.element);
 61		}
 62		loc.startPosition++;
 63		loc.match = ReFindNoCase(arguments.regex, arguments.text, loc.startPosition, true);
 64	}
 65	</cfscript>
 66	<cfreturn arguments.text>
 67</cffunction>
 68
 69<cffunction name="excerpt" returntype="string" access="public" output="false" hint="Extracts an excerpt from text that matches the first instance of a given phrase."
 70	examples=
 71	'
 72		##excerpt(text="ColdFusion Wheels is a Rails-like MVC framework for Adobe ColdFusion and Railo", phrase="framework", radius=5)##
 73		-> ... MVC framework for ...
 74	'
 75	categories="view-helper,text" functions="autoLink,highlight,simpleFormat,titleize,truncate">
 76	<cfargument name="text" type="string" required="true" hint="The text to extract an excerpt from.">
 77	<cfargument name="phrase" type="string" required="true" hint="The phrase to extract.">
 78	<cfargument name="radius" type="numeric" required="false" default="100" hint="Number of characters to extract surrounding the phrase.">
 79	<cfargument name="excerptString" type="string" required="false" default="..." hint="String to replace first and/or last characters with.">
 80	<cfscript>
 81	var loc = {};
 82	loc.pos = FindNoCase(arguments.phrase, arguments.text, 1);
 83	if (loc.pos != 0)
 84	{
 85		if ((loc.pos-arguments.radius) <= 1)
 86		{
 87			loc.startPos = 1;
 88			loc.truncateStart = "";
 89		}
 90		else
 91		{
 92			loc.startPos = loc.pos - arguments.radius;
 93			loc.truncateStart = arguments.excerptString;
 94		}
 95		if ((loc.pos+Len(arguments.phrase)+arguments.radius) > Len(arguments.text))
 96		{
 97			loc.endPos = Len(arguments.text);
 98			loc.truncateEnd = "";
 99		}
100		else
101		{
102			loc.endPos = loc.pos + arguments.radius;
103			loc.truncateEnd = arguments.excerptString;
104		}
105		loc.returnValue = loc.truncateStart & Mid(arguments.text, loc.startPos, ((loc.endPos+Len(arguments.phrase))-(loc.startPos))) & loc.truncateEnd;
106	}
107	else
108	{
109		loc.returnValue = "";
110	}
111	</cfscript>
112	<cfreturn loc.returnValue>
113</cffunction>
114
115<cffunction name="highlight" returntype="string" access="public" output="false" hint="Highlights the phrase(s) everywhere in the text if found by wrapping it in a `span` tag."
116	examples=
117	'
118		##highlight(text="You searched for: Wheels", phrases="Wheels")##
119		-> You searched for: <span class="highlight">Wheels</span>
120	'
121	categories="view-helper,text" functions="autoLink,excerpt,simpleFormat,titleize,truncate">
122	<cfargument name="text" type="string" required="true" hint="Text to search.">
123	<cfargument name="phrases" type="string" required="true" hint="List of phrases to highlight.">
124	<cfargument name="class" type="string" required="false" default="highlight" hint="Class to use in `span` tags surrounding highlighted phrase(s).">
125	<cfscript>
126		var loc = {};
127		if (!Len(arguments.text) || !Len(arguments.phrases))
128		{
129			loc.returnValue = arguments.text;
130		}
131		else
132		{
133			loc.origText = arguments.text;
134			loc.iEnd = ListLen(arguments.phrases);
135			for (loc.i=1; loc.i <= loc.iEnd; loc.i=loc.i+1)
136			{
137				loc.newText = "";
138				loc.phrase = Trim(ListGetAt(arguments.phrases, loc.i));
139				loc.pos = 1;
140				while (FindNoCase(loc.phrase, loc.origText, loc.pos))
141				{
142					loc.foundAt = FindNoCase(loc.phrase, loc.origText, loc.pos);
143					loc.prevText = Mid(loc.origText, loc.pos, loc.foundAt-loc.pos);
144					loc.newText = loc.newText & loc.prevText;
145					if (Find("<", loc.origText, loc.foundAt) < Find(">", loc.origText, loc.foundAt) || !Find(">", loc.origText, loc.foundAt))
146						loc.newText = loc.newText & "<span class=""" & arguments.class & """>" & Mid(loc.origText, loc.foundAt, Len(loc.phrase)) & "</span>";
147					else
148						loc.newText = loc.newText & Mid(loc.origText, loc.foundAt, Len(loc.phrase));
149					loc.pos = loc.foundAt + Len(loc.phrase);
150				}
151				loc.newText = loc.newText & Mid(loc.origText, loc.pos, Len(loc.origText) - loc.pos + 1);
152				loc.origText = loc.newText;
153			}
154			loc.returnValue = loc.newText;
155		}
156	</cfscript>
157	<cfreturn loc.returnValue>
158</cffunction>
159
160<cffunction name="simpleFormat" returntype="string" access="public" output="false" hint="Replaces single newline characters with HTML break tags and double newline characters with HTML paragraph tags (properly closed to comply with XHTML standards)."
161	examples=
162	'
163		<!--- How most of your calls will look --->
164		##simpleFormat(post.comments)##
165
166		<!--- Demonstrates what output looks like with specific data --->
167		<cfsavecontent variable="comment">
168			I love this post!
169
170			Here''s why:
171			* Short
172			* Succinct
173			* Awesome
174		</cfsavecontent>
175		##simpleFormat(comment)##
176		-> <p>I love this post!</p>
177		   <p>
178		       Here''s why:<br />
179			   * Short<br />
180			   * Succinct<br />
181			   * Awesome
182		   </p>
183	'
184	categories="view-helper,text" functions="autoLink,excerpt,highlight,titleize,truncate">
185	<cfargument name="text" type="string" required="true" hint="The text to format.">
186	<cfargument name="wrap" type="boolean" required="false" default="true" hint="Set to `true` to wrap the result in a paragraph.">
187	<cfscript>
188		var loc = {};
189		loc.returnValue = Trim(arguments.text);
190		loc.returnValue = Replace(loc.returnValue, "#Chr(13)#", "", "all");
191		loc.returnValue = Replace(loc.returnValue, "#Chr(10)##Chr(10)#", "</p><p>", "all");
192		loc.returnValue = Replace(loc.returnValue, "#Chr(10)#", "<br />", "all");
193		
194		// add back in our returns so we can strip the tags and re-apply them without issue
195		// this is good to be edited the textarea text in it's original format (line returns)
196		loc.returnValue = Replace(loc.returnValue, "</p><p>", "</p>#Chr(10)##Chr(10)#<p>", "all");
197		loc.returnValue = Replace(loc.returnValue, "<br />", "<br />#Chr(10)#", "all");
198		
199		if (arguments.wrap)
200			loc.returnValue = "<p>" & loc.returnValue & "</p>";
201	</cfscript>
202	<cfreturn loc.returnValue>
203</cffunction>
204
205<cffunction name="titleize" returntype="string" access="public" output="false" hint="Capitalizes all words in the text to create a nicer looking title."
206	examples=
207	'
208		##titleize("Wheels is a framework for ColdFusion")##
209		-> Wheels Is A Framework For ColdFusion
210	'
211	categories="view-helper,text" functions="autoLink,excerpt,highlight,simpleFormat,truncate">
212	<cfargument name="word" type="string" required="true" hint="The text to turn into a title.">
213	<cfscript>
214		var loc = {};
215		loc.returnValue = "";
216		loc.iEnd = ListLen(arguments.word, " ");
217		for (loc.i=1; loc.i <= loc.iEnd; loc.i++)
218		{
219			loc.returnValue = ListAppend(loc.returnValue, capitalize(ListGetAt(arguments.word, loc.i, " ")), " ");
220		}
221	</cfscript>
222	<cfreturn loc.returnValue>
223</cffunction>
224
225<cffunction name="truncate" returntype="string" access="public" output="false" hint="Truncates text to the specified length and replaces the last characters with the specified truncate string (which defaults to ""..."")."
226	examples=
227	'
228		##truncate(text="Wheels is a framework for ColdFusion", length=20)##
229		-> Wheels is a frame...
230
231		##truncate(text="Wheels is a framework for ColdFusion", truncateString=" (more)")##
232		-> Wheels is a framework f (more)
233	'
234	categories="view-helper,text" functions="autoLink,excerpt,highlight,simpleFormat,titleize">
235	<cfargument name="text" type="string" required="true" hint="The text to truncate.">
236	<cfargument name="length" type="numeric" required="false" hint="Length to truncate the text to.">
237	<cfargument name="truncateString" type="string" required="false" hint="String to replace the last characters with.">
238	<cfscript>
239		var loc = {};
240		$args(name="truncate", args=arguments);
241		if (Len(arguments.text) gt arguments.length)
242			loc.returnValue = Left(arguments.text, arguments.length-Len(arguments.truncateString)) & arguments.truncateString;
243		else
244			loc.returnValue = arguments.text;
245	</cfscript>
246	<cfreturn loc.returnValue>
247</cffunction>
248
249<cffunction name="wordTruncate" returntype="string" access="public" output="false" hint="Truncates text to the specified length of words and replaces the remaining characters with the specified truncate string (which defaults to ""..."")."
250	examples=
251	'
252		##wordTruncate(text="Wheels is a framework for ColdFusion", length=4)##
253		-> Wheels is a framework...
254
255		##truncate(text="Wheels is a framework for ColdFusion", truncateString=" (more)")##
256		-> Wheels is a framework for (more)
257	'
258	categories="view-helper,text" functions="autoLink,excerpt,highlight,simpleFormat,titleize">
259	<cfargument name="text" type="string" required="true" hint="The text to truncate.">
260	<cfargument name="length" type="numeric" required="false" default="5" hint="Number of words to truncate the text to.">
261	<cfargument name="truncateString" type="string" required="false" default="..." hint="String to replace the last characters with.">
262	<cfscript>
263		var loc = {};
264		loc.returnValue = "";
265		loc.wordArray = ListToArray(arguments.text, " ", false);
266		loc.wordLen = ArrayLen(loc.wordArray);
267		
268		if (loc.wordLen gt arguments.length)
269		{
270			for (loc.i = 1; loc.i lte arguments.length; loc.i++)
271				loc.returnValue = ListAppend(loc.returnValue, loc.wordArray[loc.i], " ");
272			loc.returnValue = loc.returnValue & arguments.truncateString;
273		}
274		else
275		{
276			loc.returnValue = arguments.text;
277		}
278	</cfscript>
279	<cfreturn loc.returnValue>
280</cffunction>