From: David Thompson <dthompson2@worcester.edu>
To: guile-user@gnu.org
Subject: [potluck dish] simple syntax highlighter
Date: Tue, 16 Feb 2016 15:08:45 -0500 [thread overview]
Message-ID: <877fi4fmeq.fsf@izanagi.i-did-not-set--mail-host-address--so-tickle-me> (raw)
[-- 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
next reply other threads:[~2016-02-16 20:08 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-02-16 20:08 David Thompson [this message]
2016-02-16 22:37 ` [potluck dish] crawling the Freenet WoT with sxml and match Arne Babenhauserheide
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/guile/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=877fi4fmeq.fsf@izanagi.i-did-not-set--mail-host-address--so-tickle-me \
--to=dthompson2@worcester.edu \
--cc=guile-user@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).