Run template through shell

This commit is contained in:
Chris Penner
2017-04-20 18:28:16 -06:00
parent 258d9cb027
commit 6caf392550
5 changed files with 28 additions and 18 deletions

View File

@ -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.

14
examples/templates/simple.md Normal file → Executable file
View File

@ -1,12 +1,12 @@
#!/bin/plated #!/Users/chris/.local/bin/plated-exe
{{ value }}
This is normal Text This is normal Text
{{[[ tr 'a-z' 'A-Z' ]]
{{[[ $this | tr 'a-z' 'A-Z' ]]
Testing and stuff Testing and stuff
}} }}
more text more text
{{[[ cat ./lorem.md - ]]
the inner text
}}
{{ value }}{{ value }} My env var: {{[[echo $test]]}}
blah

View File

@ -3,15 +3,16 @@ module Plated.Command
, interpCommand , interpCommand
) where ) where
import System.Process import System.Process (readCreateProcessWithExitCode, shell)
import qualified Data.Text as T import qualified Data.Text as T
data Command = data Command =
Command T.Text Command T.Text
deriving Show deriving Show
interpCommand :: Command -> IO T.Text interpCommand :: T.Text -> Command -> IO T.Text
interpCommand (Command cmd) = do interpCommand inp (Command cmd) = do
(_, out, _) <- readCreateProcessWithExitCode (shell $ T.unpack cmd) "" (_, out, _) <- readCreateProcessWithExitCode (shell $ T.unpack cmd) (T.unpack inp)
return . T.pack $ out return . T.pack $ out

View File

@ -28,8 +28,8 @@ templateFromFile fname = do
templateParser :: Parser (Template Directive) templateParser :: Parser (Template Directive)
templateParser = do templateParser = do
tmp <- template
optional shebang optional shebang
tmp <- template
eof eof
return tmp return tmp
@ -44,16 +44,19 @@ shebang = "she-bang" ?> liftA2 (++) (string "#!") (manyTill anyChar (char '\n'))
directive :: Parser Directive directive :: Parser Directive
directive = "Directive" ?> do directive = "Directive" ?> do
spaces
_ <- string "{{" _ <- string "{{"
spaces spaces
mCommand <- optionMaybe cmd mCommand <- optionMaybe command
txt <- manyTill anyChar (string "}}") txt <- manyTill anyChar (string "}}")
optional newline
return $ Directive mCommand (T.pack txt) return $ Directive mCommand (T.pack txt)
cmd :: Parser Command command :: Parser Command
cmd = do command = do
_ <- string "[[" _ <- string "[["
cmdString <- manyTill anyChar (string "]]") cmdString <- manyTill anyChar (string "]]")
spaces
return $ Command (T.pack cmdString) return $ Command (T.pack cmdString)
interpolate :: Template Directive -> T.Text interpolate :: Template Directive -> T.Text

View File

@ -1,3 +1,4 @@
{-# language OverloadedStrings #-}
module Plated.Template module Plated.Template
( Template(..) ( Template(..)
, Directive(..) , Directive(..)
@ -5,6 +6,7 @@ module Plated.Template
) where ) where
import Plated.Command import Plated.Command
import Data.Foldable
import qualified Data.Text as T import qualified Data.Text as T
@ -16,7 +18,7 @@ data Directive =
Directive (Maybe Command) T.Text Directive (Maybe Command) T.Text
deriving Show deriving Show
processTemplate :: Template Directive -> T.Text processTemplate :: Template Directive -> IO T.Text
processTemplate (Template elems) = foldMap (either id fromDirective) elems processTemplate (Template elems) = fold <$> mapM (either return fromDirective) elems
where where
fromDirective (Directive _ txt) = txt fromDirective (Directive mCmd txt) = maybe (return "") (interpCommand txt) mCmd