aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2020-12-09 21:14:11 -0800
committerJohn MacFarlane <jgm@berkeley.edu>2020-12-10 08:59:55 -0800
commita3eb87b2eab9def3e28364b43300043f5e13268d (patch)
tree6616cb502c29fd331ea5401f8acdb21cbbe92e13
parent8c9010864cd818031d7eff161a57459709751517 (diff)
downloadpandoc-a3eb87b2eab9def3e28364b43300043f5e13268d.tar.gz
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.
-rw-r--r--MANUAL.txt7
-rw-r--r--pandoc.cabal2
-rw-r--r--src/Text/Pandoc/Extensions.hs2
-rw-r--r--src/Text/Pandoc/Options.hs1
-rw-r--r--src/Text/Pandoc/Readers/CommonMark.hs14
5 files changed, 20 insertions, 6 deletions
diff --git a/MANUAL.txt b/MANUAL.txt
index 0a1300947..461ebf54d 100644
--- a/MANUAL.txt
+++ b/MANUAL.txt
@@ -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
diff --git a/pandoc.cabal b/pandoc.cabal
index c02dfeb38..5829856da 100644
--- a/pandoc.cabal
+++ b/pandoc.cabal
@@ -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
diff --git a/src/Text/Pandoc/Extensions.hs b/src/Text/Pandoc/Extensions.hs
index a94e24e2c..9865f897b 100644
--- a/src/Text/Pandoc/Extensions.hs
+++ b/src/Text/Pandoc/Extensions.hs
@@ -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 <>
diff --git a/src/Text/Pandoc/Options.hs b/src/Text/Pandoc/Options.hs
index c7f1a56fa..ecd65a54d 100644
--- a/src/Text/Pandoc/Options.hs
+++ b/src/Text/Pandoc/Options.hs
@@ -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
diff --git a/src/Text/Pandoc/Readers/CommonMark.hs b/src/Text/Pandoc/Readers/CommonMark.hs
index d32a38342..9eef498e1 100644
--- a/src/Text/Pandoc/Readers/CommonMark.hs
+++ b/src/Text/Pandoc/Readers/CommonMark.hs
@@ -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))