git.iko.soy/typeable/pandoc
on master help sign in
Added page breaks into Pandoc.

This requires an updated version of pandoc-types that introduces PageBreak definition. Not that this initial commit only introduces ODT pagebreaks and distinguishes for it page breaks before, after, or both, the paragraph, as read from the style definition.

commit30b3412857fc604656aac53d57730ad2442a3599 authorHubert Plociniczak <hubert.plociniczak@gmail.com> dateFri 11 Nov 2016, 13:07 (10 years ago) parent5a03ebf
3 files changed+61−13
src/Text/Pandoc/Readers/Odt/ContentReader.hs+15−4 src/Text/Pandoc/Readers/Odt/StyleReader.hs+21−6 src/Text/Pandoc/Writers/OpenDocument.hs+25−3
src/Text/Pandoc/Readers/Odt/ContentReader.hs+15−4
@@ -381,9 +381,9 @@ getParaModifier :: Style -> ParaModifier
381381getParaModifier Style{..} | Just props <- paraProperties styleProperties
382382 , isBlockQuote (indentation props)
383383 (margin_left props)
384− = blockQuote
384+ = pageBreakMaybe (paraProperties styleProperties) blockQuote
385385 | otherwise
386− = id
386+ = pageBreakMaybe (paraProperties styleProperties) id
387387 where
388388 isBlockQuote mIndent mMargin
389389 | LengthValueMM indent <- mIndent
@@ -408,7 +408,19 @@ getParaModifier Style{..} | Just props <- paraProperties styleProperties
408408
409409 | otherwise
410410 = False
411−
411+ pageBreakMaybe :: Maybe ParaProperties -> ParaModifier -> ParaModifier
412+ pageBreakMaybe (Just props) modifier = insertPageBreak (page_break props) modifier
413+ pageBreakMaybe Nothing modifier = modifier
414+
415+ insertPageBreak :: ParaBreak -> ParaModifier -> ParaModifier
416+ insertPageBreak PageAfter modifier =
417+ \x -> (fromList (toList (modifier x) ++ [Para (toList pageBreak)]))
418+ insertPageBreak PageBefore modifier =
419+ \x -> (fromList (Para (toList pageBreak) : toList (modifier x)))
420+ insertPageBreak PageBoth modifier =
421+ \x -> (fromList ((Para (toList pageBreak) : toList (modifier x)) ++ [Para (toList pageBreak)]))
422+ insertPageBreak _ modifier =
423+ modifier
412424--
413425constructPara :: OdtReaderSafe Blocks Blocks -> OdtReaderSafe Blocks Blocks
414426constructPara reader = proc blocks -> do
@@ -894,7 +906,6 @@ read_reference_ref = matchingElement NsText "reference-ref"
894906 $ maybeInAnchorRef
895907 <<< matchChildContent [] read_plain_text
896908
897−
898909----------------------
899910-- Entry point
900911----------------------
src/Text/Pandoc/Readers/Odt/StyleReader.hs+21−6
@@ -43,6 +43,7 @@ module Text.Pandoc.Readers.Odt.StyleReader
4343, TextProperties (..)
4444, ParaProperties (..)
4545, VerticalTextPosition (..)
46+, ParaBreak (..)
4647, ListItemNumberFormat (..)
4748, ListLevel
4849, ListStyle (..)
@@ -273,6 +274,7 @@ instance Default TextProperties where
273274data ParaProperties = PropP { paraNumbering :: ParaNumbering
274275 , indentation :: LengthOrPercent
275276 , margin_left :: LengthOrPercent
277+ , page_break :: ParaBreak
276278 }
277279 deriving ( Eq, Show )
278280
@@ -280,6 +282,7 @@ instance Default ParaProperties where
280282 def = PropP { paraNumbering = NumberingNone
281283 , indentation = def
282284 , margin_left = def
285+ , page_break = AutoNone
283286 }
284287
285288----
@@ -314,6 +317,9 @@ instance Lookupable UnderlineMode where
314317data ParaNumbering = NumberingNone | NumberingKeep | NumberingRestart Int
315318 deriving ( Eq, Show )
316319
320+data ParaBreak = AutoNone | PageBefore | PageAfter | PageBoth
321+ deriving ( Eq, Show )
322+
317323data LengthOrPercent = LengthValueMM Int | PercentValue Int
318324 deriving ( Eq, Show )
319325
@@ -533,16 +539,20 @@ readLineMode modeAttr styleAttr = proc x -> do
533539readParaProperties :: StyleReader _x ParaProperties
534540readParaProperties =
535541 executeIn NsStyle "paragraph-properties" $ liftAsSuccess
536− ( liftA3 PropP
542+ ( liftA4 PropP
537543 ( liftA2 readNumbering
538− ( isSet' NsText "number-lines" )
539− ( readAttr' NsText "line-number" )
544+ ( isSet' NsText "number-lines" )
545+ ( readAttr' NsText "line-number" )
540546 )
541547 ( liftA2 readIndentation
542− ( isSetWithDefault NsStyle "auto-text-indent" False )
543− ( getAttr NsXSL_FO "text-indent" )
548+ ( isSetWithDefault NsStyle "auto-text-indent" False )
549+ ( getAttr NsXSL_FO "text-indent" )
550+ )
551+ ( getAttr NsXSL_FO "margin-left" )
552+ ( liftA2 readPageBreak
553+ ( findAttrWithDefault NsXSL_FO "break-before" "auto" )
554+ ( findAttrWithDefault NsXSL_FO "break-after" "auto" )
544555 )
545− ( getAttr NsXSL_FO "margin-left" )
546556 )
547557 where readNumbering (Just True) (Just n) = NumberingRestart n
548558 readNumbering (Just True) _ = NumberingKeep
@@ -551,6 +561,11 @@ readParaProperties =
551561 readIndentation False indent = indent
552562 readIndentation True _ = def
553563
564+ readPageBreak "page" "page" = PageBoth
565+ readPageBreak "page" _ = PageBefore
566+ readPageBreak _ "page" = PageAfter
567+ readPageBreak _ _ = AutoNone
568+
554569----
555570-- List styles
556571----
src/Text/Pandoc/Writers/OpenDocument.hs+25−3
@@ -36,6 +36,7 @@ import Text.Pandoc.XML
3636import Text.Pandoc.Shared (linesToPara)
3737import Text.Pandoc.Templates (renderTemplate')
3838import Text.Pandoc.Readers.TeXMath
39+import Text.Pandoc.Readers.Odt.StyleReader
3940import Text.Pandoc.Pretty
4041import Text.Printf ( printf )
4142import Control.Arrow ( (***), (>>>) )
@@ -307,9 +308,7 @@ blockToOpenDocument o bs
307308 else inParagraphTags =<< inlinesToOpenDocument o b
308309 | Para [Image attr c (s,'f':'i':'g':':':t)] <- bs
309310 = figure attr c s t
310− | Para b <- bs = if null b
311− then return empty
312− else inParagraphTags =<< inlinesToOpenDocument o b
311+ | Para b <- bs = paragraph b
313312 | LineBlock b <- bs = blockToOpenDocument o $ linesToPara b
314313 | Div _ xs <- bs = blocksToOpenDocument o xs
315314 | Header i _ b <- bs = setFirstPara >>
@@ -370,6 +369,22 @@ blockToOpenDocument o bs
370369 captionDoc <- withParagraphStyle o "FigureCaption" [Para caption]
371370 return $ imageDoc $$ captionDoc
372371
372+ endsWithPageBreak [] = False
373+ endsWithPageBreak [PageBreak] = True
374+ endsWithPageBreak (_ : xs) = endsWithPageBreak xs
375+
376+ paragraph :: [Inline] -> State WriterState Doc
377+ paragraph [] = return empty
378+ paragraph (PageBreak : rest) | endsWithPageBreak rest = paraWithBreak PageBoth rest
379+ paragraph (PageBreak : rest) = paraWithBreak PageBefore rest
380+ paragraph inlines | endsWithPageBreak inlines = paraWithBreak PageAfter inlines
381+ paragraph inlines = inParagraphTags =<< inlinesToOpenDocument o inlines
382+
383+ paraWithBreak :: ParaBreak -> [Inline] -> State WriterState Doc
384+ paraWithBreak breakKind bs = do
385+ pn <- paraBreakStyle breakKind
386+ withParagraphStyle o ("P" ++ show pn) [Para bs]
387+
373388colHeadsToOpenDocument :: WriterOptions -> String -> [String] -> [[Block]] -> State WriterState Doc
374389colHeadsToOpenDocument o tn ns hs =
375390 inTagsIndented "table:table-header-rows" . inTagsIndented "table:table-row" . vcat <$>
@@ -562,6 +577,13 @@ paraStyle attrs = do
562577 addParaStyle $ inTags True "style:style" (styleAttr ++ attrs) paraProps
563578 return pn
564579
580+paraBreakStyle :: ParaBreak -> State WriterState Int
581+paraBreakStyle PageBefore = paraStyle "Text_20_body" [("fo:break-before", "page")]
582+paraBreakStyle PageAfter = paraStyle "Text_20_body" [("fo:break-after", "page")]
583+paraBreakStyle PageBoth = paraStyle "Text_20_body" [("fo:break-before", "page"), ("fo:break-after", "page")]
584+paraBreakStyle AutoNone = paraStyle "Text_20_body" []
585+
586+
565587paraListStyle :: Int -> State WriterState Int
566588paraListStyle l = paraStyle
567589 [("style:parent-style-name","Text_20_body")