From bc9f8083ab6c93bf11e33fba937434c71cc6886f Mon Sep 17 00:00:00 2001 From: Lars-Dominik Braun Date: Wed, 12 Aug 2015 19:17:55 +0200 Subject: Add ingredient/metadata extraction tools --- src/Codec/Pesto/Lint.lhs | 6 ++++- src/Main.lhs | 67 +++++++++++++++++++++++++++++++++++++----------- 2 files changed, 57 insertions(+), 16 deletions(-) diff --git a/src/Codec/Pesto/Lint.lhs b/src/Codec/Pesto/Lint.lhs index 4e7efa5..031d5af 100644 --- a/src/Codec/Pesto/Lint.lhs +++ b/src/Codec/Pesto/Lint.lhs @@ -3,7 +3,11 @@ Linting .. class:: nodoc -> module Codec.Pesto.Lint (lint, test, parseMetadata, extractMetadata) where +> module Codec.Pesto.Lint (lint +> , test +> , parseMetadata +> , extractMetadata +> , Metadata(..)) where > import Test.HUnit hiding (test, Node) > import Data.List (sort, isPrefixOf) > import Control.Applicative ((<*>), (<$>), (*>)) diff --git a/src/Main.lhs b/src/Main.lhs index 5cc7c64..bebdd30 100644 --- a/src/Main.lhs +++ b/src/Main.lhs @@ -5,33 +5,70 @@ User interface > module Main (main) where > import System.IO (hPrint, stderr) -> import Codec.Pesto.Parse (parse) +> import System.Environment (getArgs) +> import Data.List (intercalate) +> +> import Codec.Pesto.Parse (parse, Instruction (Ingredient), Quantity (..)) > import Codec.Pesto.Graph (extract, toGraph, firstNodeId, resolveReferences) -> import Codec.Pesto.Lint (lint, extractMetadata) +> import Codec.Pesto.Lint (lint, extractMetadata, Metadata(..)) > import Codec.Pesto.Dot (toDot) +> import Codec.Pesto.Serialize (serialize) -The pesto to dot converter can be run with ``cabal run pesto``. It expects a -pesto recipe on the standard input and prints a dot graph to stdout that can be -converted to an image by piping it through ``dot -Tpng``. Example: +The user-interface has different modes of operation. All of read a single +recipe from the standard input. + +> main = do +> (op:_) <- getArgs +> s <- getContents +> either malformedRecipe (run op) (parse s) + +> malformedRecipe = print + +> streamToGraph stream = (nodes, edges) +> where +> doc = (head . extract . snd . unzip) stream +> nodes = zip [firstNodeId..] doc +> edges = toGraph nodes ++ resolveReferences nodes + +dot +^^^ + +Convert recipe into GraphViz’ dot language. Example: .. code:: bash - cabal run --verbose=0 pesto < spaghetti.pesto | dot -Tpng > spaghetti.png + cabal run --verbose=0 pesto dot < spaghetti.pesto | dot -Tpng > spaghetti.png .. class:: todo add linting information to graph -> main = do -> s <- getContents -> (flip . either) malformedRecipe (parse s) $ \stream -> do -> let -> doc = (head . extract . snd . unzip) stream -> nodes = zip [firstNodeId..] doc -> edges = toGraph nodes ++ resolveReferences nodes -> hPrint stderr $ extractMetadata nodes edges +> run "dot" stream = do +> let (nodes, edges) = streamToGraph stream > hPrint stderr $ lint nodes edges > putStrLn $ toDot nodes edges -> malformedRecipe = print +metadata +^^^^^^^^ + +Print metadata as key-value pairs, separated by ``=``. + +> run "metadata" stream = maybe (return ()) (mapM_ printMeta) $ uncurry extractMetadata $ streamToGraph stream + +ingredients +^^^^^^^^^^^ + +Extract ingredients and print them in CSV format. This does not take +alternatives into account yet. + +> run "ingredients" stream = mapM_ (putStrLn . csvQty) $ reverse $ foldl getIngredient [] stream +> where +> getIngredient xs (_, Ingredient q) = q:xs +> getIngredient xs _ = xs +> run _ _ = putStrLn "unknown operation" + +> printMeta (_, (key, MetaStr value)) = putStrLn $ key ++ "=" ++ value +> printMeta (_, (key, MetaQty q)) = putStrLn $ key ++ "=" ++ csvQty q + +> csvQty (Quantity a b c) = intercalate "," [serialize a, b, c] -- cgit v1.2.3