Sunday, April 02, 2006

Paste Scheme

[Note: This post shows up in Planet Scheme with extra line breaks. If you know how to fix this, please send me a mail. Editing the original trying to fix the linebreak issue unfortunately tricks the Planet software to think it is a new post.]



Blogging about Scheme leads to the desire of including snippets of Scheme source. Being spoiled by the DrScheme syntax highlighting this means syntax colored snippets. Luckily Dorai Sitaram wrote some syntax coloring code for SLaTeX. This code was adapted/rewritten by Anton van Straten for the Scheme Cookbook. Using this this code I have put together a little servlet Paste Scheme which lets you submit a scheme snippet and returns XHTML ready to paste into your favorite blogging software.



The servlet source below was produced with Paste Scheme. The web.plt package haven't been submitted to PLaneT yet, since there is no documentation except for comments in the source yet.



;;; paste.ss  --  Jens Axel Soegaard

(module paste mzscheme
(provide interface-version timeout start)

(require (lib "match.ss")
(lib "kw.ss")
(planet "web.scm" ("soegaard" "web.plt"))
(planet "html.scm" ("soegaard" "web.plt"))
"scm2xexpr.scm")

;;;
;;; SERVLET INTERFACE
;;;

(define interface-version 'v1)
(define timeout 6000)

(define start
; servlet sets up the various parameters such as
; current-bindings and current-cookies, evaluates
; the body expressiosns, the last of which should
; evaluate to an xepxr.
(servlet
(report-errors-to-browser send/finish)
(html-paste-page)))

;;;
;;; VIEW
;;;

(define default-snippet "
(define (fact n)
(if (= n 0)
1
(* n (f (- n 1)))))"
)

(define (html-paste-page)
(with-binding (current-bindings) (snippet)
(let ([snippet (if snippet snippet default-snippet)])
(html-page
#:title "Paste Scheme"
#:style-sheet "http://localhost:8080/paste.css"
#:header '(h1 "Scheme Paste")
#:body `(div (h2 "Enter Snippet")
,(html-form
"submit_snippet" "paste.ss"
`(textarea ((name "snippet") (cols "80") (rows "10"))
,snippet)
'(br)
(html-input "submit" #:value "submit"))
(h2 "Preview")
,(scheme-text->xexpr snippet)
(h2 "XHTML")
(pre
,(xexpr->string
(scheme-text->xexpr snippet)))
(h2 "Stylesheet")
(pre ,"
.scheme { color: brown; margin: 4pt; } /* background punctuation */
.scheme .keyword { color: rgb(68,0,203); font-weight: bold; }
.scheme .builtin { color: navy; }
.scheme .variable { color: black; }
.scheme .global { color: purple; }
.scheme .selfeval { color: green; }
.scheme .comment { color: teal; }
"
))))))

)

5 Comments:

Blogger iamfscked said...

The paste scheme resulting XHTML doesn't work in Safari.

02:12  
Blogger Jens Axel Søgaard said...

Did you forget to include the stylesheet?

If it's something else, please send me a screenshot - I don't have a mac.

12:22  
Blogger Jens Axel Søgaard said...

I tried www.browsershots.org to see what was wrong. At first I tried this blog and the syntax highlighting worked fine. Then I tried the Paste Scheme page. Lo and behold you were right. Not only did Safari not show any colors, but none did! It turns out the link to the stylesheet were to http://localhost/paste.css so it worked perfectly on my machine.

Thanks for the tip.

00:21  
Blogger Peter Schombert said...

I would love to use Paste Scheme in my blog, but I can't add style sheets/xhtml. Would it be possible to create a version that spits out the results using some combination of pre/font/color tags?

00:30  
Blogger Jens Axel Søgaard said...

You use WordPress right? I thought you change your template to include the extra style sheet lines.

Anyways, try this right before the xhtml from Paste Scheme:

<style type="text/css">
.scheme { color: brown; margin: 4pt; }
.scheme .keyword { color: rgb(68,0,203); font-weight: bold; }
.scheme .builtin { color: navy; }
.scheme .variable { color: black; }
.scheme .global { color: purple; }
.scheme .selfeval { color: green; }
.scheme .comment { color: teal; }">
</style>

If that doesn't display properly, I'll think of something else.

00:46  

Post a Comment

Links to this post:

Create a Link

<< Home