hpaco / Main.hs

module Main where

import CommandArgs
import Control.Monad (liftM)
import Data.List
import Data.Maybe
import Prelude hiding (getContents)
import System.FilePath
import System.IO (withFile, IOMode (ReadMode, WriteMode), hPutStr)
import System.IO.Strict
import Text.HPaco.Readers.Paco
import qualified Text.HPaco.Writers.PHP as PHP

main = do
    argStruct <- parseArgs
    let filenames = caInputFiles argStruct

    if null filenames
        then processStdin argStruct
        else mapM_ (processFile argStruct) filenames

processStdin :: CommandArgs -> IO ()
processStdin cmdargs =
    getContents >>= process cmdargs "" "<STDIN>" >>= putStrLn

processFile :: CommandArgs -> FilePath -> IO ()
processFile cmdargs srcFilename = do
    src <- withFile srcFilename ReadMode hGetContents
    let dstFilename = replaceExtension srcFilename "php"
    dst <- process cmdargs (takeBaseName srcFilename) srcFilename src
    withFile dstFilename WriteMode $ \f -> hPutStr f dst

process :: CommandArgs -> String -> FilePath -> String -> IO String
process cmdargs templateName filename src = do
    let reader = readPaco filename
        phpOptions =
            PHP.defaultWriterOptions { PHP.woPrettyPrint = caPhpPretty cmdargs
                                     , PHP.woTemplateName = templateName `fromMaybe` (caTemplateName cmdargs)
                                     , PHP.woIncludePreamble = not . caPhpNoPreamble $ cmdargs
                                     , PHP.woWrapMode = caPhpWrapMode cmdargs
                                     , PHP.woExposeAllFunctions = caPhpExposeAllFunctions cmdargs
        write = PHP.writePHP phpOptions
    ast <- reader src
    return $ write ast
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.