mirror of
https://github.com/typeable/xml-lens.git
synced 2024-11-20 20:07:05 +03:00
first commit
This commit is contained in:
commit
af8c8366bc
22
.gitattributes
vendored
Normal file
22
.gitattributes
vendored
Normal file
@ -0,0 +1,22 @@
|
||||
# Auto detect text files and perform LF normalization
|
||||
* text=auto
|
||||
|
||||
# Custom for Visual Studio
|
||||
*.cs diff=csharp
|
||||
*.sln merge=union
|
||||
*.csproj merge=union
|
||||
*.vbproj merge=union
|
||||
*.fsproj merge=union
|
||||
*.dbproj merge=union
|
||||
|
||||
# Standard to msysgit
|
||||
*.doc diff=astextplain
|
||||
*.DOC diff=astextplain
|
||||
*.docx diff=astextplain
|
||||
*.DOCX diff=astextplain
|
||||
*.dot diff=astextplain
|
||||
*.DOT diff=astextplain
|
||||
*.pdf diff=astextplain
|
||||
*.PDF diff=astextplain
|
||||
*.rtf diff=astextplain
|
||||
*.RTF diff=astextplain
|
215
.gitignore
vendored
Normal file
215
.gitignore
vendored
Normal file
@ -0,0 +1,215 @@
|
||||
#################
|
||||
## Eclipse
|
||||
#################
|
||||
|
||||
*.pydevproject
|
||||
.project
|
||||
.metadata
|
||||
bin/
|
||||
tmp/
|
||||
*.tmp
|
||||
*.bak
|
||||
*.swp
|
||||
*~.nib
|
||||
local.properties
|
||||
.classpath
|
||||
.settings/
|
||||
.loadpath
|
||||
|
||||
# External tool builders
|
||||
.externalToolBuilders/
|
||||
|
||||
# Locally stored "Eclipse launch configurations"
|
||||
*.launch
|
||||
|
||||
# CDT-specific
|
||||
.cproject
|
||||
|
||||
# PDT-specific
|
||||
.buildpath
|
||||
|
||||
|
||||
#################
|
||||
## Visual Studio
|
||||
#################
|
||||
|
||||
## Ignore Visual Studio temporary files, build results, and
|
||||
## files generated by popular Visual Studio add-ons.
|
||||
|
||||
# User-specific files
|
||||
*.suo
|
||||
*.user
|
||||
*.sln.docstates
|
||||
|
||||
# Build results
|
||||
|
||||
[Dd]ebug/
|
||||
[Rr]elease/
|
||||
x64/
|
||||
build/
|
||||
[Bb]in/
|
||||
[Oo]bj/
|
||||
|
||||
# MSTest test Results
|
||||
[Tt]est[Rr]esult*/
|
||||
[Bb]uild[Ll]og.*
|
||||
|
||||
*_i.c
|
||||
*_p.c
|
||||
*.ilk
|
||||
*.meta
|
||||
*.obj
|
||||
*.pch
|
||||
*.pdb
|
||||
*.pgc
|
||||
*.pgd
|
||||
*.rsp
|
||||
*.sbr
|
||||
*.tlb
|
||||
*.tli
|
||||
*.tlh
|
||||
*.tmp
|
||||
*.tmp_proj
|
||||
*.log
|
||||
*.vspscc
|
||||
*.vssscc
|
||||
.builds
|
||||
*.pidb
|
||||
*.log
|
||||
*.scc
|
||||
|
||||
# Visual C++ cache files
|
||||
ipch/
|
||||
*.aps
|
||||
*.ncb
|
||||
*.opensdf
|
||||
*.sdf
|
||||
*.cachefile
|
||||
|
||||
# Visual Studio profiler
|
||||
*.psess
|
||||
*.vsp
|
||||
*.vspx
|
||||
|
||||
# Guidance Automation Toolkit
|
||||
*.gpState
|
||||
|
||||
# ReSharper is a .NET coding add-in
|
||||
_ReSharper*/
|
||||
*.[Rr]e[Ss]harper
|
||||
|
||||
# TeamCity is a build add-in
|
||||
_TeamCity*
|
||||
|
||||
# DotCover is a Code Coverage Tool
|
||||
*.dotCover
|
||||
|
||||
# NCrunch
|
||||
*.ncrunch*
|
||||
.*crunch*.local.xml
|
||||
|
||||
# Installshield output folder
|
||||
[Ee]xpress/
|
||||
|
||||
# DocProject is a documentation generator add-in
|
||||
DocProject/buildhelp/
|
||||
DocProject/Help/*.HxT
|
||||
DocProject/Help/*.HxC
|
||||
DocProject/Help/*.hhc
|
||||
DocProject/Help/*.hhk
|
||||
DocProject/Help/*.hhp
|
||||
DocProject/Help/Html2
|
||||
DocProject/Help/html
|
||||
|
||||
# Click-Once directory
|
||||
publish/
|
||||
|
||||
# Publish Web Output
|
||||
*.Publish.xml
|
||||
*.pubxml
|
||||
|
||||
# NuGet Packages Directory
|
||||
## TODO: If you have NuGet Package Restore enabled, uncomment the next line
|
||||
#packages/
|
||||
|
||||
# Windows Azure Build Output
|
||||
csx
|
||||
*.build.csdef
|
||||
|
||||
# Windows Store app package directory
|
||||
AppPackages/
|
||||
|
||||
# Others
|
||||
sql/
|
||||
*.Cache
|
||||
ClientBin/
|
||||
[Ss]tyle[Cc]op.*
|
||||
~$*
|
||||
*~
|
||||
*.dbmdl
|
||||
*.[Pp]ublish.xml
|
||||
*.pfx
|
||||
*.publishsettings
|
||||
|
||||
# RIA/Silverlight projects
|
||||
Generated_Code/
|
||||
|
||||
# Backup & report files from converting an old project file to a newer
|
||||
# Visual Studio version. Backup files are not needed, because we have git ;-)
|
||||
_UpgradeReport_Files/
|
||||
Backup*/
|
||||
UpgradeLog*.XML
|
||||
UpgradeLog*.htm
|
||||
|
||||
# SQL Server files
|
||||
App_Data/*.mdf
|
||||
App_Data/*.ldf
|
||||
|
||||
#############
|
||||
## Windows detritus
|
||||
#############
|
||||
|
||||
# Windows image file caches
|
||||
Thumbs.db
|
||||
ehthumbs.db
|
||||
|
||||
# Folder config file
|
||||
Desktop.ini
|
||||
|
||||
# Recycle Bin used on file shares
|
||||
$RECYCLE.BIN/
|
||||
|
||||
# Mac crap
|
||||
.DS_Store
|
||||
|
||||
|
||||
#############
|
||||
## Python
|
||||
#############
|
||||
|
||||
*.py[co]
|
||||
|
||||
# Packages
|
||||
*.egg
|
||||
*.egg-info
|
||||
dist/
|
||||
build/
|
||||
eggs/
|
||||
parts/
|
||||
var/
|
||||
sdist/
|
||||
develop-eggs/
|
||||
.installed.cfg
|
||||
|
||||
# Installer logs
|
||||
pip-log.txt
|
||||
|
||||
# Unit test / coverage reports
|
||||
.coverage
|
||||
.tox
|
||||
|
||||
#Translations
|
||||
*.mo
|
||||
|
||||
#Mr Developer
|
||||
.mr.developer.cfg
|
30
LICENSE
Normal file
30
LICENSE
Normal file
@ -0,0 +1,30 @@
|
||||
Copyright (c) 2013, Fumiaki Kinoshita
|
||||
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
|
||||
* Redistributions in binary form must reproduce the above
|
||||
copyright notice, this list of conditions and the following
|
||||
disclaimer in the documentation and/or other materials provided
|
||||
with the distribution.
|
||||
|
||||
* Neither the name of Fumiaki Kinoshita nor the names of other
|
||||
contributors may be used to endorse or promote products derived
|
||||
from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
143
Text/XML/Lens.hs
Normal file
143
Text/XML/Lens.hs
Normal file
@ -0,0 +1,143 @@
|
||||
{-# LANGUAGE Rank2Types #-}
|
||||
-----------------------------------------------------------------------------
|
||||
-- |
|
||||
-- Module : Text.XML.Lens
|
||||
-- Copyright : (C) 2013 Fumiaki Kinoshita
|
||||
-- License : BSD-style (see the file LICENSE)
|
||||
--
|
||||
-- Maintainer : Fumiaki Kinoshita <fumiexcel@gmail.com>
|
||||
-- Stability : experimental
|
||||
-- Portability : non-portable
|
||||
--
|
||||
----------------------------------------------------------------------------
|
||||
module Text.XML.Lens (
|
||||
-- * Useful traversals inspired by XPath
|
||||
(./)
|
||||
, el
|
||||
, attributeIs
|
||||
, attributeSatisfies
|
||||
, text
|
||||
, comment
|
||||
-- * Lenses, traversals for 'Element'
|
||||
, name
|
||||
, attrs
|
||||
, nodes
|
||||
, attr
|
||||
, attribute
|
||||
-- * Prisms for 'Node'
|
||||
, _Element
|
||||
, _Content
|
||||
, AsInstruction(..)
|
||||
, AsComment(..)
|
||||
-- * Lenses for 'Document'
|
||||
, root
|
||||
, prologue
|
||||
, epilogue
|
||||
, doctype
|
||||
-- * Lenses for 'Name'
|
||||
, _nameLocalName
|
||||
, _nameNamespace
|
||||
, _namePrefix
|
||||
) where
|
||||
import Text.XML
|
||||
import Control.Lens
|
||||
import Data.Text (Text)
|
||||
import Data.Map (Map)
|
||||
import Control.Applicative
|
||||
|
||||
infixr 9 ./
|
||||
|
||||
prologue :: Lens' Document Prologue
|
||||
prologue f doc = fmap (\p -> doc { documentPrologue = p} ) $ f $ documentPrologue doc
|
||||
|
||||
root :: Lens' Document Element
|
||||
root f doc = fmap (\p -> doc { documentRoot = p} ) $ f $ documentRoot doc
|
||||
|
||||
epilogue :: Lens' Document [Miscellaneous]
|
||||
epilogue f doc = fmap (\p -> doc { documentEpilogue = p} ) $ f $ documentEpilogue doc
|
||||
|
||||
doctype :: Lens' Prologue (Maybe Doctype)
|
||||
doctype f doc = fmap (\p -> doc { prologueDoctype = p} ) $ f $ prologueDoctype doc
|
||||
|
||||
class AsInstruction t where
|
||||
_Instruction :: Prism' t Instruction
|
||||
|
||||
instance AsInstruction Node where
|
||||
_Instruction = prism' NodeInstruction $ \s -> case s of
|
||||
NodeInstruction e -> Just e
|
||||
_ -> Nothing
|
||||
|
||||
instance AsInstruction Miscellaneous where
|
||||
_Instruction = prism' MiscInstruction $ \s -> case s of
|
||||
MiscInstruction e -> Just e
|
||||
_ -> Nothing
|
||||
|
||||
class AsComment t where
|
||||
_Comment :: Prism' t Text
|
||||
|
||||
instance AsComment Node where
|
||||
_Comment = prism' NodeComment $ \s -> case s of
|
||||
NodeComment e -> Just e
|
||||
_ -> Nothing
|
||||
|
||||
instance AsComment Miscellaneous where
|
||||
_Comment = prism' MiscComment $ \s -> case s of
|
||||
MiscComment e -> Just e
|
||||
_ -> Nothing
|
||||
|
||||
_nameLocalName :: Lens' Name Text
|
||||
_nameLocalName f n = fmap (\x -> n { nameLocalName = x }) $ f $ nameLocalName n
|
||||
|
||||
_nameNamespace :: Lens' Name (Maybe Text)
|
||||
_nameNamespace f n = fmap (\x -> n { nameNamespace = x }) $ f $ nameNamespace n
|
||||
|
||||
_namePrefix :: Lens' Name (Maybe Text)
|
||||
_namePrefix f n = fmap (\x -> n { namePrefix = x }) $ f $ namePrefix n
|
||||
|
||||
_Element :: Prism' Node Element
|
||||
_Element = prism' NodeElement $ \s -> case s of
|
||||
NodeElement e -> Just e
|
||||
_ -> Nothing
|
||||
|
||||
_Content :: Prism' Node Text
|
||||
_Content = prism' NodeContent $ \s -> case s of
|
||||
NodeContent e -> Just e
|
||||
_ -> Nothing
|
||||
|
||||
name :: Lens' Element Name
|
||||
name f e = fmap (\x -> e { elementName = x }) $ f $ elementName e
|
||||
|
||||
attrs :: Lens' Element (Map Name Text)
|
||||
attrs f e = fmap (\x -> e { elementAttributes = x }) $ f $ elementAttributes e
|
||||
|
||||
nodes :: Lens' Element [Node]
|
||||
nodes f e = fmap (\x -> e { elementNodes = x }) $ f $ elementNodes e
|
||||
|
||||
attr :: Name -> Traversal' Element Text
|
||||
attr n = attrs . ix n
|
||||
|
||||
attribute :: Name -> Lens' Element (Maybe Text)
|
||||
attribute n = attrs . at n
|
||||
|
||||
el :: Name -> Traversal' Element Element
|
||||
el n f s
|
||||
| elementName s == n = f s
|
||||
| otherwise = pure s
|
||||
|
||||
attributeSatisfies :: Name -> (Text -> Bool) -> Traversal' Element Element
|
||||
attributeSatisfies n p = plate . filtered (maybe False p . preview (attrs . ix n))
|
||||
|
||||
attributeIs :: Name -> Text -> Traversal' Element Element
|
||||
attributeIs n v = attributeSatisfies n (==v)
|
||||
|
||||
text :: Traversal' Element Text
|
||||
text = nodes . traverse . _Content
|
||||
|
||||
comment :: Traversal' Element Text
|
||||
comment = nodes . traverse . _Comment
|
||||
|
||||
instance Plated Element where
|
||||
plate = nodes . traverse . _Element
|
||||
|
||||
(./) :: Plated a => Traversal s t a a -> Traversal a a u v -> Traversal s t u v
|
||||
l ./ m = l . plate . m
|
24
xml-lens.cabal
Normal file
24
xml-lens.cabal
Normal file
@ -0,0 +1,24 @@
|
||||
name: xml-lens
|
||||
version: 0.1
|
||||
synopsis: Lenses, traversals, prisms for xml-conduit
|
||||
description:
|
||||
homepage: https://github.com/fumieval/xml-lens
|
||||
license: BSD3
|
||||
license-file: LICENSE
|
||||
author: Fumiaki Kinoshita
|
||||
maintainer: Fumiaki Kinoshita <fumiexcel@gmail.com>
|
||||
copyright: Copyright (C) 2013 Fumiaki Kinoshita
|
||||
category: Control
|
||||
build-type: Simple
|
||||
cabal-version: >=1.10
|
||||
|
||||
source-repository head
|
||||
type: git
|
||||
location: https://github.com/fumieval/xml-lens.git
|
||||
|
||||
library
|
||||
default-language: Haskell2010
|
||||
ghc-options: -Wall -O
|
||||
exposed-modules: Text.XML.Lens
|
||||
-- other-modules:
|
||||
build-depends: base ==4.*, lens >= 3.8 && < 4.2, containers >= 0.4.0 && < 0.6, text == 0.11.*, xml-conduit >= 1.1
|
Loading…
Reference in New Issue
Block a user