aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/data/format/css.lux
blob: dc42d50d2144715c37d1a4ff30f4f01ef7a81f5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
(.using
 [library
  [lux {"-" and}
   [control
    ["[0]" maybe]]
   [data
    ["[0]" text
     ["%" format {"+" format}]
     ["[0]" encoding {"+" Encoding}]]
    [collection
     ["[0]" list ("[1]#[0]" functor)]]]
   [math
    [number
     ["[0]" nat]]]
   [type
    [abstract {"-" Frame pattern}]]
   [world
    [net {"+" URL}]]]]
 ["[0]" / "_"
  ["[1][0]" selector {"+" Selector Combinator Specializer Generic}]
  ["[1][0]" value {"+" Value Animation Percentage}]
  ["[1][0]" property {"+" Property}]
  ["[1][0]" font {"+" Font}]
  ["[1][0]" style]
  ["[1][0]" query {"+" Query}]])

(abstract: .public Common Any)
(abstract: .public Special Any)

(abstract: .public (CSS brand)
  Text

  (def: .public css
    (-> (CSS Any) Text)
    (|>> representation))

  (def: .public empty
    (CSS Any)
    (abstraction ""))

  (type: .public Style
    (List (Ex (_ brand) [(Property brand) (Value brand)])))

  (def: .public (rule selector style)
    (-> (Selector Any) Style (CSS Common))
    (abstraction (format (/selector.selector selector) "{" (/style.inline (/style.style style)) "}")))

  (def: .public char_set
    (-> Encoding (CSS Special))
    (|>> encoding.name
         %.text
         (text.enclosed ["@charset " ";"])
         abstraction))

  (def: .public (font font)
    (-> Font (CSS Special))
    (let [with_unicode (case (the /font.#unicode_range font)
                         {.#Some unicode_range}
                         (let [unicode_range' (format "U+" (# nat.hex encoded (the /font.#start unicode_range))
                                                      "-" (# nat.hex encoded (the /font.#end unicode_range)))]
                           (list ["unicode-range" unicode_range']))
                         
                         {.#None}
                         (list))]
      (|> (list& ["font-family" (the /font.#family font)]
                 ["src" (format "url(" (the /font.#source font) ")")]
                 ["font-stretch" (|> font (the /font.#stretch) (maybe.else /value.normal_stretch) /value.value)]
                 ["font-style" (|> font (the /font.#style) (maybe.else /value.normal_style) /value.value)]
                 ["font-weight" (|> font (the /font.#weight) (maybe.else /value.normal_weight) /value.value)]
                 with_unicode)
          (list#each (function (_ [property value])
                       (format property ": " value ";")))
          text.together
          (text.enclosed ["{" "}"])
          (format "@font-face")
          abstraction)))

  (def: .public (import url query)
    (-> URL (Maybe Query) (CSS Special))
    (abstraction (format (format "@import url(" (%.text url) ")")
                         (case query
                           {.#Some query}
                           (format " " (/query.query query))
                           
                           {.#None}
                           "")
                         ";")))

  (def: separator
    text.new_line)

  (type: .public Frame
    (Record
     [#when (Value Percentage)
      #what Style]))

  (def: .public (key_frames animation frames)
    (-> (Value Animation) (List Frame) (CSS Special))
    (abstraction (format "@keyframes " (/value.value animation) " {"
                         (|> frames
                             (list#each (function (_ frame)
                                          (format (/value.value (the #when frame)) " {"
                                                  (/style.inline (/style.style (the #what frame)))
                                                  "}")))
                             (text.interposed ..separator))
                         "}")))

  (template: (!composite <pre> <post>)
    [(abstraction
      (format (representation <pre>)
              ..separator
              (representation <post>)))])
  
  (def: .public (and pre post)
    (All (_ kind) (-> (CSS kind) (CSS kind) (CSS kind)))
    (!composite pre post))

  (def: .public (in_context combinator selector css)
    (-> Combinator (Selector Any) (CSS Common) (CSS Common))
    (|> css
        representation
        (text.all_split_by ..separator)
        (list#each (let [prefix (|> selector
                                    (combinator (/selector.tag ""))
                                    /selector.selector)]
                     (|>> (format prefix))))
        (text.interposed ..separator)
        abstraction))

  (def: .public (dependent combinator selector style inner)
    (-> Combinator (Selector Any) Style (CSS Common) (CSS Common))
    (!composite (..rule selector style)
                (..in_context combinator selector inner)))

  (template [<name> <combinator>]
    [(def: .public <name>
       (-> (Selector Any) Style (CSS Common) (CSS Common))
       (..dependent <combinator>))]

    [with_descendants /selector.in]
    [with_children /selector.sub]
    )

  (def: .public (in_case specializer selector css)
    (All (_ kind)
      (-> (Specializer kind) (Selector (Generic Any)) (CSS Common) (CSS Common)))
    (|> css
        representation
        (text.all_split_by ..separator)
        (list#each (let [prefix (|> selector
                                    (specializer (as_expected (/selector.tag "")))
                                    /selector.selector)]
                     (|>> (format prefix))))
        (text.interposed ..separator)
        abstraction))

  (def: .public (specialized combinator selector style inner)
    (All (_ kind)
      (-> (Specializer kind) (Selector (Generic Any)) Style (CSS Common) (CSS Common)))
    (!composite (..rule selector style)
                (..in_case combinator selector inner)))

  (template [<name> <combinator>]
    [(def: .public <name>
       (-> (Selector (Generic Any)) Style (CSS Common) (CSS Common))
       (..specialized <combinator>))]

    [with_case /selector.and]
    [with_part /selector.at]
    [with_element /selector.for]
    )
  )