module Filter (Filt,DocSubs,docSubs,filterLine,process,doGeneric) where import Split (Split(..)) import Regex (mkRegex,matchRegex,Sub,mkSub,doSub) import Run (runCommand) type Filt = String -> String type DocSubs = [(String,String,String)] docSubs :: DocSubs -> [Sub] docSubs subs = map (mkSub.pad) $ [ ("here", "`([^` \t]+[.][^` \t]+)`", "[`\\2`](\\2 \"\\2\")") , ("here", "`([^` \t]+)`", "[`\\2`](\\2.html \"\\2\")") , ("def", "`([^` \t]+)`, `([^` \t]+)`, `([^` \t]+)`, `([^` \t]+)`", "---\n### `\\2`, `\\3`, `\\4`, `\\5`\n`\\2`, `\\3`, `\\4`, `\\5`") , ("def", "`([^` \t]+)`, `([^` \t]+)`, `([^` \t]+)`", "---\n### `\\2`, `\\3`, `\\4`\n`\\2`, `\\3`, `\\4`") , ("def", "`([^` \t]+)`, `([^` \t]+)`", "---\n### `\\2`, `\\3`\n`\\2`, `\\3`") , ("def", "`([^` \t]+)`", "---\n### `\\2`\n`\\2`") ] ++ subs where pad (r,s,t) = ("^([0-9.*]*[ ]?)@" ++ r ++ "[ \t]+" ++ s, "\\1" ++ t) codeSubsPre, codeSubsPost :: [Sub] codeSubsPre = map mkSub [ ("&","&")] codeSubsPost = map mkSub [ ("<","<") , (">",">") , ("\"",""") ] filterLine :: [Sub] -> String -> String filterLine subs line = foldl doSub line subs preSubs, postSubs :: Filt preSubs = filterLine codeSubsPre postSubs = filterLine codeSubsPost shell :: String -> Maybe (Split) shell s = let pre = "\n
\n"
        end = "
\n" in case matchRegex (mkRegex "^@shell (.*)$") s of Just [cmd] -> Just $ Shell $ do (out,_) <- runCommand cmd [] True return $ pre ++ out ++ end _ -> Nothing process :: Filt -> Filt -> [Split] -> [Split] process codeFilter docFilter xs = f xs where f [] = [] f (x:xt) = case x of Code s -> (Code $ (postSubs . codeFilter . preSubs) s) : f xt Doc s -> case shell s of Just y -> y : f xt Nothing -> (Doc $ docFilter s) : f xt _ -> x : f xt doGeneric :: [Split] -> [Split] doGeneric = process codeFilter docFilter where codeFilter = id docFilter = filterLine $ docSubs []