Clean up code and remove an undefined
Getty Ritter
6 years ago
1 | {-# LANGUAGE LambdaCase #-} | |
2 | {-# LANGUAGE GADTs #-} | |
3 | 1 | {-# LANGUAGE ScopedTypeVariables #-} |
4 | 2 | {-# LANGUAGE ExistentialQuantification #-} |
5 | {-# LANGUAGE TypeOperators #-} | |
6 | 3 | {-# LANGUAGE OverloadedStrings #-} |
7 | 4 | {-# LANGUAGE FlexibleInstances #-} |
5 | {-# LANGUAGE TypeFamilies #-} | |
8 | 6 | |
9 | 7 | module Data.TeLML.Markup where |
10 | 8 | |
94 | 92 | |
95 | 93 | -- | The 'Str' newtype will match a literal chunk of non-formatted, |
96 | 94 | -- non-structured text. |
97 |
newtype Str = Str { fromStr :: T.Text } |
|
95 | newtype Str = Str { fromStr :: T.Text } | |
98 | 96 | |
99 | 97 | -- | The 'H' newtype will match a single, pre-rendered argument |
100 |
newtype H = H |
|
98 | newtype H = H { fromHtml :: Html } | |
101 | 99 | |
102 | 100 | -- | The 'Hs' newtype will match a concatenated set of pre-rendered |
103 | 101 | -- arguments |
104 |
newtype Hs = Hs |
|
102 | newtype Hs = Hs { fromHtmlList :: Html } | |
105 | 103 | |
106 | 104 | mkTag :: TagArguments t => T.Text -> t -> TagDescription |
107 | 105 | mkTag = TagDescription |
108 | ||
109 | exec :: T.Text -> [Document] -> [TagDescription] -> Either String Html | |
110 | exec name args (TagDescription tag func:_) | |
111 | | name == tag = case taExec func args undefined of | |
112 | Nothing -> Left $ unwords [ "Tag" | |
113 | , T.unpack ('\\' `T.cons` name) | |
114 | , "expects argument structure" | |
115 | , T.unpack ('\\' `T.cons` name `T.append` | |
116 | T.intercalate "|" (toType func)) | |
117 | ] | |
118 | Just x -> x | |
119 | exec name args (_:rs) = exec name args rs | |
120 | exec name args [] = Left $ | |
121 | "Error: no match for tag " ++ T.unpack name ++ "/" ++ show (length args) | |
122 | 106 | |
123 | 107 | -- The built-in set of tags (subject to change) |
124 | 108 | basicTags :: [TagDescription] |
150 | 134 | ] |
151 | 135 | |
152 | 136 | simpleTag :: T.Text -> (Markup -> Html) -> TagDescription |
153 |
simpleTag name tag = mkTag name ( |
|
137 | simpleTag name tag = mkTag name (tag . fromHtml) | |
154 | 138 | |
155 | 139 | listTag :: T.Text -> (Markup -> Html) -> TagDescription |
156 | 140 | listTag name tag = mkTag name (\ (Hs hs) -> tag hs) |
160 | 144 | renderPara taglist ds = fmap (p . sequence_) (mapM go ds) |
161 | 145 | where go (TextFrag ts) = Right (toMarkup ts) |
162 | 146 | go (TagFrag (Tag tx rs)) = exec tx rs taglist |
147 | exec name args (TagDescription tag func:_) | |
148 | | name == tag = case taExec func args go of | |
149 | Nothing -> Left $ unwords | |
150 | [ "Tag" | |
151 | , T.unpack ('\\' `T.cons` name) | |
152 | , "expects argument structure" | |
153 | , T.unpack ('\\' `T.cons` name `T.append` | |
154 | T.intercalate "|" (toType func)) | |
155 | ] | |
156 | Just x -> x | |
157 | exec name args (_:rs) = exec name args rs | |
158 | exec name args [] = Left $ | |
159 | "Error: no match for tag " ++ T.unpack name ++ "/" ++ show (length args) |