packages feed

pandoc-query 0.1.1 → 0.1.2

raw patch · 3 files changed

+28/−4 lines, 3 files

Files

CHANGELOG.md view
@@ -1,2 +1,8 @@ # Changelog for pandoc-query++0.1.2 Improved error message when unable to parse query.+      Added a mechanism to sort the results.++0.1.1 Added "All" selector.+ 0.1.0 Initial release
lib/Text/Pandoc/Query.hs view
@@ -20,27 +20,33 @@     Query(..),     Formatter(..),     Expression(..),+    SortOrder(..),     satisfies   ) where  import Prelude hiding (concat)+import Data.List            (sortOn) import Data.Text            qualified as T import Text.Pandoc          qualified as P import Text.Pandoc.Util     (meta, body, parseMarkdown) import Text.Pandoc.Metadata (valueOf) import Text.Pandoc.Walk     (walkM)+import Text.Read            (readEither)  -- | -- Represents an executable query and specifies how to format the results.-data Query = Query Expression Formatter deriving (Read, Show)+data Query = Query Expression Formatter SortOrder deriving (Read, Show) +-- |+-- Specifies how to format the query results. -- Currently results can only be specified using markdown syntax. -- However, the query results will be parsed into Pandoc's native format -- and inserted into the document. -- So the resulting document can be output in any format supported by Pandoc. data Formatter = ParseFromMarkdown T.Text deriving (Read, Show) +-- | Document filter for query. data Expression = MetaValueIs T.Text T.Text                 | MetaValueIncludes T.Text T.Text                 | All@@ -48,6 +54,10 @@                 | Or Expression Expression                 | Not Expression deriving (Read, Show) +-- | Order in which the selected documents should be presented.+data SortOrder = Ascending T.Text | Descending T.Text deriving (Read, Show)++-- | Returns true if the document satisfies the expression; returns false otherwise. satisfies :: P.Pandoc -> Expression -> Bool p `satisfies` (MetaValueIs k v) = (meta p) `valueOf` k == [v] p `satisfies` (MetaValueIncludes k v) = v `elem` ( (meta p) `valueOf` k )@@ -69,13 +79,21 @@ runQueries _ x = return x  run :: Query -> [P.Pandoc] -> IO P.Block-run (Query e f) ps = formatResults f (runQuery e ps)+run (Query e f s) ps = formatResults f (runSort s $ runQuery e ps)  parse :: T.Text -> Query-parse = read . T.unpack+parse t =+  case readEither s of+    Right q  -> q+    Left msg -> error (msg ++ " while parsing query '" ++ s ++ "'")+  where s = T.unpack t  runQuery :: Expression -> [P.Pandoc] -> [P.Pandoc] runQuery c = filter (`satisfies` c)++runSort :: SortOrder -> [P.Pandoc] -> [P.Pandoc]+runSort (Ascending k) = sortOn (\p -> (meta p) `valueOf` k)+runSort (Descending k) = reverse . runSort (Ascending k)  -- | Formats information about a set of pandoc documents. formatResults :: Formatter -> [P.Pandoc] -> IO P.Block
pandoc-query.cabal view
@@ -1,6 +1,6 @@ cabal-version:   3.0 name:            pandoc-query-version:         0.1.1+version:         0.1.2 synopsis:        Pandoc filter to extract only the links. description:   For more information and a tutorial on how to use this package,