git.iko.soy/typeable/pandoc
on master help sign in
Improvements to RST reader:

+ Allow field lists to be indented. + Parse the contents of field lists instead of treating them as raw strings. + Represent field lists as definition lists rather than blockquotes. + Fixed bug in which metadata would be overridden if the document contained more than one field list. + Parse fields associated with ..image: blocks, and use the 'alt' field, if present, for image alt text and title. git-svn-id: https://pandoc.googlecode.com/svn/trunk@1050 788f1e2b-df1e-0410-8736-df70ead52e1b

commit8144c54f826d4d8b995e0e267a908dff459704ad authorfiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b> dateSat 13 Oct 2007, 21:39 (19 years ago) parent11e7ad2
3 files changed+81−34
src/Text/Pandoc/Readers/RST.hs+38−31 tests/rst-reader.native+26−3 tests/rst-reader.rst+17
src/Text/Pandoc/Readers/RST.hs+38−31
@@ -113,13 +113,13 @@ parseBlocks = manyTill block eof
113113block = choice [ codeBlock
114114 , rawHtmlBlock
115115 , rawLaTeXBlock
116+ , fieldList
116117 , blockQuote
117118 , imageBlock
118119 , unknownDirective
119120 , header
120121 , hrule
121122 , list
122− , fieldList
123123 , lineBlock
124124 , para
125125 , plain
@@ -129,36 +129,42 @@ block = choice [ codeBlock
129129-- field list
130130--
131131
132−fieldListItem = try $ do
132+fieldListItem indent = try $ do
133+ string indent
133134 char ':'
134135 name <- many1 alphaNum
135136 string ": "
136137 skipSpaces
137138 first <- manyTill anyChar newline
138− rest <- many (notFollowedBy ((char ':') <|> blankline) >>
139− skipSpaces >> manyTill anyChar newline)
140− return $ (name, (joinWithSep " " (first:rest)))
139+ rest <- option "" $ try $ lookAhead (string indent >> oneOf " \t") >>
140+ indentedBlock
141+ return (name, joinWithSep " " (first:(lines rest)))
141142
142143fieldList = try $ do
143− items <- many1 fieldListItem
144+ indent <- lookAhead $ many (oneOf " \t")
145+ items <- many1 $ fieldListItem indent
144146 blanklines
145147 let authors = case lookup "Authors" items of
146148 Just auth -> [auth]
147149 Nothing -> map snd (filter (\(x,y) -> x == "Author") items)
148− let date = case (lookup "Date" items) of
149− Just dat -> dat
150− Nothing -> ""
151− let title = case (lookup "Title" items) of
152− Just tit -> [Str tit]
153− Nothing -> []
150+ if null authors
151+ then return ()
152+ else updateState $ \st -> st {stateAuthors = authors}
153+ case (lookup "Date" items) of
154+ Just dat -> updateState $ \st -> st {stateDate = dat}
155+ Nothing -> return ()
156+ case (lookup "Title" items) of
157+ Just tit -> parseFromString (many inline) tit >>=
158+ \t -> updateState $ \st -> st {stateTitle = t}
159+ Nothing -> return ()
154160 let remaining = filter (\(x,y) -> (x /= "Authors") && (x /= "Author") &&
155161 (x /= "Date") && (x /= "Title")) items
156− let result = map (\(x,y) ->
157− Para [Strong [Str x], Str ":", Space, Str y]) remaining
158− updateState (\st -> st { stateAuthors = authors,
159− stateDate = date,
160− stateTitle = title })
161− return $ BlockQuote result
162+ if null remaining
163+ then return Null
164+ else do terms <- mapM (return . (:[]) . Str . fst) remaining
165+ defs <- mapM (parseFromString (many block) . snd)
166+ remaining
167+ return $ DefinitionList $ zip terms defs
162168
163169--
164170-- line block
@@ -207,8 +213,12 @@ plain = many1 inline >>= return . Plain . normalizeSpaces
207213imageBlock = try $ do
208214 string ".. image:: "
209215 src <- manyTill anyChar newline
210− return $ Plain [Image [Str "image"] (src, "")]
211−
216+ fields <- option [] $ do indent <- lookAhead $ many (oneOf " /t")
217+ many1 $ fieldListItem indent
218+ optional blanklines
219+ case lookup "alt" fields of
220+ Just alt -> return $ Plain [Image [Str alt] (src, alt)]
221+ Nothing -> return $ Plain [Image [Str "image"] (src, "")]
212222--
213223-- header blocks
214224--
@@ -282,17 +292,14 @@ indentedLine indents = try $ do
282292
283293-- two or more indented lines, possibly separated by blank lines.
284294-- any amount of indentation will work.
285−indentedBlock = try $ do
286− state <- getState
287− let tabStop = stateTabStop state
288− indents <- many1 (oneOf " \t")
289− firstline <- manyTill anyChar newline
290− rest <- many (choice [ indentedLine indents,
291− try (do b <- blanklines
292− l <- indentedLine indents
293− return (b ++ l))])
294− optional blanklines
295− return $ firstline ++ "\n" ++ concat rest
295+indentedBlock = do
296+ indents <- lookAhead $ many1 (oneOf " \t")
297+ lns <- many $ choice $ [ indentedLine indents,
298+ try $ do b <- blanklines
299+ l <- indentedLine indents
300+ return (b ++ l) ]
301+ optional blanklines
302+ return $ concat lns
296303
297304codeBlock = try $ do
298305 codeBlockStart
tests/rst-reader.native+26−3
@@ -1,7 +1,8 @@
11Pandoc (Meta [Str "Pandoc",Space,Str "Test",Space,Str "Suite",Str ":",Space,Str "Subtitle"] ["John MacFarlane","Anonymous"] "July 17, 2006")
2−[ BlockQuote
3− [ Para [Strong [Str "Revision"],Str ":",Space,Str "3"] ]
4−
2+[ DefinitionList
3+ [ ([Str "Revision"],
4+ [ Plain [Str "3"] ]
5+ ) ]
56, Header 1 [Str "Level",Space,Str "one",Space,Str "header"]
67, Para [Str "This",Space,Str "is",Space,Str "a",Space,Str "set",Space,Str "of",Space,Str "tests",Space,Str "for",Space,Str "pandoc.",Space,Str "Most",Space,Str "of",Space,Str "them",Space,Str "are",Space,Str "adapted",Space,Str "from",Space,Str "John",Space,Str "Gruber's",Space,Str "markdown",Space,Str "test",Space,Str "suite."]
78, Header 2 [Str "Level",Space,Str "two",Space,Str "header"]
@@ -156,6 +157,27 @@ Pandoc (Meta [Str "Pandoc",Space,Str "Test",Space,Str "Suite",Str ":",Space,Str
156157 ([Str "term",Space,Str "with",Space,Emph [Str "emphasis"]],
157158 [ Para [Str "Definition",Space,Str "3."] ]
158159 ) ]
160+, Header 1 [Str "Field",Space,Str "Lists"]
161+, DefinitionList
162+ [ ([Str "address"],
163+ [ Plain [Str "61",Space,Str "Main",Space,Str "St."] ]
164+ ),
165+ ([Str "city"],
166+ [ Plain [Emph [Str "Nowhere"],Str ",",Space,Str "MA,",Space,Str "USA"] ]
167+ ),
168+ ([Str "phone"],
169+ [ Plain [Str "123",Str "-",Str "4567"] ]
170+ ) ]
171+, DefinitionList
172+ [ ([Str "address"],
173+ [ Plain [Str "61",Space,Str "Main",Space,Str "St."] ]
174+ ),
175+ ([Str "city"],
176+ [ Plain [Emph [Str "Nowhere"],Str ",",Space,Str "MA,",Space,Str "USA"] ]
177+ ),
178+ ([Str "phone"],
179+ [ Plain [Str "123",Str "-",Str "4567"] ]
180+ ) ]
159181, Header 1 [Str "HTML",Space,Str "Blocks"]
160182, Para [Str "Simple",Space,Str "block",Space,Str "on",Space,Str "one",Space,Str "line",Str ":"]
161183, RawHtml "<div>foo</div>\n"
@@ -207,5 +229,6 @@ Pandoc (Meta [Str "Pandoc",Space,Str "Test",Space,Str "Suite",Str ":",Space,Str
207229, Header 1 [Str "Images"]
208230, Para [Str "From",Space,Str "\"Voyage",Space,Str "dans",Space,Str "la",Space,Str "Lune\"",Space,Str "by",Space,Str "Georges",Space,Str "Melies",Space,Str "(1902)",Str ":"]
209231, Plain [Image [Str "image"] ("lalune.jpg","")]
232+, Plain [Image [Str "Voyage dans la Lune"] ("lalune.jpg","Voyage dans la Lune")]
210233, Para [Str "Here",Space,Str "is",Space,Str "a",Space,Str "movie",Space,Image [Str "movie"] ("movie.jpg",""),Space,Str "icon."] ]
211234
tests/rst-reader.rst+17
@@ -256,6 +256,19 @@ term 2
256256term with *emphasis*
257257 Definition 3.
258258
259+Field Lists
260+===========
261+
262+ :address: 61 Main St.
263+ :city: *Nowhere*, MA,
264+ USA
265+ :phone: 123-4567
266+
267+:address: 61 Main St.
268+:city: *Nowhere*, MA,
269+ USA
270+:phone: 123-4567
271+
259272HTML Blocks
260273===========
261274
@@ -382,6 +395,10 @@ From "Voyage dans la Lune" by Georges Melies (1902):
382395
383396.. image:: lalune.jpg
384397
398+.. image:: lalune.jpg
399+ :height: 2343
400+ :alt: Voyage dans la Lune
401+
385402Here is a movie |movie| icon.
386403
387404.. |movie| image:: movie.jpg