From 3fdf8ab204cfc6f60a250b8ef0cccce8e82a4bcf Mon Sep 17 00:00:00 2001 From: Jasper Van der Jeugt Date: Thu, 23 Dec 2010 17:19:21 +0100 Subject: Add identifier/pattern modules --- src/Hakyll/Core/Identifier/Pattern.hs | 91 +++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 src/Hakyll/Core/Identifier/Pattern.hs (limited to 'src/Hakyll/Core/Identifier') diff --git a/src/Hakyll/Core/Identifier/Pattern.hs b/src/Hakyll/Core/Identifier/Pattern.hs new file mode 100644 index 0000000..02b023f --- /dev/null +++ b/src/Hakyll/Core/Identifier/Pattern.hs @@ -0,0 +1,91 @@ +-- | Module providing pattern matching and capturing on 'Identifier's. +-- +-- TODO: Documentation +-- +module Hakyll.Core.Identifier.Pattern + ( Pattern + , parsePattern + , match + , doesMatch + , matches + ) where + +import Data.List (intercalate) +import Control.Monad (msum) +import Data.Maybe (isJust) + +import GHC.Exts (IsString, fromString) + +import Hakyll.Core.Identifier + +-- | One base element of a pattern +-- +data PatternComponent = CaptureOne + | CaptureMany + | Literal String + deriving (Eq) + +instance Show PatternComponent where + show CaptureOne = "*" + show CaptureMany = "**" + show (Literal s) = s + +-- | Type that allows matching on identifiers +-- +newtype Pattern = Pattern {unPattern :: [PatternComponent]} + deriving (Eq) + +instance Show Pattern where + show = intercalate "/" . map show . unPattern + +instance IsString Pattern where + fromString = parsePattern + +-- | Parse a pattern from a string +-- +parsePattern :: String -> Pattern +parsePattern = Pattern . map toPattern . unIdentifier . parseIdentifier + where + toPattern x | x == "*" = CaptureOne + | x == "**" = CaptureMany + | otherwise = Literal x + +-- | Match an identifier against a pattern, generating a list of captures +-- +match :: Pattern -> Identifier -> Maybe [[String]] +match (Pattern p) (Identifier i) = match' p i + +-- | Check if an identifier matches a pattern +-- +doesMatch :: Pattern -> Identifier -> Bool +doesMatch p = isJust . match p + +-- | Given a list of identifiers, retain only those who match the given pattern +-- +matches :: Pattern -> [Identifier] -> [Identifier] +matches p = filter (doesMatch p) + +-- | Split a list at every possible point, generate a list of (init, tail) cases +-- +splits :: [a] -> [([a], [a])] +splits ls = reverse $ splits' [] ls + where + splits' lx ly = (lx, ly) : case ly of + [] -> [] + (y : ys) -> splits' (lx ++ [y]) ys + +-- | Internal verion of 'match' +-- +match' :: [PatternComponent] -> [String] -> Maybe [[String]] +match' [] [] = Just [] -- An empty match +match' [] _ = Nothing -- No match +match' _ [] = Nothing -- No match +match' (m : ms) (s : ss) = case m of + -- Take one string and one literal, fail on mismatch + Literal l -> if s == l then match' ms ss else Nothing + -- Take one string and one capture + CaptureOne -> fmap ([s] :) $ match' ms ss + -- Take one string, and one or many captures + CaptureMany -> + let take' (i, t) = fmap (i :) $ match' ms t + in msum $ map take' $ splits (s : ss) -- cgit v1.2.3 From 10328b890cee125d8b5bd1c2a0a8d3b2489c4267 Mon Sep 17 00:00:00 2001 From: Jasper Van der Jeugt Date: Thu, 23 Dec 2010 18:22:16 +0100 Subject: Document the pattern module a bit --- src/Hakyll/Core/Identifier/Pattern.hs | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) (limited to 'src/Hakyll/Core/Identifier') diff --git a/src/Hakyll/Core/Identifier/Pattern.hs b/src/Hakyll/Core/Identifier/Pattern.hs index 02b023f..28c31f2 100644 --- a/src/Hakyll/Core/Identifier/Pattern.hs +++ b/src/Hakyll/Core/Identifier/Pattern.hs @@ -1,6 +1,35 @@ -- | Module providing pattern matching and capturing on 'Identifier's. -- --- TODO: Documentation +-- A very simple pattern could be, for example: +-- +-- > foo/bar +-- +-- This pattern will only match the @foo\/bar@ identifier. +-- +-- To match more than one identifier, there are different captures that one can +-- use: +-- +-- * @*@: matches exactly one element of an identifier; +-- +-- * @**@: matches one or more elements of an identifier. +-- +-- Some examples: +-- +-- * @foo\/*@ will match @foo\/bar@ and @foo\/foo@, but not @foo\/bar\/qux@ nor +-- @foo@; +-- +-- * @**@ will match any non-empty identifier; +-- +-- * @foo\/**@ will match @foo\/bar@ and @foo\/bar\/qux@, but not @bar\/foo@ nor +-- @foo@; +-- +-- A small warning: patterns are not globs. Using @foo\/*.markdown@ will not do +-- what you probably intended, as it will only match the file which is literally +-- called @foo\/*.markdown@. Remember that these captures only work on elements +-- of identifiers as a whole; not on parts of these elements. +-- +-- Furthermore, the 'match' function allows the user to get access to the +-- elements captured by the capture elements in the pattern. -- module Hakyll.Core.Identifier.Pattern ( Pattern -- cgit v1.2.3 From 07ca8954a5afef265253dd7fa2b77561a8b7470a Mon Sep 17 00:00:00 2001 From: Jasper Van der Jeugt Date: Thu, 23 Dec 2010 18:24:19 +0100 Subject: Minor change to pattern documentation --- src/Hakyll/Core/Identifier/Pattern.hs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) (limited to 'src/Hakyll/Core/Identifier') diff --git a/src/Hakyll/Core/Identifier/Pattern.hs b/src/Hakyll/Core/Identifier/Pattern.hs index 28c31f2..b5f01e5 100644 --- a/src/Hakyll/Core/Identifier/Pattern.hs +++ b/src/Hakyll/Core/Identifier/Pattern.hs @@ -1,10 +1,7 @@ -- | Module providing pattern matching and capturing on 'Identifier's. -- --- A very simple pattern could be, for example: --- --- > foo/bar --- --- This pattern will only match the @foo\/bar@ identifier. +-- A very simple pattern could be, for example, @foo\/bar@. This pattern will +-- only match the exact @foo\/bar@ identifier. -- -- To match more than one identifier, there are different captures that one can -- use: -- cgit v1.2.3 From 672ecb077c7edd6a542958a2c9ede5c8ea14bbc4 Mon Sep 17 00:00:00 2001 From: Jasper Van der Jeugt Date: Fri, 7 Jan 2011 15:09:55 +0100 Subject: Add fromCapture(s) --- src/Hakyll/Core/Identifier.hs | 4 +++- src/Hakyll/Core/Identifier/Pattern.hs | 26 ++++++++++++++++++++++++-- 2 files changed, 27 insertions(+), 3 deletions(-) (limited to 'src/Hakyll/Core/Identifier') diff --git a/src/Hakyll/Core/Identifier.hs b/src/Hakyll/Core/Identifier.hs index 609e722..ea03e8c 100644 --- a/src/Hakyll/Core/Identifier.hs +++ b/src/Hakyll/Core/Identifier.hs @@ -10,6 +10,7 @@ -- -- * @error/404@ -- +{-# LANGUAGE GeneralizedNewtypeDeriving #-} module Hakyll.Core.Identifier ( Identifier (..) , parseIdentifier @@ -17,6 +18,7 @@ module Hakyll.Core.Identifier ) where import Control.Arrow (second) +import Data.Monoid (Monoid) import GHC.Exts (IsString, fromString) import System.FilePath (joinPath) @@ -24,7 +26,7 @@ import System.FilePath (joinPath) -- | An identifier used to uniquely identify a value -- newtype Identifier = Identifier {unIdentifier :: [String]} - deriving (Eq, Ord) + deriving (Eq, Ord, Monoid) instance Show Identifier where show = toFilePath diff --git a/src/Hakyll/Core/Identifier/Pattern.hs b/src/Hakyll/Core/Identifier/Pattern.hs index b5f01e5..0590387 100644 --- a/src/Hakyll/Core/Identifier/Pattern.hs +++ b/src/Hakyll/Core/Identifier/Pattern.hs @@ -34,11 +34,14 @@ module Hakyll.Core.Identifier.Pattern , match , doesMatch , matches + , fromCapture + , fromCaptures ) where import Data.List (intercalate) import Control.Monad (msum) import Data.Maybe (isJust) +import Data.Monoid (mempty, mappend) import GHC.Exts (IsString, fromString) @@ -78,8 +81,8 @@ parsePattern = Pattern . map toPattern . unIdentifier . parseIdentifier -- | Match an identifier against a pattern, generating a list of captures -- -match :: Pattern -> Identifier -> Maybe [[String]] -match (Pattern p) (Identifier i) = match' p i +match :: Pattern -> Identifier -> Maybe [Identifier] +match (Pattern p) (Identifier i) = fmap (map Identifier) $ match' p i -- | Check if an identifier matches a pattern -- @@ -115,3 +118,22 @@ match' (m : ms) (s : ss) = case m of CaptureMany -> let take' (i, t) = fmap (i :) $ match' ms t in msum $ map take' $ splits (s : ss) + +-- | Create an identifier from a pattern by filling in the captures with a given +-- string +-- +fromCapture :: Pattern -> Identifier -> Identifier +fromCapture pattern = fromCaptures pattern . repeat + +-- | Create an identifier from a pattern by filling in the captures with the +-- given list of strings +-- +fromCaptures :: Pattern -> [Identifier] -> Identifier +fromCaptures (Pattern []) _ = mempty +fromCaptures (Pattern (m : ms)) [] = case m of + Literal l -> Identifier [l] `mappend` fromCaptures (Pattern ms) [] + _ -> error $ "Hakyll.Core.Identifier.Pattern.fromCaptures: " + ++ "identifier list exhausted" +fromCaptures (Pattern (m : ms)) ids@(i : is) = case m of + Literal l -> Identifier [l] `mappend` fromCaptures (Pattern ms) ids + _ -> i `mappend` fromCaptures (Pattern ms) is -- cgit v1.2.3 From 6cecbb890f829e30e533e58287867981ca04d78a Mon Sep 17 00:00:00 2001 From: Jasper Van der Jeugt Date: Tue, 25 Jan 2011 20:56:10 +0100 Subject: Add `fromCaptureString` function --- src/Hakyll/Core/Identifier/Pattern.hs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'src/Hakyll/Core/Identifier') diff --git a/src/Hakyll/Core/Identifier/Pattern.hs b/src/Hakyll/Core/Identifier/Pattern.hs index 0590387..7c88356 100644 --- a/src/Hakyll/Core/Identifier/Pattern.hs +++ b/src/Hakyll/Core/Identifier/Pattern.hs @@ -35,6 +35,7 @@ module Hakyll.Core.Identifier.Pattern , doesMatch , matches , fromCapture + , fromCaptureString , fromCaptures ) where @@ -122,9 +123,29 @@ match' (m : ms) (s : ss) = case m of -- | Create an identifier from a pattern by filling in the captures with a given -- string -- +-- Example: +-- +-- > fromCapture (parsePattern "tags/*") (parseIdentifier "foo") +-- +-- Result: +-- +-- > "tags/foo" +-- fromCapture :: Pattern -> Identifier -> Identifier fromCapture pattern = fromCaptures pattern . repeat +-- | Simplified version of 'fromCapture' which takes a 'String' instead of an +-- 'Identifier' +-- +-- > fromCaptureString (parsePattern "tags/*") "foo" +-- +-- Result: +-- +-- > "tags/foo" +-- +fromCaptureString :: Pattern -> String -> Identifier +fromCaptureString pattern = fromCapture pattern . parseIdentifier + -- | Create an identifier from a pattern by filling in the captures with the -- given list of strings -- -- cgit v1.2.3