hakyll-4.16.2.2: A static website compiler library

Safe Haskell | Safe-Inferred
Language | Haskell2010

Hakyll.Core.Identifier.Pattern

Contents

- The pattern type
- Creating patterns
- Composing patterns
- Applying patterns
- Capturing strings
- Orphan instances

Description

As `Identifier` is used to specify a single item, a `Pattern` is used to specify a list of items.

In most cases, globs are used for patterns.

A very simple pattern of such a pattern is `"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:

- `"*"`: matches at most 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"`;
- `"**"` will match any identifier;
- `"foo/**"` will match `"foo/bar"` and `"foo/bar/qux"`, but not `"bar/foo"`;
- `"foo/*.html"` will match all HTML files in the `"foo/"` directory.

The `capture` function allows the user to get access to the elements captured by the capture elements in a glob or regex pattern.

Synopsis

- data Pattern
- fromGlob :: String -> Pattern
- fromList :: [Identifier] -> Pattern
- fromRegex :: String -> Pattern
- fromVersion :: Maybe String -> Pattern
- hasVersion :: String -> Pattern
- hasNoVersion :: Pattern
- (.&&.) :: Pattern -> Pattern -> Pattern
- (.||.) :: Pattern -> Pattern -> Pattern
- complement :: Pattern -> Pattern
- matches :: Pattern -> Identifier -> Bool
- filterMatches :: Pattern -> [Identifier] -> [Identifier]
- capture :: Pattern -> Identifier -> Maybe [String]
- fromCapture :: Pattern -> String -> Identifier
- fromCaptures :: Pattern -> [String] -> Identifier

# The pattern type

data Pattern Source #

Type that allows matching on identifiers

#### Instances

Instances details

IsString Pattern Source # |
Instance details Defined in Hakyll.Core.Identifier.Pattern Methods fromString :: String -> Pattern Source #
Monoid Pattern Source # |
Instance details Defined in Hakyll.Core.Identifier.Pattern.Internal Methods mempty :: Pattern Source # mappend :: Pattern -> Pattern -> Pattern Source # mconcat :: [Pattern] -> Pattern Source #
Semigroup Pattern Source # |
Instance details Defined in Hakyll.Core.Identifier.Pattern.Internal Methods (<>) :: Pattern -> Pattern -> Pattern Source # sconcat :: NonEmpty Pattern -> Pattern Source # stimes :: Integral b => b -> Pattern -> Pattern Source #
Show Pattern Source # |
Instance details Defined in Hakyll.Core.Identifier.Pattern.Internal Methods showsPrec :: Int -> Pattern -> ShowS Source # show :: Pattern -> String Source # showList :: [Pattern] -> ShowS Source #
Binary Pattern Source # |
Instance details Defined in Hakyll.Core.Identifier.Pattern.Internal Methods put :: Pattern -> Put Source # get :: Get Pattern Source # putList :: [Pattern] -> Put Source #

# Creating patterns

fromGlob :: String -> Pattern Source #

Parse a pattern from a string

fromList :: [Identifier] -> Pattern Source #

Create a `Pattern` from a list of `Identifier`s it should match.

Warning: use this carefully with `hasNoVersion` and `hasVersion`. The `Identifier`s in the list already have versions assigned, and the pattern will then only match the intersection of both versions.

A more concrete example,

    fromList ["foo.markdown"] .&&. hasVersion "pdf"

will not match anything! The `"foo.markdown"` `Identifier` has no version assigned, so the LHS of `.&&.` will only match this `Identifier` with no version. The RHS only matches `Identifier`s with version set to `"pdf"` -- hence, this pattern matches nothing.

The correct way to use this is:

    fromList $ map (setVersion $ Just "pdf") ["foo.markdown"]

fromRegex :: String -> Pattern Source #

Create a `Pattern` from a regex

Example:

    regex "^foo/[^x]*$

fromVersion :: Maybe String -> Pattern Source #

Create a pattern which matches all items with the given version.

hasVersion :: String -> Pattern Source #

Specify a version, e.g.

    "foo/*.markdown" .&&. hasVersion "pdf"

hasNoVersion :: Pattern Source #

Match only if the identifier has no version set, e.g.

    "foo/*.markdown" .&&. hasNoVersion

# Composing patterns

(.&&.) :: Pattern -> Pattern -> Pattern infixr 3 Source #

`&&` for patterns: the given identifier must match both subterms

(.||.) :: Pattern -> Pattern -> Pattern infixr 2 Source #

`||` for patterns: the given identifier must match any subterm

complement :: Pattern -> Pattern Source #

Inverts a pattern, e.g.

    complement "foo/bar.html"

will match anything except `"foo/bar.html"`

# Applying patterns

matches :: Pattern -> Identifier -> Bool Source #

Check if an identifier matches a pattern

filterMatches :: Pattern -> [Identifier] -> [Identifier] Source #

Given a list of identifiers, retain only those who match the given pattern

# Capturing strings

capture :: Pattern -> Identifier -> Maybe [String] Source #

Match a glob or regex pattern against an identifier, generating a list of captures

fromCapture :: Pattern -> String -> Identifier Source #

Create an identifier from a pattern by filling in the captures with a given string

Example:

    fromCapture (fromGlob "tags/*") "foo"

Result:

    "tags/foo"

fromCaptures :: Pattern -> [String] -> Identifier Source #

Create an identifier from a pattern by filling in the captures with the given list of strings

# Orphan instances

IsString Pattern Source # |
Instance details Methods fromString :: String -> Pattern Source #