|
2 | 2 |
|
3 | 3 | (defpackage #:chronogram-cldr-parser
|
4 | 4 | (:use #:cl #:esrap)
|
| 5 | + (:local-nicknames (:jzon :com.inuoe.jzon)) |
5 | 6 | (:export #:parse-cldr))
|
6 | 7 |
|
7 | 8 | (in-package #:chronogram-cldr-parser)
|
8 | 9 |
|
9 |
| -(defun parse-cldr (contents) |
10 |
| - 'test) |
| 10 | +(defun clss-select-first (selector node) |
| 11 | + (let ((results (clss:select selector node))) |
| 12 | + (when (plusp (length results)) |
| 13 | + (elt results 0)))) |
| 14 | + |
| 15 | +(defmacro defun-process-entities (entity selector value-form) |
| 16 | + (let ((sym (intern (string-upcase entity)))) |
| 17 | + (alexandria:with-gensyms (entity-type) |
| 18 | + `(defun ,(intern (concatenate 'string "PROCESS-" (string-upcase entity))) |
| 19 | + (entity-parent) |
| 20 | + (jzon:with-object* |
| 21 | + (loop :for ,sym :across (clss:select ,selector entity-parent) |
| 22 | + :for ,entity-type := (plump:get-attribute ,sym "type") |
| 23 | + :when ,entity-type |
| 24 | + :do (progn |
| 25 | + (jzon:write-key* ,entity-type) |
| 26 | + ,value-form))))))) |
| 27 | + |
| 28 | +;; Months |
| 29 | +(defun-process-entities month "month" |
| 30 | + (jzon:write-value* (plump:text month))) |
| 31 | +(defun-process-entities month-width "monthWidth" |
| 32 | + (process-month month-width)) |
| 33 | +(defun-process-entities month-context "months > monthContext" |
| 34 | + (process-month-width month-context)) |
| 35 | + |
| 36 | +;; Days |
| 37 | +(defun-process-entities day "day" |
| 38 | + (jzon:write-value* (plump:text day))) |
| 39 | +(defun-process-entities day-width "dayWidth" |
| 40 | + (process-day day-width)) |
| 41 | +(defun-process-entities day-context "days > dayContext" |
| 42 | + (process-day-width day-context)) |
| 43 | + |
| 44 | +;; Quarters |
| 45 | +(defun-process-entities quarter "quarter" |
| 46 | + (jzon:write-value* (plump:text quarter))) |
| 47 | +(defun-process-entities quarter-width "quarterWidth" |
| 48 | + (process-quarter quarter-width)) |
| 49 | +(defun-process-entities quarter-context "quarters > quarterContext" |
| 50 | + (process-quarter-width quarter-context)) |
| 51 | + |
| 52 | +;; Day periods |
| 53 | +(defun-process-entities day-period "dayPeriod" |
| 54 | + (jzon:write-value* (plump:text day-period))) |
| 55 | +(defun-process-entities day-period-width "dayPeriodWidth" |
| 56 | + (process-day-period day-period-width)) |
| 57 | +(defun-process-entities day-period-context "dayPeriods > dayPeriodContext" |
| 58 | + (process-day-period-width day-period-context)) |
| 59 | + |
| 60 | +;; Calendars |
| 61 | + |
| 62 | +(defun-process-entities calendar "ldml > dates > calendars > calendar" |
| 63 | + (jzon:with-object* |
| 64 | + (jzon:write-key* "months") |
| 65 | + (process-month-context calendar) |
| 66 | + |
| 67 | + (jzon:write-key* "days") |
| 68 | + (process-day-context calendar) |
| 69 | + |
| 70 | + (jzon:write-key* "quarters") |
| 71 | + (process-quarter-context calendar) |
| 72 | + |
| 73 | + (jzon:write-key* "day-periods") |
| 74 | + (process-day-period-context calendar))) |
| 75 | + |
| 76 | +(defun parse-cldr (contents out-stream) |
| 77 | + (let ((root (plump:parse contents))) |
| 78 | + (jzon:with-writer* (:stream out-stream :pretty nil) |
| 79 | + (jzon:with-object* |
| 80 | + (jzon:write-key* "calendars") |
| 81 | + (process-calendar root))))) |
| 82 | + |
| 83 | + |
0 commit comments