hpaco (empty) → 0.11.0.6
raw patch · 4 files changed
+140/−0 lines, 4 filesdep +basedep +cmdargsdep +filepathsetup-changed
Dependencies added: base, cmdargs, filepath, hpaco-lib, strict
Files
- LICENSE +30/−0
- Main.hs +82/−0
- Setup.hs +2/−0
- hpaco.cabal +26/−0
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2012, Tobias Dammers++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of Tobias Dammers nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Main.hs view
@@ -0,0 +1,82 @@+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+ }
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ hpaco.cabal view
@@ -0,0 +1,26 @@+name: hpaco+version: 0.11.0.6+synopsis: Modular template compiler+description: CLI front-end to the hpaco-lib library. Compiles Paco+ template source code to JavaScript or PHP, or interprets+ it directly.+homepage: https://bitbucket.org/tdammers/hpaco+license: BSD3+license-file: LICENSE+author: Tobias Dammers+maintainer: tdammers@gmail.com+-- copyright: +category: Development+build-type: Simple+cabal-version: >=1.8++executable hpaco+ main-is: Main.hs+ -- other-modules: + build-depends: base == 4.*+ , filepath >= 1.1 && < 1.4+ , cmdargs >= 0.9 && < 0.10+ , hpaco-lib == 0.11.0.6+ , strict == 0.3.*+ -- , split >= 0.1 && < 0.2+ -- , safe >= 0.3.3 && < 0.4