add-to-head.rkt (1688B)
1 #lang racket 2 3 ;; Realm of kludge: 4 ;; 5 ;; AFIK no way via Scribble to put something into the <head> section. 6 ;; 7 ;; This reads all HTML files and injects some stuff immediately before the 8 ;; </head> closing tag. 9 10 (define web-font 11 "<link href='http://fonts.googleapis.com/css?family=Fenix' rel='stylesheet' type='text/css'>") 12 13 (define ga-code 14 #<<EOF 15 <script type="text/javascript"> 16 var _gaq = _gaq || []; 17 _gaq.push(['_setAccount', 'UA-29709446-1']); 18 _gaq.push(['_setDomainName', 'greghendershott.com']); 19 _gaq.push(['_trackPageview']); 20 (function() { 21 var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; 22 ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; 23 var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); 24 })(); 25 </script> 26 EOF 27 ) 28 29 (define (meta k v) 30 (format "<meta name=\"~a\" content=\"~a\">" k v)) 31 32 (define metas 33 (string-append 34 (meta "keywords" "Racket,macros,Scheme") 35 (meta "description" "Practical Racket macros") 36 (meta "author" "Greg Hendershott") 37 (meta "charset" "utf-8"))) 38 39 (define </head> "</head>") 40 41 (define all (string-append metas web-font ga-code </head>)) 42 (define subst (regexp-replace* "\n" all "")) ;minify 43 44 (define (do-file path) 45 (define old (file->string path)) 46 (define new (regexp-replace </head> old subst)) 47 (with-output-to-file path 48 (lambda () (display new)) 49 #:mode 'text 50 #:exists 'replace)) 51 52 (require racket/runtime-path) 53 (define-runtime-path here ".") 54 (for ([path (find-files (lambda (path) 55 (regexp-match? #rx"\\.html" path)) 56 here)]) 57 (do-file path))