git.iko.soy/typeable/pandoc
on master help sign in
Shared: add filterIpynbOutput. [API change]

Add command line option `--ipynb-output=all|none|best`. Closes #5339.

commit77faccb505992c944cd1b92f50e4e00d2927682b authorJohn MacFarlane <jgm@berkeley.edu> dateThu 28 Feb 2019, 20:28 (8 years ago) parent7aeabd8
5 files changed+71−10
MANUAL.txt+9 src/Text/Pandoc/App.hs+21−8 src/Text/Pandoc/App/CommandLineOptions.hs+11−1 src/Text/Pandoc/App/Opt.hs+2 src/Text/Pandoc/Shared.hs+28−1
MANUAL.txt+9
@@ -1192,6 +1192,15 @@ Options affecting specific writers {.options}
11921192 the EPUB-specific contents. The default is `EPUB`. To put
11931193 the EPUB contents in the top level, use an empty string.
11941194
1195+`--ipynb-output=all|none|best`
1196+
1197+: Determines how ipynb output cells are treated. `all` means
1198+ that all of the data formats included in the original are
1199+ preserved. `none` means that the contents of data cells
1200+ are omitted. `best` causes pandoc to try to pick the
1201+ richest data block in each output cell that is compatible
1202+ with the output format. The default is `best`.
1203+
11951204`--pdf-engine=`*PROGRAM*
11961205
11971206: Use the specified engine when producing PDF output.
src/Text/Pandoc/App.hs+21−8
@@ -73,7 +73,7 @@ import Text.Pandoc.PDF (makePDF)
7373import Text.Pandoc.Readers.Markdown (yamlToMeta)
7474import Text.Pandoc.SelfContained (makeDataURI, makeSelfContained)
7575import Text.Pandoc.Shared (eastAsianLineBreakFilter, stripEmptyParagraphs,
76− headerShift, isURI, tabFilter, uriPathToPath)
76+ headerShift, isURI, tabFilter, uriPathToPath, filterIpynbOutput)
7777import qualified Text.Pandoc.UTF8 as UTF8
7878#ifndef _WINDOWS
7979import System.Posix.IO (stdOutput)
@@ -247,8 +247,20 @@ convertWithOpts opts = do
247247 (writerExtensions writerOptions) &&
248248 writerWrapText writerOptions == WrapPreserve)
249249 then (eastAsianLineBreakFilter :)
250− else id) $
251− []
250+ else id) .
251+ (case optIpynbOutput opts of
252+ "all" -> id
253+ "none" -> (filterIpynbOutput Nothing :)
254+ "best" -> (filterIpynbOutput (Just $
255+ if htmlFormat writerName
256+ then Format "html"
257+ else
258+ case writerName of
259+ "latex" -> Format "latex"
260+ "beamer" -> Format "latex"
261+ _ -> Format writerName) :)
262+ _ -> id) -- should not happen
263+ $ []
252264
253265 let sourceToDoc :: [FilePath] -> PandocIO Pandoc
254266 sourceToDoc sources' =
@@ -293,15 +305,12 @@ convertWithOpts opts = do
293305 TL.unpack (TE.decodeUtf8With TE.lenientDecode err')
294306
295307 Nothing -> do
296− let htmlFormat = format `elem`
297− ["html","html4","html5","s5","slidy",
298− "slideous","dzslides","revealjs"]
299− addNl = if standalone
308+ let addNl = if standalone
300309 then id
301310 else (<> T.singleton '\n')
302311 output <- addNl <$> f writerOptions doc
303312 writerFn eol outputFile =<<
304− if optSelfContained opts && htmlFormat
313+ if optSelfContained opts && htmlFormat writerName
305314 -- TODO not maximally efficient; change type
306315 -- of makeSelfContained so it works w/ Text
307316 then T.pack <$> makeSelfContained (T.unpack output)
@@ -309,6 +318,10 @@ convertWithOpts opts = do
309318
310319type Transform = Pandoc -> Pandoc
311320
321+htmlFormat :: String -> Bool
322+htmlFormat = (`elem` ["html","html4","html5","s5","slidy",
323+ "slideous","dzslides","revealjs"])
324+
312325isTextFormat :: String -> Bool
313326isTextFormat s = s `notElem` ["odt","docx","epub2","epub3","epub","pptx"]
314327
src/Text/Pandoc/App/CommandLineOptions.hs+11−1
@@ -643,7 +643,17 @@ options =
643643 "NUMBER")
644644 "" -- "Header level at which to split chapters in EPUB"
645645
646− , Option "" ["pdf-engine"]
646+ , Option "" ["ipynb-output"]
647+ (ReqArg
648+ (\arg opt ->
649+ if arg `notElem` ["all","none","best"]
650+ then E.throwIO $ PandocOptionError $
651+ "ipynb-output must be all, none, or best"
652+ else return opt { optIpynbOutput = arg })
653+ "all|none|best")
654+ "" -- "Starting number for sections, subsections, etc."
655+
656+ , Option "" ["pdf-engine"]
647657 (ReqArg
648658 (\arg opt -> do
649659 let b = takeBaseName arg
src/Text/Pandoc/App/Opt.hs+2
@@ -123,6 +123,7 @@ data Opt = Opt
123123 , optFileScope :: Bool -- ^ Parse input files before combining
124124 , optTitlePrefix :: Maybe String -- ^ Prefix for title
125125 , optCss :: [FilePath] -- ^ CSS files to link to
126+ , optIpynbOutput :: String -- ^ Maybe f to use best data; Nothing to omit
126127 , optIncludeBeforeBody :: [FilePath] -- ^ Files to include before
127128 , optIncludeAfterBody :: [FilePath] -- ^ Files to include after body
128129 , optIncludeInHeader :: [FilePath] -- ^ Files to include in header
@@ -196,6 +197,7 @@ defaultOpts = Opt
196197 , optFileScope = False
197198 , optTitlePrefix = Nothing
198199 , optCss = []
200+ , optIpynbOutput = "best"
199201 , optIncludeBeforeBody = []
200202 , optIncludeAfterBody = []
201203 , optIncludeInHeader = []
src/Text/Pandoc/Shared.hs+28−1
@@ -86,6 +86,7 @@ module Text.Pandoc.Shared (
8686 eastAsianLineBreakFilter,
8787 underlineSpan,
8888 splitSentences,
89+ filterIpynbOutput,
8990 -- * TagSoup HTML handling
9091 renderTags',
9192 -- * File handling
@@ -122,12 +123,13 @@ import Data.Char (isAlpha, isLower, isSpace, isUpper, toLower, isAlphaNum,
122123 generalCategory, GeneralCategory(NonSpacingMark,
123124 SpacingCombiningMark, EnclosingMark, ConnectorPunctuation))
124125import Data.Data (Data, Typeable)
125−import Data.List (find, intercalate, intersperse, stripPrefix)
126+import Data.List (find, intercalate, intersperse, stripPrefix, sortBy)
126127import qualified Data.Map as M
127128import Data.Maybe (mapMaybe)
128129import Data.Sequence (ViewL (..), ViewR (..), viewl, viewr)
129130import qualified Data.Set as Set
130131import qualified Data.Text as T
132+import Data.Ord (comparing)
131133import Data.Version (showVersion)
132134import Network.URI (URI (uriScheme), escapeURIString, parseURI)
133135import Paths_pandoc (version)
@@ -689,6 +691,31 @@ splitSentences xs =
689691 let (sent, rest) = breakSentence xs
690692 in if null rest then [sent] else sent : splitSentences rest
691693
694+-- | Process ipynb output cells. If mode is Nothing,
695+-- remove all output. If mode is Just format, select
696+-- best output for the format.
697+filterIpynbOutput :: Maybe Format -> Pandoc -> Pandoc
698+filterIpynbOutput mode = walk go
699+ where go (Div (ident, ("output":os), kvs) bs) =
700+ case mode of
701+ Nothing -> Div (ident, ("output":os), kvs) []
702+ Just fmt -> Div (ident, ("output":os), kvs) $
703+ take 1 $ sortBy (comparing rank) bs
704+ where
705+ rank (RawBlock (Format "html") _)
706+ | fmt == Format "html" = (1 :: Int)
707+ | fmt == Format "markdown" = 2
708+ | otherwise = 3
709+ rank (RawBlock (Format "latex") _)
710+ | fmt == Format "latex" = 1
711+ | fmt == Format "markdown" = 2
712+ | otherwise = 3
713+ rank (RawBlock f _)
714+ | fmt == f = 1
715+ | otherwise = 3
716+ rank _ = 2
717+ go x = x
718+
692719--
693720-- TagSoup HTML handling
694721--