@@ -41,6 +41,8 @@ data WriterState =
4141 , stOptions :: WriterOptions -- writer options
4242 }
4343
44+data BlockWrapper = Pad Doc | Reg Doc
45+
4446orderedListStyles = cycle ["[n]","[a]", "[r]", "[g]"]
4547
4648-- | Convert Pandoc to ConTeXt.
@@ -123,28 +125,28 @@ stringToConTeXt = concatMap escapeCharForConTeXt
123125
124126-- | Convert Pandoc block element to ConTeXt.
125127blockToConTeXt :: Block
126− -> State WriterState Doc
127−blockToConTeXt Null = return empty
128+ -> State WriterState BlockWrapper
129+blockToConTeXt Null = return $ Reg empty
128130blockToConTeXt (Plain lst) = do
129131 st <- get
130132 let options = stOptions st
131133 contents <- wrapTeXIfNeeded options False inlineListToConTeXt lst
132− return contents
134+ return $ Reg contents
133135blockToConTeXt (Para lst) = do
134136 st <- get
135137 let options = stOptions st
136138 contents <- wrapTeXIfNeeded options False inlineListToConTeXt lst
137− return $ contents <> char '\n'
139+ return $ Pad contents
138140blockToConTeXt (BlockQuote lst) = do
139141 contents <- blockListToConTeXt lst
140− return $ text "\\startblockquote\n" $$ contents $$ text "\\stopblockquote"
142+ return $ Pad $ text "\\startblockquote" $$ contents $$ text "\\stopblockquote"
141143blockToConTeXt (CodeBlock str) =
142− return $ text $ "\\starttyping\n" ++ str ++ "\n\\stoptyping\n" -- \n needed
143− -- because \stoptyping can't have anything after it
144−blockToConTeXt (RawHtml str) = return empty
144+ return $ Reg $ text $ "\\starttyping\n" ++ str ++ "\n\\stoptyping\n"
145+ -- \n because \stoptyping can't have anything after it, inc. }
146+blockToConTeXt (RawHtml str) = return $ Reg empty
145147blockToConTeXt (BulletList lst) = do
146148 contents <- mapM listItemToConTeXt lst
147− return $ text "\\startitemize" $$ vcat contents $$ text "\\stopitemize\n"
149+ return $ Pad $ text "\\startitemize" $$ vcat contents $$ text "\\stopitemize"
148150blockToConTeXt (OrderedList (start, style, delim) lst) = do
149151 st <- get
150152 let level = stOrderedListLevel st
@@ -175,20 +177,20 @@ blockToConTeXt (OrderedList (start, style, delim) lst) = do
175177 LowerAlpha -> "[a]"
176178 UpperAlpha -> "[A]"
177179 let specs = style' ++ specs2
178− return $ text ("\\startitemize" ++ specs) $$ vcat contents $$
179− text "\\stopitemize\n"
180+ return $ Pad $ text ("\\startitemize" ++ specs) $$ vcat contents $$
181+ text "\\stopitemize"
180182blockToConTeXt (DefinitionList lst) =
181− mapM defListItemToConTeXt lst >>= return . (<> char '\n') . vcat
182−blockToConTeXt HorizontalRule = return $ text "\\thinrule\n"
183+ mapM defListItemToConTeXt lst >>= return . Pad . wrappedBlocksToDoc
184+blockToConTeXt HorizontalRule = return $ Pad $ text "\\thinrule"
183185blockToConTeXt (Header level lst) = do
184186 contents <- inlineListToConTeXt lst
185187 st <- get
186188 let opts = stOptions st
187189 let base = if writerNumberSections opts then "section" else "subject"
188− return $ if level >= 1 && level <= 5
189− then char '\\' <> text (concat (replicate (level - 1) "sub")) <>
190− text base <> char '{' <> contents <> char '}' <> char '\n'
191− else contents <> char '\n'
190+ return $ Pad $ if level >= 1 && level <= 5
191+ then char '\\' <> text (concat (replicate (level - 1) "sub")) <>
192+ text base <> char '{' <> contents <> char '}'
193+ else contents
192194blockToConTeXt (Table caption aligns widths heads rows) = do
193195 let colWidths = map printDecimal widths
194196 let colDescriptor colWidth alignment = (case alignment of
@@ -203,10 +205,10 @@ blockToConTeXt (Table caption aligns widths heads rows) = do
203205 captionText <- inlineListToConTeXt caption
204206 let captionText' = if null caption then text "none" else captionText
205207 rows' <- mapM tableRowToConTeXt rows
206− return $ text "\\placetable[here]{" <> captionText' <> char '}' $$
208+ return $ Pad $ text "\\placetable[here]{" <> captionText' <> char '}' $$
207209 text "\\starttable[" <> text colDescriptors <> char ']' $$
208210 text "\\HL" $$ headers $$ text "\\HL" $$
209− vcat rows' $$ text "\\HL\n\\stoptable\n"
211+ vcat rows' $$ text "\\HL\n\\stoptable"
210212
211213printDecimal :: Float -> String
212214printDecimal = printf "%.2f"
@@ -225,11 +227,17 @@ orderedListItemToConTeXt marker list = blockListToConTeXt list >>=
225227defListItemToConTeXt (term, def) = do
226228 term' <- inlineListToConTeXt term
227229 def' <- blockListToConTeXt def
228− return $ text "\\startdescr{" <> term' <> char '}' $$ def' $$ text "\\stopdescr"
230+ return $ Pad $ text "\\startdescr{" <> term' <> char '}' $$ def' $$ text "\\stopdescr"
231+
232+wrappedBlocksToDoc :: [BlockWrapper] -> Doc
233+wrappedBlocksToDoc = foldr addBlock empty
234+ where addBlock (Pad d) accum | isEmpty accum = d
235+ addBlock (Pad d) accum = d $$ text "" $$ accum
236+ addBlock (Reg d) accum = d $$ accum
229237
230238-- | Convert list of block elements to ConTeXt.
231239blockListToConTeXt :: [Block] -> State WriterState Doc
232−blockListToConTeXt lst = mapM blockToConTeXt lst >>= return . vcat
240+blockListToConTeXt lst = mapM blockToConTeXt lst >>= return . wrappedBlocksToDoc
233241
234242-- | Convert list of inline elements to ConTeXt.
235243inlineListToConTeXt :: [Inline] -- ^ Inlines to convert