Skip to content

Commit 5fa7852

Browse files
committed
Parse calendar months, days, quarters and day-periods
1 parent f015ff7 commit 5fa7852

File tree

3 files changed

+116
-11
lines changed

3 files changed

+116
-11
lines changed

chronogram-cldr-parser.lisp

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,82 @@
22

33
(defpackage #:chronogram-cldr-parser
44
(:use #:cl #:esrap)
5+
(:local-nicknames (:jzon :com.inuoe.jzon))
56
(:export #:parse-cldr))
67

78
(in-package #:chronogram-cldr-parser)
89

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+

chronogram-dist.lisp

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,52 @@
1515
(format nil "https://github.com/unicode-org/cldr/archive/refs/tags/~a.zip" tag))
1616

1717
(defun make-chronogram-info (contents dist-dir name)
18-
(with-open-file (stream (format nil "~a~a.lisp" dist-dir name)
18+
(with-open-file (stream (format nil "~a~a.json" dist-dir name)
1919
:direction :output
2020
:if-exists :supersede
2121
:if-does-not-exist :create)
2222
(let ((*package* (find-package 'chronogram-cldr-parser)))
23-
(format stream "~s~%" (chronogram-cldr-parser:parse-cldr contents)))))
23+
(chronogram-cldr-parser:parse-cldr contents
24+
stream))))
2425

2526
(defun make-dist (&optional force-download)
2627
(let* ((system (asdf:find-system 'chronogram t))
2728
(dist-dir (asdf:system-relative-pathname system "chronogram-dist/"))
28-
(tz-submodule-available
29+
(cldr-submodule-available
2930
(uiop:file-exists-p
30-
(asdf:system-relative-pathname system "cldr/LICENSE")))
31-
(tag (when (or force-download (not tz-submodule-available))
32-
(get-cldr-release-tag system))))
31+
(asdf:system-relative-pathname system "cldr/unicode-license.txt")))
32+
(tag (when (or force-download (not cldr-submodule-available))
33+
(get-cldr-release-tag system)))
34+
(names '("en" "de" "hu" "ko")))
3335
(ensure-directories-exist dist-dir)
34-
;; DUMMY
35-
(make-chronogram-info "" dist-dir "test")))
36+
(if tag
37+
(uiop:with-temporary-file (:stream s)
38+
(format t "Downloading cldr release ~a... " tag)
39+
(force-output)
40+
(let ((bytes (dex:get (get-archive-url tag))))
41+
(write-sequence bytes s)
42+
(finish-output s))
43+
(format t "DONE~%")
44+
(zip:with-zipfile (f s)
45+
(loop :for name :in names
46+
:for entry := (zip:get-zipfile-entry
47+
(format nil "cldr-~a/~a" tag name) f)
48+
:do (format t "Writing ~a~a.json... " dist-dir name)
49+
:do (force-output)
50+
:do (make-chronogram-info
51+
(babel:octets-to-string (zip:zipfile-entry-contents entry)
52+
:encoding :utf-8)
53+
dist-dir
54+
name)
55+
:do (format t "DONE~%"))))
56+
(loop :with cldr-dir := (asdf:system-relative-pathname system "cldr/")
57+
:for name :in names
58+
:do (format t "Writing ~a~a.json... " dist-dir name)
59+
:do (force-output)
60+
:do (make-chronogram-info
61+
(uiop:read-file-string (format nil "~a/common/main/~a.xml"
62+
cldr-dir
63+
name))
64+
dist-dir
65+
name)
66+
:do (format t "DONE~%")))))

chronogram.asd

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
:depends-on (#:alexandria
1313
#:esrap
1414
#:plump
15-
#:clss)
15+
#:clss
16+
#:com.inuoe.jzon)
1617
:components ((:file "chronogram-cldr-parser")))
1718

1819
(asdf:defsystem #:chronogram/test

0 commit comments

Comments
 (0)