Run template through shell
This commit is contained in:
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
|
#!/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
|
||||||
|
@ -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
|
||||||
|
|
||||||
|
|
||||||
|
@ -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
|
||||||
|
@ -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
|
||||||
|
Reference in New Issue
Block a user