From 8670f6dc5b943a6e7eabaf69776724d54d80c9bb Mon Sep 17 00:00:00 2001 From: Siphalor Date: Thu, 25 Aug 2022 19:18:11 +0200 Subject: [PATCH] Markdown reader: fenced code block shortcuts with attributes (#8174) This allows the combination of the fenced code block shortcut form with attributes: ```` ```haskell {.class #id} ``` ```` The code syntax class will be combined with the attribute classes. This syntax allows for more intuitive writing and for better compatibility with other Markdown parsers such as GitHub or Codeberg. Closes #8174. --- MANUAL.txt | 12 ++++++++ src/Text/Pandoc/Readers/Markdown.hs | 10 +++++-- test/command/8174.md | 44 +++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 test/command/8174.md diff --git a/MANUAL.txt b/MANUAL.txt index f4e91e6c6..adcbf535a 100644 --- a/MANUAL.txt +++ b/MANUAL.txt @@ -3857,6 +3857,18 @@ This is equivalent to: qsort [] = [] ``` +This shortcut form may be combined with attributes: + + ```haskell {.numberLines} + qsort [] = [] + ``` + +Which is equivalent to: + + ``` {.haskell .numberLines} + qsort [] = [] + ``` + If the `fenced_code_attributes` extension is disabled, but input contains class attribute(s) for the code block, the first class attribute will be printed after the opening fence as a bare diff --git a/src/Text/Pandoc/Readers/Markdown.hs b/src/Text/Pandoc/Readers/Markdown.hs index 1e12a2314..3242122db 100644 --- a/src/Text/Pandoc/Readers/Markdown.hs +++ b/src/Text/Pandoc/Readers/Markdown.hs @@ -688,9 +688,13 @@ codeBlockFenced = try $ do rawattr <- (Left <$> (guardEnabled Ext_raw_attribute >> try rawAttribute)) <|> - (Right <$> option ("",[],[]) - ((guardEnabled Ext_fenced_code_attributes >> try attributes) - <|> ((\x -> ("",[toLanguageId x],[])) <$> many1Char nonspaceChar))) + (Right <$> (do + languageId <- option Nothing (Just . toLanguageId <$> try (many1Char $ satisfy (\x -> x `notElem` ['`', '{', '}'] && not (isSpace x)))) + skipMany spaceChar + maybeAttr <- option Nothing (Just <$> (guardEnabled Ext_fenced_code_attributes >> try attributes)) + return $ case maybeAttr of + Nothing -> ("", maybeToList languageId, []) + Just (elementId, classes, attrs) -> (elementId, maybe classes (: classes) languageId, attrs))) blankline contents <- T.intercalate "\n" <$> manyTill (gobbleAtMostSpaces indentLevel >> anyLine) diff --git a/test/command/8174.md b/test/command/8174.md new file mode 100644 index 000000000..61fe37b93 --- /dev/null +++ b/test/command/8174.md @@ -0,0 +1,44 @@ +```` +% pandoc -t native +```yaml {#id} +some: code +``` +^D +[ CodeBlock ( "id" , [ "yaml" ] , [] ) "some: code" ] +```` + +```` +% pandoc -t native +```yaml {.class #id} +some: code +``` +^D +[ CodeBlock + ( "id" , [ "yaml" , "class" ] , [] ) "some: code" +] +```` + + +```` +% pandoc -t native +```ab``` +^D +[ Para [ Code ( "" , [] , [] ) "ab" ] ] +```` + +```` +% pandoc -t native +```ab```{.class} +^D +[ Para [ Code ( "" , [ "class" ] , [] ) "ab" ] ] +```` + + +```` +% pandoc -t native +``` foo}{.bar} +test +``` +^D +[ Para [ Code ( "" , [] , [] ) "foo}{.bar} test" ] ] +````