aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/data/format/css.lux
blob: 8b3a9270c63f37efdd8760896bdebc70306d8a85 (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
(.module:
  [library
   [lux (#- and)
    [control
     ["." maybe]]
    [data
     [number
      ["." nat]]
     ["." text
      ["%" format (#+ format)]
      ["." encoding (#+ Encoding)]]
     [collection
      ["." list ("#\." functor)]]]
    [type
     abstract]
    [world
     [net (#+ URL)]]]]
  ["." / #_
   ["#." selector (#+ Selector Combinator)]
   ["#." value (#+ Value Animation Percentage)]
   ["#." font (#+ Font)]
   ["#." style (#+ Style)]
   ["#." 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 Common)
    (:abstraction ""))

  (def: .public (rule selector style)
    (-> (Selector Any) Style (CSS Common))
    (:abstraction (format (/selector.selector selector) "{" (/style.inline 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 (value@ #/font.unicode_range font)
                         (#.Some unicode_range)
                         (let [unicode_range' (format "U+" (\ nat.hex encode (value@ #/font.start unicode_range))
                                                      "-" (\ nat.hex encode (value@ #/font.end unicode_range)))]
                           (list ["unicode-range" unicode_range']))
                         
                         #.None
                         (list))]
      (|> (list& ["font-family" (value@ #/font.family font)]
                 ["src" (format "url(" (value@ #/font.source font) ")")]
                 ["font-stretch" (|> font (value@ #/font.stretch) (maybe.else /value.normal_stretch) /value.value)]
                 ["font-style" (|> font (value@ #/font.style) (maybe.else /value.normal_style) /value.value)]
                 ["font-weight" (|> font (value@ #/font.weight) (maybe.else /value.normal_weight) /value.value)]
                 with_unicode)
          (list\map (function (_ [property value])
                      (format property ": " value ";")))
          (text.interposed /style.separator)
          (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: css_separator
    text.new_line)

  (type: .public Frame
    {#when Percentage
     #what Style})

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

  (template: (!compose <pre> <post>)
    (:abstraction (format (:representation <pre>) ..css_separator
                          (:representation <post>))))
  
  (def: .public (and pre post)
    (-> (CSS Any) (CSS Any) (CSS Any))
    (!compose pre post))

  (def: .public (alter combinator selector css)
    (-> Combinator (Selector Any) (CSS Common) (CSS Common))
    (|> css
        :representation
        (text.all_split_by ..css_separator)
        (list\map (|>> (format (/selector.selector (|> selector (combinator (/selector.tag "")))))))
        (text.interposed ..css_separator)
        :abstraction))

  (def: .public (dependent combinator selector style inner)
    (-> Combinator (Selector Any) Style (CSS Common) (CSS Common))
    (!compose (..rule selector style)
              (..alter 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]
    )
  )