From ce6e9e8817fa7c09625a3c587095bffc8a5eb598 Mon Sep 17 00:00:00 2001 From: Nils Carlson Date: Thu, 20 Sep 2018 21:30:32 +0000 Subject: ODT Writer: Improve table header row style handling This changes the way styles for cells in the header row and normal rows are handled in ODT tables. Previously a new (but identical) style was generated for every table, specifying the style of the cells within the table. After this change there are two style definitions for table cells, one for the cells in the header row, one for all other cells. This doesn't change the actual styles, but makes post-processing changes to the table styles much simpler as it is no longer necessary to introduce new styles for header rows and there are now only two styles where there was previously one per table. --- test/tables.opendocument | 184 +++++++++++++++++++++++------------------------ 1 file changed, 92 insertions(+), 92 deletions(-) (limited to 'test/tables.opendocument') diff --git a/test/tables.opendocument b/test/tables.opendocument index c331ecc43..c04afd492 100644 --- a/test/tables.opendocument +++ b/test/tables.opendocument @@ -6,59 +6,59 @@ - + Right - + Left - + Center - + Default - + 12 - + 12 - + 12 - + 12 - + 123 - + 123 - + 123 - + 123 - + 1 - + 1 - + 1 - + 1 @@ -73,59 +73,59 @@ caption: - + Right - + Left - + Center - + Default - + 12 - + 12 - + 12 - + 12 - + 123 - + 123 - + 123 - + 123 - + 1 - + 1 - + 1 - + 1 @@ -139,59 +139,59 @@ spaces: - + Right - + Left - + Center - + Default - + 12 - + 12 - + 12 - + 12 - + 123 - + 123 - + 123 - + 123 - + 1 - + 1 - + 1 - + 1 @@ -206,46 +206,46 @@ caption: - + Centered Header - + Left Aligned - + Right Aligned - + Default aligned - + First - + row - + 12.0 - + Example of a row that spans multiple lines. - + Second - + row - + 5.0 - + Here’s another one. Note the blank line between rows. @@ -262,46 +262,46 @@ caption: - + Centered Header - + Left Aligned - + Right Aligned - + Default aligned - + First - + row - + 12.0 - + Example of a row that spans multiple lines. - + Second - + row - + 5.0 - + Here’s another one. Note the blank line between rows. @@ -315,44 +315,44 @@ headers: - + 12 - + 12 - + 12 - + 12 - + 123 - + 123 - + 123 - + 123 - + 1 - + 1 - + 1 - + 1 @@ -365,31 +365,31 @@ headers: - + First - + row - + 12.0 - + Example of a row that spans multiple lines. - + Second - + row - + 5.0 - + Here’s another one. Note the blank line between rows. -- cgit v1.2.3 From 6304720d8d7906d698018203e1c743683f0234f3 Mon Sep 17 00:00:00 2001 From: Nils Carlson Date: Fri, 5 Oct 2018 16:58:23 +0000 Subject: OpenDocument writer: Implement table caption numbering (#4949) Implement table caption numbering with a format "Table 1: ". Translations are enabled and numbering is consecutive for captioned tables, uncaptioned tables are not enumerated. Captioned figures are now also numbered consecutively and uncaptioned figures are not enumerated. --- src/Text/Pandoc/Writers/OpenDocument.hs | 89 ++++++++++++++++++++------------- test/tables.opendocument | 10 ++-- 2 files changed, 59 insertions(+), 40 deletions(-) (limited to 'test/tables.opendocument') diff --git a/src/Text/Pandoc/Writers/OpenDocument.hs b/src/Text/Pandoc/Writers/OpenDocument.hs index 676a1acb0..d9f0a8e44 100644 --- a/src/Text/Pandoc/Writers/OpenDocument.hs +++ b/src/Text/Pandoc/Writers/OpenDocument.hs @@ -52,7 +52,7 @@ import Text.Pandoc.Options import Text.Pandoc.Pretty import Text.Pandoc.Shared (linesToPara) import Text.Pandoc.Templates (renderTemplate') -import Text.Pandoc.Translations (Term(Figure)) +import qualified Text.Pandoc.Translations as Term (Term(Figure, Table)) import Text.Pandoc.Writers.Math import Text.Pandoc.Writers.Shared import Text.Pandoc.XML @@ -70,32 +70,36 @@ plainToPara x = x type OD m = StateT WriterState m data WriterState = - WriterState { stNotes :: [Doc] - , stTableStyles :: [Doc] - , stParaStyles :: [Doc] - , stListStyles :: [(Int, [Doc])] - , stTextStyles :: Map.Map (Set.Set TextStyle) (String, Doc) - , stTextStyleAttr :: Set.Set TextStyle - , stIndentPara :: Int - , stInDefinition :: Bool - , stTight :: Bool - , stFirstPara :: Bool - , stImageId :: Int + WriterState { stNotes :: [Doc] + , stTableStyles :: [Doc] + , stParaStyles :: [Doc] + , stListStyles :: [(Int, [Doc])] + , stTextStyles :: Map.Map (Set.Set TextStyle) (String, Doc) + , stTextStyleAttr :: Set.Set TextStyle + , stIndentPara :: Int + , stInDefinition :: Bool + , stTight :: Bool + , stFirstPara :: Bool + , stImageId :: Int + , stTableCaptionId :: Int + , stImageCaptionId :: Int } defaultWriterState :: WriterState defaultWriterState = - WriterState { stNotes = [] - , stTableStyles = [] - , stParaStyles = [] - , stListStyles = [] - , stTextStyles = Map.empty - , stTextStyleAttr = Set.empty - , stIndentPara = 0 - , stInDefinition = False - , stTight = False - , stFirstPara = False - , stImageId = 1 + WriterState { stNotes = [] + , stTableStyles = [] + , stParaStyles = [] + , stListStyles = [] + , stTextStyles = Map.empty + , stTextStyleAttr = Set.empty + , stIndentPara = 0 + , stInDefinition = False + , stTight = False + , stFirstPara = False + , stImageId = 1 + , stTableCaptionId = 1 + , stImageCaptionId = 1 } when :: Bool -> Doc -> Doc @@ -408,7 +412,7 @@ blockToOpenDocument o bs mapM_ addParaStyle . newPara $ paraHStyles ++ paraStyles captionDoc <- if null c then return empty - else withParagraphStyle o "Table" [Para c] + else inlinesToOpenDocument o c >>= numberedTableCaption th <- if all null h then return empty else colHeadsToOpenDocument o (map fst paraHStyles) h @@ -419,22 +423,35 @@ blockToOpenDocument o bs figure attr caption source title | null caption = withParagraphStyle o "Figure" [Para [Image attr caption (source,title)]] | otherwise = do - id' <- gets stImageId imageDoc <- withParagraphStyle o "FigureWithCaption" [Para [Image attr caption (source,title)]] - captionDoc <- inlinesToOpenDocument o caption >>= numberedFigureCaption id' + captionDoc <- inlinesToOpenDocument o caption >>= numberedFigureCaption return $ imageDoc $$ captionDoc -numberedFigureCaption :: PandocMonad m => Int -> Doc -> OD m Doc -numberedFigureCaption num caption = do - figterm <- translateTerm Figure - let t = text figterm - let r = num - 1 - let s = inTags False "text:sequence" [ ("text:ref-name", "refIllustration" ++ show r), - ("text:name", "Illustration"), - ("text:formula", "ooow:Illustration+1"), + +numberedTableCaption :: PandocMonad m => Doc -> OD m Doc +numberedTableCaption caption = do + id' <- gets stTableCaptionId + modify (\st -> st{ stTableCaptionId = id' + 1 }) + capterm <- translateTerm Term.Table + return $ numberedCaption "Table" capterm "Table" id' caption + +numberedFigureCaption :: PandocMonad m => Doc -> OD m Doc +numberedFigureCaption caption = do + id' <- gets stImageCaptionId + modify (\st -> st{ stImageCaptionId = id' + 1 }) + capterm <- translateTerm Term.Figure + return $ numberedCaption "FigureCaption" capterm "Illustration" id' caption + +numberedCaption :: String -> String -> String -> Int -> Doc -> Doc +numberedCaption style term name num caption = + let t = text term + r = num - 1 + s = inTags False "text:sequence" [ ("text:ref-name", "ref" ++ name ++ show r), + ("text:name", name), + ("text:formula", "ooow:" ++ name ++ "+1"), ("style:num-format", "1") ] $ text $ show num - let c = text ": " - return $ inParagraphTagsWithStyle "FigureCaption" $ hcat [ t, text " ", s, c, caption ] + c = text ": " + in inParagraphTagsWithStyle style $ hcat [ t, text " ", s, c, caption ] colHeadsToOpenDocument :: PandocMonad m => WriterOptions -> [String] -> [[Block]] diff --git a/test/tables.opendocument b/test/tables.opendocument index c04afd492..5c68476b8 100644 --- a/test/tables.opendocument +++ b/test/tables.opendocument @@ -63,7 +63,8 @@ -Demonstration of simple table syntax. +Table 1: Demonstration +of simple table syntax. Simple table without caption: @@ -196,7 +197,8 @@ spaces: -Demonstration of simple table syntax. +Table 2: Demonstration +of simple table syntax. Multiline table with caption: @@ -251,8 +253,8 @@ caption: -Here’s the caption. It may span multiple -lines. +Table 3: Here’s +the caption. It may span multiple lines. Multiline table without caption: -- cgit v1.2.3