guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[potluck dish] simple syntax highlighter


From: David Thompson
Subject: [potluck dish] simple syntax highlighter
Date: Tue, 16 Feb 2016 15:08:45 -0500
User-agent: Notmuch/0.21 (http://notmuchmail.org) Emacs/24.5.1 (x86_64-unknown-linux-gnu)

Hello Guilers,

I was worried that I didn't have anything to bring to the potluck, but
then I remembered that I had a library in the works that I haven't
announced here yet!

As some of you may know, I occasionally hack on a static site generator
called Haunt[0].  As I started my migration away from Pelican (written
in Python), I realized that I would be losing the syntax highlighting
features of the Pygments library.  Rather than shell-out to the Pygments
CLI (cheating ;), I decided to write my own syntax highlighting library
for Guile.

Guile-syntax-highlight is written in a simple monadic parser combinator
style.  It reads text from a string or port and produces a list of
tag+token tuples indicating what syntax element a string of text
corresponds to.  This list can be passed to the built-in
highlights->sxml procedure to produce SXML ready to be rendered as HTML
on your blog or whatever.

Here's an example:

    (use-modules (syntax-highlight)
                 (syntax-highlight scheme))
    
    (define code
      '(define (hello name)
         (display "Hello, ")
         (display name)
         (display "!\n")
         (newline)))
    
    (highlights->sxml (highlight lex-scheme (format #f "~s" code)))

SXML:

    ((span (@ (class "syntax-open")) "(")
     (span (@ (class "syntax-special")) "define")
     " "
     (span (@ (class "syntax-open")) "(")
     (span (@ (class "syntax-symbol")) "hello")
     " "
     (span (@ (class "syntax-symbol")) "name")
     (span (@ (class "syntax-close")) ")")
     " "
     (span (@ (class "syntax-open")) "(")
     (span (@ (class "syntax-symbol")) "display")
     " "
     (span (@ (class "syntax-string")) "\"Hello, \"")
     (span (@ (class "syntax-close")) ")")
     " "
     (span (@ (class "syntax-open")) "(")
     (span (@ (class "syntax-symbol")) "display")
     " "
     (span (@ (class "syntax-symbol")) "name")
     (span (@ (class "syntax-close")) ")")
     " "
     (span (@ (class "syntax-open")) "(")
     (span (@ (class "syntax-symbol")) "display")
     " "
     (span (@ (class "syntax-string")) "\"!\\n\"")
     (span (@ (class "syntax-close")) ")")
     " "
     (span (@ (class "syntax-open")) "(")
     (span (@ (class "syntax-symbol")) "newline")
     (span (@ (class "syntax-close")) ")")
     (span (@ (class "syntax-close")) ")"))

Right now, I have highlighters for Scheme and XML, and I am working
(slowly) on one for C.  They aren't perfect by any means, so patches to
improve them are more than welcome. :)

Attached also are some screenshots of my WIP new blog showing off the
syntax highlighter.

Attachment: guile-syntax-highlight-1.png
Description: PNG image

Attachment: guile-syntax-highlight-2.png
Description: PNG image

Get the code here:

  https://git.dthompson.us/guile-syntax-highlight.git

The easiest way to play around with the code is with Guix.  From the
root of the source tree, Guix users can install the latest development
snapshot like so:

    guix package -f guix.scm

Or, you can simply launch a temporary Guile REPL and play:

    guix environment --ad-hoc -l guix.scm guile -- guile

Or, you can create a development environment if you want to hack the
source:

    guix environment -l guix.scm

There is no official release yet.  Maybe some day.  :)

Itadakimasu!

-- 
David Thompson
GPG Key: 0FF1D807

[0] http://haunt.dthompson.us

reply via email to

[Prev in Thread] Current Thread [Next in Thread]