git.iko.soy/typeable/pandoc
on master help sign in
Powerpoint code formatting is now context dependent (#5573)

This commit alters the way in which the Powerpoint writer treats inline code and code blocks. - Inline code is now formatted at the same size as the surrounding text. - Code blocks are now given a margin and font size according to their level. - Furthermore this commit allows changing the font with which code is formatted via the `monofont` option. Tested in - PowerPoint 365 for Windows - 1808 (Build 10730.20344 Click-to-Run) - PowerPoint 365 for Mac - 16.26 (19060901)

commite67f4c58f2cbe0a0fc5f73d2e726e6c0a403bbea authorJeroen de Haas <jeroendehaas@users.noreply.github.com> dateFri 14 Jun 2019, 17:42 (7 years ago) parent2cd1c7b
9 files changed+91−21
MANUAL.txt+9 src/Text/Pandoc/Writers/Powerpoint/Output.hs+29−9 src/Text/Pandoc/Writers/Powerpoint/Presentation.hs+24−12 test/Tests/Writers/Powerpoint.hs+8 test/pptx/code-custom.pptx test/pptx/code-custom_templated.pptx test/pptx/code.native+21 test/pptx/code.pptx test/pptx/code_templated.pptx
MANUAL.txt+9
@@ -1557,6 +1557,15 @@ These variables change the appearance of PDF slides using [`beamer`].
15571557`titlegraphic`
15581558: image for title slide
15591559
1560+Variables for PowerPoint slide shows
1561+--------------------------------------
1562+
1563+These variables control the visual aspects of a slide show that are not easily
1564+controled via templates.
1565+
1566+`monofont`
1567+: font to use for code.
1568+
15601569Variables for LaTeX
15611570-------------------
15621571
src/Text/Pandoc/Writers/Powerpoint/Output.hs+29−9
@@ -48,6 +48,14 @@ import Text.Pandoc.Writers.Math (convertMath)
4848import Text.Pandoc.Writers.Powerpoint.Presentation
4949import Skylighting (fromColor)
5050
51+-- |The 'EMU' type is used to specify sizes in English Metric Units.
52+type EMU = Integer
53+
54+-- |The 'pixelsToEmu' function converts a size in pixels to one
55+-- in English Metric Units. It assumes a DPI of 72.
56+pixelsToEmu :: Pixels -> EMU
57+pixelsToEmu = (12700 *)
58+
5159-- This populates the global ids map with images already in the
5260-- template, so the ids won't be used by images introduced by the
5361-- user.
@@ -148,6 +156,18 @@ runP env st p = evalStateT (runReaderT p env) st
148156
149157--------------------------------------------------------------------
150158
159+monospaceFont :: Monad m => P m String
160+monospaceFont = do
161+ vars <- writerVariables <$> asks envOpts
162+ case lookup "monofont" vars of
163+ Just s -> return s
164+ Nothing -> return "Courier"
165+
166+fontSizeAttributes :: Monad m => RunProps -> P m [(String, String)]
167+fontSizeAttributes RunProps { rPropForceSize = Just sz } =
168+ return [("sz", (show $ sz * 100))]
169+fontSizeAttributes _ = return []
170+
151171copyFileToArchive :: PandocMonad m => Archive -> FilePath -> P m Archive
152172copyFileToArchive arch fp = do
153173 refArchive <- asks envRefArchive
@@ -721,13 +741,8 @@ makePicElements layout picProps mInfo alt = do
721741paraElemToElements :: PandocMonad m => ParaElem -> P m [Element]
722742paraElemToElements Break = return [mknode "a:br" [] ()]
723743paraElemToElements (Run rpr s) = do
724− let sizeAttrs = case rPropForceSize rpr of
725− Just n -> [("sz", (show $ n * 100))]
726− Nothing -> if rPropCode rpr
727− -- hardcoded size for code for now
728− then [("sz", "1800")]
729− else []
730− attrs = sizeAttrs ++
744+ sizeAttrs <- fontSizeAttributes rpr
745+ let attrs = sizeAttrs ++
731746 (if rPropBold rpr then [("b", "1")] else []) ++
732747 (if rPropItalics rpr then [("i", "1")] else []) ++
733748 (if rPropUnderline rpr then [("u", "sng")] else []) ++
@@ -773,8 +788,9 @@ paraElemToElements (Run rpr s) = do
773788 ]
774789 _ -> []
775790 Nothing -> []
791+ codeFont <- monospaceFont
776792 let codeContents = if rPropCode rpr
777− then [mknode "a:latin" [("typeface", "Courier")] ()]
793+ then [mknode "a:latin" [("typeface", codeFont)] ()]
778794 else []
779795 let propContents = linkProps ++ colorContents ++ codeContents
780796 return [mknode "a:r" [] [ mknode "a:rPr" attrs $ propContents
@@ -821,7 +837,11 @@ paragraphToElement par = do
821837 let
822838 attrs = [("lvl", show $ pPropLevel $ paraProps par)] ++
823839 (case pPropMarginLeft (paraProps par) of
824− Just px -> [("marL", show $ 12700 * px), ("indent", "0")]
840+ Just px -> [("marL", show $ pixelsToEmu px)]
841+ Nothing -> []
842+ ) ++
843+ (case pPropIndent (paraProps par) of
844+ Just px -> [("indent", show $ pixelsToEmu px)]
825845 Nothing -> []
826846 ) ++
827847 (case pPropAlign (paraProps par) of
src/Text/Pandoc/Writers/Powerpoint/Presentation.hs+24−12
@@ -33,6 +33,7 @@ module Text.Pandoc.Writers.Powerpoint.Presentation ( documentToPresentation
3333 , TableProps(..)
3434 , Strikethrough(..)
3535 , Capitals(..)
36+ , Pixels
3637 , PicProps(..)
3738 , URL
3839 , TeXString(..)
@@ -226,6 +227,7 @@ data ParaProps = ParaProps { pPropMarginLeft :: Maybe Pixels
226227 , pPropBullet :: Maybe BulletType
227228 , pPropAlign :: Maybe Algnment
228229 , pPropSpaceBefore :: Maybe Pixels
230+ , pPropIndent :: Maybe Pixels
229231 } deriving (Show, Eq)
230232
231233instance Default ParaProps where
@@ -235,6 +237,7 @@ instance Default ParaProps where
235237 , pPropBullet = Nothing
236238 , pPropAlign = Nothing
237239 , pPropSpaceBefore = Nothing
240+ , pPropIndent = Just 0
238241 }
239242
240243newtype TeXString = TeXString {unTeXString :: String}
@@ -411,18 +414,23 @@ blockToParagraphs (LineBlock ilsList) = do
411414 pProps <- asks envParaProps
412415 return [Paragraph pProps parElems]
413416-- TODO: work out the attributes
414−blockToParagraphs (CodeBlock attr str) =
415− local (\r -> r{ envParaProps = def{pPropMarginLeft = Just 100}
417+blockToParagraphs (CodeBlock attr str) = do
418+ pProps <- asks envParaProps
419+ local (\r -> r{ envParaProps = def{ pPropMarginLeft = Nothing
420+ , pPropBullet = Nothing
421+ , pPropLevel = pPropLevel pProps
422+ , pPropIndent = Just 0
423+ }
416424 , envRunProps = (envRunProps r){rPropCode = True}}) $ do
417− mbSty <- writerHighlightStyle <$> asks envOpts
418− synMap <- writerSyntaxMap <$> asks envOpts
419− case mbSty of
420− Just sty ->
421− case highlight synMap (formatSourceLines sty) attr str of
422− Right pElems -> do pProps <- asks envParaProps
423− return [Paragraph pProps pElems]
424− Left _ -> blockToParagraphs $ Para [Str str]
425− Nothing -> blockToParagraphs $ Para [Str str]
425+ mbSty <- writerHighlightStyle <$> asks envOpts
426+ synMap <- writerSyntaxMap <$> asks envOpts
427+ case mbSty of
428+ Just sty ->
429+ case highlight synMap (formatSourceLines sty) attr str of
430+ Right pElems -> do pPropsNew <- asks envParaProps
431+ return [Paragraph pPropsNew pElems]
432+ Left _ -> blockToParagraphs $ Para [Str str]
433+ Nothing -> blockToParagraphs $ Para [Str str]
426434-- We can't yet do incremental lists, but we should render a
427435-- (BlockQuote List) as a list to maintain compatibility with other
428436-- formats.
@@ -431,7 +439,9 @@ blockToParagraphs (BlockQuote (blk : blks)) | isListType blk = do
431439 ps' <- blockToParagraphs $ BlockQuote blks
432440 return $ ps ++ ps'
433441blockToParagraphs (BlockQuote blks) =
434− local (\r -> r{ envParaProps = (envParaProps r){pPropMarginLeft = Just 100}
442+ local (\r -> r{ envParaProps = (envParaProps r){ pPropMarginLeft = Just 100
443+ , pPropIndent = Just 0
444+ }
435445 , envRunProps = (envRunProps r){rPropForceSize = Just blockQuoteSize}})$
436446 concatMapM blockToParagraphs blks
437447-- TODO: work out the format
@@ -455,6 +465,7 @@ blockToParagraphs (BulletList blksLst) = do
455465 , envParaProps = pProps{ pPropLevel = lvl + 1
456466 , pPropBullet = Just Bullet
457467 , pPropMarginLeft = Nothing
468+ , pPropIndent = Nothing
458469 }}) $
459470 concatMapM multiParBullet blksLst
460471blockToParagraphs (OrderedList listAttr blksLst) = do
@@ -464,6 +475,7 @@ blockToParagraphs (OrderedList listAttr blksLst) = do
464475 , envParaProps = pProps{ pPropLevel = lvl + 1
465476 , pPropBullet = Just (AutoNumbering listAttr)
466477 , pPropMarginLeft = Nothing
478+ , pPropIndent = Nothing
467479 }}) $
468480 concatMapM multiParBullet blksLst
469481blockToParagraphs (DefinitionList entries) = do
test/Tests/Writers/Powerpoint.hs+8
@@ -115,4 +115,12 @@ tests = groupPptxTests [ pptxTests "Inline formatting"
115115 def
116116 "pptx/document-properties-short-desc.native"
117117 "pptx/document-properties-short-desc.pptx"
118+ , pptxTests "inline code and code blocks"
119+ def
120+ "pptx/code.native"
121+ "pptx/code.pptx"
122+ , pptxTests "inline code and code blocks, custom formatting"
123+ def { writerVariables = [("monofont", "Consolas")] }
124+ "pptx/code.native"
125+ "pptx/code-custom.pptx"
118126 ]
test/pptx/code-custom.pptxnew file

Binary file not shown.

test/pptx/code-custom_templated.pptxnew file

Binary file not shown.

test/pptx/code.nativenew file+21
@@ -0,0 +1,21 @@
1+[Header 1 ("header-with-inline-code",[],[]) [Str "Header",Space,Str "with",Space,Code ("",[],[]) "inline code"]
2+,CodeBlock ("",[],[]) "Code at level 0"
3+,BulletList
4+ [[Para [Str "Bullet",Space,Str "item",Space,Str "with",Space,Code ("",[],[]) "inline code"]
5+ ,CodeBlock ("",[],[]) "Code block at level 1"
6+ ,BulletList
7+ [[Para [Str "with",Space,Code ("",[],[]) "nested"]
8+ ,CodeBlock ("",[],[]) "lvl2\nlvl2\nlvl2"
9+ ,Header 2 ("second-heading-level-with-code",[],[]) [Str "Second",Space,Str "heading",Space,Str "level",Space,Str "with",Space,Code ("",[],[]) "code"]]]]]
10+,Header 1 ("syntax-highlighting",[],[]) [Str "Syntax",Space,Str "highlighting"]
11+,CodeBlock ("",["haskell"],[]) "id :: a -> a\nid x = x"
12+,BulletList
13+ [[Para [Str "Nested"]
14+ ,CodeBlock ("",["haskell"],[]) "g :: Int -> Int\ng x = x * 3"]]
15+,Header 1 ("two-column-slide",[],[]) [Str "Two",Space,Str "column",Space,Str "slide"]
16+,Div ("",["columns"],[])
17+ [Div ("",["column"],[("width","50%")])
18+ [BulletList
19+ [[Plain [Str "A",Space,Str "total",Space,Str "alternative",Space,Str "for",Space,Code ("",[],[]) "head"]]]]
20+ ,Div ("",["column"],[("width","50%")])
21+ [CodeBlock ("",[],[]) "safeHead :: [a] -> Maybe a\nsafeHead [] = Nothing\nsafeHead (x:_) = Just x"]]]
test/pptx/code.pptxnew file

Binary file not shown.

test/pptx/code_templated.pptxnew file

Binary file not shown.