git.iko.soy/typeable/pandoc
on master help sign in
Improved template handling:

+ Split template haskell functions into new module, Text.Pandoc.TH + Distinguish contentsOf and binaryContentsOf; the former uses text mode in Windows, while the latter uses binary mode git-svn-id: https://pandoc.googlecode.com/svn/trunk@1368 788f1e2b-df1e-0410-8736-df70ead52e1b

commit1e2f2bc4f63a0cacfc7eda26a6cc0887eb1c8de2 authorfiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b> dateSat 2 Aug 2008, 17:22 (18 years ago) parent6a0004a
7 files changed+63−20
Text/Pandoc/ASCIIMathML.hs+1−1 Text/Pandoc/DefaultHeaders.hs+1−1 Text/Pandoc/ODT.hs+2−2 Text/Pandoc/Shared.hs+5−15 Text/Pandoc/TH.hs+51 Text/Pandoc/Writers/S5.hs+2−1 pandoc.cabal+1
Text/Pandoc/ASCIIMathML.hs+1−1
@@ -2,7 +2,7 @@
22-- | Definitions for use of ASCIIMathML in HTML.
33-- (See <http://www1.chapman.edu/~jipsen/mathml/asciimath.html>.)
44module Text.Pandoc.ASCIIMathML ( asciiMathMLScript ) where
5−import Text.Pandoc.Shared ( contentsOf )
5+import Text.Pandoc.TH ( contentsOf )
66import System.FilePath ( (</>) )
77
88-- | String containing ASCIIMathML javascript.
Text/Pandoc/DefaultHeaders.hs+1−1
@@ -38,7 +38,7 @@ module Text.Pandoc.DefaultHeaders (
3838 ) where
3939import Text.Pandoc.Writers.S5
4040import System.FilePath ( (</>) )
41−import Text.Pandoc.Shared ( contentsOf )
41+import Text.Pandoc.TH ( contentsOf )
4242
4343defaultLaTeXHeader :: String
4444#ifndef __HADDOCK__
Text/Pandoc/ODT.hs+2−2
@@ -29,7 +29,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2929Functions for producing an ODT file from OpenDocument XML.
3030-}
3131module Text.Pandoc.ODT ( saveOpenDocumentAsODT ) where
32−import Text.Pandoc.Shared ( contentsOf )
32+import Text.Pandoc.TH ( binaryContentsOf )
3333import Data.Maybe ( fromJust, isJust )
3434import Data.List ( partition, intersperse )
3535import Prelude hiding ( writeFile, readFile )
@@ -61,7 +61,7 @@ saveOpenDocumentAsODT destinationODTPath sourceDirRelative xml = do
6161 "Windows: See http://gnuwin32.sourceforge.net/packages/zip.htm"
6262 withTempDir "pandoc-odt" $ \tempDir -> do
6363 let tempODT = tempDir </> "reference.odt"
64− B.writeFile tempODT $ B.pack $(contentsOf $ "odt-styles" </> "reference.odt")
64+ B.writeFile tempODT $ B.pack $(binaryContentsOf $ "odt-styles" </> "reference.odt")
6565 createDirectory $ tempDir </> "Pictures"
6666 xml' <- handlePictures tempODT sourceDirRelative xml
6767 writeFile (tempDir </> "content.xml") xml'
Text/Pandoc/Shared.hs+5−15
@@ -1,4 +1,3 @@
1−{-# OPTIONS_GHC -fno-warn-orphans #-}
21{-
32Copyright (C) 2006-8 John MacFarlane <jgm@berkeley.edu>
43
@@ -101,9 +100,7 @@ module Text.Pandoc.Shared (
101100 WriterOptions (..),
102101 defaultWriterOptions,
103102 -- * File handling
104− withTempDir,
105− -- * Template haskell
106− contentsOf
103+ withTempDir
107104 ) where
108105
109106import Text.Pandoc.Definition
@@ -118,9 +115,6 @@ import Network.URI ( parseURI, URI (..), isAllowedInURI )
118115import System.FilePath ( (</>), (<.>) )
119116import System.IO.Error ( catch, ioError, isAlreadyExistsError )
120117import System.Directory
121−import Language.Haskell.TH
122−import Language.Haskell.TH.Syntax (Lift (..))
123−import qualified Data.ByteString.Char8 as B
124118
125119--
126120-- List processing
@@ -912,6 +906,10 @@ defaultWriterOptions =
912906 , writerWrapText = True
913907 }
914908
909+--
910+-- File handling
911+--
912+
915913-- | Perform a function in a temporary directory and clean up.
916914withTempDir :: FilePath -> (FilePath -> IO a) -> IO a
917915withTempDir baseName func = do
@@ -929,11 +927,3 @@ createTempDir num baseName = do
929927 \e -> if isAlreadyExistsError e
930928 then createTempDir (num + 1) baseName
931929 else ioError e
932−
933−-- | Template haskell function to insert bytestring contents of file into a template.
934−contentsOf :: FilePath -> ExpQ
935−contentsOf p = lift =<< runIO (B.readFile p)
936−
937−instance Lift B.ByteString where
938− lift x = return (LitE (StringL $ B.unpack x))
939−
Text/Pandoc/TH.hsnew file+51
@@ -0,0 +1,51 @@
1+{-# OPTIONS_GHC -fno-warn-orphans #-}
2+{-
3+Copyright (C) 2008 John MacFarlane <jgm@berkeley.edu>
4+
5+This program is free software; you can redistribute it and/or modify
6+it under the terms of the GNU General Public License as published by
7+the Free Software Foundation; either version 2 of the License, or
8+(at your option) any later version.
9+
10+This program is distributed in the hope that it will be useful,
11+but WITHOUT ANY WARRANTY; without even the implied warranty of
12+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+GNU General Public License for more details.
14+
15+You should have received a copy of the GNU General Public License
16+along with this program; if not, write to the Free Software
17+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18+-}
19+
20+{- |
21+ Module : Text.Pandoc.TH
22+ Copyright : Copyright (C) 2006-8 John MacFarlane
23+ License : GNU GPL, version 2 or above
24+
25+ Maintainer : John MacFarlane <jgm@berkeley.edu>
26+ Stability : alpha
27+ Portability : portable
28+
29+Template haskell functions used by Pandoc modules.
30+-}
31+module Text.Pandoc.TH (
32+ contentsOf,
33+ binaryContentsOf
34+ ) where
35+
36+import Language.Haskell.TH
37+import Language.Haskell.TH.Syntax (Lift (..))
38+import qualified Data.ByteString as B
39+import Data.ByteString.Internal ( w2c )
40+
41+-- | Insert contents of text file into a template.
42+contentsOf :: FilePath -> ExpQ
43+contentsOf p = lift =<< (runIO $ readFile p)
44+
45+-- | Insert contents of binary file into a template.
46+-- Note that @Data.ByteString.readFile@ uses binary mode on windows.
47+binaryContentsOf :: FilePath -> ExpQ
48+binaryContentsOf p = lift =<< (runIO $ B.readFile p)
49+
50+instance Lift B.ByteString where
51+ lift x = return (LitE (StringL $ map w2c $ B.unpack x))
Text/Pandoc/Writers/S5.hs+2−1
@@ -40,7 +40,8 @@ module Text.Pandoc.Writers.S5 (
4040 writeS5String,
4141 insertS5Structure
4242 ) where
43−import Text.Pandoc.Shared ( joinWithSep, WriterOptions, contentsOf )
43+import Text.Pandoc.Shared ( joinWithSep, WriterOptions )
44+import Text.Pandoc.TH ( contentsOf )
4445import Text.Pandoc.Writers.HTML ( writeHtml, writeHtmlString )
4546import Text.Pandoc.Definition
4647import Text.XHtml.Strict
pandoc.cabal+1
@@ -103,6 +103,7 @@ Library
103103 Text.Pandoc.Writers.RTF,
104104 Text.Pandoc.Writers.S5
105105 Other-Modules: Text.Pandoc.XML,
106+ Text.Pandoc.TH,
106107 Text.XML.Light,
107108 Text.XML.Light.Types,
108109 Text.XML.Light.Output,