Add raw_markdown extension affecting ipynb reader.

Specifying `-f ipynb+raw_markdown` will cause Markdown cells
to be represented as raw Markdown blocks, instead of being
parsed.  This is not what you want when going from `ipynb`
to other formats, but it may be useful when going from `ipynb`
to Markdown or to `ipynb`, to avoid semantically insignificant
changes in the contents of the Markdown cells that might
otherwise be introduced.

Closes #5408.
This commit is contained in:
John MacFarlane 2020-07-23 17:47:02 -07:00
parent ac1f45c553
commit 48fb6d947d
4 changed files with 18 additions and 4 deletions

View file

@ -2910,6 +2910,14 @@ input formats
In the `muse` input format, this enables Text::Amuse In the `muse` input format, this enables Text::Amuse
extensions to Emacs Muse markup. extensions to Emacs Muse markup.
#### Extension: `raw_markdown` ####
In the `ipynb` input format, this causes Markdown cells
to be included as raw Markdown blocks (allowing lossless
round-tripping) rather than being parsed. Use this only
when you are targetting `ipynb` or a markdown-based
output format.
#### Extension: `citations` {#org-citations} #### Extension: `citations` {#org-citations}
Some aspects of [Pandoc's Markdown citation syntax](#citations) are also accepted Some aspects of [Pandoc's Markdown citation syntax](#citations) are also accepted

View file

@ -133,6 +133,7 @@ data Extension =
| Ext_raw_attribute -- ^ Allow explicit raw blocks/inlines | Ext_raw_attribute -- ^ Allow explicit raw blocks/inlines
| Ext_raw_html -- ^ Allow raw HTML | Ext_raw_html -- ^ Allow raw HTML
| Ext_raw_tex -- ^ Allow raw TeX (other than math) | Ext_raw_tex -- ^ Allow raw TeX (other than math)
| Ext_raw_markdown -- ^ Parse markdown in ipynb as raw markdown
| Ext_shortcut_reference_links -- ^ Shortcut reference links | Ext_shortcut_reference_links -- ^ Shortcut reference links
| Ext_simple_tables -- ^ Pandoc-style simple tables | Ext_simple_tables -- ^ Pandoc-style simple tables
| Ext_smart -- ^ "Smart" quotes, apostrophes, ellipses, dashes | Ext_smart -- ^ "Smart" quotes, apostrophes, ellipses, dashes
@ -442,7 +443,8 @@ getAllExtensions f = universalExtensions <> getAll f
getAll "markdown_mmd" = allMarkdownExtensions getAll "markdown_mmd" = allMarkdownExtensions
getAll "markdown_github" = allMarkdownExtensions getAll "markdown_github" = allMarkdownExtensions
getAll "markdown" = allMarkdownExtensions getAll "markdown" = allMarkdownExtensions
getAll "ipynb" = allMarkdownExtensions getAll "ipynb" = allMarkdownExtensions <> extensionsFromList
[ Ext_raw_markdown ]
getAll "docx" = extensionsFromList getAll "docx" = extensionsFromList
[ Ext_empty_paragraphs [ Ext_empty_paragraphs
, Ext_styles , Ext_styles

View file

@ -78,7 +78,11 @@ cellToBlocks opts lang c = do
mapM_ addAttachment attachments mapM_ addAttachment attachments
case cellType c of case cellType c of
Ipynb.Markdown -> do Ipynb.Markdown -> do
Pandoc _ bs <- walk fixImage <$> readMarkdown opts source bs <- if isEnabled Ext_raw_markdown opts
then return [RawBlock (Format "markdown") source]
else do
Pandoc _ bs <- walk fixImage <$> readMarkdown opts source
return bs
return $ B.divWith ("",["cell","markdown"],kvs) return $ B.divWith ("",["cell","markdown"],kvs)
$ B.fromList bs $ B.fromList bs
Ipynb.Heading lev -> do Ipynb.Heading lev -> do

View file

@ -483,10 +483,10 @@ blockToMarkdown' opts b@(RawBlock f str) = do
let renderEmpty = mempty <$ report (BlockNotRendered b) let renderEmpty = mempty <$ report (BlockNotRendered b)
case variant of case variant of
PlainText -> renderEmpty PlainText -> renderEmpty
_ | isEnabled Ext_raw_attribute opts -> rawAttribBlock _ | f `elem` ["markdown", "markdown_github", "markdown_phpextra",
| f `elem` ["markdown", "markdown_github", "markdown_phpextra",
"markdown_mmd", "markdown_strict"] -> "markdown_mmd", "markdown_strict"] ->
return $ literal str <> literal "\n" return $ literal str <> literal "\n"
| isEnabled Ext_raw_attribute opts -> rawAttribBlock
| f `elem` ["html", "html5", "html4"] -> | f `elem` ["html", "html5", "html4"] ->
case () of case () of
_ | isEnabled Ext_markdown_attribute opts -> return $ _ | isEnabled Ext_markdown_attribute opts -> return $