multifile 0.1.0.3 → 0.1.0.4
raw patch · 2 files changed
+44/−17 lines, 2 filesdep +optparse-applicativedep +processdep ~directory
Dependencies added: optparse-applicative, process
Dependency ranges changed: directory
Files
- multifile.cabal +2/−2
- src/Main.hs +42/−15
multifile.cabal view
@@ -10,7 +10,7 @@ -- PVP summary: +-+------- breaking API changes -- | | +----- non-breaking API additions -- | | | +--- code changes with no API change-version: 0.1.0.3+version: 0.1.0.4 -- A short (one-line) description of the package. synopsis: create many files from one@@ -60,7 +60,7 @@ -- other-extensions: -- Other library packages from which modules are imported.- build-depends: base >=4.9 && <4.10 , HaXml < 10000 , directory >= 1.2.7 , pretty <10000,transformers <10000+ build-depends: base >=4.9 && <4.10 , HaXml < 10000 , directory <10000 , pretty <10000,transformers <10000 , process<10000,optparse-applicative <10000 -- Directories containing source files. hs-source-dirs: src
src/Main.hs view
@@ -1,10 +1,15 @@+{-# Language NoMonomorphismRestriction #-} module Main where +import System.Process+import System.IO+import System.Directory+ import System.Environment import System.IO-import Text.XML.HaXml+import Text.XML.HaXml hiding (info,Parser) import Text.XML.HaXml.Escape-import Text.XML.HaXml.XmlContent.Haskell+import Text.XML.HaXml.XmlContent.Haskell hiding (Parser) import Extsubset import Data.Either@@ -19,6 +24,8 @@ import Control.Monad.Trans.Class import Data.Char +import Options.Applicative+ isRegularPath :: FilePath -> Bool isRegularPath f = f /= "." && f /= ".." @@ -50,9 +57,10 @@ s = xmlEscapeContent stdXmlEscaper create xs = create' xs >>= \x -> case x of - (Left x) -> putStrLn ("unknown file "++x)+ (Left x) -> putStrLn ("unknown file "++x++"\n") (Right x) -> putStr x + cdatafy x = "<![CDATA[" ++ x ++ "]]>" @@ -64,9 +72,9 @@ i < 10 || (10<i && i<32) || i >= 127 || case ch of '\'' -> True- '\"' -> True- '&' -> True- '<' -> True+ '\"' -> True + '&' -> True + '<' -> True '>' -> True _ -> False )@@ -76,25 +84,44 @@ myFun (CElem e i) = CElem (myFun' e) i myFun x = x -myFun' (Elem a b cs) = Elem a b (map myFun cs)+myFun' (Elem a b cs) = Elem a b (map myFun cs) -dir x = getDirectoryContents x >>= create -main :: IO ()+run x = print 2+ main = do args <- getArgs case args of [] -> do- x <- getContents - let p = (readXml x :: Either String Multifile)- either print processFiles p+ x <- getContents + extractMultiFile x x -> z x where - z ("-create":xs) = create xs+ z ("--create":xs) = create xs z ("-c":xs) = create xs+ z ("--edit":xs) = edit xs+ z ("-e":xs) = edit xs z _ = putStr "unknown usage" -processFiles (Multifile xs) = mapM_ processFile xs-processFile (File (File_Attrs path) content) = writeFile path content+edit xs = do+ dir <- getTemporaryDirectory+ (filename,handle) <- openTempFile dir "a"+ hClose handle+ eitherMultifile <- create' xs+ case eitherMultifile of + (Right multifile) -> do+ writeFile filename multifile+ maybeEditor <- lookupEnv "EDITOR"+ let editor = fromMaybe "vim" maybeEditor+ system (editor++" "++filename)+ x <- readFile filename+ extractMultiFile x+ (Left errorMsg) -> putStrLn ("file: "++errorMsg++" does not exist.")+ return () +extractMultiFile x = do + let p = (readXml x :: Either String Multifile)+ either print processFiles p +processFiles (Multifile xs) = mapM_ processFile xs+processFile (File (File_Attrs path) content) = writeFile path content