PageRenderTime 58ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/wheels/view/text.cfm

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