git.iko.soy/typeable/pandoc
on master help sign in
Merge pull request #2266 from PromyLOPh/fieldinline

RST: Support inline markup for field list names

commitb587acb2247c5af49142542dec2a509d8aa6862f authorJohn MacFarlane <jgm@berkeley.edu> dateWed 8 Jul 2015, 22:45 (11 years ago) parentac79429d9e17cb
2 files changed+49−31
src/Text/Pandoc/Readers/RST.hs+11−15 tests/Tests/Readers/RST.hs+38−16
src/Text/Pandoc/Readers/RST.hs+11−15
@@ -209,7 +209,7 @@ rawFieldListItem minIndent = try $ do
209209fieldListItem :: Int -> RSTParser (Inlines, [Blocks])
210210fieldListItem minIndent = try $ do
211211 (name, raw) <- rawFieldListItem minIndent
212− let term = B.str name
212+ term <- parseInlineFromString name
213213 contents <- parseFromString parseBlocks raw
214214 optional blanklines
215215 return (term, [contents])
@@ -229,8 +229,7 @@ fieldList = try $ do
229229lineBlock :: RSTParser Blocks
230230lineBlock = try $ do
231231 lines' <- lineBlockLines
232− lines'' <- mapM (parseFromString
233− (trimInlines . mconcat <$> many inline)) lines'
232+ lines'' <- mapM parseInlineFromString lines'
234233 return $ B.para (mconcat $ intersperse B.linebreak lines'')
235234
236235--
@@ -549,39 +548,33 @@ directive' = do
549548 "role" -> addNewRole top $ map (\(k,v) -> (k, trim v)) fields
550549 "container" -> parseFromString parseBlocks body'
551550 "replace" -> B.para <$> -- consumed by substKey
552− parseFromString (trimInlines . mconcat <$> many inline)
553− (trim top)
551+ parseInlineFromString (trim top)
554552 "unicode" -> B.para <$> -- consumed by substKey
555− parseFromString (trimInlines . mconcat <$> many inline)
556− (trim $ unicodeTransform top)
553+ parseInlineFromString (trim $ unicodeTransform top)
557554 "compound" -> parseFromString parseBlocks body'
558555 "pull-quote" -> B.blockQuote <$> parseFromString parseBlocks body'
559556 "epigraph" -> B.blockQuote <$> parseFromString parseBlocks body'
560557 "highlights" -> B.blockQuote <$> parseFromString parseBlocks body'
561− "rubric" -> B.para . B.strong <$> parseFromString
562− (trimInlines . mconcat <$> many inline) top
558+ "rubric" -> B.para . B.strong <$> parseInlineFromString top
563559 _ | label `elem` ["attention","caution","danger","error","hint",
564560 "important","note","tip","warning"] ->
565561 do let tit = B.para $ B.strong $ B.str label
566562 bod <- parseFromString parseBlocks $ top ++ "\n\n" ++ body'
567563 return $ B.blockQuote $ tit <> bod
568564 "admonition" ->
569− do tit <- B.para . B.strong <$> parseFromString
570− (trimInlines . mconcat <$> many inline) top
565+ do tit <- B.para . B.strong <$> parseInlineFromString top
571566 bod <- parseFromString parseBlocks body'
572567 return $ B.blockQuote $ tit <> bod
573568 "sidebar" ->
574569 do let subtit = maybe "" trim $ lookup "subtitle" fields
575− tit <- B.para . B.strong <$> parseFromString
576− (trimInlines . mconcat <$> many inline)
570+ tit <- B.para . B.strong <$> parseInlineFromString
577571 (trim top ++ if null subtit
578572 then ""
579573 else (": " ++ subtit))
580574 bod <- parseFromString parseBlocks body'
581575 return $ B.blockQuote $ tit <> bod
582576 "topic" ->
583− do tit <- B.para . B.strong <$> parseFromString
584− (trimInlines . mconcat <$> many inline) top
577+ do tit <- B.para . B.strong <$> parseInlineFromString top
585578 bod <- parseFromString parseBlocks body'
586579 return $ tit <> bod
587580 "default-role" -> mempty <$ updateState (\s ->
@@ -962,6 +955,9 @@ inline = choice [ whitespace
962955 , escapedChar
963956 , symbol ] <?> "inline"
964957
958+parseInlineFromString :: String -> RSTParser Inlines
959+parseInlineFromString = parseFromString (trimInlines . mconcat <$> many inline)
960+
965961hyphens :: RSTParser Inlines
966962hyphens = do
967963 result <- many1 (char '-')
tests/Tests/Readers/RST.hs+38−16
@@ -22,7 +22,8 @@ tests :: [Test]
2222tests = [ "line block with blank line" =:
2323 "| a\n|\n| b" =?> para (str "a") <>
2424 para (str "\160b")
25− , "field list" =: unlines
25+ , testGroup "field list"
26+ [ "general" =: unlines
2627 [ "para"
2728 , ""
2829 , ":Hostname: media08"
@@ -36,17 +37,17 @@ tests = [ "line block with blank line" =:
3637 , ":Parameter i: integer"
3738 , ":Final: item"
3839 , " on two lines" ]
39− =?> ( doc
40− $ para "para" <>
41− definitionList [ (str "Hostname", [para "media08"])
42− , (str "IP address", [para "10.0.0.19"])
43− , (str "Size", [para "3ru"])
44− , (str "Version", [para "1"])
45− , (str "Indentation", [para "Since the field marker may be quite long, the second and subsequent lines of the field body do not have to line up with the first line, but they must be indented relative to the field name marker, and they must line up with each other."])
46− , (str "Parameter i", [para "integer"])
47− , (str "Final", [para "item on two lines"])
48− ])
49− , "initial field list" =: unlines
40+ =?> ( doc
41+ $ para "para" <>
42+ definitionList [ (str "Hostname", [para "media08"])
43+ , (text "IP address", [para "10.0.0.19"])
44+ , (str "Size", [para "3ru"])
45+ , (str "Version", [para "1"])
46+ , (str "Indentation", [para "Since the field marker may be quite long, the second and subsequent lines of the field body do not have to line up with the first line, but they must be indented relative to the field name marker, and they must line up with each other."])
47+ , (text "Parameter i", [para "integer"])
48+ , (str "Final", [para "item on two lines"])
49+ ])
50+ , "metadata" =: unlines
5051 [ "====="
5152 , "Title"
5253 , "====="
@@ -56,10 +57,31 @@ tests = [ "line block with blank line" =:
5657 , ""
5758 , ":Version: 1"
5859 ]
59− =?> ( setMeta "version" (para "1")
60− $ setMeta "title" ("Title" :: Inlines)
61− $ setMeta "subtitle" ("Subtitle" :: Inlines)
62− $ doc mempty )
60+ =?> ( setMeta "version" (para "1")
61+ $ setMeta "title" ("Title" :: Inlines)
62+ $ setMeta "subtitle" ("Subtitle" :: Inlines)
63+ $ doc mempty )
64+ , "with inline markup" =: unlines
65+ [ ":*Date*: today"
66+ , ""
67+ , ".."
68+ , ""
69+ , ":*one*: emphasis"
70+ , ":two_: reference"
71+ , ":`three`_: another one"
72+ , ":``four``: literal"
73+ , ""
74+ , ".. _two: http://example.com"
75+ , ".. _three: http://example.org"
76+ ]
77+ =?> ( setMeta "date" (str "today")
78+ $ doc
79+ $ definitionList [ (emph "one", [para "emphasis"])
80+ , (link "http://example.com" "" "two", [para "reference"])
81+ , (link "http://example.org" "" "three", [para "another one"])
82+ , (code "four", [para "literal"])
83+ ])
84+ ]
6385 , "URLs with following punctuation" =:
6486 ("http://google.com, http://yahoo.com; http://foo.bar.baz.\n" ++
6587 "http://foo.bar/baz_(bam) (http://foo.bar)") =?>