Created
April 14, 2012 16:39
-
-
Save cfjedimaster/2385721 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!--- generic function to replace gists with raw ---> | |
<cffunction name="replaceGistWithRaw" output="false" returnType="string"> | |
<cfargument name="input" type="string" required="true"> | |
<cfargument name="prefix" type="string" required="false" default="<pre>"> | |
<cfargument name="postfix" type="string" required="false" default="</pre>"> | |
<!--- This is a prefix used for our cache. It ensures all our values share a common prefix and won't conflict with others. ---> | |
<cfset var CACHE_KEY = "gist_"> | |
<cfset var x = ""> | |
<cfset var result = ""> | |
<cfset var raw = ""> | |
<cfset var matches = reMatchNoCase("<script src=""https://gist\.github\.com/[0-9]+\.js\?file=.*?""></script>",input)> | |
<!--- loop backwards incase a replacement adds an exampe of a gist, ie, a gist of a gist ---> | |
<cfloop index="x" from="#arrayLen(matches)#" to="1" step="-1"> | |
<cfset var match = matches[x]> | |
<cfset var gistid = reMatchNoCase("[0-9]+(?=\.js)",match)[1]> | |
<cfset var filename = rematchNoCase("(?=file=).*?(?="")",match)> | |
<!--- oddly, file= is still there ---> | |
<cfset filename = replacenocase(filename[1], "file=","")> | |
<!--- Before we bother fetching the content, see if we have it in cache. ---> | |
<cfset raw = cacheGet(CACHE_KEY & gistid & filename)> | |
<cfif isNull(raw)> | |
<cfset var gistURL = "https://api.github.com/gists/#gistid#"> | |
<cfhttp url="#gisturl#" result="result"> | |
<cfset var data = deserializeJSON(result.fileContent)> | |
<cfif structKeyExists(data.files, filename)> | |
<cfset var raw = data.files[filename].content> | |
<cfelse> | |
<cfset raw = ""> | |
</cfif> | |
<!--- note we cache even a bad response so as to not keep hitting the remote url ---> | |
<cfset cachePut(CACHE_KEY & gistid & filename, raw, createTimeSpan(0,1,0,0))> | |
</cfif> | |
<cfset input = replaceNoCase(input, match, prefix & chr(10) & htmlEditFormat(raw) & chr(10) & postfix)> | |
</cfloop> | |
<cfreturn input> | |
</cffunction> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment