git.iko.soy/typeable/pandoc
on master help sign in
Added wrappedTeX and wrapTeXIfNeeded functions to Text.Pandoc.Shared.

These ensure that footnotes occur on lines by themselves (to make them easier to move around), and that they don't screw up text wrapping. git-svn-id: https://pandoc.googlecode.com/svn/trunk@1070 788f1e2b-df1e-0410-8736-df70ead52e1b

commite73c3d3561a743e0687536695e8241e9eaa3f565 authorfiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b> dateThu 15 Nov 2007, 03:09 (19 years ago) parent1ba0c8f
1 file changed+32−1
src/Text/Pandoc/Shared.hs+32−1
src/Text/Pandoc/Shared.hs+32−1
@@ -45,6 +45,8 @@ module Text.Pandoc.Shared (
4545 toRomanNumeral,
4646 wrapped,
4747 wrapIfNeeded,
48+ wrappedTeX,
49+ wrapTeXIfNeeded,
4850 -- * Parsing
4951 (>>~),
5052 anyLine,
@@ -96,7 +98,8 @@ module Text.Pandoc.Shared (
9698
9799import Text.Pandoc.Definition
98100import Text.ParserCombinators.Parsec
99−import Text.PrettyPrint.HughesPJ ( Doc, fsep )
101+import Text.PrettyPrint.HughesPJ ( Doc, fsep, ($$), (<>), empty )
102+import qualified Text.PrettyPrint.HughesPJ as PP
100103import Text.Pandoc.CharacterReferences ( characterReference )
101104import Data.Char ( toLower, toUpper, ord, isLower, isUpper )
102105import Data.List ( find, isPrefixOf )
@@ -211,12 +214,40 @@ wrapped :: Monad m => ([Inline] -> m Doc) -> [Inline] -> m Doc
211214wrapped listWriter sect = (mapM listWriter $ splitBy Space sect) >>=
212215 return . fsep
213216
217+-- | Wrap inlines if the text wrap option is selected.
214218wrapIfNeeded :: Monad m => WriterOptions -> ([Inline] -> m Doc) ->
215219 [Inline] -> m Doc
216220wrapIfNeeded opts = if writerWrapText opts
217221 then wrapped
218222 else ($)
219223
224+-- auxiliary function for wrappedTeX
225+isNote (Note _) = True
226+isNote _ = False
227+
228+-- | Wrap inlines to line length, treating footnotes in a way that
229+-- makes sense in LaTeX and ConTeXt.
230+wrappedTeX :: Monad m => ([Inline] -> m Doc) -> [Inline] -> m Doc
231+wrappedTeX listWriter sect = do
232+ let (firstpart, rest) = break isNote sect
233+ firstpartWrapped <- wrapped listWriter firstpart
234+ if null rest
235+ then return firstpartWrapped
236+ else do let (note:rest') = rest
237+ restWrapped <- if null rest'
238+ then return empty
239+ else wrappedTeX listWriter rest'
240+ noteText <- listWriter [note]
241+ return $ firstpartWrapped <> PP.char '%' $$ noteText $$ restWrapped
242+
243+-- | Wrap inlines if the text wrap option is selected, specialized
244+-- for LaTeX and ConTeXt.
245+wrapTeXIfNeeded :: Monad m => WriterOptions -> ([Inline] -> m Doc) ->
246+ [Inline] -> m Doc
247+wrapTeXIfNeeded opts = if writerWrapText opts
248+ then wrappedTeX
249+ else ($)
250+
220251--
221252-- Parsing
222253--