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