git.iko.soy/typeable/pandoc
on master help sign in
Text.Pandoc.Extensions: Added `Ext_raw_attribute`.

Documented in MANUAL.txt. This is enabled by default in pandoc markdown and multimarkdown.

commit2b34337a9cf8b025914e8219498b4c0258772be0 authorJohn MacFarlane <jgm@berkeley.edu> dateThu 22 Jun 2017, 23:38 (9 years ago) parent4a68688
4 files changed+89−12
MANUAL.txt+24−3 src/Text/Pandoc/Extensions.hs+4 src/Text/Pandoc/Readers/Markdown.hs+33−9 test/command/3537.md+28
MANUAL.txt+24−3
@@ -3033,9 +3033,6 @@ For the most part this should give the same output as `raw_html`,
30333033but it makes it easier to write pandoc filters to manipulate groups
30343034of inlines.
30353035
3036−Raw TeX
3037−-------
3038−
30393036#### Extension: `raw_tex` ####
30403037
30413038In addition to raw HTML, pandoc allows raw LaTeX, TeX, and ConTeXt to be
@@ -3060,6 +3057,30 @@ LaTeX, not as Markdown.
30603057Inline LaTeX is ignored in output formats other than Markdown, LaTeX,
30613058Emacs Org mode, and ConTeXt.
30623059
3060+### Generic raw attribute ###
3061+
3062+#### Extension: `raw_attribute` ####
3063+
3064+Inline spans and fenced code blocks with a special
3065+kind of attribute will be parsed as raw content with the
3066+designated format. For example, the following produces a raw
3067+groff `ms` block:
3068+
3069+ ```{=ms}
3070+ .MYMACRO
3071+ blah blah
3072+ ```
3073+And the following produces a raw `html` inline element:
3074+
3075+ This is `<a>html</a>`{=html}
3076+
3077+This extension presupposes that the relevant kind of
3078+inline code or fenced code block is enabled. Thus, for
3079+example, to use a raw attribute with a backtick code block,
3080+`backtick_code_blocks` must be enabled.
3081+
3082+The raw attribute cannot be combined with regular attributes.
3083+
30633084LaTeX macros
30643085------------
30653086
src/Text/Pandoc/Extensions.hs+4
@@ -94,6 +94,7 @@ data Extension =
9494 | Ext_fenced_code_attributes -- ^ Allow attributes on fenced code blocks
9595 | Ext_backtick_code_blocks -- ^ GitHub style ``` code blocks
9696 | Ext_inline_code_attributes -- ^ Allow attributes on inline code
97+ | Ext_raw_attribute -- ^ Allow explicit raw blocks/inlines
9798 | Ext_markdown_in_html_blocks -- ^ Interpret as markdown inside HTML blocks
9899 | Ext_native_divs -- ^ Use Div blocks for contents of <div> tags
99100 | Ext_native_spans -- ^ Use Span inlines for contents of <span>
@@ -162,6 +163,7 @@ pandocExtensions = extensionsFromList
162163 , Ext_fenced_code_attributes
163164 , Ext_backtick_code_blocks
164165 , Ext_inline_code_attributes
166+ , Ext_raw_attribute
165167 , Ext_markdown_in_html_blocks
166168 , Ext_native_divs
167169 , Ext_native_spans
@@ -275,6 +277,8 @@ multimarkdownExtensions = extensionsFromList
275277 , Ext_subscript
276278 , Ext_backtick_code_blocks
277279 , Ext_spaced_reference_links
280+ -- So far only in dev version of mmd:
281+ , Ext_raw_attribute
278282 ]
279283
280284-- | Language extensions to be used with strict markdown.
src/Text/Pandoc/Readers/Markdown.hs+33−9
@@ -681,19 +681,36 @@ specialAttr = do
681681 char '-'
682682 return $ \(id',cs,kvs) -> (id',cs ++ ["unnumbered"],kvs)
683683
684+rawAttribute :: PandocMonad m => MarkdownParser m String
685+rawAttribute = do
686+ char '{'
687+ skipMany spaceChar
688+ char '='
689+ format <- many1 $ satisfy (\c -> isAlphaNum c || c `elem` "-_")
690+ skipMany spaceChar
691+ char '}'
692+ return format
693+
684694codeBlockFenced :: PandocMonad m => MarkdownParser m (F Blocks)
685695codeBlockFenced = try $ do
686696 c <- try (guardEnabled Ext_fenced_code_blocks >> lookAhead (char '~'))
687697 <|> (guardEnabled Ext_backtick_code_blocks >> lookAhead (char '`'))
688698 size <- blockDelimiter (== c) Nothing
689699 skipMany spaceChar
690− attr <- option ([],[],[]) $
691− try (guardEnabled Ext_fenced_code_attributes >> attributes)
692− <|> ((\x -> ("",[toLanguageId x],[])) <$> many1 nonspaceChar)
700+ rawattr <-
701+ (Left <$> try (guardEnabled Ext_raw_attribute >> rawAttribute))
702+ <|>
703+ (Right <$> option ("",[],[])
704+ (try (guardEnabled Ext_fenced_code_attributes >> attributes)
705+ <|> ((\x -> ("",[toLanguageId x],[])) <$> many1 nonspaceChar)))
693706 blankline
694− contents <- manyTill anyLine (blockDelimiter (== c) (Just size))
707+ contents <- intercalate "\n" <$>
708+ manyTill anyLine (blockDelimiter (== c) (Just size))
695709 blanklines
696− return $ return $ B.codeBlockWith attr $ intercalate "\n" contents
710+ return $ return $
711+ case rawattr of
712+ Left syn -> B.rawBlock syn contents
713+ Right attr -> B.codeBlockWith attr contents
697714
698715-- correctly handle github language identifiers
699716toLanguageId :: String -> String
@@ -1516,13 +1533,20 @@ code :: PandocMonad m => MarkdownParser m (F Inlines)
15161533code = try $ do
15171534 starts <- many1 (char '`')
15181535 skipSpaces
1519− result <- many1Till (many1 (noneOf "`\n") <|> many1 (char '`') <|>
1536+ result <- (trim . concat) <$>
1537+ many1Till (many1 (noneOf "`\n") <|> many1 (char '`') <|>
15201538 (char '\n' >> notFollowedBy' blankline >> return " "))
15211539 (try (skipSpaces >> count (length starts) (char '`') >>
15221540 notFollowedBy (char '`')))
1523− attr <- option ([],[],[]) (try $ guardEnabled Ext_inline_code_attributes
1524− >> attributes)
1525− return $ return $ B.codeWith attr $ trim $ concat result
1541+ rawattr <-
1542+ (Left <$> try (guardEnabled Ext_raw_attribute >> rawAttribute))
1543+ <|>
1544+ (Right <$> option ("",[],[])
1545+ (try (guardEnabled Ext_inline_code_attributes >> attributes)))
1546+ return $ return $
1547+ case rawattr of
1548+ Left syn -> B.rawInline syn result
1549+ Right attr -> B.codeWith attr result
15261550
15271551math :: PandocMonad m => MarkdownParser m (F Inlines)
15281552math = (return . B.displayMath <$> (mathDisplay >>= applyMacros'))
test/command/3537.mdnew file+28
@@ -0,0 +1,28 @@
1+Generalized raw attributes.
2+
3+````
4+% pandoc -t native
5+```{=ms}
6+.MACRO
7+foo bar
8+```
9+^D
10+[RawBlock (Format "ms") ".MACRO\nfoo bar"]
11+````
12+
13+````
14+% pandoc -t native
15+Hi `there`{=ms}.
16+^D
17+[Para [Str "Hi",Space,RawInline (Format "ms") "there",Str "."]]
18+````
19+
20+````
21+% pandoc -t native
22+~~~ {=ms}
23+.MACRO
24+foo bar
25+~~~
26+^D
27+[RawBlock (Format "ms") ".MACRO\nfoo bar"]
28+````