git.iko.soy/typeable/pandoc
on master help sign in
Support github syntax for fenced code blocks.

You can now write ```ruby x = 2 ``` instead of ~~~ {.ruby} x = 2 ~~~~

commitd1ded4b0260b32550ac45329d7c43a82a7e7e911 authorJohn MacFarlane <fiddlosopher@gmail.com> dateSat 28 Jan 2012, 12:25 (15 years ago) parentd0e70cb
2 files changed+42−24
README+28−14 src/Text/Pandoc/Readers/Markdown.hs+14−10
README+28−14
@@ -876,9 +876,9 @@ Note: blank lines in the verbatim text need not begin with four spaces.
876876
877877In addition to standard indented code blocks, Pandoc supports
878878*delimited* code blocks. These begin with a row of three or more
879−tildes (`~`) and end with a row of tildes that must be at least
880−as long as the starting row. Everything between the tilde-lines
881−is treated as code. No indentation is necessary:
879+tildes (`~`) or backticks (`` ` ``) and end with a row of tildes or
880+backticks that must be at least as long as the starting row. Everything
881+between these lines is treated as code. No indentation is necessary:
882882
883883 ~~~~~~~
884884 if (a > 3) {
@@ -889,8 +889,8 @@ is treated as code. No indentation is necessary:
889889Like regular code blocks, delimited code blocks must be separated
890890from surrounding text by blank lines.
891891
892−If the code itself contains a row of tildes, just use a longer
893−row of tildes at the start and end:
892+If the code itself contains a row of tildes or backticks, just use a longer
893+row of tildes or backticks at the start and end:
894894
895895 ~~~~~~~~~~~~~~~~
896896 ~~~~~~~~~~
@@ -898,28 +898,42 @@ row of tildes at the start and end:
898898 ~~~~~~~~~~
899899 ~~~~~~~~~~~~~~~~
900900
901−Optionally, you may specify the language of the code block using
901+Optionally, you may attach attributes to the code block using
902902this syntax:
903903
904− ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ {.haskell .numberLines}
904+ ~~~~ {#mycode .haskell .numberLines startFrom="100"}
905905 qsort [] = []
906906 qsort (x:xs) = qsort (filter (< x) xs) ++ [x] ++
907907 qsort (filter (>= x) xs)
908908 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
909909
910−Some output formats can use this information to do syntax highlighting.
911−Currently, the only output formats that uses this information are HTML
912−and LaTeX. If highlighting is supported for your output format and language,
913−then the code block above will appear highlighted, with numbered lines. (To
914−see which languages are supported, do `pandoc --version`.) Otherwise, the code
915−block above will appear as follows:
910+Here `mycode` is an identifier, `haskell` and `numberLines` are classes, and
911+`startFrom` is an attribute with value `100`. Some output formats can use this
912+information to do syntax highlighting. Currently, the only output formats
913+that uses this information are HTML and LaTeX. If highlighting is supported
914+for your output format and language, then the code block above will appear
915+highlighted, with numbered lines. (To see which languages are supported, do
916+`pandoc --version`.) Otherwise, the code block above will appear as follows:
916917
917− <pre class="haskell">
918+ <pre id="mycode" class="haskell numberLines" startFrom="100">
918919 <code>
919920 ...
920921 </code>
921922 </pre>
922923
924+A shortcut form can also be used for specifying the language of
925+the code block:
926+
927+ ```haskell
928+ qsort [] = []
929+ ```
930+
931+This is equivalent to:
932+
933+ ``` {.haskell}
934+ qsort [] = []
935+ ```
936+
923937To prevent all highlighting, use the `--no-highlight` flag.
924938To set the highlighting style, use `--highlight-style`.
925939
src/Text/Pandoc/Readers/Markdown.hs+14−10
@@ -371,17 +371,21 @@ hrule = try $ do
371371indentedLine :: GenParser Char ParserState [Char]
372372indentedLine = indentSpaces >> manyTill anyChar newline >>= return . (++ "\n")
373373
374−codeBlockDelimiter :: Maybe Int
375− -> GenParser Char st (Int, ([Char], [[Char]], [([Char], [Char])]))
376−codeBlockDelimiter len = try $ do
374+blockDelimiter :: (Char -> Bool)
375+ -> Maybe Int
376+ -> GenParser Char st (Int, (String, [String], [(String, String)]), Char)
377+blockDelimiter f len = try $ do
378+ c <- lookAhead (satisfy f)
377379 size <- case len of
378− Just l -> count l (char '~') >> many (char '~') >> return l
379− Nothing -> count 3 (char '~') >> many (char '~') >>=
380− return . (+ 3) . length
380+ Just l -> count l (char c) >> many (char c) >> return l
381+ Nothing -> count 3 (char c) >> many (char c) >>=
382+ return . (+ 3) . length
381383 many spaceChar
382− attr <- option ([],[],[]) attributes
384+ attr <- option ([],[],[])
385+ $ (try $ many spaceChar >> attributes) -- ~~~ {.ruby}
386+ <|> (many1 alphaNum >>= \x -> return ([],[x],[])) -- github variant ```ruby
383387 blankline
384− return (size, attr)
388+ return (size, attr, c)
385389
386390attributes :: GenParser Char st ([Char], [[Char]], [([Char], [Char])])
387391attributes = try $ do
@@ -425,8 +429,8 @@ keyValAttr = try $ do
425429
426430codeBlockDelimited :: GenParser Char st Block
427431codeBlockDelimited = try $ do
428− (size, attr) <- codeBlockDelimiter Nothing
429− contents <- manyTill anyLine (codeBlockDelimiter (Just size))
432+ (size, attr, c) <- blockDelimiter (\c -> c == '~' || c == '`') Nothing
433+ contents <- manyTill anyLine (blockDelimiter (== c) (Just size))
430434 blanklines
431435 return $ CodeBlock attr $ intercalate "\n" contents
432436