| 1 | #!/usr/local/bin/gosh |
|---|
| 2 | |
|---|
| 3 | (use srfi-1) |
|---|
| 4 | (use util.list) |
|---|
| 5 | (use rfc.uri) |
|---|
| 6 | |
|---|
| 7 | ;; |
|---|
| 8 | ;; "20081231" => "2008_12_31" |
|---|
| 9 | ;; |
|---|
| 10 | (define (convert-date date) |
|---|
| 11 | (let ((y (substring date 0 4)) |
|---|
| 12 | (m (substring date 4 6)) |
|---|
| 13 | (d (substring date 6 8))) |
|---|
| 14 | (string-append y "_" m "_" d))) |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | ;; |
|---|
| 18 | ;; "<%=a 'link|str' %> => [[link|str]] |
|---|
| 19 | ;; "<%=isbn_image 'id' %> => ##(amazon id) |
|---|
| 20 | ;; |
|---|
| 21 | (define (convert-command str) |
|---|
| 22 | (let* ((str |
|---|
| 23 | (regexp-replace-all #/<%=[ ]?a[ ]+'([^|]+)[|]([^']+)'[ ]+%>/ #?=str |
|---|
| 24 | (lambda (m) |
|---|
| 25 | (string-append "[[" |
|---|
| 26 | #?=(rxmatch-substring m 2) |
|---|
| 27 | "|" |
|---|
| 28 | #?=(rxmatch-substring m 1) |
|---|
| 29 | "]]")))) |
|---|
| 30 | (str |
|---|
| 31 | (regexp-replace-all #/<%=[ ]?isbn_image[ ]+'([^']+)'[ ]+%>/ #?=str |
|---|
| 32 | (lambda (m) |
|---|
| 33 | (string-append "##(amazon " |
|---|
| 34 | (rxmatch-substring m 1) |
|---|
| 35 | ")"))))) |
|---|
| 36 | #?=str)) |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | (define (output-oldtype-file username date entry-data) |
|---|
| 40 | |
|---|
| 41 | (define (display-diary entry port) |
|---|
| 42 | (for-each |
|---|
| 43 | (lambda (lst) |
|---|
| 44 | (when (string? (car lst)) |
|---|
| 45 | (begin |
|---|
| 46 | (display (convert-command (car lst)) port) |
|---|
| 47 | (newline port) |
|---|
| 48 | (newline port)))) |
|---|
| 49 | entry)) |
|---|
| 50 | |
|---|
| 51 | (define (display-comment entry port) |
|---|
| 52 | (let1 name (assq-ref entry 'Name:) |
|---|
| 53 | (for-each |
|---|
| 54 | (lambda (x) |
|---|
| 55 | (let1 str (car x) |
|---|
| 56 | (when (string? str) |
|---|
| 57 | (when (not (#/http:\/\// str)) |
|---|
| 58 | (begin |
|---|
| 59 | (display (format "##(comment-data ~a ~a)" |
|---|
| 60 | (uri-encode-string name) |
|---|
| 61 | (uri-encode-string str)) |
|---|
| 62 | port) |
|---|
| 63 | (newline port)))))) |
|---|
| 64 | entry))) |
|---|
| 65 | |
|---|
| 66 | (call-with-output-file (format "./out/~a.~a.ot" username (convert-date date)) |
|---|
| 67 | (lambda (port) |
|---|
| 68 | port |
|---|
| 69 | (begin |
|---|
| 70 | (for-each |
|---|
| 71 | (lambda (entry) ;; entry is a diary or a comment. |
|---|
| 72 | (cond |
|---|
| 73 | ((assq-ref entry 'Title:) |
|---|
| 74 | ;; diary |
|---|
| 75 | (display-diary entry port)) |
|---|
| 76 | ;; comment |
|---|
| 77 | (else |
|---|
| 78 | (display-comment entry port)))) |
|---|
| 79 | entry-data) |
|---|
| 80 | (display "##(comment)" port) |
|---|
| 81 | (newline port))))) |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | (define (save-oldtype-data username diary-data comment-data) |
|---|
| 86 | (let1 h (make-hash-table 'string=?) |
|---|
| 87 | ;; make hash data ( key is string of date , value is alist of diary or comment) |
|---|
| 88 | (for-each |
|---|
| 89 | (lambda (pair) |
|---|
| 90 | (hash-table-push! h (car pair) (cdr pair))) |
|---|
| 91 | (append |
|---|
| 92 | diary-data |
|---|
| 93 | comment-data |
|---|
| 94 | )) |
|---|
| 95 | |
|---|
| 96 | (hash-table-for-each |
|---|
| 97 | h |
|---|
| 98 | (lambda (k v) |
|---|
| 99 | (output-oldtype-file username k (reverse v)))))) |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | (define (main argv) |
|---|
| 103 | (let ((username (cadr argv)) |
|---|
| 104 | (diary-file (caddr argv)) |
|---|
| 105 | (comment-file (cadddr argv))) |
|---|
| 106 | (let ((diary-data |
|---|
| 107 | (call-with-input-file diary-file read)) |
|---|
| 108 | (comment-data |
|---|
| 109 | (call-with-input-file comment-file read))) |
|---|
| 110 | (save-oldtype-data |
|---|
| 111 | username |
|---|
| 112 | diary-data |
|---|
| 113 | comment-data)))) |
|---|
| 114 | |
|---|