unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* [potluck dish] simple syntax highlighter
@ 2016-02-16 20:08 David Thompson
  2016-02-16 22:37 ` [potluck dish] crawling the Freenet WoT with sxml and match Arne Babenhauserheide
  0 siblings, 1 reply; 2+ messages in thread
From: David Thompson @ 2016-02-16 20:08 UTC (permalink / raw)
  To: guile-user

[-- Attachment #1: Type: text/plain, Size: 2596 bytes --]

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 #2: guile-syntax-highlight-1.png --]
[-- Type: image/png, Size: 128600 bytes --]

[-- Attachment #3: guile-syntax-highlight-2.png --]
[-- Type: image/png, Size: 71014 bytes --]

[-- Attachment #4: Type: text/plain, Size: 633 bytes --]


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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2016-02-16 22:37 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-02-16 20:08 [potluck dish] simple syntax highlighter David Thompson
2016-02-16 22:37 ` [potluck dish] crawling the Freenet WoT with sxml and match Arne Babenhauserheide

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).