1. Tobias Dammers
  2. hpaco

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 qualified Text.HPaco.Writers.PHP as PHP
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>" >>= putStrLn

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
    ast <- reader src
    return $ write 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
                                }