git.iko.soy/typeable/pandoc
on master help sign in
Added wrappedTeX and wrapTeXIfNeeded functions to Text.Pandoc.Shared.
e73c3d3fiddlosopher
src/Text/Pandoc/Shared.hs+32−1
@@ -45,6 +45,8 @@ module Text.Pandoc.Shared (
45 toRomanNumeral,
45 toRomanNumeral,
46 wrapped,
46 wrapped,
47 wrapIfNeeded,
47 wrapIfNeeded,
48+ wrappedTeX,
49+ wrapTeXIfNeeded,
48 -- * Parsing
50 -- * Parsing
49 (>>~),
51 (>>~),
50 anyLine,
52 anyLine,
@@ -96,7 +98,8 @@ module Text.Pandoc.Shared (
96
98
97import Text.Pandoc.Definition
99import Text.Pandoc.Definition
98import Text.ParserCombinators.Parsec
100import 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
100import Text.Pandoc.CharacterReferences ( characterReference )
103import Text.Pandoc.CharacterReferences ( characterReference )
101import Data.Char ( toLower, toUpper, ord, isLower, isUpper )
104import Data.Char ( toLower, toUpper, ord, isLower, isUpper )
102import Data.List ( find, isPrefixOf )
105import Data.List ( find, isPrefixOf )
@@ -211,12 +214,40 @@ wrapped :: Monad m => ([Inline] -> m Doc) -> [Inline] -> m Doc
211wrapped listWriter sect = (mapM listWriter $ splitBy Space sect) >>=
214wrapped listWriter sect = (mapM listWriter $ splitBy Space sect) >>=
212 return . fsep
215 return . fsep
213
216
217+-- | Wrap inlines if the text wrap option is selected.
214wrapIfNeeded :: Monad m => WriterOptions -> ([Inline] -> m Doc) ->
218wrapIfNeeded :: Monad m => WriterOptions -> ([Inline] -> m Doc) ->
215 [Inline] -> m Doc
219 [Inline] -> m Doc
216wrapIfNeeded opts = if writerWrapText opts
220wrapIfNeeded opts = if writerWrapText opts
217 then wrapped
221 then wrapped
218 else ($)
222 else ($)
219
223
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+
220--
251--
221-- Parsing
252-- Parsing
222--
253--