Last active
November 30, 2015 02:36
-
-
Save jl2/f61b8a09e87b05d0cde9 to your computer and use it in GitHub Desktop.
Quick and dirty Common Lisp function to generate static HTML from a gist URL.
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
(defun make-static-gist (gist-url) | |
"Retrieve a Gist from GitHub and format it as static CSS and HTML." | |
(let* ((nl-string (format nil "~c" #\newline)) | |
;; Make sure the url has ".js" on the end | |
(ending (subseq gist-url (- (length gist-url) 3))) | |
(js-url (if (string= ending ".js") | |
gist-url | |
(concatenate 'string gist-url ".js"))) | |
;; Fetch the embedded gist from GitHub | |
(gist-data (drakma:http-request js-url))) | |
;; Read the string data | |
(with-input-from-string (strm gist-data) | |
(let* ( | |
;; Get the CSS link | |
(css-html (read-line strm)) | |
;; Get the document data | |
(doc-part (read-line strm)) | |
;; Parse out the CSS URL and fetch it | |
;; 45 is the length of "document.write...", | |
;; 4 is the length of "\");" on the end | |
(css-url (subseq css-html 45 (- (length css-html) 4))) | |
(style-sheet (drakma:http-request css-url)) | |
;; Remove \n and \ from the html | |
(escaped-html (subseq doc-part 16 (- (length doc-part) 4))) | |
(html-nl (cl-ppcre:regex-replace-all "\\\\n" escaped-html nl-string)) | |
(raw-html (cl-ppcre:regex-replace-all "\\" html-nl ""))) | |
;; Print everything out | |
(format t "~a~%" style-sheet) | |
(format t "~a~%" raw-html))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment