diff options
Diffstat (limited to 'src/exe/Main.lhs')
| -rw-r--r-- | src/exe/Main.lhs | 42 | 
1 files changed, 23 insertions, 19 deletions
| diff --git a/src/exe/Main.lhs b/src/exe/Main.lhs index 2f67ffd..ae663ad 100644 --- a/src/exe/Main.lhs +++ b/src/exe/Main.lhs @@ -8,31 +8,24 @@ User interface  > import Data.List (intercalate)  >  > import Codec.Pesto.Parse (parse, Instruction (Ingredient), Quantity (..)) -> import Codec.Pesto.Graph (extract, toGraph, firstNodeId, resolveReferences) +> import Codec.Pesto.Graph (extract, toGraph, firstNodeId, resolveReferences, extractIngredients, mergeQuantity)  > import Codec.Pesto.Lint (lint, extractMetadata, Metadata(..), LintResult (LintResult))  > import Codec.Pesto.Serialize (serialize) -The user-interface has different modes of operation. All of them read a single -recipe from the standard input. +The user-interface reads a single recipe from the standard input.  > main = do  > 	(op:_) <- getArgs  > 	s <- getContents ->	either malformedRecipe (run op) (parse s) +> 	either malformedRecipe (run op) (parse s) + +It has three modes of operation, described in the next sections.  > run "dot" = runDot  > run "metadata" = runMeta  > run "ingredients" = runIngredients  > run _ = const (putStrLn "unknown operation") -> malformedRecipe = print - -> streamToGraph stream = (nodes, edges) -> 	where -> 		doc = (head . extract . snd . unzip) stream -> 		nodes = zip [firstNodeId..] doc -> 		edges = toGraph nodes ++ resolveReferences nodes -  dot  ^^^ @@ -41,7 +34,7 @@ can represent recipes as well. Example:  .. code:: bash -	cabal run --verbose=0 pesto dot < spaghetti.pesto | dot -Tpng > spaghetti.png +    cabal run --verbose=0 pesto dot < spaghetti.pesto | dot -Tpng > spaghetti.png  > runDot stream = putStrLn $ toDot dotNodes dotEdges  > 	where @@ -50,10 +43,10 @@ can represent recipes as well. Example:  > 		(lintNodes, lintEdges) = unzip $ map (uncurry lintToNodesEdges)  > 				$ zip [maxId..] (lint nodes edges)  > 		dotNodes = concat [ ->				  [("node", [("fontname", "Roboto Semi-Light")])] +> 				  [("node", [("fontname", "Roboto Semi-Light")])]  > 				, map (\(a, label) -> (show a, [("label", serialize label)])) nodes  > 				, lintNodes ->				] +> 				]  > 		dotEdges = concat [  > 				  map (both show) edges  > 				, concat lintEdges @@ -72,7 +65,7 @@ can represent recipes as well. Example:  > 		<> "}"  > 	where  > 		edgeToDot (a, b) = a <> " -> " <> b <> ";" ->		nodeToDot (a, b) = a <> " [" <> mconcat (mapToDot b) <> "];" +> 		nodeToDot (a, b) = a <> " [" <> mconcat (mapToDot b) <> "];"  > mapToDot = map kvToDot  > kvToDot (k, v) = k <> "=\"" <> quoteString v <> "\"" @@ -94,13 +87,24 @@ ingredients  Extract ingredients and print them in CSV format. This does not take  alternatives into account yet. -> runIngredients stream = mapM_ (putStrLn . csvQty) $ reverse $ foldl getIngredient [] stream +> runIngredients stream = mapM_ (putStrLn . csvQty) $ ingredients  > 	where -> 		getIngredient xs (_, Ingredient q) = q:xs -> 		getIngredient xs _ = xs +> 		(nodes, _) = streamToGraph stream +> 		ingredients = mergeQuantity $ extractIngredients nodes  > 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] +Appendix +^^^^^^^^ + +> malformedRecipe = print + +> streamToGraph stream = (nodes, edges) +> 	where +> 		doc = (head . extract . snd . unzip) stream +> 		nodes = zip [firstNodeId..] doc +> 		edges = toGraph nodes ++ resolveReferences nodes + | 
