Explorar el Código

split everything into modules

Lucas Stadler %!s(int64=11) %!d(string=hace) años
padre
commit
087547a38b
Se han modificado 3 ficheros con 31 adiciones y 16 borrados
  1. 14 0
      elm/Post.elm
  2. 5 16
      elm/Stream.elm
  3. 12 0
      elm/Util/Html.elm

+ 14 - 0
elm/Post.elm

@ -0,0 +1,14 @@
1
module Post where
2
3
import Date (Date)
4
import Util.Html (viewDate)
5
import Html (..)
6
7
type alias Post = { title:String, content:String, created:Date }
8
9
view : Date -> Post -> Html
10
view ref post = div [] [
11
                 h3 [] [text post.title],
12
                 p [] [text post.content],
13
                 span [] [text "Written ", viewDate ref post.created]
14
                ]

+ 5 - 16
elm/Stream.elm

@ -9,7 +9,8 @@ import List
9 9
import String
10 10
import PrettyDate (prettyDate)
11 11
12
type alias Post = { title:String, content:String, created:Date }
12
import Post
13
import Post (Post)
13 14
14 15
date s = case (Date.fromString s) of
15 16
    Ok  d -> d
@ -23,20 +24,6 @@ posts = [{title = "Something else", content = "Well, I can say more than \"Hello
23 24
         {title = "Ancient history", content = "Teenage angst!!!!", created = date "2009-06-07T02:54:29"}
24 25
        ]
25 26
26
referenceDate = date "2015-03-01T14:09"
27
28
viewDate : Date -> Html
29
viewDate d = let dateString = prettyDate referenceDate d
30
                 isoDate = formatDate "%Y-%m-%dT%H:%M:%SZ" d
31
             in time [Attr.title isoDate, Attr.datetime isoDate] [(text dateString)]
32
33
viewPost : Post -> Html
34
viewPost post = div [] [
35
                 h3 [] [text post.title],
36
                 p [] [text post.content],
37
                 span [] [text "Written ", viewDate post.created]
38
                ]
39
40 27
flipOrder : Order -> Order
41 28
flipOrder o = case o of
42 29
                LT -> GT
@ -52,4 +39,6 @@ compareBy f a b = compare (f a) (f b)
52 39
sortByDate = List.sortBy (.created >> Date.toTime)
53 40
sortByDateReverse = List.sortWith (flipCompare <| compareBy (.created >> Date.toTime))
54 41
55
main = div [] (List.map viewPost (sortByDateReverse posts))
42
referenceDate = date "2015-03-01T14:09"
43
44
main = div [] (List.map (Post.view referenceDate) (sortByDateReverse posts))

+ 12 - 0
elm/Util/Html.elm

@ -0,0 +1,12 @@
1
module Util.Html where
2
3
import Date (Date)
4
import Html (..)
5
import Html.Attributes as Attr
6
import FormatDate (formatDate)
7
import PrettyDate (prettyDate)
8
9
viewDate : Date -> Date -> Html
10
viewDate ref d = let dateString = prettyDate ref d
11
                     isoDate = formatDate "%Y-%m-%dT%H:%M:%SZ" d
12
                 in time [Attr.title isoDate, Attr.datetime isoDate] [(text dateString)]