From 6a3d1cf2108a2a6408098621b31cffb189c561f2 Mon Sep 17 00:00:00 2001
From: Jesse Rosenthal <jrosenthal@jhu.edu>
Date: Mon, 3 Oct 2016 07:50:40 -0400
Subject: [PATCH] Add ReaderT env to the docx writer:

This will allow us to add text and paragraph properties depending on if
rtl is already set or not.

(It would probably be cleaner and safer to move the paraprops and
textprops to this part of the stack in the future.)
---
 src/Text/Pandoc/Writers/Docx.hs | 25 +++++++++++++++++--------
 1 file changed, 17 insertions(+), 8 deletions(-)

diff --git a/src/Text/Pandoc/Writers/Docx.hs b/src/Text/Pandoc/Writers/Docx.hs
index d31928b01..50c28d20c 100644
--- a/src/Text/Pandoc/Writers/Docx.hs
+++ b/src/Text/Pandoc/Writers/Docx.hs
@@ -54,6 +54,7 @@ import Text.XML.Light as XML
 import Text.TeXMath
 import Text.Pandoc.Readers.Docx.StyleMap
 import Text.Pandoc.Readers.Docx.Util (elemName)
+import Control.Monad.Reader
 import Control.Monad.State
 import Text.Highlighting.Kate
 import Data.Unique (hashUnique, newUnique)
@@ -91,6 +92,11 @@ listMarkerToId (NumberMarker sty delim n) =
                       OneParen       -> '2'
                       TwoParens      -> '3'
 
+data WriterEnv = WriterEnv{ envRTL :: Bool }
+
+defaultWriterEnv :: WriterEnv
+defaultWriterEnv = WriterEnv{ envRTL = False }
+
 data WriterState = WriterState{
          stTextProperties :: [Element]
        , stParaProperties :: [Element]
@@ -138,7 +144,7 @@ defaultWriterState = WriterState{
       , stDynamicTextProps = []
       }
 
-type WS a = StateT WriterState IO a
+type WS = ReaderT WriterEnv (StateT WriterState IO)
 
 mknode :: Node t => String -> [(String,String)] -> t -> Element
 mknode s attrs =
@@ -249,13 +255,16 @@ writeDocx opts doc@(Pandoc meta _) = do
   let tocTitle = fromMaybe (stTocTitle defaultWriterState) $
                     metaValueToInlines <$> lookupMeta "toc-title" meta
 
-  ((contents, footnotes), st) <- runStateT (writeOpenXML opts{writerWrapText = WrapNone} doc')
-                       defaultWriterState{ stChangesAuthor = fromMaybe "unknown" username
-                                         , stChangesDate   = formatTime defaultTimeLocale "%FT%XZ" utctime
-                                         , stPrintWidth = (maybe 420 (\x -> quot x 20) pgContentWidth)
-                                         , stStyleMaps  = styleMaps
-                                         , stTocTitle   = tocTitle
-                                         }
+  ((contents, footnotes), st) <- runStateT (
+    runReaderT
+    (writeOpenXML opts{writerWrapText = WrapNone} doc')
+    defaultWriterEnv
+    ) defaultWriterState{ stChangesAuthor = fromMaybe "unknown" username
+                        , stChangesDate   = formatTime defaultTimeLocale "%FT%XZ" utctime
+                        , stPrintWidth = (maybe 420 (\x -> quot x 20) pgContentWidth)
+                        , stStyleMaps  = styleMaps
+                        , stTocTitle   = tocTitle
+                        }
   let epochtime = floor $ utcTimeToPOSIXSeconds utctime
   let imgs = M.elems $ stImages st