import System.Environment
import System.Directory
import Data.List
main :: IO()
main = do
arg <- getArgs --command line to list
case arg of
[target] -> reader target –list of arguments must have length of 1
_ -> putStrLn (“Error: Enter 1 filename argument (./tangler sample.org)”)
reader :: FilePath -> IO()
reader thefile = do
content <- readFile thefile --store the file content
let c = (lines content) --sends input file lines to a list of strings
filer (c) --makes empty source files to be appended to
parse (c) [] --appends code blocks to generated source files
filer :: [String] -> IO()
filer [] = putStrLn (“Files created…”)
filer (x:xs) = if isPrefixOf “#+BEGIN_SRC” x
then do
if (isInfixOf “:tangle ” x) && (isSuffixOf “:tangle ” x) == False –needs space to have filename parameter
then do
paramer (words x)
filer xs
else do
filer xs
else do
filer xs –continue
paramer :: [String] -> IO()
paramer (w:ws) | w == “:tangle” = if isInfixOf “:” (head ws) –guarenteed to find, return next word (parameter)
then do
putStrLn (“Paramater contains illegal character.”)
else do
writeFile (head ws) “” –empty file with name w
| otherwise = paramer ws
parse :: [String] -> [Char] -> IO()
parse [] _ = putStrLn (“Files written!”)
parse (x:xs) [] = if isPrefixOf “#+BEGIN_SRC” x && (isSuffixOf “:tangle ” x) == False
then do
if isInfixOf “:tangle ” x
then do
parse xs (appender (words x)) –tangle found
else do
parse xs [] –no tangle
else do
parse xs [] –continue
parse (x:xs) m = if isPrefixOf “#+END_SRC” x
then do
parse xs [] –stop appending
else do
appendFile m (x ++ “\n”) –append line
parse xs m –continue
appender :: [String] -> [Char]
appender (w:ws) | w == “:tangle” = case isInfixOf “:” (head ws) of
True -> []
False -> (head ws)
| otherwise = appender ws