highlight.scm 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. ;;; This stuff snarfed from David Thompson
  2. ;;; so Copyright © 2018-2021 David Thompson <davet@gnu.org>
  3. ;;; and GPLv3+
  4. ;; https://git.dthompson.us/blog.git/tree/highlight.scm
  5. (define-module (highlight)
  6. #:use-module (ice-9 match)
  7. #:use-module (sxml match)
  8. #:use-module (syntax-highlight)
  9. #:use-module (syntax-highlight scheme)
  10. #:use-module (syntax-highlight xml)
  11. #:use-module (syntax-highlight c)
  12. #:export (highlight-code
  13. highlight-scheme))
  14. (define (maybe-highlight-code lang source)
  15. (let ((lexer (match lang
  16. ('scheme lex-scheme)
  17. ('xml lex-xml)
  18. ('c lex-c)
  19. (_ #f))))
  20. (if lexer
  21. (highlights->sxml (highlight lexer source))
  22. source)))
  23. (define (highlight-code . tree)
  24. (sxml-match tree
  25. ((code (@ (class ,class) . ,attrs) ,source)
  26. (let ((lang (string->symbol
  27. (string-drop class (string-length "language-")))))
  28. `(code (@ ,@attrs)
  29. ,(maybe-highlight-code lang source))))
  30. (,other other)))
  31. (define (highlight-scheme code)
  32. `(pre (code ,(highlights->sxml (highlight lex-scheme code)))))