From 6caf39255029f6a2791a5eb7711fefef19e1819d Mon Sep 17 00:00:00 2001 From: Chris Penner Date: Thu, 20 Apr 2017 18:28:16 -0600 Subject: [PATCH] Run template through shell --- examples/templates/lorem.md | 4 ++++ examples/templates/simple.md | 14 +++++++------- src/Plated/Command.hs | 9 +++++---- src/Plated/Parser.hs | 11 +++++++---- src/Plated/Template.hs | 8 +++++--- 5 files changed, 28 insertions(+), 18 deletions(-) create mode 100644 examples/templates/lorem.md mode change 100644 => 100755 examples/templates/simple.md diff --git a/examples/templates/lorem.md b/examples/templates/lorem.md new file mode 100644 index 0000000..eba6da5 --- /dev/null +++ b/examples/templates/lorem.md @@ -0,0 +1,4 @@ +Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod +tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At +vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, +no sea takimata sanctus est Lorem ipsum dolor sit amet. diff --git a/examples/templates/simple.md b/examples/templates/simple.md old mode 100644 new mode 100755 index 8f1eb13..df9ffd2 --- a/examples/templates/simple.md +++ b/examples/templates/simple.md @@ -1,12 +1,12 @@ -#!/bin/plated -{{ value }} - +#!/Users/chris/.local/bin/plated-exe This is normal Text - -{{[[ $this | tr 'a-z' 'A-Z' ]] +{{[[ tr 'a-z' 'A-Z' ]] Testing and stuff }} - more text +{{[[ cat ./lorem.md - ]] +the inner text +}} -{{ value }}{{ value }} +My env var: {{[[echo $test]]}} +blah diff --git a/src/Plated/Command.hs b/src/Plated/Command.hs index 39c3ff9..65b2706 100644 --- a/src/Plated/Command.hs +++ b/src/Plated/Command.hs @@ -3,15 +3,16 @@ module Plated.Command , interpCommand ) where -import System.Process +import System.Process (readCreateProcessWithExitCode, shell) import qualified Data.Text as T data Command = Command T.Text deriving Show -interpCommand :: Command -> IO T.Text -interpCommand (Command cmd) = do - (_, out, _) <- readCreateProcessWithExitCode (shell $ T.unpack cmd) "" +interpCommand :: T.Text -> Command -> IO T.Text +interpCommand inp (Command cmd) = do + (_, out, _) <- readCreateProcessWithExitCode (shell $ T.unpack cmd) (T.unpack inp) return . T.pack $ out + diff --git a/src/Plated/Parser.hs b/src/Plated/Parser.hs index 1ec3e11..a6a8ac1 100644 --- a/src/Plated/Parser.hs +++ b/src/Plated/Parser.hs @@ -28,8 +28,8 @@ templateFromFile fname = do templateParser :: Parser (Template Directive) templateParser = do - tmp <- template optional shebang + tmp <- template eof return tmp @@ -44,16 +44,19 @@ shebang = "she-bang" ?> liftA2 (++) (string "#!") (manyTill anyChar (char '\n')) directive :: Parser Directive directive = "Directive" ?> do + spaces _ <- string "{{" spaces - mCommand <- optionMaybe cmd + mCommand <- optionMaybe command txt <- manyTill anyChar (string "}}") + optional newline return $ Directive mCommand (T.pack txt) -cmd :: Parser Command -cmd = do +command :: Parser Command +command = do _ <- string "[[" cmdString <- manyTill anyChar (string "]]") + spaces return $ Command (T.pack cmdString) interpolate :: Template Directive -> T.Text diff --git a/src/Plated/Template.hs b/src/Plated/Template.hs index bd1482c..7c47311 100644 --- a/src/Plated/Template.hs +++ b/src/Plated/Template.hs @@ -1,3 +1,4 @@ +{-# language OverloadedStrings #-} module Plated.Template ( Template(..) , Directive(..) @@ -5,6 +6,7 @@ module Plated.Template ) where import Plated.Command +import Data.Foldable import qualified Data.Text as T @@ -16,7 +18,7 @@ data Directive = Directive (Maybe Command) T.Text deriving Show -processTemplate :: Template Directive -> T.Text -processTemplate (Template elems) = foldMap (either id fromDirective) elems +processTemplate :: Template Directive -> IO T.Text +processTemplate (Template elems) = fold <$> mapM (either return fromDirective) elems where - fromDirective (Directive _ txt) = txt + fromDirective (Directive mCmd txt) = maybe (return "") (interpCommand txt) mCmd