forked from HeinrichApfelmus/threepenny-gui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UseWords.hs
101 lines (81 loc) · 3.52 KB
/
UseWords.hs
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
{-# LANGUAGE ViewPatterns #-}
import Control.Applicative hiding ((<|>),many)
import Control.Monad
import Control.Arrow (second)
import Data.Maybe
import Text.Parsec
import System.FilePath ((</>))
import qualified Graphics.UI.Threepenny as UI
import Graphics.UI.Threepenny.Core hiding (string, (<|>), many)
import Paths
{-----------------------------------------------------------------------------
GUI
------------------------------------------------------------------------------}
main :: IO ()
main = do
static <- getStaticDir
startGUI defaultConfig
{ tpPort = 10000
, tpStatic = Just static
} setup
setup :: Window -> UI ()
setup w = do
filename <- liftIO $ fmap (</> "and-then-haskell.txt") getStaticDir
andthen <- liftIO $ readFile filename
case parts filename andthen of
Left parseerror -> debug $ show parseerror
Right parts -> void $ do
UI.addStyleSheet w "use-words.css"
let (header, Prelude.drop 2 -> rest) = splitAt 3 parts
(views1, vars1) <- renderParts header
(views2, vars2) <- renderParts rest
varChoices <- mapM (renderVarChoice (vars1 ++ vars2)) vars
getBody w #+
[ viewSource
, UI.div #. "wrap" #+ (
[ UI.div #. "header" #+ map element views1
, UI.ul #. "vars" #+ map element varChoices
]
++ map element views2 )
]
type VariableViews = [(Name, Element)]
renderVarChoice :: VariableViews -> Variable -> UI Element
renderVarChoice views (label,(name,def)) = do
input <- UI.input #. "var-value" # set value def
on (domEvent "livechange") input $ \(EventData xs) -> do
let s = concat $ catMaybes xs
forM_ (filter ((==name).fst) views) $ \(_,el) -> do
element el # set text s
UI.li #+ [UI.string (label ++ ":"), element input]
renderParts :: [Part] -> UI ([Element], VariableViews)
renderParts parts = do
views <- mapM renderPart parts
let variables = [(var, view) | (Ref var, view) <- zip parts views]
return (views, variables)
renderPart :: Part -> UI Element
renderPart (Text str) = UI.div #. "text" #+ [UI.string str]
renderPart (Ref var) = UI.div #. "var"
# maybe (set text $ "{" ++ var ++ "}") (either (set html) (set text))
(lookup var templatevars)
viewSource :: UI Element
viewSource = UI.p #+
[UI.anchor #. "view-source" # set UI.href url #+ [UI.string "View source code"]]
where
url = "https://github.com/HeinrichApfelmus/threepenny-gui/blob/master/src/UseWords.hs"
{-----------------------------------------------------------------------------
Parsing
------------------------------------------------------------------------------}
type Name = String
type Variable = (String, (Name, String))
templatevars = map (second Right . snd) vars ++ map (second Left) exts
vars :: [Variable]
vars = [("Favourite technology",("favourite-language","Haskell"))
,("Technology used at work",("work-language","Python"))
,("Cool forum",("bar","LtU"))
,("Particular to technology",("particular-stuff","monads"))]
exts = [("br","<br><br>")]
data Part = Text String | Ref String deriving Show
parts :: SourceName -> String -> Either ParseError [Part]
parts = parse (many (ref <|> text)) where
text = Text <$> many1 (notFollowedBy (string "{") *> anyChar)
ref = Ref <$> (string "{" *> many1 (noneOf "}") <* (string "}"))