aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/data/format/css.lux
blob: 1de9e0192cf027c403f16a11d788e911fa12f8f7 (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
(;module:
  lux
  (lux (data [color #+ Color]
             [number]
             [text]
             text/format
             (coll [list "L/" 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
      (L/map (function [[key val]] (format key ": " val)))
      (text;join-with "; ")))

(def: #export (css sheet)
  (-> Sheet CSS)
  (|> sheet
      (L/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 nat-to-int %i)
            "," (|> green nat-to-int %i)
            "," (|> blue nat-to-int %i)
            ")")))

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

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

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

  [merge   Style]
  [cascade Sheet]
  )

(do-template [<name> <suffix>]
  [(def: #export (<name> value)
     (-> Frac Value)
     (format (%r 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"]
  )