From 56c0e317093d2e25412cfa7dd10099c1fe729640 Mon Sep 17 00:00:00 2001 From: Alexander Biehl Date: Sun, 17 Dec 2017 11:40:03 +0100 Subject: Treat escaped \] better in definition lists (#717) This fixes #546. --- .../src/Documentation/Haddock/Parser.hs | 15 +- html-test/ref/Bug546.html | 273 +++++++++++++++++++++ html-test/src/Bug546.hs | 55 +++++ 3 files changed, 342 insertions(+), 1 deletion(-) create mode 100644 html-test/ref/Bug546.html create mode 100644 html-test/src/Bug546.hs diff --git a/haddock-library/src/Documentation/Haddock/Parser.hs b/haddock-library/src/Documentation/Haddock/Parser.hs index 8dc2a801..dd1044cb 100644 --- a/haddock-library/src/Documentation/Haddock/Parser.hs +++ b/haddock-library/src/Documentation/Haddock/Parser.hs @@ -338,7 +338,7 @@ definitionList :: BS.ByteString -> Parser (DocH mod Identifier) definitionList indent = DocDefList <$> p where p = do - label <- "[" *> (parseStringBS <$> takeWhile1 (notInClass "]\n")) <* ("]" <* optional ":") + label <- "[" *> (parseStringBS <$> scan False accept) <* ("]" <* optional ":") c <- takeLine (cs, items) <- more indent p let contents = parseString . dropNLs . unlines $ c : cs @@ -346,6 +346,19 @@ definitionList indent = DocDefList <$> p Left x -> [(label, contents `docAppend` x)] Right i -> (label, contents) : i + -- handle '\]' escapes + accept True ']' = Just False + + -- stop on ']' or '\n' + accept _ ']' = Nothing + accept _ '\n' = Nothing + + -- starting an escape sequence + accept _ '\\' = Just True + + -- anything else + accept _ _ = Just False + -- | Drops all trailing newlines. dropNLs :: String -> String dropNLs = reverse . dropWhile (== '\n') . reverse diff --git a/html-test/ref/Bug546.html b/html-test/ref/Bug546.html new file mode 100644 index 00000000..1ed6657f --- /dev/null +++ b/html-test/ref/Bug546.html @@ -0,0 +1,273 @@ +Bug546

Safe HaskellSafe

Bug546

Synopsis

Documentation

x :: Integer #

Test:

[code with square \ brackets]
lorem ipsum

compile :: String -> String #

[..]
Matches any of the enclosed characters. Ranges of characters can + be specified by separating the endpoints with a '-'. '-' or + ']' can be matched by including them as the first character(s) + in the list. Never matches path separators: [/] matches + nothing at all. Named character classes can also be matched: + [:x:] within [] specifies the class named x, which matches + certain predefined characters. See below for a full list.
[^..] or [!..]
Like [..], but matches any character not listed. + Note that [^-x] is not the inverse of [-x], but + the range [^-x].
<m-n>
Matches any integer in the range m to n, inclusive. The range may + be open-ended by leaving out either number: "<->", for + instance, matches any integer.
**/
Matches any number of characters, including path separators, + excluding the empty string.

Supported character classes:

[:alnum:]
Equivalent to "0-9A-Za-z".
[:alpha:]
Equivalent to "A-Za-z".
[:blank:]
Equivalent to "\t ".
[:cntrl:]
Equivalent to "\0-\x1f\x7f".
[:digit:]
Equivalent to "0-9".
[:graph:]
Equivalent to "!-~".
[:lower:]
Equivalent to "a-z".
[:print:]
Equivalent to " -~".
[:punct:]
Equivalent to "!-/:-@[-`{-~".
[:space:]
Equivalent to "\t-\r ".
[:upper:]
Equivalent to "A-Z".
[:xdigit:]
Equivalent to "0-9A-Fa-f".
\ No newline at end of file diff --git a/html-test/src/Bug546.hs b/html-test/src/Bug546.hs new file mode 100644 index 00000000..4493b1d9 --- /dev/null +++ b/html-test/src/Bug546.hs @@ -0,0 +1,55 @@ +module Bug546 where + +-- |Test: +-- +-- [@[code with square \\ brackets\]@] lorem ipsum +x = 1 + +-- | +-- +-- [@[..\]@] Matches any of the enclosed characters. Ranges of characters can +-- be specified by separating the endpoints with a @\'-'@. @\'-'@ or +-- @']'@ can be matched by including them as the first character(s) +-- in the list. Never matches path separators: @[\/]@ matches +-- nothing at all. Named character classes can also be matched: +-- @[:x:]@ within @[]@ specifies the class named @x@, which matches +-- certain predefined characters. See below for a full list. +-- +-- [@[^..\]@ or @[!..\]@] Like @[..]@, but matches any character /not/ listed. +-- Note that @[^-x]@ is not the inverse of @[-x]@, but +-- the range @[^-x]@. +-- +-- [@\@] Matches any integer in the range m to n, inclusive. The range may +-- be open-ended by leaving out either number: @\"\<->\"@, for +-- instance, matches any integer. +-- +-- [@**/@] Matches any number of characters, including path separators, +-- excluding the empty string. +-- +-- Supported character classes: +-- +-- [@[:alnum:\]@] Equivalent to @\"0-9A-Za-z\"@. +-- +-- [@[:alpha:\]@] Equivalent to @\"A-Za-z\"@. +-- +-- [@[:blank:\]@] Equivalent to @\"\\t \"@. +-- +-- [@[:cntrl:\]@] Equivalent to @\"\\0-\\x1f\\x7f\"@. +-- +-- [@[:digit:\]@] Equivalent to @\"0-9\"@. +-- +-- [@[:graph:\]@] Equivalent to @\"!-~\"@. +-- +-- [@[:lower:\]@] Equivalent to @\"a-z\"@. +-- +-- [@[:print:\]@] Equivalent to @\" -~\"@. +-- +-- [@[:punct:\]@] Equivalent to @\"!-\/:-\@[-`{-~\"@. +-- +-- [@[:space:\]@] Equivalent to @\"\\t-\\r \"@. +-- +-- [@[:upper:\]@] Equivalent to @\"A-Z\"@. +-- +-- [@[:xdigit:\]@] Equivalent to @\"0-9A-Fa-f\"@. +compile :: String -> String +compile = id \ No newline at end of file -- cgit v1.2.3