From 736a38419f49d4b96bd845f5767a028df59e4cff Mon Sep 17 00:00:00 2001 From: Serge Krot Date: Thu, 6 Dec 2018 19:11:58 +0100 Subject: sw: DOCX: recognize TOC title during import Change-Id: Ifa4fb59858d61580f76e3d104aa4caa6b5902d1b Reviewed-on: https://gerrit.libreoffice.org/64735 Tested-by: Jenkins Reviewed-by: Thorsten Behrens tdf#121561: sw: DOCX: add std/stdPr/stdContent around TOC During export into DOCX from ODT we need to do it because in this case the TOC title will be recognized inside MS Word as part of the TOC. Later we could add support of these keywords in LO import in order to detect TOC title from DOCX input. Added unit test for export. Change-Id: I7135e91dc04d4c0501e6074a046fc473e041f014 Reviewed-on: https://gerrit.libreoffice.org/63786 Tested-by: Jenkins Reviewed-by: Thorsten Behrens Reviewed-on: https://gerrit.libreoffice.org/66307 Reviewed-by: Michael Stahl --- sw/source/filter/ww8/wrtw8nds.cxx | 96 +++++++++++++++++++++++++++++++++++++++ sw/source/filter/ww8/wrtww8.hxx | 1 + 2 files changed, 97 insertions(+) (limited to 'sw/source/filter') diff --git a/sw/source/filter/ww8/wrtw8nds.cxx b/sw/source/filter/ww8/wrtw8nds.cxx index c1d94eda7f6c..f147adce2078 100644 --- a/sw/source/filter/ww8/wrtw8nds.cxx +++ b/sw/source/filter/ww8/wrtw8nds.cxx @@ -91,6 +91,8 @@ #include #include #include +#include +#include #include "sprmids.hxx" @@ -3083,7 +3085,101 @@ void MSWordExportBase::OutputSectionNode( const SwSectionNode& rSectionNode ) } } if ( TOX_CONTENT_SECTION == rSection.GetType() ) + { m_bStartTOX = true; + UpdateTocSectionNodeProperties(rSectionNode); + } +} + +// tdf#121561: During export of the ODT file with TOC inside into DOCX format, +// the TOC title is being exported as regular paragraph. We should surround it +// with to make it (TOC title) recognizable +// by MS Word as part of the TOC. +void MSWordExportBase::UpdateTocSectionNodeProperties(const SwSectionNode& rSectionNode) +{ + // check section type + { + const SwSection& rSection = rSectionNode.GetSection(); + if (TOX_CONTENT_SECTION != rSection.GetType()) + return; + + const SwTOXBase* pTOX = rSection.GetTOXBase(); + if (pTOX) + { + TOXTypes type = pTOX->GetType(); + if (type != TOXTypes::TOX_CONTENT) + return; + } + } + + // get section node, skip toc-header node + const SwSectionNode* pSectNd = &rSectionNode; + { + SwNodeIndex aIdxNext( *pSectNd, 1 ); + const SwNode& rNdNext = aIdxNext.GetNode(); + + if (rNdNext.IsSectionNode()) + { + const SwSectionNode* pSectNdNext = static_cast(&rNdNext); + if (TOX_HEADER_SECTION == pSectNdNext->GetSection().GetType() && + pSectNdNext->StartOfSectionNode()->IsSectionNode()) + { + pSectNd = pSectNdNext; + } + } + } + + // get node of the first paragraph inside TOC + SwNodeIndex aIdxNext( *pSectNd, 1 ); + const SwNode& rNdTocPara = aIdxNext.GetNode(); + const SwContentNode* pNode = rNdTocPara.GetContentNode(); + if (!pNode) + return; + + // put required flags into grab bag of the first node in TOC + { + uno::Sequence aDocPropertyValues(comphelper::InitPropertySequence( + { + {"ooxml:CT_SdtDocPart_docPartGallery", uno::makeAny(OUString("Table of Contents"))}, + {"ooxml:CT_SdtDocPart_docPartUnique", uno::makeAny(OUString("true"))}, + })); + + uno::Sequence aSdtPrPropertyValues(comphelper::InitPropertySequence( + { + {"ooxml:CT_SdtPr_docPartObj", uno::makeAny(aDocPropertyValues)}, + })); + + SfxGrabBagItem aGrabBag(RES_PARATR_GRABBAG); + aGrabBag.GetGrabBag()["SdtPr"] <<= aSdtPrPropertyValues; + + // create temp attr set + SwAttrSet aSet(pNode->GetSwAttrSet()); + aSet.Put(aGrabBag); + + // set new attr to node + const_cast(pNode)->SetAttr(aSet); + } + + // set flag for the next node after TOC + // in order to indicate that std area has been finished + // see, DomainMapper::lcl_startParagraphGroup() for the same functionality during load + { + SwNodeIndex aEndTocNext( *rSectionNode.EndOfSectionNode(), 1 ); + const SwNode& rEndTocNextNode = aEndTocNext.GetNode(); + const SwContentNode* pNodeAfterToc = rEndTocNextNode.GetContentNode(); + if (pNodeAfterToc) + { + SfxGrabBagItem aGrabBag(RES_PARATR_GRABBAG); + aGrabBag.GetGrabBag()["ParaSdtEndBefore"] <<= true; + + // create temp attr set + SwAttrSet aSet(pNodeAfterToc->GetSwAttrSet()); + aSet.Put(aGrabBag); + + // set new attr to node + const_cast(pNodeAfterToc)->SetAttr(aSet); + } + } } void WW8Export::AppendSection( const SwPageDesc *pPageDesc, const SwSectionFormat* pFormat, sal_uLong nLnNum ) diff --git a/sw/source/filter/ww8/wrtww8.hxx b/sw/source/filter/ww8/wrtww8.hxx index 8a911bd381e9..ec31a1b41622 100644 --- a/sw/source/filter/ww8/wrtww8.hxx +++ b/sw/source/filter/ww8/wrtww8.hxx @@ -847,6 +847,7 @@ protected: /// Output SwSectionNode void OutputSectionNode( const SwSectionNode& ); + static void UpdateTocSectionNodeProperties(const SwSectionNode& rSectionNode); virtual void AppendSection( const SwPageDesc *pPageDesc, const SwSectionFormat* pFormat, sal_uLong nLnNum ) = 0; -- cgit v1.2.3