git.iko.soy/typeable/pandoc
on master help sign in
improve formatting of formulas in OpenDocument
commit19bab48dccd670e272a6f949321de5413eb96136 authoroltolm <oleg.tolmatcev@gmail.com> dateSat 30 Aug 2014, 22:57 (12 years ago) parent7e8cfc0
3 files changed+51−9
src/Text/Pandoc/Writers/ODT.hs+30−8 src/Text/Pandoc/Writers/OpenDocument.hs+19−1 test/writer.opendocument+2
src/Text/Pandoc/Writers/ODT.hs+30−8
@@ -56,7 +56,7 @@ import Text.Pandoc.XML
5656import Text.TeXMath
5757import Text.XML.Light
5858
59−data ODTState = ODTState { stEntries :: [Entry]
59+newtype ODTState = ODTState { stEntries :: [Entry]
6060 }
6161
6262type O m = StateT ODTState m
@@ -224,17 +224,39 @@ transformPicMath _ (Math t math) = do
224224 let dirname = "Formula-" ++ show (length entries) ++ "/"
225225 let fname = dirname ++ "content.xml"
226226 let entry = toEntry fname epochtime (fromStringLazy mathml)
227− modify $ \st -> st{ stEntries = entry : entries }
227+ let fname' = dirname ++ "settings.xml"
228+ let entry' = toEntry fname' epochtime $ documentSettings (t == InlineMath)
229+ modify $ \st -> st{ stEntries = entry' : (entry : entries) }
228230 return $ RawInline (Format "opendocument") $ render Nothing $
229− inTags False "draw:frame" [("text:anchor-type",
230− if t == DisplayMath
231− then "paragraph"
232− else "as-char")
233− ,("style:vertical-pos", "middle")
234− ,("style:vertical-rel", "text")] $
231+ inTags False "draw:frame" (if t == DisplayMath
232+ then [("draw:style-name","fr2")
233+ -- `draw:frame` does not support either
234+ -- `style:vertical-pos` or `style:vertical-rel`,
235+ -- therefore those attributes must go into the
236+ -- `style:style` element
237+ ,("text:anchor-type","paragraph")]
238+ else [("draw:style-name","fr1")
239+ ,("text:anchor-type","as-char")]) $
235240 selfClosingTag "draw:object" [("xlink:href", dirname)
236241 , ("xlink:type", "simple")
237242 , ("xlink:show", "embed")
238243 , ("xlink:actuate", "onLoad")]
239244
240245transformPicMath _ x = return x
246+
247+documentSettings :: Bool -> B.ByteString
248+documentSettings isTextMode = fromStringLazy $ render Nothing
249+ $ text "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
250+ $$
251+ (inTags True "office:document-settings"
252+ [("xmlns:office","urn:oasis:names:tc:opendocument:xmlns:office:1.0")
253+ ,("xmlns:xlink","http://www.w3.org/1999/xlink")
254+ ,("xmlns:config","urn:oasis:names:tc:opendocument:xmlns:config:1.0")
255+ ,("xmlns:ooo","http://openoffice.org/2004/office")
256+ ,("office:version","1.2")] $
257+ inTagsSimple "office:settings" $
258+ inTags False "config:config-item-set"
259+ [("config:name", "ooo:configuration-settings")] $
260+ inTags False "config:config-item" [("config:name", "IsTextMode")
261+ ,("config:type", "boolean")] $
262+ text $ if isTextMode then "true" else "false")
src/Text/Pandoc/Writers/OpenDocument.hs+19−1
@@ -173,6 +173,24 @@ inTextStyle d = do
173173 return $ inTags False
174174 "text:span" [("text:style-name",styleName)] d
175175
176+formulaStyles :: [Doc]
177+formulaStyles = [formulaStyle InlineMath, formulaStyle DisplayMath]
178+
179+formulaStyle :: MathType -> Doc
180+formulaStyle mt = inTags False "style:style"
181+ [("style:name", if mt == InlineMath then "fr1" else "fr2")
182+ ,("style:family", "graphic")
183+ ,("style:parent-style-name", "Formula")]
184+ $ selfClosingTag "style:graphic-properties" $ if mt == InlineMath then
185+ [("style:vertical-pos", "middle")
186+ ,("style:vertical-rel", "text")]
187+ else
188+ [("style:vertical-pos", "middle")
189+ ,("style:vertical-rel", "paragraph-content")
190+ ,("style:horizontal-pos", "center")
191+ ,("style:horizontal-rel", "paragraph-content")
192+ ,("style:wrap", "none")]
193+
176194inHeaderTags :: PandocMonad m => Int -> Doc -> OD m Doc
177195inHeaderTags i d =
178196 return $ inTags False "text:h" [ ("text:style-name", "Heading_20_" ++ show i)
@@ -211,7 +229,7 @@ writeOpenDocument opts (Pandoc meta blocks) = do
211229 meta
212230 b <- render' `fmap` blocksToOpenDocument opts blocks
213231 return (b, m)
214− let styles = stTableStyles s ++ stParaStyles s ++
232+ let styles = stTableStyles s ++ stParaStyles s ++ formulaStyles ++
215233 map snd (sortBy (flip (comparing fst)) (
216234 Map.elems (stTextStyles s)))
217235 listStyle (n,l) = inTags True "text:list-style"
test/writer.opendocument+2
@@ -639,6 +639,8 @@
639639 <style:style style:name="T6" style:family="text"><style:text-properties style:text-position="super 58%" /></style:style>
640640 <style:style style:name="T7" style:family="text"><style:text-properties fo:font-style="italic" style:font-style-asian="italic" style:font-style-complex="italic" style:text-position="super 58%" /></style:style>
641641 <style:style style:name="T8" style:family="text"><style:text-properties style:text-position="sub 58%" /></style:style>
642+ <style:style style:name="fr2" style:family="graphic" style:parent-style-name="Formula"><style:graphic-properties style:vertical-pos="middle" style:vertical-rel="paragraph-content" style:horizontal-pos="center" style:horizontal-rel="paragraph-content" style:wrap="none" /></style:style>
643+ <style:style style:name="fr1" style:family="graphic" style:parent-style-name="Formula"><style:graphic-properties style:vertical-pos="middle" style:vertical-rel="text" /></style:style>
642644 <style:style style:name="P1" style:family="paragraph" style:parent-style-name="Quotations">
643645 <style:paragraph-properties fo:margin-left="0.5in" fo:margin-right="0in" fo:text-indent="0in" style:auto-text-indent="false" />
644646 </style:style>