Source

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 Text.HPaco.Optimizer
import qualified Text.HPaco.Writers.PHP as PHP
import qualified Text.HPaco.Writers.Run as Run
import qualified Text.HPaco.Writers.Javascript as JS
import Text.HPaco.Writer (Writer)

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>" >>= putStr

processFile :: CommandArgs -> FilePath -> IO ()
processFile cmdargs srcFilename = do
    src <- withFile srcFilename ReadMode hGetContents
    let dstFilename = replaceExtension srcFilename (outputFormatExtension . caOutputFormat $ cmdargs)
    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
        write = getWriter cmdargs templateName
        rundata = caRunData cmdargs
        pre = "{%with " ++ rundata ++ " %}\n"
        post = "{%endwith%}\n"
        src' = if null rundata
                    then src
                    else pre ++ src ++ post
        opt = case caOptimizationLevel cmdargs of
                1 -> optimize
                otherwise -> id
    ast <- reader src'
    case caOutputFormat cmdargs of
        RunInterpreted -> runInterpreted (opt ast) cmdargs templateName >> return "\n"
        otherwise -> return . write . opt $ ast
    where runInterpreted ast cmdargs templateName =
            let opts = Run.defaultOptions
                        { Run.roTemplateName = templateName `fromMaybe` (caTemplateName cmdargs)
                        }
            in Run.run opts ast

outputFormatExtension :: OutputFormat -> String
outputFormatExtension OutputPHP = "php"
outputFormatExtension OutputJavascript = "js"

getWriter :: CommandArgs -> String -> Writer
getWriter cmdargs templateName =
    go (caOutputFormat cmdargs) cmdargs templateName
    where
        go :: OutputFormat -> CommandArgs -> String -> Writer
        go OutputJavascript cmdargs templateName =
            JS.writeJavascript $ JS.defaultWriterOptions
                                    { JS.woPrettyPrint = caJsPretty cmdargs
                                    , JS.woTemplateName = templateName `fromMaybe` (caTemplateName cmdargs)
                                    , JS.woWrapMode = caJsWrapMode cmdargs
                                    }
        go OutputPHP cmdargs templateName =
            PHP.writePHP $ 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
                                }