frontmatter (empty) → 0.1.0.0
raw patch · 7 files changed
+170/−0 lines, 7 filesdep +QuickCheckdep +attoparsecdep +basesetup-changed
Dependencies added: QuickCheck, attoparsec, base, bytestring, frontmatter, hspec, text, yaml
Files
- LICENSE +20/−0
- Setup.hs +2/−0
- frontmatter.cabal +45/−0
- src/Data/Frontmatter.hs +59/−0
- src/Data/Frontmatter/Internal.hs +24/−0
- src/Data/Yaml/Frontmatter.hs +19/−0
- test/Spec.hs +1/−0
+ LICENSE view
@@ -0,0 +1,20 @@+Copyright (c) 2015 Pedro Tacla Yamada++Permission is hereby granted, free of charge, to any person obtaining+a copy of this software and associated documentation files (the+"Software"), to deal in the Software without restriction, including+without limitation the rights to use, copy, modify, merge, publish,+distribute, sublicense, and/or sell copies of the Software, and to+permit persons to whom the Software is furnished to do so, subject to+the following conditions:++The above copyright notice and this permission notice shall be included+in all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ frontmatter.cabal view
@@ -0,0 +1,45 @@+name: frontmatter+version: 0.1.0.0+synopsis: Parses frontmatter as used in Jekyll markdown files.+description: Provides a parser that'll parse the frontmatter only and+ . one that'll execute a YAML parser on it, so that it's a+ . YAML frontmatter parser.+homepage: https://github.com/yamadapc/haskell-frontmatter+license: MIT+license-file: LICENSE+author: Pedro Tacla Yamada+maintainer: tacla.yamada@gmail.com+copyright: Copyright (c) 2015 Pedro Tacla Yamada+category: Data+build-type: Simple+cabal-version: >=1.10+tested-with: GHC >= 7.8++source-repository head+ type: git+ location: git://github.com/yamadapc/haskell-frontmatter.git++library+ exposed-modules: Data.Frontmatter+ , Data.Frontmatter.Internal+ , Data.Yaml.Frontmatter+ build-depends: attoparsec >= 0.11.3.0+ , base >= 4 && <5+ , bytestring+ , yaml+ hs-source-dirs: src+ default-language: Haskell2010++test-suite hspec+ main-is: Spec.hs+ type: exitcode-stdio-1.0+ build-depends: QuickCheck+ , attoparsec+ , base+ , yaml+ , bytestring+ , frontmatter+ , text+ , hspec+ hs-source-dirs: test+ default-language: Haskell2010
+ src/Data/Frontmatter.hs view
@@ -0,0 +1,59 @@+{-# LANGUAGE ScopedTypeVariables #-}+module Data.Frontmatter+ ( -- * Frontmatter parser+ frontmatter+ , frontmatterYaml+ -- * Utility functions+ , parseFrontmatter+ , parseFrontmatterMaybe+ , parseFrontmatterEither+ , parseYamlFrontmatter+ , parseYamlFrontmatterMaybe+ , parseYamlFrontmatterEither+ -- * Re-exports+ , parse+ , maybeResult+ , eitherResult+ , Parser+ , Result+ , IResult(..)+ )+ where++import Data.Attoparsec.ByteString+import Data.ByteString (ByteString)+import Data.Frontmatter.Internal+import Data.Yaml (FromJSON)+import Data.Yaml.Frontmatter++-- |+-- Parse a frontmatter from a 'ByteString' returning a 'Result'. Just extracts+-- whatever is on the frontmatter; doesn't care what it is.+parseFrontmatter :: ByteString -> Result ByteString+parseFrontmatter = parse frontmatter++-- |+-- 'parseFrontmatter' but returning a 'Maybe'+parseFrontmatterMaybe :: ByteString -> Maybe ByteString+parseFrontmatterMaybe = maybeResult . parse frontmatter++-- |+-- 'parseFrontmatter' but returning an 'Either'+parseFrontmatterEither :: ByteString -> Either String ByteString+parseFrontmatterEither = eitherResult . parse frontmatter++-- |+-- Parse a frontmatter from a 'ByteString' returning a 'FromJSON a'. Will parse+-- both JSON and YAML.+parseYamlFrontmatter :: FromJSON a => ByteString -> Result a+parseYamlFrontmatter = parse frontmatterYaml++-- |+-- 'parseYamlFrontmatter' but returning a 'Maybe'+parseYamlFrontmatterMaybe :: FromJSON a => ByteString -> Maybe a+parseYamlFrontmatterMaybe = maybeResult . parse frontmatterYaml++-- |+-- 'parseYamlFrontmatter' but returning an 'Either'+parseYamlFrontmatterEither :: FromJSON a => ByteString -> Either String a+parseYamlFrontmatterEither = eitherResult . parse frontmatterYaml
+ src/Data/Frontmatter/Internal.hs view
@@ -0,0 +1,24 @@+{-# LANGUAGE OverloadedStrings #-}+module Data.Frontmatter.Internal where++import Control.Applicative ((*>))+import Data.Attoparsec.ByteString+import Data.Attoparsec.ByteString.Char8+import Data.ByteString (ByteString)+import qualified Data.ByteString.Char8 as ByteString (pack)++-- |+-- A parser for a frontmatter; returns it as a 'ByteString'. Doesn't fail even+-- if it's empty. When it fails; returns a a IResult with the whole input+-- rather than consuming it.+frontmatter :: Parser ByteString+frontmatter = frontmatter' <?> "frontmatter"+ where+ frontmatter' = do+ f <- frontmatterSeparator *> manyTill anyChar frontmatterSeparator+ return $ ByteString.pack f++-- |+-- Internal parser for the frontmatter separator+frontmatterSeparator :: Parser ()+frontmatterSeparator = string "---" >> endOfLine
+ src/Data/Yaml/Frontmatter.hs view
@@ -0,0 +1,19 @@+{-# LANGUAGE ScopedTypeVariables #-}+module Data.Yaml.Frontmatter where++import Data.Attoparsec.ByteString+import Data.Frontmatter.Internal+import Data.Yaml (FromJSON, Value, decodeEither)++-- |+-- Parses a YAML frontmatter or JSON frontmatter from a 'ByteString' as a+-- 'Value'. Because of how @Data.Yaml@ is implemented using @aeson@, this will+-- succeed for JSON frontmatters as well as YAML ones.+frontmatterYaml :: FromJSON a => Parser a+frontmatterYaml = frontmatterYaml' <?> "frontmatterYaml"+ where+ frontmatterYaml' = do+ f <- frontmatter+ case decodeEither f of+ Left e -> fail e+ Right v -> return v
+ test/Spec.hs view
@@ -0,0 +1,1 @@+{-# OPTIONS_GHC -F -pgmF hspec-discover #-}