Options.WriterOptions: Change type of writerVariables to Context Text.
This will allow structured values. [API change]
This commit is contained in:
parent
1b10b5cea9
commit
aceee9ca48
13 changed files with 136 additions and 51 deletions
|
@ -596,6 +596,7 @@ library
|
||||||
Text.Pandoc.Lua.Marshaling.CommonState,
|
Text.Pandoc.Lua.Marshaling.CommonState,
|
||||||
Text.Pandoc.Lua.Marshaling.MediaBag,
|
Text.Pandoc.Lua.Marshaling.MediaBag,
|
||||||
Text.Pandoc.Lua.Marshaling.ReaderOptions,
|
Text.Pandoc.Lua.Marshaling.ReaderOptions,
|
||||||
|
Text.Pandoc.Lua.Marshaling.Context,
|
||||||
Text.Pandoc.Lua.Marshaling.Version,
|
Text.Pandoc.Lua.Marshaling.Version,
|
||||||
Text.Pandoc.Lua.Module.MediaBag,
|
Text.Pandoc.Lua.Module.MediaBag,
|
||||||
Text.Pandoc.Lua.Module.Pandoc,
|
Text.Pandoc.Lua.Module.Pandoc,
|
||||||
|
@ -724,6 +725,7 @@ test-suite test-pandoc
|
||||||
executable-path >= 0.0 && < 0.1,
|
executable-path >= 0.0 && < 0.1,
|
||||||
zip-archive >= 0.2.3.4 && < 0.5,
|
zip-archive >= 0.2.3.4 && < 0.5,
|
||||||
xml >= 1.3.12 && < 1.4,
|
xml >= 1.3.12 && < 1.4,
|
||||||
|
doctemplates >= 0.6.1 && < 0.7,
|
||||||
Glob >= 0.7 && < 0.11
|
Glob >= 0.7 && < 0.11
|
||||||
if impl(ghc < 8.4)
|
if impl(ghc < 8.4)
|
||||||
hs-source-dirs: prelude
|
hs-source-dirs: prelude
|
||||||
|
|
|
@ -20,6 +20,9 @@ module Text.Pandoc.App.OutputSettings
|
||||||
) where
|
) where
|
||||||
import Prelude
|
import Prelude
|
||||||
import qualified Control.Exception as E
|
import qualified Control.Exception as E
|
||||||
|
import qualified Data.Text as T
|
||||||
|
import qualified Data.Map as M
|
||||||
|
import Text.DocTemplates (Context(..), ToContext(toVal))
|
||||||
import Control.Monad
|
import Control.Monad
|
||||||
import Control.Monad.Except (catchError, throwError)
|
import Control.Monad.Except (catchError, throwError)
|
||||||
import Control.Monad.Trans
|
import Control.Monad.Trans
|
||||||
|
@ -159,6 +162,8 @@ optToOutputSettings opts = do
|
||||||
$ lines dztempl
|
$ lines dztempl
|
||||||
return $ ("dzslides-core", dzcore) : vars
|
return $ ("dzslides-core", dzcore) : vars
|
||||||
else return vars)
|
else return vars)
|
||||||
|
>>= fmap (Context . M.fromList) .
|
||||||
|
traverse (\(x,y) -> return (T.pack x, toVal (T.pack y)))
|
||||||
|
|
||||||
templStr <- case optTemplate opts of
|
templStr <- case optTemplate opts of
|
||||||
_ | not standalone -> return Nothing
|
_ | not standalone -> return Nothing
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
{-# LANGUAGE NoImplicitPrelude #-}
|
{-# LANGUAGE NoImplicitPrelude #-}
|
||||||
{- |
|
{- |
|
||||||
Module : Text.Pandoc.BCP47
|
Module : Text.Pandoc.BCP47
|
||||||
|
@ -24,6 +25,8 @@ import Data.Char (isAlphaNum, isAscii, isLetter, isLower, isUpper, toLower,
|
||||||
import Data.List (intercalate)
|
import Data.List (intercalate)
|
||||||
import Text.Pandoc.Definition
|
import Text.Pandoc.Definition
|
||||||
import Text.Pandoc.Options
|
import Text.Pandoc.Options
|
||||||
|
import Text.DocTemplates (FromContext(..))
|
||||||
|
import qualified Data.Text as T
|
||||||
import qualified Text.Parsec as P
|
import qualified Text.Parsec as P
|
||||||
|
|
||||||
-- | Represents BCP 47 language/country code.
|
-- | Represents BCP 47 language/country code.
|
||||||
|
@ -41,8 +44,8 @@ renderLang lang = intercalate "-" (langLanguage lang : filter (not . null)
|
||||||
-- | Get the contents of the `lang` metadata field or variable.
|
-- | Get the contents of the `lang` metadata field or variable.
|
||||||
getLang :: WriterOptions -> Meta -> Maybe String
|
getLang :: WriterOptions -> Meta -> Maybe String
|
||||||
getLang opts meta =
|
getLang opts meta =
|
||||||
case lookup "lang" (writerVariables opts) of
|
case lookupContext "lang" (writerVariables opts) of
|
||||||
Just s -> Just s
|
Just s -> Just $ T.unpack s
|
||||||
_ ->
|
_ ->
|
||||||
case lookupMeta "lang" meta of
|
case lookupMeta "lang" meta of
|
||||||
Just (MetaInlines [Str s]) -> Just s
|
Just (MetaInlines [Str s]) -> Just s
|
||||||
|
|
|
@ -14,4 +14,5 @@ module Text.Pandoc.Lua.Marshaling () where
|
||||||
import Text.Pandoc.Lua.Marshaling.AST ()
|
import Text.Pandoc.Lua.Marshaling.AST ()
|
||||||
import Text.Pandoc.Lua.Marshaling.CommonState ()
|
import Text.Pandoc.Lua.Marshaling.CommonState ()
|
||||||
import Text.Pandoc.Lua.Marshaling.ReaderOptions ()
|
import Text.Pandoc.Lua.Marshaling.ReaderOptions ()
|
||||||
|
import Text.Pandoc.Lua.Marshaling.Context ()
|
||||||
import Text.Pandoc.Lua.Marshaling.Version ()
|
import Text.Pandoc.Lua.Marshaling.Version ()
|
||||||
|
|
31
src/Text/Pandoc/Lua/Marshaling/Context.hs
Normal file
31
src/Text/Pandoc/Lua/Marshaling/Context.hs
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
{-# LANGUAGE NoImplicitPrelude #-}
|
||||||
|
{-# LANGUAGE FlexibleInstances #-}
|
||||||
|
{-# LANGUAGE ScopedTypeVariables #-}
|
||||||
|
{-# LANGUAGE LambdaCase #-}
|
||||||
|
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
||||||
|
{- |
|
||||||
|
Module : Text.Pandoc.Lua.Marshaling.Context
|
||||||
|
Copyright : © 2012-2019 John MacFarlane
|
||||||
|
© 2017-2019 Albert Krewinkel
|
||||||
|
License : GNU GPL, version 2 or above
|
||||||
|
|
||||||
|
Maintainer : Albert Krewinkel <tarleb+pandoc@moltkeplatz.de>
|
||||||
|
Stability : alpha
|
||||||
|
|
||||||
|
Marshaling instance for doctemplates Context and its components.
|
||||||
|
-}
|
||||||
|
module Text.Pandoc.Lua.Marshaling.Context () where
|
||||||
|
|
||||||
|
import qualified Foreign.Lua as Lua
|
||||||
|
import Foreign.Lua (Pushable)
|
||||||
|
import Text.DocTemplates (Context(..), Val(..))
|
||||||
|
|
||||||
|
instance Pushable a => Pushable (Context a) where
|
||||||
|
push (Context m) = Lua.push m
|
||||||
|
|
||||||
|
instance Pushable a => Pushable (Val a) where
|
||||||
|
push NullVal = Lua.push ()
|
||||||
|
push (MapVal ctx) = Lua.push ctx
|
||||||
|
push (ListVal xs) = Lua.push xs
|
||||||
|
push (SimpleVal x) = Lua.push x
|
||||||
|
|
|
@ -34,6 +34,8 @@ import Prelude
|
||||||
import Data.Char (toLower)
|
import Data.Char (toLower)
|
||||||
import Data.Data (Data)
|
import Data.Data (Data)
|
||||||
import Data.Default
|
import Data.Default
|
||||||
|
import Data.Text (Text)
|
||||||
|
import Text.DocTemplates (Context(..))
|
||||||
import qualified Data.Set as Set
|
import qualified Data.Set as Set
|
||||||
import Data.Typeable (Typeable)
|
import Data.Typeable (Typeable)
|
||||||
import GHC.Generics (Generic)
|
import GHC.Generics (Generic)
|
||||||
|
@ -148,7 +150,7 @@ data ReferenceLocation = EndOfBlock -- ^ End of block
|
||||||
-- | Options for writers
|
-- | Options for writers
|
||||||
data WriterOptions = WriterOptions
|
data WriterOptions = WriterOptions
|
||||||
{ writerTemplate :: Maybe Template -- ^ Template to use
|
{ writerTemplate :: Maybe Template -- ^ Template to use
|
||||||
, writerVariables :: [(String, String)] -- ^ Variables to set in template
|
, writerVariables :: Context Text -- ^ Variables to set in template
|
||||||
, writerTabStop :: Int -- ^ Tabstop for conversion btw spaces and tabs
|
, writerTabStop :: Int -- ^ Tabstop for conversion btw spaces and tabs
|
||||||
, writerTableOfContents :: Bool -- ^ Include table of contents
|
, writerTableOfContents :: Bool -- ^ Include table of contents
|
||||||
, writerIncremental :: Bool -- ^ True if lists should be incremental
|
, writerIncremental :: Bool -- ^ True if lists should be incremental
|
||||||
|
@ -185,7 +187,7 @@ data WriterOptions = WriterOptions
|
||||||
|
|
||||||
instance Default WriterOptions where
|
instance Default WriterOptions where
|
||||||
def = WriterOptions { writerTemplate = Nothing
|
def = WriterOptions { writerTemplate = Nothing
|
||||||
, writerVariables = []
|
, writerVariables = mempty
|
||||||
, writerTabStop = 4
|
, writerTabStop = 4
|
||||||
, writerTableOfContents = False
|
, writerTableOfContents = False
|
||||||
, writerIncremental = False
|
, writerIncremental = False
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
{-# LANGUAGE CPP #-}
|
{-# LANGUAGE CPP #-}
|
||||||
{-# LANGUAGE FlexibleContexts #-}
|
{-# LANGUAGE FlexibleContexts #-}
|
||||||
{-# LANGUAGE PatternGuards #-}
|
{-# LANGUAGE PatternGuards #-}
|
||||||
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
{-# LANGUAGE ScopedTypeVariables #-}
|
{-# LANGUAGE ScopedTypeVariables #-}
|
||||||
{- |
|
{- |
|
||||||
Module : Text.Pandoc.Writers.EPUB
|
Module : Text.Pandoc.Writers.EPUB
|
||||||
|
@ -58,6 +59,8 @@ import Text.XML.Light (Attr (..), Element (..), Node (..), QName (..),
|
||||||
add_attrs, lookupAttr, node, onlyElems, parseXML,
|
add_attrs, lookupAttr, node, onlyElems, parseXML,
|
||||||
ppElement, showElement, strContent, unode, unqual)
|
ppElement, showElement, strContent, unode, unqual)
|
||||||
import Text.Pandoc.XML (escapeStringForXML)
|
import Text.Pandoc.XML (escapeStringForXML)
|
||||||
|
import Text.DocTemplates (FromContext(lookupContext), Context(..),
|
||||||
|
ToContext(toVal), Val(..))
|
||||||
|
|
||||||
-- A Chapter includes a list of blocks.
|
-- A Chapter includes a list of blocks.
|
||||||
data Chapter = Chapter [Block]
|
data Chapter = Chapter [Block]
|
||||||
|
@ -136,6 +139,9 @@ removeNote :: Inline -> Inline
|
||||||
removeNote (Note _) = Str ""
|
removeNote (Note _) = Str ""
|
||||||
removeNote x = x
|
removeNote x = x
|
||||||
|
|
||||||
|
toVal' :: String -> Val TS.Text
|
||||||
|
toVal' = toVal . TS.pack
|
||||||
|
|
||||||
mkEntry :: PandocMonad m => FilePath -> B.ByteString -> E m Entry
|
mkEntry :: PandocMonad m => FilePath -> B.ByteString -> E m Entry
|
||||||
mkEntry path content = do
|
mkEntry path content = do
|
||||||
epubSubdir <- gets stEpubSubdir
|
epubSubdir <- gets stEpubSubdir
|
||||||
|
@ -163,8 +169,8 @@ getEPUBMetadata opts meta = do
|
||||||
else return m
|
else return m
|
||||||
let addLanguage m =
|
let addLanguage m =
|
||||||
if null (epubLanguage m)
|
if null (epubLanguage m)
|
||||||
then case lookup "lang" (writerVariables opts) of
|
then case lookupContext "lang" (writerVariables opts) of
|
||||||
Just x -> return m{ epubLanguage = x }
|
Just x -> return m{ epubLanguage = TS.unpack x }
|
||||||
Nothing -> do
|
Nothing -> do
|
||||||
mLang <- lift $ P.lookupEnv "LANG"
|
mLang <- lift $ P.lookupEnv "LANG"
|
||||||
let localeLang =
|
let localeLang =
|
||||||
|
@ -345,11 +351,14 @@ metadataFromMeta opts meta = EPUBMetadata{
|
||||||
relation = metaValueToString <$> lookupMeta "relation" meta
|
relation = metaValueToString <$> lookupMeta "relation" meta
|
||||||
coverage = metaValueToString <$> lookupMeta "coverage" meta
|
coverage = metaValueToString <$> lookupMeta "coverage" meta
|
||||||
rights = metaValueToString <$> lookupMeta "rights" meta
|
rights = metaValueToString <$> lookupMeta "rights" meta
|
||||||
coverImage = lookup "epub-cover-image" (writerVariables opts) `mplus`
|
coverImage =
|
||||||
(metaValueToString <$> lookupMeta "cover-image" meta)
|
(TS.unpack <$> lookupContext "epub-cover-image"
|
||||||
|
(writerVariables opts))
|
||||||
|
`mplus` (metaValueToString <$> lookupMeta "cover-image" meta)
|
||||||
mCss = lookupMeta "css" meta <|> lookupMeta "stylesheet" meta
|
mCss = lookupMeta "css" meta <|> lookupMeta "stylesheet" meta
|
||||||
stylesheets = fromMaybe [] (metaValueToPaths <$> mCss) ++
|
stylesheets = fromMaybe [] (metaValueToPaths <$> mCss) ++
|
||||||
[f | ("css",f) <- writerVariables opts]
|
maybe [] (\t -> [TS.unpack t])
|
||||||
|
(lookupContext "css" (writerVariables opts))
|
||||||
pageDirection = case map toLower . metaValueToString <$>
|
pageDirection = case map toLower . metaValueToString <$>
|
||||||
lookupMeta "page-progression-direction" meta of
|
lookupMeta "page-progression-direction" meta of
|
||||||
Just "ltr" -> Just LTR
|
Just "ltr" -> Just LTR
|
||||||
|
@ -424,10 +433,13 @@ pandocToEPUB version opts doc = do
|
||||||
(\bs n -> mkEntry ("styles/stylesheet" ++ show n ++ ".css") bs)
|
(\bs n -> mkEntry ("styles/stylesheet" ++ show n ++ ".css") bs)
|
||||||
stylesheets [(1 :: Int)..]
|
stylesheets [(1 :: Int)..]
|
||||||
|
|
||||||
let vars = ("epub3", if epub3 then "true" else "false")
|
let vars = Context $
|
||||||
: [(x,y) | (x,y) <- writerVariables opts, x /= "css"]
|
M.delete "css" . M.insert "epub3"
|
||||||
|
(toVal' $ if epub3 then "true" else "false") $
|
||||||
|
unContext $ writerVariables opts
|
||||||
|
|
||||||
let cssvars useprefix = map (\e -> ("css",
|
let cssvars useprefix = Context $ M.fromList $ map
|
||||||
|
(\e -> ("css", toVal' $
|
||||||
(if useprefix
|
(if useprefix
|
||||||
then "../"
|
then "../"
|
||||||
else "")
|
else "")
|
||||||
|
@ -457,14 +469,16 @@ pandocToEPUB version opts doc = do
|
||||||
(CouldNotDetermineImageSize img err')
|
(CouldNotDetermineImageSize img err')
|
||||||
cpContent <- lift $ writeHtml
|
cpContent <- lift $ writeHtml
|
||||||
opts'{ writerVariables =
|
opts'{ writerVariables =
|
||||||
("coverpage","true"):
|
Context (M.fromList [
|
||||||
("pagetitle",
|
("coverpage", toVal' "true"),
|
||||||
escapeStringForXML plainTitle):
|
("pagetitle", toVal' $
|
||||||
("cover-image", coverImage):
|
escapeStringForXML plainTitle),
|
||||||
("cover-image-width", show coverImageWidth):
|
("cover-image", toVal' coverImage),
|
||||||
("cover-image-height",
|
("cover-image-width", toVal' $
|
||||||
show coverImageHeight):
|
show coverImageWidth),
|
||||||
cssvars True ++ vars }
|
("cover-image-height", toVal' $
|
||||||
|
show coverImageHeight)]) <>
|
||||||
|
cssvars True <> vars }
|
||||||
(Pandoc meta [])
|
(Pandoc meta [])
|
||||||
coverEntry <- mkEntry "text/cover.xhtml" cpContent
|
coverEntry <- mkEntry "text/cover.xhtml" cpContent
|
||||||
coverImageEntry <- mkEntry ("media/" ++ coverImage)
|
coverImageEntry <- mkEntry ("media/" ++ coverImage)
|
||||||
|
@ -474,10 +488,13 @@ pandocToEPUB version opts doc = do
|
||||||
|
|
||||||
-- title page
|
-- title page
|
||||||
tpContent <- lift $ writeHtml opts'{
|
tpContent <- lift $ writeHtml opts'{
|
||||||
writerVariables = ("titlepage","true"):
|
writerVariables =
|
||||||
("body-type", "frontmatter"):
|
Context (M.fromList [
|
||||||
("pagetitle", escapeStringForXML plainTitle):
|
("titlepage", toVal' "true"),
|
||||||
cssvars True ++ vars }
|
("body-type", toVal' "frontmatter"),
|
||||||
|
("pagetitle", toVal' $
|
||||||
|
escapeStringForXML plainTitle)])
|
||||||
|
<> cssvars True <> vars }
|
||||||
(Pandoc meta [])
|
(Pandoc meta [])
|
||||||
tpEntry <- mkEntry "text/title_page.xhtml" tpContent
|
tpEntry <- mkEntry "text/title_page.xhtml" tpContent
|
||||||
|
|
||||||
|
@ -564,9 +581,12 @@ pandocToEPUB version opts doc = do
|
||||||
|
|
||||||
let chapToEntry num (Chapter bs) =
|
let chapToEntry num (Chapter bs) =
|
||||||
mkEntry ("text/" ++ showChapter num) =<<
|
mkEntry ("text/" ++ showChapter num) =<<
|
||||||
writeHtml opts'{ writerVariables = ("body-type", bodyType) :
|
writeHtml opts'{ writerVariables =
|
||||||
("pagetitle", showChapter num) :
|
Context (M.fromList
|
||||||
cssvars True ++ vars } pdoc
|
[("body-type", toVal' bodyType),
|
||||||
|
("pagetitle", toVal' $
|
||||||
|
showChapter num)])
|
||||||
|
<> cssvars True <> vars } pdoc
|
||||||
where (pdoc, bodyType) =
|
where (pdoc, bodyType) =
|
||||||
case bs of
|
case bs of
|
||||||
(Header _ (_,_,kvs) xs : _) ->
|
(Header _ (_,_,kvs) xs : _) ->
|
||||||
|
@ -776,9 +796,10 @@ pandocToEPUB version opts doc = do
|
||||||
(writeHtmlStringForEPUB version
|
(writeHtmlStringForEPUB version
|
||||||
opts{ writerTemplate = Nothing
|
opts{ writerTemplate = Nothing
|
||||||
, writerVariables =
|
, writerVariables =
|
||||||
("pagetitle",
|
Context (M.fromList
|
||||||
escapeStringForXML plainTitle):
|
[("pagetitle", toVal' $
|
||||||
writerVariables opts}
|
escapeStringForXML plainTitle)])
|
||||||
|
<> writerVariables opts}
|
||||||
(Pandoc nullMeta
|
(Pandoc nullMeta
|
||||||
[Plain $ walk clean tit])) of
|
[Plain $ walk clean tit])) of
|
||||||
Left _ -> TS.pack $ stringify tit
|
Left _ -> TS.pack $ stringify tit
|
||||||
|
@ -801,13 +822,13 @@ pandocToEPUB version opts doc = do
|
||||||
then [ unode "li"
|
then [ unode "li"
|
||||||
[ unode "a" ! [("href", "text/cover.xhtml")
|
[ unode "a" ! [("href", "text/cover.xhtml")
|
||||||
,("epub:type", "cover")] $
|
,("epub:type", "cover")] $
|
||||||
"Cover"] |
|
("Cover" :: String)] |
|
||||||
isJust (epubCoverImage metadata)
|
isJust (epubCoverImage metadata)
|
||||||
] ++
|
] ++
|
||||||
[ unode "li"
|
[ unode "li"
|
||||||
[ unode "a" ! [("href", "#toc")
|
[ unode "a" ! [("href", "#toc")
|
||||||
,("epub:type", "toc")] $
|
,("epub:type", "toc")] $
|
||||||
"Table of contents"
|
("Table of contents" :: String)
|
||||||
] | writerTableOfContents opts
|
] | writerTableOfContents opts
|
||||||
]
|
]
|
||||||
else []
|
else []
|
||||||
|
@ -819,8 +840,9 @@ pandocToEPUB version opts doc = do
|
||||||
,("hidden","hidden")] $
|
,("hidden","hidden")] $
|
||||||
[ unode "ol" landmarkItems ]
|
[ unode "ol" landmarkItems ]
|
||||||
]
|
]
|
||||||
navData <- lift $ writeHtml opts'{ writerVariables = ("navpage","true"):
|
navData <- lift $ writeHtml opts'{ writerVariables =
|
||||||
cssvars False ++ vars }
|
Context (M.fromList [("navpage", toVal' "true")])
|
||||||
|
<> cssvars False <> vars }
|
||||||
(Pandoc (setMeta "title"
|
(Pandoc (setMeta "title"
|
||||||
(walk removeNote $ fromList $ docTitle' meta) nullMeta)
|
(walk removeNote $ fromList $ docTitle' meta) nullMeta)
|
||||||
(navBlocks ++ landmarks))
|
(navBlocks ++ landmarks))
|
||||||
|
@ -846,7 +868,7 @@ pandocToEPUB version opts doc = do
|
||||||
let apple = UTF8.fromStringLazy $ ppTopElement $
|
let apple = UTF8.fromStringLazy $ ppTopElement $
|
||||||
unode "display_options" $
|
unode "display_options" $
|
||||||
unode "platform" ! [("name","*")] $
|
unode "platform" ! [("name","*")] $
|
||||||
unode "option" ! [("name","specified-fonts")] $ "true"
|
unode "option" ! [("name","specified-fonts")] $ ("true" :: String)
|
||||||
appleEntry <- mkEntry "META-INF/com.apple.ibooks.display-options.xml" apple
|
appleEntry <- mkEntry "META-INF/com.apple.ibooks.display-options.xml" apple
|
||||||
|
|
||||||
-- construct archive
|
-- construct archive
|
||||||
|
@ -949,6 +971,7 @@ metadataElement version md currentTime =
|
||||||
(("id",id') :
|
(("id",id') :
|
||||||
maybe [] (\x -> [("opf:event",x)]) (dateEvent date)) $
|
maybe [] (\x -> [("opf:event",x)]) (dateEvent date)) $
|
||||||
dateText date]
|
dateText date]
|
||||||
|
schemeToOnix :: String -> String
|
||||||
schemeToOnix "ISBN-10" = "02"
|
schemeToOnix "ISBN-10" = "02"
|
||||||
schemeToOnix "GTIN-13" = "03"
|
schemeToOnix "GTIN-13" = "03"
|
||||||
schemeToOnix "UPC" = "04"
|
schemeToOnix "UPC" = "04"
|
||||||
|
|
|
@ -39,6 +39,7 @@ import Data.List.Split (splitWhen)
|
||||||
import Data.Text (Text)
|
import Data.Text (Text)
|
||||||
import qualified Data.Text as T
|
import qualified Data.Text as T
|
||||||
import qualified Data.Text.Lazy as TL
|
import qualified Data.Text.Lazy as TL
|
||||||
|
import Text.DocTemplates (FromContext(lookupContext))
|
||||||
import Network.HTTP (urlEncode)
|
import Network.HTTP (urlEncode)
|
||||||
import Network.URI (URI (..), parseURIReference)
|
import Network.URI (URI (..), parseURIReference)
|
||||||
import Numeric (showHex)
|
import Numeric (showHex)
|
||||||
|
@ -220,8 +221,8 @@ writeHtmlString' st opts d = do
|
||||||
case getField "pagetitle" context of
|
case getField "pagetitle" context of
|
||||||
Just (s :: Text) | not (T.null s) -> return context
|
Just (s :: Text) | not (T.null s) -> return context
|
||||||
_ -> do
|
_ -> do
|
||||||
let fallback = maybe "Untitled" takeBaseName $
|
let fallback = maybe "Untitled" (takeBaseName . T.unpack) $
|
||||||
lookup "sourcefile" (writerVariables opts)
|
lookupContext "sourcefile" (writerVariables opts)
|
||||||
report $ NoTitleElement fallback
|
report $ NoTitleElement fallback
|
||||||
return $ resetField "pagetitle" (T.pack fallback) context
|
return $ resetField "pagetitle" (T.pack fallback) context
|
||||||
return $ renderTemplate tpl
|
return $ renderTemplate tpl
|
||||||
|
@ -286,11 +287,13 @@ pandocToHtml opts (Pandoc meta blocks) = do
|
||||||
H.link ! A.rel "stylesheet" !
|
H.link ! A.rel "stylesheet" !
|
||||||
A.href (toValue $ url ++ "katex.min.css")
|
A.href (toValue $ url ++ "katex.min.css")
|
||||||
|
|
||||||
_ -> case lookup "mathml-script" (writerVariables opts) of
|
_ -> case lookupContext "mathml-script"
|
||||||
|
(writerVariables opts) of
|
||||||
Just s | not (stHtml5 st) ->
|
Just s | not (stHtml5 st) ->
|
||||||
H.script ! A.type_ "text/javascript"
|
H.script ! A.type_ "text/javascript"
|
||||||
$ preEscapedString
|
$ preEscapedString
|
||||||
("/*<![CDATA[*/\n" ++ s ++ "/*]]>*/\n")
|
("/*<![CDATA[*/\n" ++ T.unpack s ++
|
||||||
|
"/*]]>*/\n")
|
||||||
| otherwise -> mempty
|
| otherwise -> mempty
|
||||||
Nothing -> mempty
|
Nothing -> mempty
|
||||||
let context = (if stHighlighting st
|
let context = (if stHighlighting st
|
||||||
|
|
|
@ -30,6 +30,7 @@ import qualified Data.Map as M
|
||||||
import Data.Text (Text)
|
import Data.Text (Text)
|
||||||
import qualified Data.Text as T
|
import qualified Data.Text as T
|
||||||
import Network.URI (unEscapeString)
|
import Network.URI (unEscapeString)
|
||||||
|
import Text.DocTemplates (FromContext(lookupContext))
|
||||||
import Text.Pandoc.BCP47 (Lang (..), getLang, renderLang)
|
import Text.Pandoc.BCP47 (Lang (..), getLang, renderLang)
|
||||||
import Text.Pandoc.Class (PandocMonad, report, toLang)
|
import Text.Pandoc.Class (PandocMonad, report, toLang)
|
||||||
import Text.Pandoc.Definition
|
import Text.Pandoc.Definition
|
||||||
|
@ -146,8 +147,9 @@ pandocToLaTeX options (Pandoc meta blocks) = do
|
||||||
-- these have \frontmatter etc.
|
-- these have \frontmatter etc.
|
||||||
beamer <- gets stBeamer
|
beamer <- gets stBeamer
|
||||||
let documentClass =
|
let documentClass =
|
||||||
case lookup "documentclass" (writerVariables options) `mplus`
|
case (lookupContext "documentclass"
|
||||||
fmap stringify (lookupMeta "documentclass" meta) of
|
(writerVariables options)) `mplus`
|
||||||
|
(T.pack . stringify <$> lookupMeta "documentclass" meta) of
|
||||||
Just x -> x
|
Just x -> x
|
||||||
Nothing | beamer -> "beamer"
|
Nothing | beamer -> "beamer"
|
||||||
| otherwise -> case writerTopLevelDivision options of
|
| otherwise -> case writerTopLevelDivision options of
|
||||||
|
@ -208,7 +210,7 @@ pandocToLaTeX options (Pandoc meta blocks) = do
|
||||||
defField "title-meta" (T.pack titleMeta) $
|
defField "title-meta" (T.pack titleMeta) $
|
||||||
defField "author-meta"
|
defField "author-meta"
|
||||||
(T.pack $ intercalate "; " authorsMeta) $
|
(T.pack $ intercalate "; " authorsMeta) $
|
||||||
defField "documentclass" (T.pack documentClass) $
|
defField "documentclass" documentClass $
|
||||||
defField "verbatim-in-note" (stVerbInNote st) $
|
defField "verbatim-in-note" (stVerbInNote st) $
|
||||||
defField "tables" (stTable st) $
|
defField "tables" (stTable st) $
|
||||||
defField "strikeout" (stStrikeout st) $
|
defField "strikeout" (stStrikeout st) $
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
{-# LANGUAGE NoImplicitPrelude #-}
|
{-# LANGUAGE NoImplicitPrelude #-}
|
||||||
{-# LANGUAGE PatternGuards #-}
|
{-# LANGUAGE PatternGuards #-}
|
||||||
{- |
|
{- |
|
||||||
|
@ -24,6 +25,7 @@ import Codec.Archive.Zip
|
||||||
import Data.Char (toUpper)
|
import Data.Char (toUpper)
|
||||||
import Data.List (intercalate, stripPrefix, nub, union, isPrefixOf, intersperse)
|
import Data.List (intercalate, stripPrefix, nub, union, isPrefixOf, intersperse)
|
||||||
import Data.Default
|
import Data.Default
|
||||||
|
import qualified Data.Text as T
|
||||||
import Data.Time (formatTime, defaultTimeLocale)
|
import Data.Time (formatTime, defaultTimeLocale)
|
||||||
import Data.Time.Clock (UTCTime)
|
import Data.Time.Clock (UTCTime)
|
||||||
import Data.Time.Clock.POSIX (utcTimeToPOSIXSeconds, posixSecondsToUTCTime)
|
import Data.Time.Clock.POSIX (utcTimeToPOSIXSeconds, posixSecondsToUTCTime)
|
||||||
|
@ -43,6 +45,7 @@ import Data.Maybe (mapMaybe, listToMaybe, fromMaybe, maybeToList, catMaybes, isN
|
||||||
import Text.Pandoc.ImageSize
|
import Text.Pandoc.ImageSize
|
||||||
import Control.Applicative ((<|>))
|
import Control.Applicative ((<|>))
|
||||||
import System.FilePath.Glob
|
import System.FilePath.Glob
|
||||||
|
import Text.DocTemplates (FromContext(lookupContext))
|
||||||
import Text.TeXMath
|
import Text.TeXMath
|
||||||
import Text.Pandoc.Writers.Math (convertMath)
|
import Text.Pandoc.Writers.Math (convertMath)
|
||||||
import Text.Pandoc.Writers.Powerpoint.Presentation
|
import Text.Pandoc.Writers.Powerpoint.Presentation
|
||||||
|
@ -159,8 +162,8 @@ runP env st p = evalStateT (runReaderT p env) st
|
||||||
monospaceFont :: Monad m => P m String
|
monospaceFont :: Monad m => P m String
|
||||||
monospaceFont = do
|
monospaceFont = do
|
||||||
vars <- writerVariables <$> asks envOpts
|
vars <- writerVariables <$> asks envOpts
|
||||||
case lookup "monofont" vars of
|
case lookupContext "monofont" vars of
|
||||||
Just s -> return s
|
Just s -> return (T.unpack s)
|
||||||
Nothing -> return "Courier"
|
Nothing -> return "Courier"
|
||||||
|
|
||||||
fontSizeAttributes :: Monad m => RunProps -> P m [(String, String)]
|
fontSizeAttributes :: Monad m => RunProps -> P m [(String, String)]
|
||||||
|
|
|
@ -39,6 +39,7 @@ module Text.Pandoc.Writers.Shared (
|
||||||
where
|
where
|
||||||
import Prelude
|
import Prelude
|
||||||
import Safe (lastMay)
|
import Safe (lastMay)
|
||||||
|
import qualified Data.ByteString.Lazy as BL
|
||||||
import Data.Maybe (fromMaybe)
|
import Data.Maybe (fromMaybe)
|
||||||
import Control.Monad (zipWithM)
|
import Control.Monad (zipWithM)
|
||||||
import Data.Aeson (ToJSON (..), encode)
|
import Data.Aeson (ToJSON (..), encode)
|
||||||
|
@ -90,12 +91,15 @@ metaToContext' blockWriter inlineWriter (Meta metamap) = do
|
||||||
-- | Add variables to a template Context, replacing any existing values.
|
-- | Add variables to a template Context, replacing any existing values.
|
||||||
addVariablesToContext :: TemplateTarget a
|
addVariablesToContext :: TemplateTarget a
|
||||||
=> WriterOptions -> Context a -> Context a
|
=> WriterOptions -> Context a -> Context a
|
||||||
addVariablesToContext opts (Context m1) = Context (m1 `M.union` m2)
|
addVariablesToContext opts (Context m1) =
|
||||||
|
Context (m1 `M.union` m2 `M.union` m3)
|
||||||
where
|
where
|
||||||
m2 = M.fromList $ map (\(k,v)
|
m2 = case traverse go (writerVariables opts) of
|
||||||
-> (T.pack k,SimpleVal (fromText (T.pack v)))) $
|
Just (Context x) -> x
|
||||||
("meta-json", jsonrep) : writerVariables opts
|
Nothing -> mempty
|
||||||
jsonrep = UTF8.toStringLazy $ encode $ toJSON m1
|
m3 = M.insert "meta-json" (SimpleVal $ fromText jsonrep) mempty
|
||||||
|
go = Just . fromText
|
||||||
|
jsonrep = UTF8.toText $ BL.toStrict $ encode $ toJSON m1
|
||||||
|
|
||||||
metaValueToVal :: (Monad m, TemplateTarget a)
|
metaValueToVal :: (Monad m, TemplateTarget a)
|
||||||
=> ([Block] -> m a)
|
=> ([Block] -> m a)
|
||||||
|
|
|
@ -22,7 +22,9 @@ extra-deps:
|
||||||
- doclayout-0.1
|
- doclayout-0.1
|
||||||
- HsYAML-0.2.0.0
|
- HsYAML-0.2.0.0
|
||||||
- HsYAML-aeson-0.2.0.0
|
- HsYAML-aeson-0.2.0.0
|
||||||
- doctemplates-0.6.1
|
# - doctemplates-0.6.1
|
||||||
|
- git: https://github.com/jgm/doctemplates.git
|
||||||
|
commit: b0e92bd6e32eb1a8c021598b4e8a5f25b9c5cd40
|
||||||
ghc-options:
|
ghc-options:
|
||||||
"$locals": -fhide-source-paths -Wno-missing-home-modules
|
"$locals": -fhide-source-paths -Wno-missing-home-modules
|
||||||
resolver: lts-14.6
|
resolver: lts-14.6
|
||||||
|
|
|
@ -6,6 +6,9 @@ import Tests.Writers.OOXML (ooxmlTest)
|
||||||
import Text.Pandoc
|
import Text.Pandoc
|
||||||
import Test.Tasty
|
import Test.Tasty
|
||||||
import System.FilePath
|
import System.FilePath
|
||||||
|
import Text.DocTemplates (ToContext(toVal), Context(..))
|
||||||
|
import qualified Data.Map as M
|
||||||
|
import Data.Text (pack)
|
||||||
|
|
||||||
-- templating is important enough, and can break enough things, that
|
-- templating is important enough, and can break enough things, that
|
||||||
-- we want to run all our tests with both default formatting and a
|
-- we want to run all our tests with both default formatting and a
|
||||||
|
@ -124,7 +127,8 @@ tests = groupPptxTests [ pptxTests "Inline formatting"
|
||||||
"pptx/code.native"
|
"pptx/code.native"
|
||||||
"pptx/code.pptx"
|
"pptx/code.pptx"
|
||||||
, pptxTests "inline code and code blocks, custom formatting"
|
, pptxTests "inline code and code blocks, custom formatting"
|
||||||
def { writerVariables = [("monofont", "Consolas")] }
|
def { writerVariables = Context $ M.fromList
|
||||||
|
[(pack "monofont", toVal $ pack "Consolas")] }
|
||||||
"pptx/code.native"
|
"pptx/code.native"
|
||||||
"pptx/code-custom.pptx"
|
"pptx/code-custom.pptx"
|
||||||
]
|
]
|
||||||
|
|
Loading…
Add table
Reference in a new issue