expandAcronyms()
expandAcronyms()
| Version: | 1.1 build 20050321 |
|---|---|
| Requires: | Adobe ColdFusion MX 6.0 or greater |
| Total size: | 4.94 KB |
| Download time: | 0.88 seconds at 56kbps |
| Edition: | Freeware |
Description
This function parses a block of text and attaches definitions to any found acronyms, abbreviations, foreign words or other words (e.g. chemicals or technical jargon) from a developer-supplied list. The definitions may be attached using the HTML acronym tag or other tags, or as a parenthesised phrase after the acronym. Download a sample set of acronym definitions.
Returns
A string.
Category
String
Parameters
| Parameter | Type | Required? | Default | Description |
|---|---|---|---|---|
| text | string | Yes | The block of text or to be parsed. | |
| acronyms | struct | Yes | A structure containing the set of acronyms (or abbreviations, or words). The key is the acronym and the value is the definition. | |
| scope | string | No | One |
|
| caseSensitive | boolean | No | No |
|
| skipTags | string | No | Yes |
|
| tag | string | No | Tag to wrap around acronym, e.g. "acronym" (default), "abbr", "em". If blank then no tag will be wrapped around the acronym. | |
| parentheses | string | No | provide no parentheses |
|
UDF source
<!--- * Add definitions to acronyms and abbreviations in a block of text * * @param text The block of text or to be parsed (Required) * @param acronyms A structure containing the set of acronyms (or abbreviations, or words). The key is the acronym and the value is the definition. (Required) * @param scope One: replace just the first appearance of the acronym (default); All: replace all occurences. * @param caseSensitive No: perform a case-insensitive search for acronyms (default); Yes: perform a case-sensitive search for acronyms * @param skipTags No: search for acronyms even if within tags. This is faster and suitable for non- data; Yes: avoid searching for acronyms inside and similar tags (that is between the < and the >. The function will still identify acronyms between opening and closing tags. (default) * @param tag Tag to wrap around acronym, e.g. "acronym" (default), "abbr", "em". If blank then no tag will be wrapped around the acronym. * @param parentheses Acronym: provide the acronym in parentheses after the definition; definition: provide the definition in parentheses after the acronym; [all other values]: provide no parentheses (default). If this attribute is set to "acronym" or "definition", it may be preferable to set the tag attribute to "". * @return string * @author Matthew Walker, WWW.eswsoftware.com * @version 1.1, 2005-03-21 Bug where acronyms abutted with a number in between * @version 1, 2004-04-07 ---> <cffunction name="expandAcronyms" returntype="string" output="no"> <cfargument name="text" type="string" required="yes"> <cfargument name="acronyms" type="struct" required="yes"> <cfargument name="scope" type="string" default="one"> <cfargument name="caseSensitive" type="boolean" default="no"> <cfargument name="skipTags" type="boolean" default="yes"> <cfargument name="tag" type="string" default="acronym"> <cfargument name="parentheses" type="string" default="none"> <cfset var definition = ""> <cfset var result = arguments.text> <cfset var esc = chr(27)> <cfset var replacements = arraynew(1)> <cfset var placeholder = ""> <cfset var acronymLengths = structNew()> <cfset var sortedAcronyms = ""> <cfset var i = 0> <cfset var replacement = ""> <cfset var notDone = true> <cfset var pos = 1> <cfset var nextTag = ""> <cfparam name="request.eswsoftware" default="#structNew()#"> <cfparam name="request.eswsoftware.expandAcronyms" default="#structNew()#"> <cfset request.eswsoftware.expandAcronyms.version = 1.1> <!--- In order to catch cases where one acronym contains a shorter acronym, we'll process the longest first. So we sort acronyms in descending order of length. ---> <cfloop collection="#arguments.acronyms#" item="acronym"> <cfset acronymLengths[acronym] = len(acronym)> </cfloop> <cfset sortedAcronyms = structsort(acronymLengths, "numeric", "desc")> <cfif arguments.skipTags> <!--- replace all tags with a placeholder so that we don't accidentally identify acronyms within the tags. ---> <cfloop condition="#notDone#"> <cfset nextTag = reFind("<[^>]+>", result, pos, true)> <cfif nextTag.pos[1]> <cfset replacement = mid(result, nextTag.pos[1], nextTag.len[1])> <cfset arrayappend(replacements, replacement)> <cfset placeholder = "#esc##arraylen(replacements)#;"> <cfset result = replace(result, replacement, placeholder, "all")> <cfelse> <cfset notDone = false> </cfif> </cfloop> </cfif> <!--- Replace each found acronym with a placeholder. ---> <cfloop from="1" to="#arraylen(sortedAcronyms)#" index="i"> <cfset acronym = sortedAcronyms[i]> <cfset definition = arguments.acronyms[acronym]> <cfif len(arguments.tag)> <cfset replacement = "<#arguments.tag# title=""#definition#"">#acronym#</#arguments.tag#>"> <cfelse> <cfset replacement = acronym> </cfif> <cfswitch expression="#arguments.parentheses#"> <cfcase value="acronym"> <cfset replacement = "#definition# (#replacement#)"> </cfcase> <cfcase value="definition"> <cfset replacement = replacement & " (#definition#)"> </cfcase> </cfswitch> <cfset arrayappend(replacements, replacement)> <cfset placeholder = "#esc##arraylen(replacements)#;"> <cfif arguments.caseSensitive> <cfset result = reReplace(result, "\b#acronym#\b", placeholder, arguments.scope)> <cfelse> <cfset result = reReplaceNoCase(result, "\b#acronym#\b", placeholder, arguments.scope)> </cfif> </cfloop> <!--- Replace all the placeholders with the desired text ---> <cfloop index="i" from="1" to="#arraylen(replacements)#"> <cfset placeholder = "#esc##i#;"> <cfset result = replace(result, placeholder, replacements[i], "all")> </cfloop> <cfreturn result> </cffunction>
World Wide Web
