Skip to content

Commit

Permalink
RST reader: avoid putting metadata in Para.
Browse files Browse the repository at this point in the history
Create MetaInlines when possible, just as with markdown input.
MetaBlocks is still used when there are multiple paragraphs or
non-paragraph content.

This change also affects field lists.

Closes #7766.
  • Loading branch information
jgm committed Oct 16, 2024
1 parent 019f5d1 commit 812b264
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 27 deletions.
17 changes: 9 additions & 8 deletions src/Text/Pandoc/Readers/RST.hs
Original file line number Diff line number Diff line change
Expand Up @@ -117,18 +117,16 @@ titleTransform (bs, meta) =

metaFromDefList :: [([Inline], [[Block]])] -> Meta -> Meta
metaFromDefList ds meta = adjustAuthors $ foldr f meta ds
where f (k,v) = setMeta (T.toLower $ stringify k) (mconcat $ map fromList v)
where f (k,v) =
case v of
[[Plain ils]] -> setMeta (T.toLower (stringify k)) $ MetaInlines ils
_ -> setMeta (T.toLower (stringify k)) $ mconcat $ map fromList v
adjustAuthors (Meta metamap) = Meta $ M.adjust splitAuthors "author"
$ M.adjust toPlain "date"
$ M.adjust toPlain "title"
$ M.mapKeys (\k ->
if k == "authors"
then "author"
else k) metamap
toPlain (MetaBlocks [Para xs]) = MetaInlines xs
toPlain x = x
splitAuthors (MetaBlocks [Para xs])
= MetaList $ map MetaInlines
splitAuthors (MetaInlines xs) = MetaList $ map MetaInlines
$ splitAuthors' xs
splitAuthors x = x
splitAuthors' = map normalizeSpaces .
Expand Down Expand Up @@ -284,7 +282,10 @@ fieldListItem minIndent = try $ do
term <- parseInlineFromText name
contents <- parseFromString' parseBlocks raw
optional blanklines
return (term, [contents])
let defn = case B.toList contents of
[Para ils] -> [B.plain $ B.fromList ils] -- see #7766
_ -> [contents]
return (term, defn)

fieldList :: PandocMonad m => RSTParser m Blocks
fieldList = try $ do
Expand Down
24 changes: 12 additions & 12 deletions test/Tests/Readers/RST.hs
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ tests = [ "line block with blank line" =:
, " on two lines" ]
=?>
doc (para "para" <>
definitionList [ (str "Hostname", [para "media08"])
, (text "IP address", [para "10.0.0.19"])
, (str "Size", [para "3ru"])
, (str "Version", [para "1"])
, (str "Indentation", [para "Since the field marker may be quite long, the second\nand subsequent lines of the field body do not have to line up\nwith the first line, but they must be indented relative to the\nfield name marker, and they must line up with each other."])
, (text "Parameter i", [para "integer"])
, (str "Final", [para "item\non two lines"])
definitionList [ (str "Hostname", [plain "media08"])
, (text "IP address", [plain "10.0.0.19"])
, (str "Size", [plain "3ru"])
, (str "Version", [plain "1"])
, (str "Indentation", [plain "Since the field marker may be quite long, the second\nand subsequent lines of the field body do not have to line up\nwith the first line, but they must be indented relative to the\nfield name marker, and they must line up with each other."])
, (text "Parameter i", [plain "integer"])
, (str "Final", [plain "item\non two lines"])
])
, "metadata" =: T.unlines
[ "====="
Expand All @@ -69,7 +69,7 @@ tests = [ "line block with blank line" =:
, ":Version: 1"
]
=?>
setMeta "version" (para "1") (setMeta "title" ("Title" :: Inlines)
setMeta "version" (str "1") (setMeta "title" ("Title" :: Inlines)
$ setMeta "subtitle" ("Subtitle" :: Inlines)
$ doc mempty)
, "with inline markup" =: T.unlines
Expand All @@ -87,10 +87,10 @@ tests = [ "line block with blank line" =:
]
=?>
setMeta "date" (str "today") (doc
$ definitionList [ (emph "one", [para "emphasis"])
, (link "http://example.com" "" "two", [para "reference"])
, (link "http://example.org" "" "three", [para "another one"])
, (code "four", [para "literal"])
$ definitionList [ (emph "one", [plain "emphasis"])
, (link "http://example.com" "" "two", [plain "reference"])
, (link "http://example.org" "" "three", [plain "another one"])
, (code "four", [plain "literal"])
])
]
, "URLs with following punctuation" =:
Expand Down
14 changes: 7 additions & 7 deletions test/rst-reader.native
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Pandoc
, MetaInlines
[ Str "July" , Space , Str "17," , Space , Str "2006" ]
)
, ( "revision" , MetaBlocks [ Para [ Str "3" ] ] )
, ( "revision" , MetaInlines [ Str "3" ] )
, ( "subtitle" , MetaInlines [ Str "Subtitle" ] )
, ( "title"
, MetaInlines
Expand Down Expand Up @@ -598,11 +598,11 @@ Pandoc
, BlockQuote
[ DefinitionList
[ ( [ Str "address" ]
, [ [ Para [ Str "61" , Space , Str "Main" , Space , Str "St." ] ]
, [ [ Plain [ Str "61" , Space , Str "Main" , Space , Str "St." ] ]
]
)
, ( [ Str "city" ]
, [ [ Para
, [ [ Plain
[ Emph [ Str "Nowhere" ]
, Str ","
, Space
Expand All @@ -613,16 +613,16 @@ Pandoc
]
]
)
, ( [ Str "phone" ] , [ [ Para [ Str "123-4567" ] ] ] )
, ( [ Str "phone" ] , [ [ Plain [ Str "123-4567" ] ] ] )
]
]
, DefinitionList
[ ( [ Str "address" ]
, [ [ Para [ Str "61" , Space , Str "Main" , Space , Str "St." ] ]
, [ [ Plain [ Str "61" , Space , Str "Main" , Space , Str "St." ] ]
]
)
, ( [ Str "city" ]
, [ [ Para
, [ [ Plain
[ Emph [ Str "Nowhere" ]
, Str ","
, Space
Expand All @@ -633,7 +633,7 @@ Pandoc
]
]
)
, ( [ Str "phone" ] , [ [ Para [ Str "123-4567" ] ] ] )
, ( [ Str "phone" ] , [ [ Plain [ Str "123-4567" ] ] ] )
]
, Header
1 ( "html-blocks" , [] , [] ) [ Str "HTML" , Space , Str "Blocks" ]
Expand Down

0 comments on commit 812b264

Please sign in to comment.