;; highlight.scm -- source code highlighting library ;; Copyright (c) 2011 Alex Shinn. All rights reserved. ;; BSD-style license: http://synthcode.com/license.txt ;;> Library for highlighting source code in different ;;> languages. Currently supports Scheme, C and Assembly. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define (string-concatenate-reverse ls) (string-concatenate (reverse ls))) (define (reverse-list->string ls) (list->string (reverse ls))) ;;> Returns an sxml structure representing the code from source ;;> with various language constructs wrapped in highlighting ;;> forms. @var{source} should be a string or port. The ;;> language to highlight for is auto-detected. (define (highlight source) (let ((str (if (string? source) source (port->string source)))) ((highlighter-for (highlight-detect-language str)) str))) ;;> Attempst to auto-detect which language @var{str} is code ;;> for, and returns a symbol representing that language. (define (highlight-detect-language str) (cond ((guard (exn (else #f)) (call-with-input-string str (lambda (in) (do ((x #f (read in))) ((eof-object? x))))) #t) 'scheme) (else 'c))) ;;> Return a procedure for highlighting the given language. (define (highlighter-for language) (case language ((scheme) highlight-scheme) ((asm) highlight-assembly) ((none) (lambda (x) x)) (else highlight-c))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define highlight-themes '((light (keyword . "#800080") (type . "#008000") (function . "#0000FF") (variable . "#B8860B") (comment . "#FF0000") (string . "#BC8F8F") (attribute . "#FF5000") (preprocessor . "#FF00FF") (builtin . "#FF00FF") (character . "#0055AA") (syntaxerror . "#FF0000") (diff-deleted . "#5F2121") (diff-added . "#215F21") ))) (define highlight-paren-styles ;;'("#BAFFFF" "#FFCACA" "#FFFFBA" "#CACAFF" "#CAFFCA" "FFBAFF") '("#AAAAAA" "#888888" "#666666" "#444444" "#222222" "#000000")) ;;> Returns a string representing the CSS needed for the output ;;> of @var{highlight}. This should be included in a referenced ;;> CSS file, or in a @var{