aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/data/format/css.lux
blob: e2b2737d2ac033179bc0a1b4496929b59030044a (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
(.module:
  [lux #*
   [data
    ["." color (#+ Color)]
    ["." number]
    ["." text
     format]
    [collection
     ["." list ("list/" Functor<List> Monoid<List>)]]]])

(type: #export Selector
  Text)

(type: #export Property Text)
(type: #export Value Text)

(type: #export Style
  {#.doc "The style associated with a CSS selector."}
  (List [Property Value]))

(type: #export Rule [Selector Style])

(type: #export Sheet (List Rule))

(type: #export CSS Text)

(def: #export (inline style)
  (-> Style Text)
  (|> style
      (list/map (function (_ [key val]) (format key ": " val)))
      (text.join-with "; ")))

(def: #export (css sheet)
  (-> Sheet CSS)
  (|> sheet
      (list/map (function (_ [selector style])
                  (if (list.empty? style)
                    ""
                    (format selector "{" (inline style) "}"))))
      (text.join-with "\n")))

(def: #export (rgb color)
  (-> Color Value)
  (let [[red green blue] (color.unpack color)]
    (format "rgb(" (|> red .int %i)
            "," (|> green .int %i)
            "," (|> blue .int %i)
            ")")))

(def: #export (rgba color alpha)
  (-> Color Rev Value)
  (let [[red green blue] (color.unpack color)]
    (format "rgba(" (|> red .int %i)
            "," (|> green .int %i)
            "," (|> blue .int %i)
            "," (if (r/= (:: number.Interval<Rev> top) alpha)
                  "1.0"
                  (format "0" (%r alpha)))
            ")")))

(def: #export (rule selector style children)
  (-> Selector Style Sheet Sheet)
  (list& [selector style]
         (list/map (function (_ [sub-selector sub-style])
                     [(format selector sub-selector) sub-style])
                   children)))

(do-template [<name> <type>]
  [(def: #export <name>
     (-> <type> <type> <type>)
     list/compose)]

  [merge   Style]
  [cascade Sheet]
  )

(do-template [<name> <suffix>]
  [(def: #export (<name> value)
     (-> Frac Value)
     (format (%f value) <suffix>))]

  [em "em"]
  [ex "ex"]
  [rem "rem"]
  [ch "ch"]
  [vw "vw"]
  [vh "vh"]
  [vmin "vmin"]
  [vmax "vmax"]
  [% "%"]
  [cm "cm"]
  [mm "mm"]
  [in "in"]
  [px "px"]
  [pt "pt"]
  [pc "pc"]
  )