~ehmry/dhall-atom

dhall-atom/package.dhall -rw-r--r-- 10.1 KiB
c5c4537dEmery Hemingway Date is now a builtin, for some reason… 27 days ago
                                                                                
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
-- SPDX-License-Identifier: Unlicense
let Prelude =
      https://raw.githubusercontent.com/dhall-lang/dhall-lang/v20.2.0/Prelude/package.dhall
        sha256:a6036bc38d883450598d1de7c98ead113196fe2db02e9733855668b18096f07b

let XML = Prelude.XML

let X =
      { Type =
          { attributes : Prelude.Map.Type Text Text, content : List XML.Type }
      , default =
        { attributes = XML.emptyAttributes, content = [] : List XML.Type }
      }

let x = λ(name : Text) → λ(rest : X.Type) → XML.element ({ name } ∧ rest)

let xText =
      λ(name : Text) →
      λ(body : Text) →
        x name X::{ content = [ XML.text body ] }

let mergeXML =
      λ(a : Type) →
      λ(f : a → XML.Type) →
      λ(x : Optional a) →
        merge { Some = λ(y : a) → [ f y ], None = [] : List XML.Type } x

let mergeXMLTextElement =
      λ(tag : Text) →
      λ(value : Optional Text) →
        merge
          { Some = λ(value : Text) → [ xText tag value ]
          , None = [] : List XML.Type
          }
          value

let DateRecord = { year : Natural, month : Natural, day : Natural }

let Date/Type = < Raw : Text | Record : DateRecord >

let Date/show =
      λ(_ : Date/Type) →
        merge
          { Raw = λ(date : Text) → date
          , Record =
              let showDoubleDigits =
                    λ(x : Natural) →
                      "${if    Prelude.Natural.lessThanEqual x 9
                         then  "0"
                         else  ""}${Natural/show x}"

              in  λ(date : DateRecord) →
                        Natural/show date.year
                    ++  "-"
                    ++  showDoubleDigits date.month
                    ++  "-"
                    ++  showDoubleDigits date.day
                    ++  "T00:00:00Z"
          }
          _

let Date_ =
      { Type = Date/Type
      , show = Date/show
      , raw = λ(text : Text) → Date/Type.Raw text
      , parse =
          λ(year : Natural) →
          λ(month : Natural) →
          λ(day : Natural) →
            Date/Type.Record { year, month, day }
      , toXML =
          λ(name : Text) → λ(date : Date/Type) → xText name (Date/show date)
      }

let Link/Type =
      { href : Text
      , rel : Text
      , type : Optional Text
      , hreflang : Optional Text
      , title : Optional Text
      , length : Optional Natural
      }

let Link/default =
      { rel = "alternate"
      , type = None Text
      , hreflang = None Text
      , title = None Text
      , length = None Natural
      }

let Link/generate =
      λ(rel : Text) → λ(href : Text) → Link/default ⫽ { rel, href }

let Link =
      { Type = Link/Type
      , default = Link/default
      , alternate = Link/generate "alternate"
      , related = Link/generate "related"
      , self = Link/generate "self"
      , enclosure = Link/generate "enclosure"
      , via = Link/generate "via"
      , toXML =
          λ(link : Link/Type) →
            x
              "link"
              X::{
              , attributes =
                    toMap { href = link.href, rel = link.rel }
                  # Prelude.Map.unpackOptionals
                      Text
                      Text
                      (toMap link.{ type, hreflang, title })
                  # merge
                      { Some =
                          λ(length : Natural) →
                            [ { mapKey = "length"
                              , mapValue = Prelude.Natural.show length
                              }
                            ]
                      , None = [] : Prelude.Map.Type Text Text
                      }
                      link.length
              }
      }

let Links/Type = List Link.Type

let Links/toXML = Prelude.List.map Link.Type XML.Type Link.toXML

let Person/Type = { name : Text, uri : Optional Text, email : Optional Text }

let Person =
      { Type = Person/Type
      , default = { uri = None Text, email = None Text }
      , toXML =
          λ(tag : Text) →
          λ(person : Person/Type) →
            x
              tag
              X::{
              , content =
                    [ xText "name" person.name ]
                  # mergeXMLTextElement "uri" person.uri
                  # mergeXMLTextElement "email" person.email
              }
      }

let Persons/Type = List Person.Type

let Persons/toXML =
      λ(tag : Text) → Prelude.List.map Person.Type XML.Type (Person.toXML tag)

let TextConstruct =
      let Construct = { type : Text, body : Text }

      let toXML =
            λ(tag : Text) →
            λ(tc : Construct) →
              x
                tag
                X::{
                , attributes = toMap tc.{ type }
                , content = [ XML.text tc.body ]
                }

      in  { Type = Construct
          , make = λ(type : Text) → λ(body : Text) → { type, body }
          , toXML
          , mergeToXML = λ(tag : Text) → mergeXML Construct (toXML tag)
          }

let Content/Type =
      { type : Text, attributes : Prelude.Map.Type Text Text, body : XML.Type }

let Content/default = { attributes = XML.emptyAttributes }

let Content =
      { Type = Content/Type
      , default = Content/default
      , text =
          λ(body : Text) →
            Content/default ∧ { type = "text", body = XML.text body }
      , toXML =
          λ(content : Content/Type) →
            x
              "content"
              { attributes = toMap { type = content.type } # content.attributes
              , content = [ content.body ]
              }
      }

let Entry/Type =
      { authors : Persons/Type
      , contributors : Persons/Type
      , id : Text
      , links : Links/Type
      , summary : Optional TextConstruct.Type
      , title : TextConstruct.Type
      , updated : Date_.Type
      , published : Optional Date_.Type
      , rights : Optional TextConstruct.Type
      , contents : List Content.Type
      }

let Entry =
      { Type = Entry/Type
      , default =
        { authors = [] : Persons/Type
        , contributors = [] : Persons/Type
        , links = [] : Links/Type
        , summary = None TextConstruct.Type
        , published = None Date_.Type
        , rights = None TextConstruct.Type
        , contents = [] : List Content.Type
        }
      , toXML =
          λ(entry : Entry/Type) →
            x
              "entry"
              X::{
              , content =
                    [ TextConstruct.toXML "title" entry.title
                    , xText "id" entry.id
                    , Date_.toXML "updated" entry.updated
                    ]
                  # mergeXML
                      Date_.Type
                      (Date_.toXML "published")
                      entry.published
                  # Links/toXML entry.links
                  # TextConstruct.mergeToXML "summary" entry.summary
                  # Persons/toXML "author" entry.authors
                  # Persons/toXML "contributor" entry.contributors
                  # TextConstruct.mergeToXML "rights" entry.rights
                  # Prelude.List.map
                      Content.Type
                      XML.Type
                      Content.toXML
                      entry.contents
              }
      }

let Generator/Type =
      { uri : Optional Text, version : Optional Text, text : Text }

let Generator =
      { Type = Generator/Type
      , default = { uri = None Text, version = None Text }
      , toXML =
          λ(gen : Optional Generator/Type) →
            merge
              { Some =
                  λ(gen : Generator/Type) →
                    [ x
                        "generator"
                        X::{
                        , attributes =
                            Prelude.Map.unpackOptionals
                              Text
                              Text
                              (toMap gen.{ uri, version })
                        , content = [ XML.text gen.text ]
                        }
                    ]
              , None = [] : List XML.Type
              }
              gen
      }

let Feed/Type =
      { authors : Persons/Type
      , contributors : Persons/Type
      , entries : List Entry.Type
      , generator : Optional Generator.Type
      , id : Text
      , rights : Optional TextConstruct.Type
      , links : Links/Type
      , subtitle : Optional TextConstruct.Type
      , title : TextConstruct.Type
      , updated : Date_.Type
      }

let Feed =
      { Type = Feed/Type
      , default =
        { authors = [] : Persons/Type
        , contributors = [] : Persons/Type
        , entries = [] : List Entry.Type
        , generator = Some Generator::{
          , uri = Some "https://git.sr.ht/~ehmry/dhall-atom"
          , text = "dhall-atom"
          }
        , links = [] : List Link/Type
        , rights = None TextConstruct.Type
        , subtitle = None TextConstruct.Type
        }
      , render =
          λ(feed : Feed/Type) →
            let xml =
                  XML.element
                    { name = "feed"
                    , attributes = toMap
                        { xmlns = "http://www.w3.org/2005/Atom" }
                    , content =
                          [ TextConstruct.toXML "title" feed.title
                          , Date_.toXML "updated" feed.updated
                          , xText "id" feed.id
                          ]
                        # TextConstruct.mergeToXML "subtitle" feed.subtitle
                        # TextConstruct.mergeToXML "rights" feed.rights
                        # Generator.toXML feed.generator
                        # Persons/toXML "author" feed.authors
                        # Persons/toXML "contributor" feed.contributors
                        # Links/toXML feed.links
                        # Prelude.List.map
                            Entry.Type
                            XML.Type
                            Entry.toXML
                            feed.entries
                    }

            in  XML.render xml
      }

in  { Content
    , Date_
    , Entry
    , Feed
    , Generator
    , Link
    , Person
    , TextConstruct
    , text = TextConstruct.make "text"
    , html = TextConstruct.make "html"
    , xhtml = TextConstruct.make "xhtml"
    }