15
15
module Lucid.Svg
16
16
( -- * Intro
17
17
-- $intro
18
- -- * Types
19
- Svg
20
- -- * Rendering
21
- , prettyText
22
18
-- * Re-exports
19
+ module Lucid.Svg.Core
23
20
, module Lucid.Svg.Path
24
21
, module Lucid.Svg.Elements
25
22
, module Lucid.Svg.Attributes
26
- -- * From Lucid.Base
27
- , renderText
28
- , renderBS
29
- , renderTextT
30
- , renderBST
31
- , renderToFile
32
- -- ** Running
33
- -- $running
34
- , execHtmlT
35
- , evalHtmlT
36
- , runHtmlT
37
- -- ** Types
38
- , Attribute (.. )
39
- -- ** Classes
40
- -- $overloaded
41
- , Term (.. )
42
- , ToHtml (.. )
43
- , With (.. )
23
+ , (<>)
24
+ -- * Rendering
25
+ , prettyText
44
26
) where
45
27
46
28
import Data.Functor.Identity
47
29
import Data.Int (Int64 )
48
30
import Data.Monoid
49
- import Data.Text.Lazy
50
31
import Data.Text.Lazy as LT
51
32
import Data.Text.Lazy.Builder as B
52
- import Lucid.Base
53
- import qualified Lucid.Svg.Attributes as A
54
- import Lucid.Svg.Attributes hiding (cursor_ , filter_ , path_ , style_ )
33
+ import Lucid.Svg.Core
34
+ import Lucid.Svg.Attributes
55
35
import Lucid.Svg.Elements
56
36
import Lucid.Svg.Path
57
37
58
- type Svg = SvgT Identity
59
-
60
- prettyText :: Svg a -> Text
38
+ prettyText :: Element -> Text
61
39
prettyText svg = B. toLazyText $ LT. foldr go mempty text Nothing (- 1 )
62
- where
40
+ where
63
41
text = renderText svg
64
- go c f Nothing n
42
+ go c f Nothing n
65
43
| c == ' <' || c == ' /' = f (Just c) n
66
- go c f (Just ' <' ) n
44
+ go c f (Just ' <' ) n
67
45
| c == ' ?' = " <?" <> f Nothing n
68
46
| c == ' !' = " <!" <> f Nothing n
69
47
| c == ' /' = " \n "
@@ -78,81 +56,63 @@ prettyText svg = B.toLazyText $ LT.foldr go mempty text Nothing (-1)
78
56
go ' >' f (Just _) n = " />" <> f Nothing (n- 1 )
79
57
go c f s n = s' <> B. singleton c <> f Nothing n
80
58
where s' = maybe mempty B. singleton s
81
-
59
+
82
60
-- $intro
83
61
--
84
- -- SVG elements and attributes in Lucid-Svg are written with a postfix ‘@_@’.
62
+ -- SVG elements in Lucid-Svg are written with a postfix ‘@_@’.
85
63
-- Some examples:
86
64
--
87
65
-- 'path_', 'circle_', 'color_', 'scale_'
88
66
--
89
- -- Note: If you're testing in the REPL you need to add a type annotation to
90
- -- indicate that you want SVG. In normal code your top-level
91
- -- declaration signatures handle that.
92
- --
93
- -- Plain text is written using the @OverloadedStrings@ and
94
- -- @ExtendedDefaultRules@ extensions, and is automatically escaped:
67
+ -- Plain text is written using the @OverloadedStrings@
68
+ -- extension, and is automatically escaped:
95
69
--
96
- -- As in Lucid, elements nest by function application:
70
+ -- As in Lucid, elements nest by function application (unlike Lucid, there
71
+ -- is no Monad instance for 'Element's and an 'Attribute' list is always required):
97
72
--
98
- -- >>> g_ (text_ "Hello SVG") :: Svg ( )
73
+ -- >>> g_ [] (text_ [] "Hello SVG")
99
74
-- <g><text>Hello SVG</text></g>
100
75
--
101
- -- and elements are juxtaposed via monoidal append or monadic sequencing :
76
+ -- and elements are juxtaposed via monoidal append:
102
77
--
103
- -- >>> text_ "Hello" <> text_ "SVG" :: Svg ()
78
+ -- >>> text_ [] "Hello" <> text_ [] "SVG"
104
79
-- <text>Hello</text><text>SVG</text>
105
80
--
106
- -- >>> do text_ "Hello"; text_ "SVG" :: Svg ()
107
- -- <text>Hello</text><text>SVG</text>
81
+ -- Attributes are set by providing an argument list. Each argument is set
82
+ -- using the 'bindAttr' function or operators, '<<-' and '->>'.
108
83
--
109
- -- Attributes are set by providing an argument list. In contrast to HTML
110
- -- many SVG elements have no content, only attributes.
111
- --
112
- -- >>> rect_ [width_ "100%", height_ "100%", fill_ "red"] :: Svg ()
84
+ -- >>> rect_ [Width_ <<- "100%", Height_ <<- "100%", "red" ->> Fill_] nil
113
85
-- <rect height="100%" width="100%" fill="red"></rect>
114
86
--
115
- -- Attributes and elements that share the same name are not conflicting
116
- -- unless they appear on the list in the note below:
117
- --
118
- -- >>> mask_ [mask_ "attribute"] "element" :: Svg ()
119
- -- <mask mask="attribute">element</mask>
120
- --
121
- -- Note: The following element and attribute names overlap and cannot be
122
- -- handled polymorphically since doing so would create conflicting functional
123
- -- dependencies. The unqualifed name refers to the element.
124
- -- We qualify the attribute name as @A@. For example, 'path_' and 'A.path_'.
125
- --
126
- -- 'colorProfile_', 'cursor_', 'filter_', 'path_', and 'style_'
127
- --
128
87
-- Path data can be constructed using the functions in 'Lucid.Svg.Path'
129
88
-- and combined monoidally:
130
89
--
131
90
-- @
132
- -- path_ (
133
- -- [ d_ (mA 10 80 <> qA 52.5 10 95 80 <> tA 180 80 <> z)
134
- -- , stroke_ "blue"
135
- -- , fill_ "orange"
136
- -- ])
91
+ -- path_
92
+ -- [ D <<- (mA 10 80 <> qA 52.5 10 95 80 <> tA 180 80 <> z)
93
+ -- , Stroke_ <<- "blue"
94
+ -- , Fill_ <<- "orange"
95
+ -- ]
137
96
-- @
138
- -- > <path d="M 10,80 Q 52.5,10 95,80 T 180,80 Z" stroke="blue" fill="orange"></path >
97
+ -- > <path d="M 10,80 Q 52.5,10 95,80 T 180,80 Z" stroke="blue" fill="orange"/ >
139
98
--
140
99
-- __A slightly longer example__:
141
- --
100
+ --
142
101
-- > import Lucid.Svg
143
- -- >
144
- -- > svg :: Svg () -> Svg ()
145
- -- > svg content = do
146
- -- > doctype_
147
- -- > with (svg11_ content) [version_ "1.1", width_ "300" , height_ "200"]
148
- -- >
149
- -- > contents :: Svg ()
150
- -- > contents = do
151
- -- > rect_ [width_ "100%", height_ "100%", fill_ "red"]
152
- -- > circle_ [cx_ "150", cy_ "100", r_ "80", fill_ "green"]
153
- -- > text_ [x_ "150", y_ "125", fontSize_ "60", textAnchor_ "middle", fill_ "white"] "SVG"
154
- -- >
155
- -- >
102
+ -- >
103
+ -- > svg :: Element -> Element
104
+ -- > svg content =
105
+ -- > doctype
106
+ -- > <> with (svg11_ content) [Version_ <<- "1.1", Width_ <<- "300" , Height_ <<- "200"]
107
+ -- >
108
+ -- > contents :: Element
109
+ -- > contents =
110
+ -- > rect_ [Width_ <<- "100%", Height_ <<- "100%", Fill_ <<- "red"]
111
+ -- > <> circle_ [Cx_ <<- "150", Cy_ <<- "100", R_ <<- "80", Fill_ <<- "green"]
112
+ -- > <> text_ [ X_ <<- "150", Y_ <<- "125", FontSize_ <<- "60"
113
+ -- > , TextAnchor_ <<- "middle", Fill_ <<- "white" ] "SVG"
114
+ -- >
115
+ -- >
156
116
-- > main :: IO ()
157
117
-- > main = do
158
118
-- > print $ svg contents
0 commit comments