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

* [potluck dish] crawling the Freenet WoT with sxml and match
  2016-02-16 20:08 [potluck dish] simple syntax highlighter David Thompson
@ 2016-02-16 22:37 ` Arne Babenhauserheide
  0 siblings, 0 replies; 2+ messages in thread
From: Arne Babenhauserheide @ 2016-02-16 22:37 UTC (permalink / raw
  To: David Thompson; +Cc: guile-user


[-- Attachment #1.1: Type: text/plain, Size: 1530 bytes --]

Hi Guilers,

I’d like to contribute a small dish, I’ve been working on during the
past two weeks.

A first script downloads xml files of anonymous identities in Freenet,
extracts trust given to other identities and downloads these, too. A
second script parses the identities and turns them into a csv file which
social network researchers can use to investigate the effects of
anonymity on social interaction (at least that’s the goal). A third and
fourth script anonymize and deduplicate the results.

What I like best is using match and sxml to extract the relevant
identifiers, though I’m using mutation to separate the match in
the let-recursion and a final cons which saves the filename:


    (define (parse-trust-values filename)
      (let* ((port (open-input-file filename))
             (sxml (non-breaking-sxml-reader port))
             (closed (close-port port))
             (trust '()))
        (let extract-trust ((sxml sxml))
          (match sxml
            (('Trust ('@ ('Value value) ('Identity uri) rest ...))
             (set! trust
               (cons (cons (wot-uri-key uri)
                           (string->number value))
                     trust)))
            ((a b ...)
             (map extract-trust sxml))
            (else '())))
        (cons (wot-file-key filename) trust)))


I attached a network visualization generated from the data. Some
explanation is available from my website:
http://draketo.de/english/freenet/social-graph-snapshot


[-- Attachment #1.2: Sone social graph in Reingold Layout --]
[-- Type: image/png, Size: 976091 bytes --]

[-- Attachment #1.3: Type: text/plain, Size: 604 bytes --]


The code is available in the guile-freenet repository on
https://notabug.org/arnebab/guile-freenet and on Bitbucket
https://bitbucket.org/ArneBab/freenet-guile and from a static clone:
http://draketo.de/proj/guile-freenet/


Happy Hacking!
Arne Babenhauserheide

PS: I wrote this code without wisp to make it possible to run it with
    Guile out-of-the-box. To be suitable for programs I share, I’d need
    either guildhall integrated into Guile (to provide wisp with just a
    single command) or wisp itself in Guile.
-- 
Unpolitisch sein
heißt politisch sein
ohne es zu merken

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 298 bytes --]

^ 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).