Run template through shell
This commit is contained in:
parent
258d9cb027
commit
6caf392550
4
examples/templates/lorem.md
Normal file
4
examples/templates/lorem.md
Normal 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
14
examples/templates/simple.md
Normal file → Executable file
@ -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
|
||||
|
@ -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
|
||||
|
||||
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user