Add sourcepos extension for commonmarke

* Add `Ext_sourcepos` constructor for `Extension`.
* Add `sourcepos` extension (only for commonmark).
* Bump to 2.11.3

With the `sourcepos` extension set set, `data-pos` attributes are added
to the AST by the commonmark reader. No other readers are affected.  The
`data-pos` attributes are put on elements that accept attributes; for
other elements, an enlosing Div or Span is added to hold the attributes.

Closes #4565.
This commit is contained in:
John MacFarlane 2020-12-09 21:14:11 -08:00
parent 8c9010864c
commit a3eb87b2ea
5 changed files with 20 additions and 6 deletions

View file

@ -5127,6 +5127,13 @@ for regular emphasis, add extra blank space around headings.
[Project Gutenberg]: https://www.gutenberg.org
#### Extension: `sourcepos` ####
Include source position attributes when parsing `commonmark`.
For elements that accept attributes, a `data-pos` attribute
is added; other elements are placed in a surrounding
Div or Span elemnet with a `data-pos` attribute.
## Markdown variants
In addition to pandoc's extended Markdown, the following Markdown

View file

@ -1,6 +1,6 @@
cabal-version: 2.2
name: pandoc
version: 2.11.2
version: 2.11.3
build-type: Simple
license: GPL-2.0-or-later
license-file: COPYING.md

View file

@ -154,6 +154,7 @@ data Extension =
| Ext_yaml_metadata_block -- ^ YAML metadata block
| Ext_gutenberg -- ^ Use Project Gutenberg conventions for plain
| Ext_attributes -- ^ Generic attribute syntax
| Ext_sourcepos -- ^ Include source position attributes
deriving (Show, Read, Enum, Eq, Ord, Bounded, Data, Typeable, Generic)
-- | Extensions to be used with pandoc-flavored markdown.
@ -503,6 +504,7 @@ getAllExtensions f = universalExtensions <> getAll f
, Ext_implicit_header_references
, Ext_attributes
, Ext_fenced_code_attributes
, Ext_sourcepos
]
getAll "commonmark_x" = getAll "commonmark"
getAll "org" = autoIdExtensions <>

View file

@ -65,6 +65,7 @@ data ReaderOptions = ReaderOptions{
, readerDefaultImageExtension :: Text -- ^ Default extension for images
, readerTrackChanges :: TrackChanges -- ^ Track changes setting for docx
, readerStripComments :: Bool -- ^ Strip HTML comments instead of parsing as raw HTML
-- (only implemented in commonmark)
} deriving (Show, Read, Data, Typeable, Generic)
instance HasSyntaxExtensions ReaderOptions where

View file

@ -32,11 +32,15 @@ import Data.Typeable
-- | Parse a CommonMark formatted string into a 'Pandoc' structure.
readCommonMark :: PandocMonad m => ReaderOptions -> Text -> m Pandoc
readCommonMark opts s = do
let res = runIdentity $ commonmarkWith (specFor opts) "" s
case res of
Left err -> throwError $ PandocParsecError s err
Right (Cm bls :: Cm () Blocks) -> return $ B.doc bls
readCommonMark opts s
| isEnabled Ext_sourcepos opts =
case runIdentity (commonmarkWith (specFor opts) "" s) of
Left err -> throwError $ PandocParsecError s err
Right (Cm bls :: Cm SourceRange Blocks) -> return $ B.doc bls
| otherwise =
case runIdentity (commonmarkWith (specFor opts) "" s) of
Left err -> throwError $ PandocParsecError s err
Right (Cm bls :: Cm () Blocks) -> return $ B.doc bls
specFor :: (Monad m, Typeable m, Typeable a,
Rangeable (Cm a Inlines), Rangeable (Cm a Blocks))