git.iko.soy/typeable/xml-lens
on 114e276 help sign in
8 files changed+36−112
since splitsnapshots
unifiedsplit
.github/workflows/haskell.yml−37 .gitignore−1 CHANGELOG.md−10 README.md+4−8 Text/XML/Lens.hs+24−40 cabal.project−1 stack.yaml−4 xml-lens.cabal+8−11
.github/workflows/haskell.ymldeleted−37
@@ -1,37 +0,0 @@
1−name: Haskell CI
2−
3−on: [push, pull_request]
4−
5−jobs:
6− build:
7−
8− runs-on: ubuntu-latest
9− strategy:
10− matrix:
11− ghc: [ '8.8.4', '8.10.7', '9.0.1', '9.2.2' ]
12− steps:
13− - uses: actions/checkout@v3
14− - uses: haskell/actions/setup@v1
15− with:
16− ghc-version: ${{ matrix.ghc }}
17− cabal-version: '3.4'
18−
19− - name: cabal Cache
20− uses: actions/cache@v1
21− env:
22− cache-name: cache-cabal
23− with:
24− path: ~/.cabal
25− key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/*.cabal') }}-${{ hashFiles('**/cabal.project') }}
26− restore-keys: |
27− ${{ runner.os }}-build-${{ env.cache-name }}-
28− ${{ runner.os }}-build-
29− ${{ runner.os }}-
30− - name: Install dependencies
31− run: |
32− cabal update
33− cabal build --only-dependencies --enable-tests --enable-benchmarks all
34− - name: Build
35− run: cabal build --enable-tests --enable-benchmarks all
36− - name: Run tests
37− run: cabal test --enable-tests --enable-benchmarks all
.gitignore−1
@@ -1,4 +1,3 @@
1−dist-newstyle
21#################
32## Eclipse
43#################
CHANGELOG.mddeleted−10
@@ -1,10 +0,0 @@
1−## 0.3.1
2−
3−* Supported GHC 9
4−
5−## 0.3 (2021/01)
6−
7−* Deprecated `el`
8−* Removed `./` and `entire`
9−
10−
README.md+4−8
@@ -9,16 +9,16 @@ Example
99```haskell
1010> doc <- Text.XML.readFile def "examples/books.xml"
1111
12−> doc ^.. root . named "books" ... named "book" . attributeIs "category" "Textbooks" ... named "title" . text
12+> doc ^.. root . el "books" ./ el "book" . attributeIs "category" "Textbooks" ./ el "title" . text
1313["Learn You a Haskell for Great Good!","Programming in Haskell","Real World Haskell"]
1414
15−> lengthOf ?? doc $ root . named "books" ... named "book"
15+> lengthOf ?? doc $ root . el "books" ./ el "book"
16167
1717
18−> doc ^? root . named "books" ... attributeIs "category" "Joke" ... named "title" . text
18+> doc ^? root . el "books" ./ attributeIs "category" "Joke" ./ el "title" . text
1919Just "Functional Ikamusume"
2020
21−> doc & root . named "books" ... named "book" ... named "pages" . text <>~ " pages" & renderLBS def & BL.putStrLn
21+> doc & root . el "books" ./ el "book" ./ el "pages" . text <>~ " pages" & renderLBS def & BL.putStrLn
2222```
2323
2424```xml
@@ -43,8 +43,4 @@ Just "Functional Ikamusume"
4343…
4444```
4545
46−Tips
47−----
4846
49−* Use [cosmos](http://hackage.haskell.org/package/lens-4.19.2/docs/Control-Lens-Plated.html) to traverse over all children
50−* You can do or-composition of traversals with [failing](http://hackage.haskell.org/package/lens-4.19.2/docs/Control-Lens-Traversal.html#v:failing)
Text/XML/Lens.hs+24−40
@@ -14,6 +14,7 @@
1414module Text.XML.Lens (
1515 -- * Lenses, traversals for 'Element'
1616 Element(..)
17+ , (./)
1718 , (...)
1819 -- ** Names
1920 , name
@@ -24,8 +25,6 @@ module Text.XML.Lens (
2425 -- ** Attributes
2526 , attributeIs
2627 , attributeSatisfies
27− , attributeSatisfies'
28− , withoutAttribute
2928 , attr
3029 , attribute
3130 , attrs
@@ -33,6 +32,7 @@ module Text.XML.Lens (
3332 , text
3433 , comment
3534 -- ** Children
35+ , entire
3636 , nodes
3737 -- * Prisms for 'Node'
3838 , Node(..)
@@ -60,48 +60,42 @@ import Text.XML
6060import Control.Lens
6161import Data.Text (Text)
6262import Data.Map (Map)
63+import Control.Applicative
6364import qualified Data.CaseInsensitive as CI
64−import Data.Maybe (isNothing)
65+
66+infixr 9 ./
6567
6668prologue :: Lens' Document Prologue
6769prologue f doc = fmap (\p -> doc { documentPrologue = p} ) $ f $ documentPrologue doc
68−{-# INLINE prologue #-}
6970
7071-- | The root element of the document.
7172root :: Lens' Document Element
7273root f doc = fmap (\p -> doc { documentRoot = p} ) $ f $ documentRoot doc
73−{-# INLINE root #-}
7474
7575epilogue :: Lens' Document [Miscellaneous]
7676epilogue f doc = fmap (\p -> doc { documentEpilogue = p} ) $ f $ documentEpilogue doc
77−{-# INLINE epilogue #-}
7877
7978doctype :: Lens' Prologue (Maybe Doctype)
8079doctype f doc = fmap (\p -> doc { prologueDoctype = p} ) $ f $ prologueDoctype doc
81−{-# INLINE doctype #-}
8280
8381class AsInstruction t where
8482 _Instruction :: Prism' t Instruction
8583
8684_instructionTarget :: Lens' Instruction Text
8785_instructionTarget f (Instruction t d) = f t <&> \t' -> Instruction t' d
88−{-# INLINE _instructionTarget #-}
8986
9087_instructionData :: Lens' Instruction Text
9188_instructionData f (Instruction t d) = f d <&> \d' -> Instruction t d'
92−{-# INLINE _instructionData #-}
9389
9490instance AsInstruction Node where
9591 _Instruction = prism' NodeInstruction $ \s -> case s of
9692 NodeInstruction e -> Just e
9793 _ -> Nothing
98− {-# INLINE _Instruction #-}
9994
10095instance AsInstruction Miscellaneous where
10196 _Instruction = prism' MiscInstruction $ \s -> case s of
10297 MiscInstruction e -> Just e
10398 _ -> Nothing
104− {-# INLINE _Instruction #-}
10599
106100class AsComment t where
107101 _Comment :: Prism' t Text
@@ -110,41 +104,33 @@ instance AsComment Node where
110104 _Comment = prism' NodeComment $ \s -> case s of
111105 NodeComment e -> Just e
112106 _ -> Nothing
113− {-# INLINE _Comment #-}
114107
115108instance AsComment Miscellaneous where
116109 _Comment = prism' MiscComment $ \s -> case s of
117110 MiscComment e -> Just e
118111 _ -> Nothing
119− {-# INLINE _Comment #-}
120112
121113_nameLocalName :: Lens' Name Text
122114_nameLocalName f n = f (nameLocalName n) <&> \x -> n { nameLocalName = x }
123−{-# INLINE _nameLocalName #-}
124115
125116_nameNamespace :: Lens' Name (Maybe Text)
126117_nameNamespace f n = f (nameNamespace n) <&> \x -> n { nameNamespace = x }
127−{-# INLINE _nameNamespace #-}
128118
129119_namePrefix :: Lens' Name (Maybe Text)
130120_namePrefix f n = f (namePrefix n) <&> \x -> n { namePrefix = x }
131−{-# INLINE _namePrefix #-}
132121
133122_Element :: Prism' Node Element
134123_Element = prism' NodeElement $ \s -> case s of
135124 NodeElement e -> Just e
136125 _ -> Nothing
137−{-# INLINE _Element #-}
138126
139127_Content :: Prism' Node Text
140128_Content = prism' NodeContent $ \s -> case s of
141129 NodeContent e -> Just e
142130 _ -> Nothing
143−{-# INLINE _Content #-}
144131
145132name :: Lens' Element Name
146133name f e = f (elementName e) <&> \x -> e { elementName = x }
147−{-# INLINE name #-}
148134
149135localName :: Lens' Element Text
150136localName = name . _nameLocalName
@@ -152,65 +138,63 @@ localName = name . _nameLocalName
152138
153139attrs :: Lens' Element (Map Name Text)
154140attrs f e = fmap (\x -> e { elementAttributes = x }) $ f $ elementAttributes e
155−{-# INLINE attrs #-}
156141
157142nodes :: Lens' Element [Node]
158143nodes f e = fmap (\x -> e { elementNodes = x }) $ f $ elementNodes e
159−{-# INLINE nodes #-}
160144
161145attr :: Name -> Traversal' Element Text
162146attr n = attrs . ix n
163−{-# INLINE attr #-}
164147
165148attribute :: Name -> Lens' Element (Maybe Text)
166149attribute n = attrs . at n
167−{-# INLINE attribute #-}
150+
151+-- | Traverse itself with its all children. Rewriting subnodes of each children will break a traversal law.
152+entire :: Traversal' Element Element
153+entire f e@(Element _ _ ns) = com <$> f e <*> traverse (_Element (entire f)) ns where
154+ com (Element n a _) = Element n a
155+{-# DEPRECATED entire "Use cosmos or deep instead" #-}
168156
169157-- | Traverse elements which has the specified *local* name (case-insensitive).
170158named :: CI.CI Text -> Traversal' Element Element
171159named n f s
172160 | CI.mk (nameLocalName (elementName s)) == n = f s
173161 | otherwise = pure s
174−{-# INLINE named #-}
175162
176163-- | Old name for 'named'
177164ell :: Text -> Traversal' Element Element
178−ell n = named $ CI.mk n
165+ell = named . CI.mk
179166
180167-- | Traverse elements which has the specified name.
181168el :: Name -> Traversal' Element Element
182169el n f s
183170 | elementName s == n = f s
184171 | otherwise = pure s
185−{-# DEPRECATED el "Use named instead" #-}
186172
187173attributeSatisfies :: Name -> (Text -> Bool) -> Traversal' Element Element
188−attributeSatisfies n p = attributeSatisfies' n (maybe False p)
189−{-# INLINE attributeSatisfies #-}
190−
191−attributeSatisfies' :: Name -> (Maybe Text -> Bool) -> Traversal' Element Element
192−attributeSatisfies' n p = filtered (p . preview (attrs . ix n))
193−{-# INLINE attributeSatisfies' #-}
194−
195−withoutAttribute :: Name -> Traversal' Element Element
196−withoutAttribute n = attributeSatisfies' n isNothing
197−{-# INLINE withoutAttribute #-}
174+attributeSatisfies n p = filtered (maybe False p . preview (attrs . ix n))
198175
199176attributeIs :: Name -> Text -> Traversal' Element Element
200177attributeIs n v = attributeSatisfies n (==v)
201−{-# INLINE attributeIs #-}
202178
203179-- | Traverse all contents of the element.
204180text :: Traversal' Element Text
205181text = nodes . traverse . _Content
206−{-# INLINE text #-}
207182
208183-- | Traverse all comments of the element.
209184comment :: Traversal' Element Text
210185comment = nodes . traverse . _Comment
211−{-# INLINE comment #-}
212186
213187-- | 'plate' traverses over its sub-elements.
214188instance Plated Element where
215189 plate = nodes . traverse . _Element
216− {-# INLINE plate #-}
190+
191+-- | Combine two 'Traversal's just like XPath's slash. Identical to ('...').
192+--
193+-- @
194+-- l ./ m ≡ l . 'plate' . m
195+-- @
196+(./) :: (Applicative f, Plated c) => LensLike f s t c c -> Over p f c c a b -> Over p f s t a b
197+(./) = (...)
198+{-# INLINE (./) #-}
199+
200+{-# DEPRECATED (./) "Use (...) instead" #-}
cabal.projectdeleted−1
@@ -1 +0,0 @@
1−packages: ./
stack.yamldeleted−4
@@ -1,4 +0,0 @@
1−resolver: lts-21.3
2−
3−packages:
4−- .
xml-lens.cabal+8−11
@@ -1,20 +1,17 @@
1−cabal-version: 2.4
21name: xml-lens
3−version: 0.3.1
2+version: 0.2
43synopsis: Lenses, traversals, and prisms for xml-conduit
5−description: This package provides DOM selectors based on lenses and prisms. See README.md for examples
4+description: Lens-based DOM selector
65homepage: https://github.com/fumieval/xml-lens
76bug-reports: https://github.com/fumieval/xml-lens/issues
8−license: BSD-3-Clause
7+license: BSD3
98license-file: LICENSE
109author: Fumiaki Kinoshita
1110maintainer: Fumiaki Kinoshita <fumiexcel@gmail.com>
12−copyright: Copyright (C) 2021 Fumiaki Kinoshita
11+copyright: Copyright (C) 2019 Fumiaki Kinoshita
1312category: XML
1413build-type: Simple
15−extra-source-files:
16− README.md
17− CHANGELOG.md
14+cabal-version: >=1.10
1815
1916source-repository head
2017 type: git
@@ -25,8 +22,8 @@ library
2522 ghc-options: -Wall
2623 exposed-modules: Text.XML.Lens
2724 build-depends: base ==4.*
28− , lens >= 4.0 && < 5.4
25+ , lens >= 4.0 && < 5
2926 , containers >= 0.4.0 && < 0.7
30− , text >= 0.7 && < 1.3 || >= 2 && < 2.2
31− , xml-conduit >= 1.1 && < 1.10
27+ , text >= 0.7 && <2
28+ , xml-conduit >= 1.1 && < 1.9
3229 , case-insensitive