kaka.farm

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

weechatlog.scm (1927B)


      1 (define-library (haunt kakafarm weechatlog)
      2   (import (scheme base)
      3           (srfi :9)
      4           (srfi :19)
      5           (ice-9 match)
      6           (ice-9 peg))
      7   (export parse-weechatlog
      8           weechatlog-line?
      9           weechatlog-datetime
     10           weechatlog-user-status
     11           weechatlog-nickname
     12           weechatlog-message)
     13 
     14   (begin
     15     (define-record-type <weechatlog-line>
     16       (make-weechatlog-line datetime
     17                             user-status
     18                             nickname
     19                             message)
     20       weechatlog-line?
     21       (datetime weechatlog-datetime)
     22       (user-status weechatlog-user-status)
     23       (nickname weechatlog-nickname)
     24       (message weechatlog-message))
     25 
     26     (define-peg-string-patterns
     27       "weechatlog <-- (NL* weechatlog-line)* NL*
     28 weechatlog-line <-- datetime-field T BRA user-status nickname-field KET T message-field
     29 datetime-field <-- (!NL !T .)*
     30 nickname-field <-- (!NL !T !KET .)*
     31 message-field <-- (!NL .)*
     32 user-status <-- (.)
     33 BRA < '<'
     34 KET < '>'
     35 T < '\t'
     36 NL < '\n'")
     37 
     38 
     39     (define (parse-datetime str)
     40       (string->date str "~Y-~m-~dT~H:~M:~S~z"))
     41 
     42     (define (parse-weechatlog str)
     43       (let ((tree (peg:tree (match-pattern weechatlog str))))
     44         (match tree
     45           (`(weechatlog
     46              . ,weechatlog-lines)
     47            (map parse-weechatlog-line
     48                 weechatlog-lines))
     49           (else
     50            (error "Bad weechatlog:" tree)))))
     51 
     52     (define (parse-weechatlog-line tree)
     53       (match tree
     54         (`(weechatlog-line (datetime-field ,datetime-field)
     55                            (user-status ,user-status)
     56                            (nickname-field ,nickname-field)
     57                            (message-field ,message-field))
     58          (make-weechatlog-line (parse-datetime datetime-field)
     59                                user-status
     60                                nickname-field
     61                                message-field))))))