How is this different from codemirror? Codemirror's preview utility easily handles this (and much more versatile in other ways):
http://codemirror.net/
http://codemirror.net/demo/preview.html