Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC
Commits:
-
51f38f3a
by Sylvain Henry at 2026-11-11T13:58:14-05:00
-
394d9d6d
by Sylvain Henry at 2026-11-11T13:58:14-05:00
-
7ae41c37
by Sylvain Henry at 2026-11-11T13:58:14-05:00
-
ce96f0b6
by Sylvain Henry at 2026-11-11T13:58:14-05:00
-
744b3036
by Sylvain Henry at 2026-11-11T13:58:15-05:00
15 changed files:
- compiler/Language/Haskell/Syntax/Decls/Foreign.hs
- libraries/base/src/Data/Foldable.hs
- utils/haddock/CHANGES.md
- utils/haddock/doc/markup.rst
- utils/haddock/haddock-api/haddock-api.cabal
- utils/haddock/haddock-library/CHANGES.md
- utils/haddock/haddock-library/fixtures/Fixtures.hs
- + utils/haddock/haddock-library/fixtures/examples/codeBackticks.input
- + utils/haddock/haddock-library/fixtures/examples/codeBackticks.parsed
- + utils/haddock/haddock-library/fixtures/examples/codeDoubleBackticks.input
- + utils/haddock/haddock-library/fixtures/examples/codeDoubleBackticks.parsed
- utils/haddock/haddock-library/haddock-library.cabal
- utils/haddock/haddock-library/src/Documentation/Haddock/Parser.hs
- utils/haddock/haddock-library/src/Documentation/Haddock/Parser/Identifier.hs
- utils/haddock/haddock-library/test/Documentation/Haddock/ParserSpec.hs
Changes:
| ... | ... | @@ -130,14 +130,14 @@ data ForeignImport pass |
| 130 | 130 | -- * the two strings specifying a header file or library
|
| 131 | 131 | -- may be empty, which indicates the absence of a
|
| 132 | 132 | -- header or object specification (both are not used
|
| 133 | - -- in the case of `CWrapper' and when `CFunction'
|
|
| 133 | + -- in the case of 'CWrapper' and when 'CFunction'
|
|
| 134 | 134 | -- has a dynamic target)
|
| 135 | 135 | --
|
| 136 | 136 | -- * the calling convention is irrelevant for code
|
| 137 | - -- generation in the case of `CLabel', but is needed
|
|
| 137 | + -- generation in the case of 'CLabel', but is needed
|
|
| 138 | 138 | -- for pretty printing
|
| 139 | 139 | --
|
| 140 | - -- * `Safety' is irrelevant for `CLabel' and `CWrapper'
|
|
| 140 | + -- * 'Safety' is irrelevant for 'CLabel' and 'CWrapper'
|
|
| 141 | 141 | --
|
| 142 | 142 | CImport
|
| 143 | 143 | (XCImport pass)
|
| ... | ... | @@ -1075,7 +1075,7 @@ import GHC.Internal.Data.Foldable |
| 1075 | 1075 | -- abstract potentially faster than linear search over general container
|
| 1076 | 1076 | -- structures. This can be achieved by defining an additional type class (e.g.
|
| 1077 | 1077 | -- @HasMember@ below). Instances of such a type class (that are also
|
| 1078 | --- `Foldable') can employ the `elem` linear search as a last resort, when
|
|
| 1078 | +-- 'Foldable') can employ the `elem` linear search as a last resort, when
|
|
| 1079 | 1079 | -- faster search is not supported.
|
| 1080 | 1080 | --
|
| 1081 | 1081 | -- > {-# LANGUAGE FlexibleInstances, MultiParamTypeClasses #-}
|
| 1 | 1 | ## Changes in next
|
| 2 | 2 | |
| 3 | + * Backtick spans that are not valid identifiers, e.g. `` `foo bar` ``, are now
|
|
| 4 | + rendered as monospaced text, like `@foo bar@`, instead of plain text. The
|
|
| 5 | + content of such a span is taken literally. As in Markdown, a span opened
|
|
| 6 | + with N backticks closes with N backticks, e.g. ``` ``x `plus` y`` ```.
|
|
| 7 | + A closed span is a link only when its whole content is one identifier, so
|
|
| 8 | + `` `foldl' f z xs` `` is code, not a link to `foldl`. (#28090)
|
|
| 9 | + |
|
| 10 | + * Backtick spans are not parsed inside `@...@`: their backticks stay as
|
|
| 11 | + text. An identifier between backticks inside `@...@` in an infix
|
|
| 12 | + application or an operator section, as in `` @f `elem` xs@ `` or
|
|
| 13 | + `` @(`elem` xs)@ ``, now keeps its backticks in the
|
|
| 14 | + output instead of rendering as `f elem xs`.
|
|
| 15 | + |
|
| 3 | 16 | * Allow the `--incremental` flag for modules in other units than main.
|
| 4 | 17 | In the initial implementation, the argument to incremental could only
|
| 5 | 18 | be the name of a module in the main unit. Now this can be changed by
|
| ... | ... | @@ -906,6 +906,8 @@ Haddock provides markup for properties: :: |
| 906 | 906 | |
| 907 | 907 | This allows third-party applications to extract and verify them.
|
| 908 | 908 | |
| 909 | +.. _hyperlinked-identifiers:
|
|
| 910 | + |
|
| 909 | 911 | Hyperlinked Identifiers
|
| 910 | 912 | ~~~~~~~~~~~~~~~~~~~~~~~
|
| 911 | 913 | |
| ... | ... | @@ -981,6 +983,51 @@ Frequent special cases: |
| 981 | 983 | * ``@__FILE__@`` will render ``FILE`` in bold with no underscores,
|
| 982 | 984 | which may not be what you had in mind.
|
| 983 | 985 | |
| 986 | +Monospaced text can also be written between backticks, as in Markdown: ::
|
|
| 987 | + |
|
| 988 | + -- | Run `ghc -O2 Main.hs` to build it.
|
|
| 989 | + |
|
| 990 | +A backtick span is interpreted as a hyperlinked identifier when its
|
|
| 991 | +content is a valid Haskell identifier (see :ref:`hyperlinked-identifiers`),
|
|
| 992 | +and as monospaced text otherwise. Both render in a typewriter font, so
|
|
| 993 | +```foo``` and ```foo bar``` look alike; the first is also a link when
|
|
| 994 | +``foo`` is in scope.
|
|
| 995 | + |
|
| 996 | +Unlike ``@...@``, no markup is interpreted inside a backtick span: the
|
|
| 997 | +content is taken literally, backslashes included: ::
|
|
| 998 | + |
|
| 999 | + -- | The span `/not emphasized/` is literal, and so is `\x -> x`.
|
|
| 1000 | + |
|
| 1001 | +As in Markdown, a span can start with more than one backtick. It ends
|
|
| 1002 | +at the next run of the same number of backticks, so a span with
|
|
| 1003 | +backticks inside, such as an infix application, uses double backticks.
|
|
| 1004 | +When the content both starts and ends with a space, one space is removed
|
|
| 1005 | +at each end: ::
|
|
| 1006 | + |
|
| 1007 | + -- | Write ``x `plus` y`` or `` `plus` x y ``.
|
|
| 1008 | + |
|
| 1009 | +The span must be non-empty and closed on the same line; otherwise the
|
|
| 1010 | +opening backticks are plain text.
|
|
| 1011 | + |
|
| 1012 | +A closed backtick span is a hyperlinked identifier only when the whole
|
|
| 1013 | +content is one identifier. Thus ```foldl' f z xs``` is monospaced text,
|
|
| 1014 | +not a link to ``foldl`` closed by the single quote. The mixed form
|
|
| 1015 | +```T'`` is still a link when no backtick closes the span on the same
|
|
| 1016 | +line. Where it does, use ``'T'`` instead: ::
|
|
| 1017 | + |
|
| 1018 | + -- | 'Foldable' and `elem`.
|
|
| 1019 | + |
|
| 1020 | +Inside ``@...@``, the text is already code, so backtick spans are not
|
|
| 1021 | +parsed and their backticks stay as text: ::
|
|
| 1022 | + |
|
| 1023 | + -- | @x <- f -- of type `M Bool`@
|
|
| 1024 | + |
|
| 1025 | +A backticked identifier inside ``@...@`` is still a hyperlinked
|
|
| 1026 | +identifier. When it is Haskell infix syntax, in an infix application or
|
|
| 1027 | +an operator section, the backticks are shown: ``@f `elem` xs@`` renders
|
|
| 1028 | +as ``f `elem` xs`` with ``elem`` linked, and so does ``@(`elem` xs)@``. Elsewhere the backticks only mark
|
|
| 1029 | +the link, as in ``@`foldlM` :: ...@``.
|
|
| 1030 | + |
|
| 984 | 1031 | Linking to Modules
|
| 985 | 1032 | ~~~~~~~~~~~~~~~~~~
|
| 986 | 1033 |
| ... | ... | @@ -81,7 +81,7 @@ library |
| 81 | 81 | -- this package typically supports only single major versions
|
| 82 | 82 | build-depends: base >= 4.16 && < 4.24
|
| 83 | 83 | , ghc ^>= 10.1
|
| 84 | - , haddock-library ^>= 1.11
|
|
| 84 | + , haddock-library ^>= 1.12
|
|
| 85 | 85 | , xhtml ^>= 3000.4.0.0
|
| 86 | 86 | , parsec ^>= 3.1.13.0
|
| 87 | 87 | , text >= 2.0 && < 2.2
|
| ... | ... | @@ -214,7 +214,7 @@ test-suite spec |
| 214 | 214 | |
| 215 | 215 | build-depends: ghc ^>= 9.7
|
| 216 | 216 | , ghc-paths ^>= 0.1.0.12
|
| 217 | - , haddock-library ^>= 1.11
|
|
| 217 | + , haddock-library ^>= 1.12
|
|
| 218 | 218 | , xhtml ^>= 3000.4.0.0
|
| 219 | 219 | , hspec ^>= 2.9
|
| 220 | 220 | , parsec ^>= 3.1.13.0
|
| 1 | +## Changes in version 1.12.0
|
|
| 2 | + |
|
| 3 | + * Backtick spans that are not valid identifiers, e.g. `` `foo bar` ``, are
|
|
| 4 | + now parsed as monospaced text (like `@foo bar@`) instead of plain text.
|
|
| 5 | + The content of such a span is taken literally. As in Markdown, a span
|
|
| 6 | + opened with N backticks closes with N backticks, e.g.
|
|
| 7 | + ``` ``x `plus` y`` ```. A closed span is an identifier only when its
|
|
| 8 | + whole content is one identifier, so `` `foldl' f z xs` `` is code, not
|
|
| 9 | + the identifier `foldl`. (#28090)
|
|
| 10 | + |
|
| 11 | + * Backtick spans are not parsed inside `@...@`. An identifier between
|
|
| 12 | + backticks inside `@...@` in an infix application or an operator section,
|
|
| 13 | + as in `` @f `elem` xs@ `` or `` @(`elem` xs)@ ``, is now parsed in infix form (like ``` ``elem`` ```),
|
|
| 14 | + so its backticks are rendered.
|
|
| 15 | + |
|
| 1 | 16 | ## Changes in version 1.11.0
|
| 2 | 17 | |
| 3 | 18 | * Add support for linking identifiers with a quote between backticks (#1408)
|
| ... | ... | @@ -9,6 +9,7 @@ import Control.Exception (IOException, catch) |
| 9 | 9 | import Control.Monad (when)
|
| 10 | 10 | import Data.Foldable (traverse_)
|
| 11 | 11 | import qualified Data.List as List
|
| 12 | +import qualified Data.Text as T
|
|
| 12 | 13 | import Data.Traversable (for)
|
| 13 | 14 | import Data.TreeDiff
|
| 14 | 15 | import Data.TreeDiff.Golden
|
| ... | ... | @@ -102,7 +103,7 @@ acceptFixtures = traverse_ $ \(Fixture i o) -> do |
| 102 | 103 | writeFile o actual
|
| 103 | 104 | |
| 104 | 105 | parseString :: String -> Doc String
|
| 105 | -parseString = Parse.toRegular . _doc . Parse.parseParas Nothing
|
|
| 106 | +parseString = Parse.toRegular . _doc . Parse.parseParas Nothing . T.pack
|
|
| 106 | 107 | |
| 107 | 108 | data Cmd = CmdRun | CmdAccept | CmdList
|
| 108 | 109 |
| 1 | +Run `ghc -O2 Main.hs` and then `main`. |
| 1 | +DocParagraph
|
|
| 2 | + (DocAppend
|
|
| 3 | + (DocString "Run ")
|
|
| 4 | + (DocAppend
|
|
| 5 | + (DocMonospaced (DocString "ghc -O2 Main.hs"))
|
|
| 6 | + (DocAppend
|
|
| 7 | + (DocString " and then ")
|
|
| 8 | + (DocAppend (DocIdentifier "main") (DocString "."))))) |
| 1 | +Use ``x `plus` y`` or `\x -> x`. |
| 1 | +DocParagraph
|
|
| 2 | + (DocAppend
|
|
| 3 | + (DocString "Use ")
|
|
| 4 | + (DocAppend
|
|
| 5 | + (DocMonospaced
|
|
| 6 | + (DocString "x `plus` y"))
|
|
| 7 | + (DocAppend
|
|
| 8 | + (DocString " or ")
|
|
| 9 | + (DocAppend
|
|
| 10 | + (DocMonospaced
|
|
| 11 | + (DocString "\\x -> x"))
|
|
| 12 | + (DocString "."))))) |
| 1 | 1 | cabal-version: 3.0
|
| 2 | 2 | name: haddock-library
|
| 3 | -version: 1.11.0
|
|
| 3 | +version: 1.12.0
|
|
| 4 | 4 | synopsis: Library exposing some functionality of Haddock.
|
| 5 | 5 | description: Haddock is a documentation-generation tool for Haskell
|
| 6 | 6 | libraries. These modules expose some
|
| ... | ... | @@ -148,32 +148,54 @@ parseString = parseText . T.pack |
| 148 | 148 | parseText :: Text -> DocH mod Identifier
|
| 149 | 149 | parseText = parseParagraph . T.dropWhile isSpace . T.filter (/= '\r')
|
| 150 | 150 | |
| 151 | +-- | Where inline markup is parsed. See Note [Backticks in code].
|
|
| 152 | +data SpanContext
|
|
| 153 | + = -- | Normal text
|
|
| 154 | + Prose
|
|
| 155 | + | -- | Text between \@...\@
|
|
| 156 | + Code
|
|
| 157 | + |
|
| 151 | 158 | parseParagraph :: Text -> DocH mod Identifier
|
| 152 | -parseParagraph = snd . parse p
|
|
| 159 | +parseParagraph = parse_paragraph Prose
|
|
| 160 | + |
|
| 161 | +parse_paragraph :: SpanContext -> Text -> DocH mod Identifier
|
|
| 162 | +parse_paragraph ctx = snd . parse p
|
|
| 153 | 163 | where
|
| 154 | 164 | p :: Parser (DocH mod Identifier)
|
| 155 | 165 | p =
|
| 156 | - docConcat
|
|
| 166 | + docConcat . mark_infix
|
|
| 157 | 167 | <$> many
|
| 158 | - ( choice'
|
|
| 168 | + ( choice' $
|
|
| 159 | 169 | [ monospace
|
| 160 | 170 | , anchor
|
| 161 | - , identifier
|
|
| 162 | - , moduleName
|
|
| 163 | - , picture
|
|
| 164 | - , mathDisplay
|
|
| 165 | - , mathInline
|
|
| 166 | - , markdownImage
|
|
| 167 | - , markdownLink
|
|
| 168 | - , hyperlink
|
|
| 169 | - , bold
|
|
| 170 | - , emphasis
|
|
| 171 | + ]
|
|
| 172 | + ++ backtick_parsers
|
|
| 173 | + ++ [ backtickRun
|
|
| 174 | + , moduleName
|
|
| 175 | + , picture
|
|
| 176 | + , mathDisplay
|
|
| 177 | + , mathInline
|
|
| 178 | + , markdownImage
|
|
| 179 | + , markdownLink ctx
|
|
| 180 | + , hyperlink
|
|
| 181 | + , bold ctx
|
|
| 182 | + , emphasis ctx
|
|
| 171 | 183 | , encodedChar
|
| 172 | 184 | , string'
|
| 173 | - , skipSpecialChar
|
|
| 174 | - ]
|
|
| 185 | + , skipSpecialChar
|
|
| 186 | + ]
|
|
| 175 | 187 | )
|
| 176 | 188 | |
| 189 | + backtick_parsers :: [Parser (DocH mod Identifier)]
|
|
| 190 | + backtick_parsers = case ctx of
|
|
| 191 | + Prose -> [backtickSpan, identifier]
|
|
| 192 | + Code -> [identifier]
|
|
| 193 | + |
|
| 194 | + mark_infix :: [DocH mod Identifier] -> [DocH mod Identifier]
|
|
| 195 | + mark_infix = case ctx of
|
|
| 196 | + Prose -> id
|
|
| 197 | + Code -> infixInCode
|
|
| 198 | + |
|
| 177 | 199 | -- | Parses and processes
|
| 178 | 200 | -- <https://en.wikipedia.org/wiki/Numeric_character_reference Numeric character references>
|
| 179 | 201 | --
|
| ... | ... | @@ -230,17 +252,17 @@ skipSpecialChar = DocString . T.singleton <$> Parsec.oneOf specialChar |
| 230 | 252 | --
|
| 231 | 253 | -- >>> parseString "/Hello world/"
|
| 232 | 254 | -- DocEmphasis (DocString "Hello world")
|
| 233 | -emphasis :: Parser (DocH mod Identifier)
|
|
| 234 | -emphasis =
|
|
| 235 | - DocEmphasis . parseParagraph
|
|
| 255 | +emphasis :: SpanContext -> Parser (DocH mod Identifier)
|
|
| 256 | +emphasis ctx =
|
|
| 257 | + DocEmphasis . parse_paragraph ctx
|
|
| 236 | 258 | <$> disallowNewline ("/" *> takeWhile1_ (/= '/') <* "/")
|
| 237 | 259 | |
| 238 | 260 | -- | Bold parser.
|
| 239 | 261 | --
|
| 240 | 262 | -- >>> parseString "__Hello world__"
|
| 241 | 263 | -- DocBold (DocString "Hello world")
|
| 242 | -bold :: Parser (DocH mod Identifier)
|
|
| 243 | -bold = DocBold . parseParagraph <$> disallowNewline ("__" *> takeUntil "__")
|
|
| 264 | +bold :: SpanContext -> Parser (DocH mod Identifier)
|
|
| 265 | +bold ctx = DocBold . parse_paragraph ctx <$> disallowNewline ("__" *> takeUntil "__")
|
|
| 244 | 266 | |
| 245 | 267 | disallowNewline :: Parser Text -> Parser Text
|
| 246 | 268 | disallowNewline = mfilter (T.all (/= '\n'))
|
| ... | ... | @@ -273,9 +295,110 @@ anchor = |
| 273 | 295 | -- DocMonospaced (DocString "cruel")
|
| 274 | 296 | monospace :: Parser (DocH mod Identifier)
|
| 275 | 297 | monospace =
|
| 276 | - DocMonospaced . parseParagraph
|
|
| 298 | + DocMonospaced . parse_paragraph Code
|
|
| 277 | 299 | <$> ("@" *> takeWhile1_ (/= '@') <* "@")
|
| 278 | 300 | |
| 301 | +-- Note [Backtick code spans]
|
|
| 302 | +-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
| 303 | +-- A backtick span has two readings:
|
|
| 304 | +--
|
|
| 305 | +-- `foo` a hyperlinked identifier, same as 'foo'
|
|
| 306 | +-- `foo bar` a monospaced code span, same as @foo bar@
|
|
| 307 | +--
|
|
| 308 | +-- In prose, 'backtickSpan' is tried before 'identifier'. Text between @...@
|
|
| 309 | +-- does not use it: see Note [Backticks in code]. 'backtickSpan' parses a
|
|
| 310 | +-- closed code span (rules below) and then decides:
|
|
| 311 | +--
|
|
| 312 | +-- * If 'takeIdentifier' parses the whole span, the result is the
|
|
| 313 | +-- identifier. Thus `foo`, `Foo.bar`, `(++)` and `foldl'` are links, and
|
|
| 314 | +-- ``foo`` is the identifier foo in infix form, not code "foo".
|
|
| 315 | +--
|
|
| 316 | +-- * Else the result is code. This includes spans in which 'takeIdentifier'
|
|
| 317 | +-- reads a shorter identifier closed by a single quote:
|
|
| 318 | +--
|
|
| 319 | +-- `foldl' f z xs` code "foldl' f z xs", not identifier foldl
|
|
| 320 | +-- `don't panic` code "don't panic", not identifier don
|
|
| 321 | +--
|
|
| 322 | +-- If no closing run exists, 'backtickSpan' fails and 'identifier' gets the
|
|
| 323 | +-- input. Thus the TeX-style form `foo' stays the identifier foo. Then
|
|
| 324 | +-- 'backtickRun' makes the opening run plain text.
|
|
| 325 | +--
|
|
| 326 | +-- Code spans follow the CommonMark rules:
|
|
| 327 | +--
|
|
| 328 | +-- * A span opens with a run of N backticks and closes with the next run of
|
|
| 329 | +-- exactly N backticks on the same line. A run of a different length is
|
|
| 330 | +-- part of the content. Thus a code span that contains a backtick, such as
|
|
| 331 | +-- an infix application, uses more backticks:
|
|
| 332 | +--
|
|
| 333 | +-- ``x `plus` y`` code "x `plus` y"
|
|
| 334 | +--
|
|
| 335 | +-- * If the content both starts and ends with a space, and is not only
|
|
| 336 | +-- spaces, one space is removed at each end:
|
|
| 337 | +--
|
|
| 338 | +-- `` `foo` `` code "`foo`"
|
|
| 339 | +--
|
|
| 340 | +-- * The content is literal: no markup is parsed and backslash escapes are
|
|
| 341 | +-- not removed, so `\x -> x` keeps its backslash.
|
|
| 342 | +--
|
|
| 343 | +-- * If no closing run exists, the whole opening run is plain text. Thus
|
|
| 344 | +-- "``" and a lone backtick keep their old meaning, and in "`` `a b`" only
|
|
| 345 | +-- "`a b`" is a code span.
|
|
| 346 | +--
|
|
| 347 | +-- A closed code span wins over the mixed form `foo'. Thus a line such as
|
|
| 348 | +-- "`Foldable') or `elem`" gives code "Foldable') or ". Write 'Foldable'
|
|
| 349 | +-- there instead.
|
|
| 350 | + |
|
| 351 | +-- | Backtick span parser: an identifier or a code span. Fails if the span
|
|
| 352 | +-- is not closed. See Note [Backtick code spans].
|
|
| 353 | +--
|
|
| 354 | +-- >>> parseString "`foo bar`"
|
|
| 355 | +-- DocMonospaced (DocString "foo bar")
|
|
| 356 | +--
|
|
| 357 | +-- >>> parseString "``x `plus` y``"
|
|
| 358 | +-- DocMonospaced (DocString "x `plus` y")
|
|
| 359 | +--
|
|
| 360 | +-- >>> parseString "`foldl' f z xs`"
|
|
| 361 | +-- DocMonospaced (DocString "foldl' f z xs")
|
|
| 362 | +backtickSpan :: Parser (DocH mod Identifier)
|
|
| 363 | +backtickSpan = do
|
|
| 364 | + (n, content) <- code_span
|
|
| 365 | + let fence = T.replicate n "`"
|
|
| 366 | + pure $ case takeIdentifier (fence <> content <> fence) of
|
|
| 367 | + Just (ns, op, ident, cl, rest)
|
|
| 368 | + | T.null rest -> DocIdentifier (Identifier ns op (T.unpack ident) cl)
|
|
| 369 | + _ -> DocMonospaced (DocString (strip_space content))
|
|
| 370 | + where
|
|
| 371 | + -- The length of the opening run and the content up to the closing run
|
|
| 372 | + -- of exactly as many backticks.
|
|
| 373 | + code_span :: Parser (Int, Text)
|
|
| 374 | + code_span = do
|
|
| 375 | + n <- T.length <$> takeWhile1 (== '`')
|
|
| 376 | + content <- T.concat <$> many (try (content_chunk n))
|
|
| 377 | + _ <- backtick_run n
|
|
| 378 | + if T.null content then empty else pure (n, content)
|
|
| 379 | + |
|
| 380 | + content_chunk :: Int -> Parser Text
|
|
| 381 | + content_chunk n =
|
|
| 382 | + takeWhile1 (\c -> c /= '`' && c /= '\n')
|
|
| 383 | + <|> mfilter ((/= n) . T.length) (takeWhile1 (== '`'))
|
|
| 384 | + |
|
| 385 | + backtick_run :: Int -> Parser Text
|
|
| 386 | + backtick_run n = mfilter ((== n) . T.length) (takeWhile1 (== '`'))
|
|
| 387 | + |
|
| 388 | + strip_space :: Text -> Text
|
|
| 389 | + strip_space t
|
|
| 390 | + | T.length t >= 2
|
|
| 391 | + , T.head t == ' '
|
|
| 392 | + , T.last t == ' '
|
|
| 393 | + , not (T.all (== ' ') t) =
|
|
| 394 | + T.init (T.tail t)
|
|
| 395 | + | otherwise = t
|
|
| 396 | + |
|
| 397 | +-- | A run of backticks that opens neither a code span nor an identifier is
|
|
| 398 | +-- plain text. See Note [Backtick code spans].
|
|
| 399 | +backtickRun :: Parser (DocH mod a)
|
|
| 400 | +backtickRun = DocString <$> takeWhile1 (== '`')
|
|
| 401 | + |
|
| 279 | 402 | -- | Module names.
|
| 280 | 403 | --
|
| 281 | 404 | -- Note that we allow '#' and '\' to support anchors (old style anchors are of
|
| ... | ... | @@ -304,9 +427,9 @@ moduleNameString = modid `maybeFollowedBy` anchor_ |
| 304 | 427 | |
| 305 | 428 | -- | A labeled link to an indentifier, module or url using markdown
|
| 306 | 429 | -- syntax.
|
| 307 | -markdownLink :: Parser (DocH mod Identifier)
|
|
| 308 | -markdownLink = do
|
|
| 309 | - lbl <- markdownLinkText
|
|
| 430 | +markdownLink :: SpanContext -> Parser (DocH mod Identifier)
|
|
| 431 | +markdownLink ctx = do
|
|
| 432 | + lbl <- markdownLinkText ctx
|
|
| 310 | 433 | choice' [markdownModuleName lbl, markdownURL lbl]
|
| 311 | 434 | where
|
| 312 | 435 | markdownModuleName lbl = do
|
| ... | ... | @@ -361,7 +484,7 @@ mathDisplay = |
| 361 | 484 | -- DocPic (Picture "www.site.com" (Just "some emphasis in a description"))
|
| 362 | 485 | markdownImage :: Parser (DocH mod Identifier)
|
| 363 | 486 | markdownImage = do
|
| 364 | - text <- markup stringMarkup <$> ("!" *> markdownLinkText)
|
|
| 487 | + text <- markup stringMarkup <$> ("!" *> markdownLinkText Prose)
|
|
| 365 | 488 | url <- markdownLinkTarget
|
| 366 | 489 | pure $ DocPic (Picture url (Just text))
|
| 367 | 490 | where
|
| ... | ... | @@ -629,7 +752,7 @@ textParagraph :: Parser (DocH mod Identifier) |
| 629 | 752 | textParagraph = parseText . T.intercalate "\n" <$> some nonEmptyLine
|
| 630 | 753 | |
| 631 | 754 | textParagraphThatStartsWithMarkdownLink :: Parser (DocH mod Identifier)
|
| 632 | -textParagraphThatStartsWithMarkdownLink = docParagraph <$> (docAppend <$> markdownLink <*> optionalTextParagraph)
|
|
| 755 | +textParagraphThatStartsWithMarkdownLink = docParagraph <$> (docAppend <$> markdownLink Prose <*> optionalTextParagraph)
|
|
| 633 | 756 | where
|
| 634 | 757 | optionalTextParagraph :: Parser (DocH mod Identifier)
|
| 635 | 758 | optionalTextParagraph =
|
| ... | ... | @@ -874,7 +997,7 @@ property = DocProperty . T.strip <$> ("prop>" *> takeWhile1 (/= '\n')) |
| 874 | 997 | -- for markup.
|
| 875 | 998 | codeblock :: Parser (DocH mod Identifier)
|
| 876 | 999 | codeblock =
|
| 877 | - DocCodeBlock . parseParagraph . dropSpaces
|
|
| 1000 | + DocCodeBlock . parse_paragraph Code . dropSpaces
|
|
| 878 | 1001 | <$> ("@" *> skipHorizontalSpace *> "\n" *> block' <* "@")
|
| 879 | 1002 | where
|
| 880 | 1003 | dropSpaces xs = let ys = splitByNl xs in
|
| ... | ... | @@ -919,8 +1042,8 @@ angleBracketLink = |
| 919 | 1042 | <$> disallowNewline ("<" *> takeUntil ">")
|
| 920 | 1043 | |
| 921 | 1044 | -- | The text for a markdown link, enclosed in square brackets.
|
| 922 | -markdownLinkText :: Parser (DocH mod Identifier)
|
|
| 923 | -markdownLinkText = parseParagraph . T.strip <$> ("[" *> takeUntil "]")
|
|
| 1045 | +markdownLinkText :: SpanContext -> Parser (DocH mod Identifier)
|
|
| 1046 | +markdownLinkText ctx = parse_paragraph ctx . T.strip <$> ("[" *> takeUntil "]")
|
|
| 924 | 1047 | |
| 925 | 1048 | -- | The target for a markdown link, enclosed in parenthesis.
|
| 926 | 1049 | markdownLinkTarget :: Parser Text
|
| ... | ... | @@ -956,3 +1079,134 @@ autoUrl = mkLink <$> url |
| 956 | 1079 | -- | Parses identifiers with help of 'parseValid'.
|
| 957 | 1080 | identifier :: Parser (DocH mod Identifier)
|
| 958 | 1081 | identifier = DocIdentifier <$> parseValid
|
| 1082 | + |
|
| 1083 | +-- Note [Backticks in code]
|
|
| 1084 | +-- ~~~~~~~~~~~~~~~~~~~~~~~~
|
|
| 1085 | +-- Text between @...@ is already code, so 'parse_paragraph' does not use
|
|
| 1086 | +-- 'backtickSpan' there. The backticks of a span stay as text:
|
|
| 1087 | +--
|
|
| 1088 | +-- @x <- f -- of type `M Bool`@ code "x <- f -- of type `M Bool`"
|
|
| 1089 | +--
|
|
| 1090 | +-- 'identifier' still parses `x`, and its backticks have two uses:
|
|
| 1091 | +--
|
|
| 1092 | +-- * A link marker, as in prose: @`foldlM` :: ...@ or @`seq`@. The
|
|
| 1093 | +-- backticks are not shown.
|
|
| 1094 | +--
|
|
| 1095 | +-- * The infix form of Haskell: @f `elem` xs@. The backticks must be shown,
|
|
| 1096 | +-- else the code reads "f elem xs".
|
|
| 1097 | +--
|
|
| 1098 | +-- 'infixInCode' uses the Haskell grammar to choose: `x` is infix only in
|
|
| 1099 | +-- an infix application or in an operator section. Thus, spaces excluded,
|
|
| 1100 | +-- one of these must be true:
|
|
| 1101 | +--
|
|
| 1102 | +-- * An operand ends before `x` (a name, a literal, a closing bracket or
|
|
| 1103 | +-- other markup) and an operand starts after it.
|
|
| 1104 | +--
|
|
| 1105 | +-- * A section: '(' is before `x` and an operand starts after it, or an
|
|
| 1106 | +-- operand ends before `x` and ')' is after it.
|
|
| 1107 | +--
|
|
| 1108 | +-- @f `elem` xs@ infix: link "`elem`", as if written ``elem``
|
|
| 1109 | +-- @(`elem` xs)@ infix (right section)
|
|
| 1110 | +-- @(x `elem`)@ infix (left section)
|
|
| 1111 | +-- @`foldlM` :: t@ link marker: link "foldlM"
|
|
| 1112 | +-- @(`elem`)@ link marker: no operand
|
|
| 1113 | +-- @`map` `show` xs@ link markers: `map` is not an operand
|
|
| 1114 | +-- @(op \``on`\` f)@ link marker: '`' does not end an operand
|
|
| 1115 | +--
|
|
| 1116 | +-- Only an alphanumeric identifier can be infix, so @`(<*>)`@ is always a
|
|
| 1117 | +-- link marker.
|
|
| 1118 | +--
|
|
| 1119 | +-- If GHC cannot parse an infix identifier (for example the keyword
|
|
| 1120 | +-- `case`), it becomes text with both pairs of backticks: "``case``".
|
|
| 1121 | + |
|
| 1122 | +-- | Mark the infix identifiers of a code span. See Note [Backticks in code].
|
|
| 1123 | +--
|
|
| 1124 | +-- >>> parseString "@f `elem` xs@"
|
|
| 1125 | +-- DocMonospaced (DocAppend (DocString "f ") (DocAppend (DocIdentifier (Identifier None '`' "`elem`" '`')) (DocString " xs")))
|
|
| 1126 | +infixInCode :: [DocH mod Identifier] -> [DocH mod Identifier]
|
|
| 1127 | +infixInCode = go []
|
|
| 1128 | + where
|
|
| 1129 | + -- The first argument holds the previous pieces, nearest first.
|
|
| 1130 | + go :: [DocH mod Identifier] -> [DocH mod Identifier] -> [DocH mod Identifier]
|
|
| 1131 | + go _ [] = []
|
|
| 1132 | + go before (d : after) = d' : go (d : before) after
|
|
| 1133 | + where
|
|
| 1134 | + d' = case d of
|
|
| 1135 | + DocIdentifier (Identifier ns '`' x '`')
|
|
| 1136 | + | is_alpha_ident x
|
|
| 1137 | + , is_infix (side_before before) (side_after after) ->
|
|
| 1138 | + DocIdentifier (Identifier ns '`' ("`" ++ x ++ "`") '`')
|
|
| 1139 | + _ -> d
|
|
| 1140 | + |
|
| 1141 | + -- An infix application, or a left or right operator section.
|
|
| 1142 | + is_infix :: InfixSide -> InfixSide -> Bool
|
|
| 1143 | + is_infix l r = case (l, r) of
|
|
| 1144 | + (Operand, Operand) -> True
|
|
| 1145 | + (SectionParen, Operand) -> True
|
|
| 1146 | + (Operand, SectionParen) -> True
|
|
| 1147 | + _ -> False
|
|
| 1148 | + |
|
| 1149 | + -- Only an alphanumeric identifier, possibly qualified, can be infix.
|
|
| 1150 | + is_alpha_ident :: String -> Bool
|
|
| 1151 | + is_alpha_ident x = case x of
|
|
| 1152 | + c : _ -> isAlpha c || c == '_'
|
|
| 1153 | + [] -> False
|
|
| 1154 | + |
|
| 1155 | + side_before :: [DocH mod Identifier] -> InfixSide
|
|
| 1156 | + side_before = infix_side (fmap snd . T.unsnoc . T.stripEnd) classify
|
|
| 1157 | + where
|
|
| 1158 | + classify c
|
|
| 1159 | + | isAlphaNum c || c `elem` (")]}_'\"#" :: String) = Operand
|
|
| 1160 | + | c == '(' = SectionParen
|
|
| 1161 | + | otherwise = NoOperand
|
|
| 1162 | + |
|
| 1163 | + side_after :: [DocH mod Identifier] -> InfixSide
|
|
| 1164 | + side_after = infix_side (fmap fst . T.uncons . T.stripStart) classify
|
|
| 1165 | + where
|
|
| 1166 | + classify c
|
|
| 1167 | + | isAlphaNum c || c `elem` ("([_'\"\\" :: String) = Operand
|
|
| 1168 | + | c == ')' = SectionParen
|
|
| 1169 | + | otherwise = NoOperand
|
|
| 1170 | + |
|
| 1171 | +-- | Classify the side of a backticked identifier in code. See
|
|
| 1172 | +-- 'infixInCode'.
|
|
| 1173 | +--
|
|
| 1174 | +-- The pieces are the ones on that side, nearest first. The first function
|
|
| 1175 | +-- gives the character of a text that is nearest to the identifier,
|
|
| 1176 | +-- spaces excluded; the second classifies it.
|
|
| 1177 | +--
|
|
| 1178 | +-- * A text of only spaces (or newlines, in a code block) says nothing,
|
|
| 1179 | +-- so the next piece decides. Thus @f `elem` xs@ is infix.
|
|
| 1180 | +--
|
|
| 1181 | +-- * An identifier between backticks is not an operand: next to it, the
|
|
| 1182 | +-- backticks are link markers. Thus in @`map` `show` xs@ both are links,
|
|
| 1183 | +-- and the code reads "map show xs".
|
|
| 1184 | +--
|
|
| 1185 | +-- * Any other piece comes from markup inside the code: an identifier, a
|
|
| 1186 | +-- module name, a link, emphasis, and so on. Such a piece stands for a
|
|
| 1187 | +-- name or an expression, so it is an operand. Thus @'f' `elem` xs@ is
|
|
| 1188 | +-- infix.
|
|
| 1189 | +--
|
|
| 1190 | +-- * No piece means the edge of the code span: there is no operand.
|
|
| 1191 | +infix_side
|
|
| 1192 | + :: (Text -> Maybe Char)
|
|
| 1193 | + -> (Char -> InfixSide)
|
|
| 1194 | + -> [DocH mod Identifier]
|
|
| 1195 | + -> InfixSide
|
|
| 1196 | +infix_side nearest_char classify = go
|
|
| 1197 | + where
|
|
| 1198 | + go ds = case ds of
|
|
| 1199 | + DocString t : rest -> maybe (go rest) classify (nearest_char t)
|
|
| 1200 | + DocIdentifier (Identifier _ '`' _ '`') : _ -> NoOperand
|
|
| 1201 | + _ : _ -> Operand
|
|
| 1202 | + [] -> NoOperand
|
|
| 1203 | + |
|
| 1204 | +-- | What is on one side of a backticked identifier in code. See
|
|
| 1205 | +-- 'infixInCode'.
|
|
| 1206 | +data InfixSide
|
|
| 1207 | + = -- | The end of an operand before, or the start of an operand after
|
|
| 1208 | + Operand
|
|
| 1209 | + | -- | The parenthesis of an operator section: '(' before or ')' after
|
|
| 1210 | + SectionParen
|
|
| 1211 | + | -- | Anything else, or the edge of the code span
|
|
| 1212 | + NoOperand |
| ... | ... | @@ -13,6 +13,7 @@ |
| 13 | 13 | module Documentation.Haddock.Parser.Identifier
|
| 14 | 14 | ( Identifier (..)
|
| 15 | 15 | , parseValid
|
| 16 | + , takeIdentifier
|
|
| 16 | 17 | ) where
|
| 17 | 18 | |
| 18 | 19 | import Control.Monad (guard)
|
| ... | ... | @@ -22,19 +22,19 @@ infixr 6 <> |
| 22 | 22 | type Doc id = DocH () id
|
| 23 | 23 | |
| 24 | 24 | instance IsString (Doc String) where
|
| 25 | - fromString = DocString
|
|
| 25 | + fromString = DocString . T.pack
|
|
| 26 | 26 | |
| 27 | 27 | instance IsString a => IsString (Maybe a) where
|
| 28 | 28 | fromString = Just . fromString
|
| 29 | 29 | |
| 30 | -parseParas :: T.Text -> MetaDoc () String
|
|
| 31 | -parseParas = overDoc Parse.toRegular . Parse.parseParas Nothing
|
|
| 30 | +parseParas :: String -> MetaDoc () String
|
|
| 31 | +parseParas = overDoc Parse.toRegular . Parse.parseParas Nothing . T.pack
|
|
| 32 | 32 | |
| 33 | -parseString :: T.Text -> Doc String
|
|
| 33 | +parseString :: String -> Doc String
|
|
| 34 | 34 | parseString = Parse.toRegular . Parse.parseString
|
| 35 | 35 | |
| 36 | 36 | hyperlink :: String -> Maybe (Doc String) -> Doc String
|
| 37 | -hyperlink url = DocHyperlink . Hyperlink url
|
|
| 37 | +hyperlink url = DocHyperlink . Hyperlink (T.pack url)
|
|
| 38 | 38 | |
| 39 | 39 | main :: IO ()
|
| 40 | 40 | main = hspec spec
|
| ... | ... | @@ -63,7 +63,7 @@ spec = do |
| 63 | 63 | it "allows to backslash-escape characters except \\r" $ do
|
| 64 | 64 | property $ \y -> case y of
|
| 65 | 65 | '\r' -> "\\\r" `shouldParseTo` DocString "\\"
|
| 66 | - x -> ['\\', x] `shouldParseTo` DocString [x]
|
|
| 66 | + x -> ['\\', x] `shouldParseTo` DocString (T.singleton x)
|
|
| 67 | 67 | |
| 68 | 68 | context "when parsing strings contaning numeric character references" $ do
|
| 69 | 69 | it "will implicitly convert digits to characters" $ do
|
| ... | ... | @@ -271,7 +271,7 @@ spec = do |
| 271 | 271 | |
| 272 | 272 | context "when parsing images" $ do
|
| 273 | 273 | let image :: String -> Maybe String -> Doc String
|
| 274 | - image uri = DocPic . Picture uri
|
|
| 274 | + image uri = DocPic . Picture (T.pack uri) . fmap T.pack
|
|
| 275 | 275 | |
| 276 | 276 | it "accepts markdown syntax for images" $ do
|
| 277 | 277 | "" `shouldParseTo` image "url" "label"
|
| ... | ... | @@ -377,6 +377,105 @@ spec = do |
| 377 | 377 | it "requires the closing @" $ do
|
| 378 | 378 | "@foo /bar/ baz" `shouldParseTo` "@foo " <> DocEmphasis "bar" <> " baz"
|
| 379 | 379 | |
| 380 | + context "when parsing backtick code spans" $ do
|
|
| 381 | + it "parses a backtick span that is not an identifier as monospaced text" $ do
|
|
| 382 | + "`foo bar`" `shouldParseTo` DocMonospaced "foo bar"
|
|
| 383 | + |
|
| 384 | + it "parses inline backtick code spans" $ do
|
|
| 385 | + "foo `bar baz` qux" `shouldParseTo` "foo " <> DocMonospaced "bar baz" <> " qux"
|
|
| 386 | + |
|
| 387 | + it "prefers the identifier reading for a valid identifier" $ do
|
|
| 388 | + "`foo`" `shouldParseTo` DocIdentifier "foo"
|
|
| 389 | + "`Foo.bar`" `shouldParseTo` DocIdentifier "Foo.bar"
|
|
| 390 | + "`(++)`" `shouldParseTo` DocIdentifier "(++)"
|
|
| 391 | + |
|
| 392 | + it "does not parse markup inside a backtick code span" $ do
|
|
| 393 | + "`/foo/ @bar@ 'baz'`" `shouldParseTo` DocMonospaced "/foo/ @bar@ 'baz'"
|
|
| 394 | + |
|
| 395 | + it "does not remove backslashes inside a backtick code span" $ do
|
|
| 396 | + "`\\x -> x`" `shouldParseTo` DocMonospaced "\\x -> x"
|
|
| 397 | + |
|
| 398 | + it "closes a span with a run of the same number of backticks" $ do
|
|
| 399 | + "``x `plus` y``" `shouldParseTo` DocMonospaced "x `plus` y"
|
|
| 400 | + "```a `` b```" `shouldParseTo` DocMonospaced "a `` b"
|
|
| 401 | + |
|
| 402 | + it "keeps the identifier reading of a backticked infix identifier" $ do
|
|
| 403 | + "``foo``" `shouldParseTo` DocIdentifier "`foo`"
|
|
| 404 | + |
|
| 405 | + it "keeps the identifier reading of an identifier with a prime" $ do
|
|
| 406 | + "`foldl'`" `shouldParseTo` DocIdentifier "foldl'"
|
|
| 407 | + |
|
| 408 | + it "does not close a span with a single quote inside it" $ do
|
|
| 409 | + "`foldl' f z xs`" `shouldParseTo` DocMonospaced "foldl' f z xs"
|
|
| 410 | + "`don't panic`" `shouldParseTo` DocMonospaced "don't panic"
|
|
| 411 | + "`a' b'`" `shouldParseTo` DocMonospaced "a' b'"
|
|
| 412 | + |
|
| 413 | + it "keeps the identifier reading of `foo' when no span closes" $ do
|
|
| 414 | + "`foo' and 'bar'" `shouldParseTo` DocIdentifier "foo" <> " and " <> DocIdentifier "bar"
|
|
| 415 | + |
|
| 416 | + it "prefers a closed span to `foo'" $ do
|
|
| 417 | + "`foo' or `bar`" `shouldParseTo` DocMonospaced "foo' or " <> "bar`"
|
|
| 418 | + |
|
| 419 | + it "strips one space at each end when both are present" $ do
|
|
| 420 | + "`` `foo` ``" `shouldParseTo` DocMonospaced "`foo`"
|
|
| 421 | + "`` a ``" `shouldParseTo` DocMonospaced " a "
|
|
| 422 | + "`` a``" `shouldParseTo` DocMonospaced " a"
|
|
| 423 | + "`` ``" `shouldParseTo` DocMonospaced " "
|
|
| 424 | + |
|
| 425 | + it "treats an unclosed run of backticks as plain text" $ do
|
|
| 426 | + "``foo`" `shouldParseTo` "``foo`"
|
|
| 427 | + "`` and `x y`" `shouldParseTo` "`` and " <> DocMonospaced "x y"
|
|
| 428 | + |
|
| 429 | + it "accepts unicode" $ do
|
|
| 430 | + "`foo 灼眼のシャナ bar`" `shouldParseTo` DocMonospaced "foo 灼眼のシャナ bar"
|
|
| 431 | + |
|
| 432 | + it "requires the closing backtick" $ do
|
|
| 433 | + "`foo /bar/ baz" `shouldParseTo` "`foo " <> DocEmphasis "bar" <> " baz"
|
|
| 434 | + |
|
| 435 | + it "requires the span to be on a single line" $ do
|
|
| 436 | + "`foo\nbar`" `shouldParseTo` "`foo\nbar`"
|
|
| 437 | + |
|
| 438 | + it "does not treat an empty span as code" $ do
|
|
| 439 | + "``" `shouldParseTo` "``"
|
|
| 440 | + |
|
| 441 | + it "allows backtick code spans inside other markup" $ do
|
|
| 442 | + "/foo `bar baz`/" `shouldParseTo` DocEmphasis ("foo " <> DocMonospaced "bar baz")
|
|
| 443 | + |
|
| 444 | + it "does not parse backtick code spans inside @...@" $ do
|
|
| 445 | + "@x `M Bool` y@" `shouldParseTo` DocMonospaced "x `M Bool` y"
|
|
| 446 | + "@/a `b c`/@" `shouldParseTo` DocMonospaced (DocEmphasis "a `b c`")
|
|
| 447 | + |
|
| 448 | + it "keeps the backticks of an infix identifier inside @...@" $ do
|
|
| 449 | + "@f `elem` xs@" `shouldParseTo` DocMonospaced ("f " <> DocIdentifier "`elem`" <> " xs")
|
|
| 450 | + "@x `M.member` m@" `shouldParseTo` DocMonospaced ("x " <> DocIdentifier "`M.member`" <> " m")
|
|
| 451 | + "@x ``elem`` y@" `shouldParseTo` DocMonospaced ("x " <> DocIdentifier "`elem`" <> " y")
|
|
| 452 | + "@'elem' x xs@" `shouldParseTo` DocMonospaced (DocIdentifier "elem" <> " x xs")
|
|
| 453 | + "@'f' `elem` xs@" `shouldParseTo` DocMonospaced (DocIdentifier "f" <> " " <> DocIdentifier "`elem`" <> " xs")
|
|
| 454 | + "@x `seq` \\\\y -> y@" `shouldParseTo` DocMonospaced ("x " <> DocIdentifier "`seq`" <> " \\y -> y")
|
|
| 455 | + "@r{a = 1} `seq` x@" `shouldParseTo` DocMonospaced ("r{a = 1} " <> DocIdentifier "`seq`" <> " x")
|
|
| 456 | + "@x# `seq` y@" `shouldParseTo` DocMonospaced ("x# " <> DocIdentifier "`seq`" <> " y")
|
|
| 457 | + |
|
| 458 | + it "keeps the backticks of an operator section inside @...@" $ do
|
|
| 459 | + "@(`elem` xs)@" `shouldParseTo` DocMonospaced ("(" <> DocIdentifier "`elem`" <> " xs)")
|
|
| 460 | + "@(x `elem`)@" `shouldParseTo` DocMonospaced ("(x " <> DocIdentifier "`elem`" <> ")")
|
|
| 461 | + "@filter (`elem` xs) ys@" `shouldParseTo` DocMonospaced ("filter (" <> DocIdentifier "`elem`" <> " xs) ys")
|
|
| 462 | + |
|
| 463 | + it "keeps backticks as link markers inside @...@ where infix is not possible" $ do
|
|
| 464 | + "@`seq`@" `shouldParseTo` DocMonospaced (DocIdentifier "seq")
|
|
| 465 | + "@`foldlM` :: t@" `shouldParseTo` DocMonospaced (DocIdentifier "foldlM" <> " :: t")
|
|
| 466 | + "@(`elem`)@" `shouldParseTo` DocMonospaced ("(" <> DocIdentifier "elem" <> ")")
|
|
| 467 | + "@x `seq`@" `shouldParseTo` DocMonospaced ("x " <> DocIdentifier "seq")
|
|
| 468 | + "@`flip` `(<*>)`@" `shouldParseTo` DocMonospaced (DocIdentifier "flip" <> " " <> DocIdentifier "(<*>)")
|
|
| 469 | + "@`map` `show` xs@" `shouldParseTo` DocMonospaced (DocIdentifier "map" <> " " <> DocIdentifier "show" <> " xs")
|
|
| 470 | + "@`x` `f` y@" `shouldParseTo` DocMonospaced (DocIdentifier "x" <> " " <> DocIdentifier "f" <> " y")
|
|
| 471 | + |
|
| 472 | + it "handles several infix identifiers inside @...@" $ do
|
|
| 473 | + "@a `f` b `g` c@" `shouldParseTo` DocMonospaced ("a " <> DocIdentifier "`f`" <> " b " <> DocIdentifier "`g`" <> " c")
|
|
| 474 | + "@a`f`b`g`c@" `shouldParseTo` DocMonospaced ("a" <> DocIdentifier "`f`" <> "b" <> DocIdentifier "`g`" <> "c")
|
|
| 475 | + "@(a `f` b) `g` c@" `shouldParseTo` DocMonospaced ("(a " <> DocIdentifier "`f`" <> " b) " <> DocIdentifier "`g`" <> " c")
|
|
| 476 | + "@x `f` 'y' `g` z@" `shouldParseTo` DocMonospaced ("x " <> DocIdentifier "`f`" <> " " <> DocIdentifier "y" <> " " <> DocIdentifier "`g`" <> " z")
|
|
| 477 | + "@((`f` a) . (`g` b))@" `shouldParseTo` DocMonospaced ("((" <> DocIdentifier "`f`" <> " a) . (" <> DocIdentifier "`g`" <> " b))")
|
|
| 478 | + |
|
| 380 | 479 | context "when parsing bold strings" $ do
|
| 381 | 480 | it "allows for a bold string on its own" $ do
|
| 382 | 481 | "__bold string__"
|
| ... | ... | @@ -616,7 +715,7 @@ spec = do |
| 616 | 715 | property $ \xs ->
|
| 617 | 716 | let input = filterSpecial xs
|
| 618 | 717 | in (not . null) input ==>
|
| 619 | - input `shouldParseTo` DocParagraph (DocString input)
|
|
| 718 | + input `shouldParseTo` DocParagraph (DocString (T.pack input))
|
|
| 620 | 719 | |
| 621 | 720 | it "separates paragraphs by empty lines" $ do
|
| 622 | 721 | unlines
|
| ... | ... | @@ -631,6 +730,34 @@ spec = do |
| 631 | 730 | it "turns it into a code block" $ do
|
| 632 | 731 | "@foo@" `shouldParseTo` DocCodeBlock "foo"
|
| 633 | 732 | |
| 733 | + context "when a paragraph only contains a backtick code span" $ do
|
|
| 734 | + it "turns it into a code block" $ do
|
|
| 735 | + "`foo bar`" `shouldParseTo` DocCodeBlock "foo bar"
|
|
| 736 | + |
|
| 737 | + context "when a code block contains backticks" $ do
|
|
| 738 | + it "keeps them" $ do
|
|
| 739 | + unlines
|
|
| 740 | + [ "@"
|
|
| 741 | + , "myExample opt1 opt2 ="
|
|
| 742 | + , " Code.do"
|
|
| 743 | + , " x <- someSideEffect -- This one is of type `M Bool`"
|
|
| 744 | + , " if x then opt1 else opt2"
|
|
| 745 | + , "@"
|
|
| 746 | + ]
|
|
| 747 | + `shouldParseTo` DocCodeBlock
|
|
| 748 | + ( DocString
|
|
| 749 | + ( T.unlines
|
|
| 750 | + [ "myExample opt1 opt2 ="
|
|
| 751 | + , " Code.do"
|
|
| 752 | + , " x <- someSideEffect -- This one is of type `M Bool`"
|
|
| 753 | + , " if x then opt1 else opt2"
|
|
| 754 | + ]
|
|
| 755 | + )
|
|
| 756 | + )
|
|
| 757 | + |
|
| 758 | + it "keeps the backticks of an infix identifier" $ do
|
|
| 759 | + "@\nf `elem` xs\n@" `shouldParseTo` DocCodeBlock ("f " <> DocIdentifier "`elem`" <> " xs\n")
|
|
| 760 | + |
|
| 634 | 761 | context "when a paragraph contains a markdown link" $ do
|
| 635 | 762 | it "correctly parses the link" $ do
|
| 636 | 763 | "Blah [label](url)"
|
| ... | ... | @@ -843,7 +970,7 @@ spec = do |
| 843 | 970 | |
| 844 | 971 | it "parses bird-tracks inside of paragraphs as plain strings" $ do
|
| 845 | 972 | let xs = "foo\n>>> bar"
|
| 846 | - xs `shouldParseTo` DocParagraph (DocString xs)
|
|
| 973 | + xs `shouldParseTo` DocParagraph (DocString (T.pack xs))
|
|
| 847 | 974 | |
| 848 | 975 | it "skips empty lines in front of an example" $ do
|
| 849 | 976 | "\n \n\n>>> foo" `shouldParseTo` DocExamples [Example "foo" []]
|