From cacd245a5e0a0f2e14d4ed34e877835fdef3367f Mon Sep 17 00:00:00 2001 From: Oleg Grenrus Date: Mon, 4 Feb 2019 18:44:25 +0200 Subject: Make a fixture of weird parsing of lists (#997) The second example is interesting. If there's a list directly after the header, and that list has deeper structure, the parser is confused: It finds two lists: - One with the first nested element, - everything after it I'm not trying to fix this, as I'm not even sure this is a bug, and not a feature. --- haddock-library/fixtures/examples/list-blocks1.input | 15 +++++++++++++++ haddock-library/fixtures/examples/list-blocks1.parsed | 12 ++++++++++++ haddock-library/fixtures/examples/list-blocks2.input | 10 ++++++++++ haddock-library/fixtures/examples/list-blocks2.parsed | 10 ++++++++++ 4 files changed, 47 insertions(+) create mode 100644 haddock-library/fixtures/examples/list-blocks1.input create mode 100644 haddock-library/fixtures/examples/list-blocks1.parsed create mode 100644 haddock-library/fixtures/examples/list-blocks2.input create mode 100644 haddock-library/fixtures/examples/list-blocks2.parsed (limited to 'haddock-library/fixtures/examples') diff --git a/haddock-library/fixtures/examples/list-blocks1.input b/haddock-library/fixtures/examples/list-blocks1.input new file mode 100644 index 00000000..72a0640b --- /dev/null +++ b/haddock-library/fixtures/examples/list-blocks1.input @@ -0,0 +1,15 @@ +* Something about foo + + @ + foo :: a -> b -> c + foo a b = bar c b + @ + +* Something about bar + + @ + bar :: a -> b -> c + bar a b = foo b a + @ + +* And then we continue diff --git a/haddock-library/fixtures/examples/list-blocks1.parsed b/haddock-library/fixtures/examples/list-blocks1.parsed new file mode 100644 index 00000000..9fc4f0ba --- /dev/null +++ b/haddock-library/fixtures/examples/list-blocks1.parsed @@ -0,0 +1,12 @@ +DocUnorderedList + [DocAppend + (DocParagraph (DocString "Something about foo")) + (DocCodeBlock + (DocString + (concat ["foo :: a -> b -> c\n", "foo a b = bar c b\n"]))), + DocAppend + (DocParagraph (DocString "Something about bar")) + (DocCodeBlock + (DocString + (concat ["bar :: a -> b -> c\n", "bar a b = foo b a\n"]))), + DocParagraph (DocString "And then we continue")] diff --git a/haddock-library/fixtures/examples/list-blocks2.input b/haddock-library/fixtures/examples/list-blocks2.input new file mode 100644 index 00000000..91492adb --- /dev/null +++ b/haddock-library/fixtures/examples/list-blocks2.input @@ -0,0 +1,10 @@ +=== Title + +* List directly +* after the title + + @ + with some inline things + @ + +* is parsed weirdly diff --git a/haddock-library/fixtures/examples/list-blocks2.parsed b/haddock-library/fixtures/examples/list-blocks2.parsed new file mode 100644 index 00000000..169677b7 --- /dev/null +++ b/haddock-library/fixtures/examples/list-blocks2.parsed @@ -0,0 +1,10 @@ +DocAppend + (DocAppend + (DocHeader + Header {headerLevel = 3, headerTitle = DocString "Title"}) + (DocUnorderedList + [DocParagraph (DocString "List directly"), + DocAppend + (DocParagraph (DocString "after the title")) + (DocCodeBlock (DocString "with some inline things\n"))])) + (DocUnorderedList [DocParagraph (DocString "is parsed weirdly")]) -- cgit v1.2.3 From dec888641006eb685a642ec489b198c61a5736dc Mon Sep 17 00:00:00 2001 From: Alina Banerjee Date: Thu, 1 Aug 2019 16:01:39 -0500 Subject: Update parsing to strip whitespace from table cells (#1074) * Update parsing to strip leading & trailing whitespace from table cells * Update fixture data to disallow whitespaces at both ends in table cells * Add test case for whitespaces stripped from both ends of table cells * Update table reference test data for html tests --- .../examples/table-cell-strip-whitespaces.input | 5 ++ .../examples/table-cell-strip-whitespaces.parsed | 29 +++++++ .../fixtures/examples/table-simple.parsed | 28 ++----- haddock-library/fixtures/examples/table1.parsed | 45 ++++------ haddock-library/fixtures/examples/table2.parsed | 20 ++--- haddock-library/fixtures/examples/table3.parsed | 22 ++--- haddock-library/fixtures/examples/table4.parsed | 10 +-- haddock-library/fixtures/examples/table5.parsed | 27 +++--- .../src/Documentation/Haddock/Parser.hs | 14 ++-- html-test/ref/Table.html | 96 +++++++++++----------- 10 files changed, 150 insertions(+), 146 deletions(-) create mode 100644 haddock-library/fixtures/examples/table-cell-strip-whitespaces.input create mode 100644 haddock-library/fixtures/examples/table-cell-strip-whitespaces.parsed (limited to 'haddock-library/fixtures/examples') diff --git a/haddock-library/fixtures/examples/table-cell-strip-whitespaces.input b/haddock-library/fixtures/examples/table-cell-strip-whitespaces.input new file mode 100644 index 00000000..f5e3756d --- /dev/null +++ b/haddock-library/fixtures/examples/table-cell-strip-whitespaces.input @@ -0,0 +1,5 @@ ++------+--------------+-------------------------------------------------+ +| C1 | C2 | C3 | ++======+==============+=================================================+ +| row | 'test' | 'test table cell with .. whitepspace ' | ++------+--------------+-------------------------------------------------+ diff --git a/haddock-library/fixtures/examples/table-cell-strip-whitespaces.parsed b/haddock-library/fixtures/examples/table-cell-strip-whitespaces.parsed new file mode 100644 index 00000000..19002369 --- /dev/null +++ b/haddock-library/fixtures/examples/table-cell-strip-whitespaces.parsed @@ -0,0 +1,29 @@ +DocTable + Table + {tableBodyRows = [TableRow + [TableCell + {tableCellColspan = 1, + tableCellContents = DocString "row", + tableCellRowspan = 1}, + TableCell + {tableCellColspan = 1, + tableCellContents = DocIdentifier "test", + tableCellRowspan = 1}, + TableCell + {tableCellColspan = 1, + tableCellContents = DocString + "'test table cell with .. whitepspace '", + tableCellRowspan = 1}]], + tableHeaderRows = [TableRow + [TableCell + {tableCellColspan = 1, + tableCellContents = DocString "C1", + tableCellRowspan = 1}, + TableCell + {tableCellColspan = 1, + tableCellContents = DocString "C2", + tableCellRowspan = 1}, + TableCell + {tableCellColspan = 1, + tableCellContents = DocString "C3", + tableCellRowspan = 1}]]} diff --git a/haddock-library/fixtures/examples/table-simple.parsed b/haddock-library/fixtures/examples/table-simple.parsed index b5e62453..d027c75d 100644 --- a/haddock-library/fixtures/examples/table-simple.parsed +++ b/haddock-library/fixtures/examples/table-simple.parsed @@ -3,50 +3,40 @@ DocTable {tableBodyRows = [TableRow [TableCell {tableCellColspan = 1, - tableCellContents = DocString " 200 ", + tableCellContents = DocString "200", tableCellRowspan = 1}, TableCell {tableCellColspan = 1, - tableCellContents = DocAppend - (DocString " ") - (DocAppend - (DocMonospaced (DocString "OK")) - (DocString " ")), + tableCellContents = DocMonospaced (DocString "OK"), tableCellRowspan = 1}, TableCell {tableCellColspan = 1, - tableCellContents = DocString - " operation successful ", + tableCellContents = DocString "operation successful", tableCellRowspan = 1}], TableRow [TableCell {tableCellColspan = 1, - tableCellContents = DocString " 204 ", + tableCellContents = DocString "204", tableCellRowspan = 1}, TableCell {tableCellColspan = 1, - tableCellContents = DocAppend - (DocString " ") - (DocAppend - (DocMonospaced (DocString "No Content")) - (DocString " ")), + tableCellContents = DocMonospaced (DocString "No Content"), tableCellRowspan = 1}, TableCell {tableCellColspan = 1, tableCellContents = DocString - " operation successful, no body returned ", + "operation successful, no body returned", tableCellRowspan = 1}]], tableHeaderRows = [TableRow [TableCell {tableCellColspan = 1, - tableCellContents = DocString " code ", + tableCellContents = DocString "code", tableCellRowspan = 1}, TableCell {tableCellColspan = 1, - tableCellContents = DocString " message ", + tableCellContents = DocString "message", tableCellRowspan = 1}, TableCell {tableCellColspan = 1, - tableCellContents = DocString - " description ", + tableCellContents = DocString "description", tableCellRowspan = 1}]]} diff --git a/haddock-library/fixtures/examples/table1.parsed b/haddock-library/fixtures/examples/table1.parsed index 2fa58fd8..8b8908f4 100644 --- a/haddock-library/fixtures/examples/table1.parsed +++ b/haddock-library/fixtures/examples/table1.parsed @@ -3,79 +3,66 @@ DocTable {tableBodyRows = [TableRow [TableCell {tableCellColspan = 1, - tableCellContents = DocString " body row 1, column 1 ", + tableCellContents = DocString "body row 1, column 1", tableCellRowspan = 1}, TableCell {tableCellColspan = 1, - tableCellContents = DocString " column 2 ", + tableCellContents = DocString "column 2", tableCellRowspan = 1}, TableCell {tableCellColspan = 1, - tableCellContents = DocString " column 3 ", + tableCellContents = DocString "column 3", tableCellRowspan = 1}, TableCell {tableCellColspan = 1, - tableCellContents = DocString " column 4 ", + tableCellContents = DocString "column 4", tableCellRowspan = 1}], TableRow [TableCell {tableCellColspan = 1, - tableCellContents = DocString " body row 2 ", + tableCellContents = DocString "body row 2", tableCellRowspan = 1}, TableCell {tableCellColspan = 3, - tableCellContents = DocString " Cells may span columns. ", + tableCellContents = DocString "Cells may span columns.", tableCellRowspan = 1}], TableRow [TableCell {tableCellColspan = 1, - tableCellContents = DocString " body row 3 ", + tableCellContents = DocString "body row 3", tableCellRowspan = 1}, TableCell {tableCellColspan = 1, tableCellContents = DocString - (concat - [" Cells may \n", - " span rows. \n", - " "]), + (concat ["Cells may\n", "span rows.\n"]), tableCellRowspan = 2}, TableCell {tableCellColspan = 2, - tableCellContents = DocAppend - (DocString " ") - (DocAppend - (DocMathDisplay - (concat - [" \n", - " f(n) = \\sum_{i=1} \n", - " "])) - (DocString " ")), + tableCellContents = DocMathDisplay + (concat ["\n", "f(n) = \\sum_{i=1}\n"]), tableCellRowspan = 2}], TableRow [TableCell {tableCellColspan = 1, - tableCellContents = DocString " body row 4 ", + tableCellContents = DocString "body row 4", tableCellRowspan = 1}]], tableHeaderRows = [TableRow [TableCell {tableCellColspan = 1, tableCellContents = DocString (concat - [" Header row, column 1 \n", - " (header rows optional) "]), + ["Header row, column 1\n", + "(header rows optional)"]), tableCellRowspan = 1}, TableCell {tableCellColspan = 1, - tableCellContents = DocString - (concat [" Header 2 \n", " "]), + tableCellContents = DocString "Header 2\n", tableCellRowspan = 1}, TableCell {tableCellColspan = 1, - tableCellContents = DocString - (concat [" Header 3 \n", " "]), + tableCellContents = DocString "Header 3\n", tableCellRowspan = 1}, TableCell {tableCellColspan = 1, - tableCellContents = DocString - (concat [" Header 4 \n", " "]), + tableCellContents = DocString "Header 4\n", tableCellRowspan = 1}]]} diff --git a/haddock-library/fixtures/examples/table2.parsed b/haddock-library/fixtures/examples/table2.parsed index e3dbf0b4..44cc813b 100644 --- a/haddock-library/fixtures/examples/table2.parsed +++ b/haddock-library/fixtures/examples/table2.parsed @@ -3,44 +3,44 @@ DocTable {tableBodyRows = [TableRow [TableCell {tableCellColspan = 1, - tableCellContents = DocString " row 1, col 1 ", + tableCellContents = DocString "row 1, col 1", tableCellRowspan = 1}, TableCell {tableCellColspan = 1, - tableCellContents = DocString " column 2 ", + tableCellContents = DocString "column 2", tableCellRowspan = 1}, TableCell {tableCellColspan = 1, - tableCellContents = DocString " column 3 ", + tableCellContents = DocString "column 3", tableCellRowspan = 1}, TableCell {tableCellColspan = 1, - tableCellContents = DocString " column 4 ", + tableCellContents = DocString "column 4", tableCellRowspan = 1}], TableRow [TableCell {tableCellColspan = 1, - tableCellContents = DocString " row 2 ", + tableCellContents = DocString "row 2", tableCellRowspan = 1}, TableCell {tableCellColspan = 3, - tableCellContents = DocString " ", + tableCellContents = DocEmpty, tableCellRowspan = 1}], TableRow [TableCell {tableCellColspan = 1, - tableCellContents = DocString " row 3 ", + tableCellContents = DocString "row 3", tableCellRowspan = 1}, TableCell {tableCellColspan = 1, - tableCellContents = DocString " ", + tableCellContents = DocEmpty, tableCellRowspan = 1}, TableCell {tableCellColspan = 1, - tableCellContents = DocString " ", + tableCellContents = DocEmpty, tableCellRowspan = 1}, TableCell {tableCellColspan = 1, - tableCellContents = DocString " ", + tableCellContents = DocEmpty, tableCellRowspan = 1}]], tableHeaderRows = []} diff --git a/haddock-library/fixtures/examples/table3.parsed b/haddock-library/fixtures/examples/table3.parsed index cabff9cb..c978b50f 100644 --- a/haddock-library/fixtures/examples/table3.parsed +++ b/haddock-library/fixtures/examples/table3.parsed @@ -3,48 +3,48 @@ DocTable {tableBodyRows = [TableRow [TableCell {tableCellColspan = 1, - tableCellContents = DocString " row 1, col 1 ", + tableCellContents = DocString "row 1, col 1", tableCellRowspan = 1}, TableCell {tableCellColspan = 1, - tableCellContents = DocString " column 2 ", + tableCellContents = DocString "column 2", tableCellRowspan = 1}, TableCell {tableCellColspan = 1, - tableCellContents = DocString " column 3 ", + tableCellContents = DocString "column 3", tableCellRowspan = 1}, TableCell {tableCellColspan = 1, - tableCellContents = DocString " column 4 ", + tableCellContents = DocString "column 4", tableCellRowspan = 1}], TableRow [TableCell {tableCellColspan = 1, - tableCellContents = DocString " row 2 ", + tableCellContents = DocString "row 2", tableCellRowspan = 1}, TableCell {tableCellColspan = 2, - tableCellContents = DocString " Use the command ``ls ", + tableCellContents = DocString "Use the command ``ls", tableCellRowspan = 1}, TableCell {tableCellColspan = 1, - tableCellContents = DocString " more``. ", + tableCellContents = DocString "more``.", tableCellRowspan = 1}], TableRow [TableCell {tableCellColspan = 1, - tableCellContents = DocString " row 3 ", + tableCellContents = DocString "row 3", tableCellRowspan = 1}, TableCell {tableCellColspan = 1, - tableCellContents = DocString " ", + tableCellContents = DocEmpty, tableCellRowspan = 1}, TableCell {tableCellColspan = 1, - tableCellContents = DocString " ", + tableCellContents = DocEmpty, tableCellRowspan = 1}, TableCell {tableCellColspan = 1, - tableCellContents = DocString " ", + tableCellContents = DocEmpty, tableCellRowspan = 1}]], tableHeaderRows = []} diff --git a/haddock-library/fixtures/examples/table4.parsed b/haddock-library/fixtures/examples/table4.parsed index cfdd6f0f..c4dabb0d 100644 --- a/haddock-library/fixtures/examples/table4.parsed +++ b/haddock-library/fixtures/examples/table4.parsed @@ -8,10 +8,10 @@ DocAppend {tableCellColspan = 1, tableCellContents = DocString (concat - [" outer \n", - " \n", - "-------+ \n", - " inner | "]), + ["outer\n", + "\n", + "-------+\n", + "inner |"]), tableCellRowspan = 1}]], tableHeaderRows = []}) (DocAppend @@ -21,6 +21,6 @@ DocAppend {tableBodyRows = [TableRow [TableCell {tableCellColspan = 1, - tableCellContents = DocString " inner ", + tableCellContents = DocString "inner", tableCellRowspan = 1}]], tableHeaderRows = []}))) diff --git a/haddock-library/fixtures/examples/table5.parsed b/haddock-library/fixtures/examples/table5.parsed index 9a547ad3..f9a387bb 100644 --- a/haddock-library/fixtures/examples/table5.parsed +++ b/haddock-library/fixtures/examples/table5.parsed @@ -4,50 +4,43 @@ DocTable [TableCell {tableCellColspan = 1, tableCellContents = DocString - (concat - [" row 2 \n", - " \n", - " \n", - " row 3 "]), + (concat ["row 2\n", "\n", "\n", "row 3"]), tableCellRowspan = 2}, TableCell {tableCellColspan = 3, tableCellContents = DocAppend - (DocString " Use the command ") + (DocString "Use the command ") (DocAppend (DocMonospaced (DocString "ls | more")) - (DocString - (concat - [". \n", - " "]))), + (DocString ".\n")), tableCellRowspan = 1}], TableRow [TableCell {tableCellColspan = 1, - tableCellContents = DocString " ", + tableCellContents = DocEmpty, tableCellRowspan = 1}, TableCell {tableCellColspan = 1, - tableCellContents = DocString " ", + tableCellContents = DocEmpty, tableCellRowspan = 1}, TableCell {tableCellColspan = 1, - tableCellContents = DocString " ", + tableCellContents = DocEmpty, tableCellRowspan = 1}]], tableHeaderRows = [TableRow [TableCell {tableCellColspan = 1, - tableCellContents = DocString " row 1, col 1 ", + tableCellContents = DocString "row 1, col 1", tableCellRowspan = 1}, TableCell {tableCellColspan = 1, - tableCellContents = DocString " column 2 ", + tableCellContents = DocString "column 2", tableCellRowspan = 1}, TableCell {tableCellColspan = 1, - tableCellContents = DocString " column 3 ", + tableCellContents = DocString "column 3", tableCellRowspan = 1}, TableCell {tableCellColspan = 1, - tableCellContents = DocString " column 4 ", + tableCellContents = DocString "column 4", tableCellRowspan = 1}]]} diff --git a/haddock-library/src/Documentation/Haddock/Parser.hs b/haddock-library/src/Documentation/Haddock/Parser.hs index 028422a7..17240792 100644 --- a/haddock-library/src/Documentation/Haddock/Parser.hs +++ b/haddock-library/src/Documentation/Haddock/Parser.hs @@ -267,7 +267,7 @@ picture = DocPic . makeLabeled Picture -- >>> parseString "\\(\\int_{-\\infty}^{\\infty} e^{-x^2/2} = \\sqrt{2\\pi}\\)" -- DocMathInline "\\int_{-\\infty}^{\\infty} e^{-x^2/2} = \\sqrt{2\\pi}" mathInline :: Parser (DocH mod a) -mathInline = DocMathInline . T.unpack +mathInline = DocMathInline . T.unpack <$> disallowNewline ("\\(" *> takeUntil "\\)") -- | Display math parser, surrounded by \\[ and \\]. @@ -492,7 +492,7 @@ tableStepFour rs hdrIndex cells = case hdrIndex of -- extract cell contents given boundaries extract :: Int -> Int -> Int -> Int -> Text extract x y x2 y2 = T.intercalate "\n" - [ T.take (x2 - x + 1) $ T.drop x $ rs !! y' + [ T.stripEnd $ T.stripStart $ T.take (x2 - x + 1) $ T.drop x $ rs !! y' | y' <- [y .. y2] ] @@ -579,7 +579,7 @@ definitionList indent = DocDefList <$> p Right i -> (label, contents) : i -- | Drops all trailing newlines. -dropNLs :: Text -> Text +dropNLs :: Text -> Text dropNLs = T.dropWhileEnd (== '\n') -- | Main worker for 'innerList' and 'definitionList'. @@ -653,7 +653,7 @@ takeNonEmptyLine = do -- -- More precisely: skips all whitespace-only lines and returns indentation -- (horizontal space, might be empty) of that non-empty line. -takeIndent :: Parser Text +takeIndent :: Parser Text takeIndent = do indent <- takeHorizontalSpace choice' [ "\n" *> takeIndent @@ -711,14 +711,14 @@ examples = DocExamples <$> (many (try (skipHorizontalSpace *> "\n")) *> go) substituteBlankLine "" = "" substituteBlankLine xs = xs -nonEmptyLine :: Parser Text +nonEmptyLine :: Parser Text nonEmptyLine = try (mfilter (T.any (not . isSpace)) takeLine) takeLine :: Parser Text takeLine = try (takeWhile (/= '\n') <* endOfLine) endOfLine :: Parser () -endOfLine = void "\n" <|> Parsec.eof +endOfLine = void "\n" <|> Parsec.eof -- | Property parser. -- @@ -800,7 +800,7 @@ autoUrl :: Parser (DocH mod a) autoUrl = mkLink <$> url where url = mappend <$> choice' [ "http://", "https://", "ftp://"] <*> takeWhile1 (not . isSpace) - + mkLink :: Text -> DocH mod a mkLink s = case T.unsnoc s of Just (xs,x) | x `elem` (",.!?" :: String) -> DocHyperlink (mkHyperlink xs) `docAppend` DocString [x] diff --git a/html-test/ref/Table.html b/html-test/ref/Table.html index 8a299018..26b0254d 100644 --- a/html-test/ref/Table.html +++ b/html-test/ref/Table.html @@ -87,33 +87,33 @@ > code code message message description description 200 200 OK operation successful operation successful 204 204 No Content operation successful, no body returned operation successful, no body returned 200 200 OK operation successful operation successful 204 204 No Content operation successful, no body returned operation successful, no body returned 404 404 Not Found resource not found resource not found Header row, column 1 - (header rows optional) Header row, column 1 +(header rows optional) Header 2 - Header 2 + Header 3 - Header 3 + Header 4 - Header 4 + body row 1, column 1 body row 1, column 1 column 2 column 2 column 3 column 3 column 4 column 4 tableWithHeader Cells may span columns. Cells may span columns. body row 3 body row 3 Cells may - span rows. - Cells may +span rows. + \[ - f(n) = \sum_{i=1} - \] \[ +f(n) = \sum_{i=1} +\] body row 4 body row 4