git.iko.soy/typeable/pandoc
on master help sign in
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.

commita3eb87b2eab9def3e28364b43300043f5e13268d authorJohn MacFarlane <jgm@berkeley.edu> dateWed 9 Dec 2020, 21:14 (6 years ago) parent8c90108
5 files changed+20−6
MANUAL.txt+7 pandoc.cabal+1−1 src/Text/Pandoc/Extensions.hs+2 src/Text/Pandoc/Options.hs+1 src/Text/Pandoc/Readers/CommonMark.hs+9−5
MANUAL.txt+7
@@ -5127,6 +5127,13 @@ for regular emphasis, add extra blank space around headings.
51275127
51285128 [Project Gutenberg]: https://www.gutenberg.org
51295129
5130+#### Extension: `sourcepos` ####
5131+
5132+Include source position attributes when parsing `commonmark`.
5133+For elements that accept attributes, a `data-pos` attribute
5134+is added; other elements are placed in a surrounding
5135+Div or Span elemnet with a `data-pos` attribute.
5136+
51305137## Markdown variants
51315138
51325139In addition to pandoc's extended Markdown, the following Markdown
pandoc.cabal+1−1
@@ -1,6 +1,6 @@
11cabal-version: 2.2
22name: pandoc
3−version: 2.11.2
3+version: 2.11.3
44build-type: Simple
55license: GPL-2.0-or-later
66license-file: COPYING.md
src/Text/Pandoc/Extensions.hs+2
@@ -154,6 +154,7 @@ data Extension =
154154 | Ext_yaml_metadata_block -- ^ YAML metadata block
155155 | Ext_gutenberg -- ^ Use Project Gutenberg conventions for plain
156156 | Ext_attributes -- ^ Generic attribute syntax
157+ | Ext_sourcepos -- ^ Include source position attributes
157158 deriving (Show, Read, Enum, Eq, Ord, Bounded, Data, Typeable, Generic)
158159
159160-- | Extensions to be used with pandoc-flavored markdown.
@@ -503,6 +504,7 @@ getAllExtensions f = universalExtensions <> getAll f
503504 , Ext_implicit_header_references
504505 , Ext_attributes
505506 , Ext_fenced_code_attributes
507+ , Ext_sourcepos
506508 ]
507509 getAll "commonmark_x" = getAll "commonmark"
508510 getAll "org" = autoIdExtensions <>
src/Text/Pandoc/Options.hs+1
@@ -65,6 +65,7 @@ data ReaderOptions = ReaderOptions{
6565 , readerDefaultImageExtension :: Text -- ^ Default extension for images
6666 , readerTrackChanges :: TrackChanges -- ^ Track changes setting for docx
6767 , readerStripComments :: Bool -- ^ Strip HTML comments instead of parsing as raw HTML
68+ -- (only implemented in commonmark)
6869} deriving (Show, Read, Data, Typeable, Generic)
6970
7071instance HasSyntaxExtensions ReaderOptions where
src/Text/Pandoc/Readers/CommonMark.hs+9−5
@@ -32,11 +32,15 @@ import Data.Typeable
3232
3333-- | Parse a CommonMark formatted string into a 'Pandoc' structure.
3434readCommonMark :: PandocMonad m => ReaderOptions -> Text -> m Pandoc
35−readCommonMark opts s = do
36− let res = runIdentity $ commonmarkWith (specFor opts) "" s
37− case res of
38− Left err -> throwError $ PandocParsecError s err
39− Right (Cm bls :: Cm () Blocks) -> return $ B.doc bls
35+readCommonMark opts s
36+ | isEnabled Ext_sourcepos opts =
37+ case runIdentity (commonmarkWith (specFor opts) "" s) of
38+ Left err -> throwError $ PandocParsecError s err
39+ Right (Cm bls :: Cm SourceRange Blocks) -> return $ B.doc bls
40+ | otherwise =
41+ case runIdentity (commonmarkWith (specFor opts) "" s) of
42+ Left err -> throwError $ PandocParsecError s err
43+ Right (Cm bls :: Cm () Blocks) -> return $ B.doc bls
4044
4145specFor :: (Monad m, Typeable m, Typeable a,
4246 Rangeable (Cm a Inlines), Rangeable (Cm a Blocks))