git.iko.soy/typeable/pandoc
on master help sign in
RST reader: support scale and align attributes of images.

Closes #2662.

commit99b39ffc1726a86e6a6ad45ef6b81a28b46594ef authorJohn MacFarlane <jgm@berkeley.edu> dateSun 26 Feb 2017, 23:40 (10 years ago) parent092c8f7
3 files changed+37−6
src/Text/Pandoc/Readers/RST.hs+25−5 test/command/2662.md+11 test/rst-reader.native+1−1
src/Text/Pandoc/Readers/RST.hs+25−5
@@ -38,6 +38,7 @@ import Text.Pandoc.Parsing
3838import Text.Pandoc.Options
3939import Text.Pandoc.Logging
4040import Text.Pandoc.Error
41+import Text.Pandoc.ImageSize (lengthToDim, scaleDimension)
4142import Control.Monad ( when, liftM, guard, mzero )
4243import Data.List ( findIndex, intercalate, isInfixOf,
4344 transpose, sort, deleteFirstsBy, isSuffixOf , nub, union)
@@ -624,12 +625,31 @@ directive' = do
624625 body <- option "" $ try $ blanklines >> indentedBlock
625626 optional blanklines
626627 let body' = body ++ "\n\n"
627− imgAttr cl = ("", classes, getAtt "width" ++ getAtt "height")
628+ imgAttr cl = ("", classes, widthAttr ++ heightAttr)
628629 where
629− classes = words $ maybe "" trim $ lookup cl fields
630− getAtt k = case lookup k fields of
631− Just v -> [(k, filter (not . isSpace) v)]
632− Nothing -> []
630+ classes = words $ maybe "" trim (lookup cl fields) ++
631+ maybe "" (\x -> "align-" ++ trim x)
632+ (lookup "align" fields)
633+ scale = case trim <$> lookup "scale" fields of
634+ Just v -> case reverse v of
635+ '%':vv ->
636+ case safeRead (reverse vv) of
637+ Just (percent :: Double)
638+ -> percent / 100.0
639+ Nothing -> 1.0
640+ _ ->
641+ case safeRead v of
642+ Just (s :: Double) -> s
643+ Nothing -> 1.0
644+ Nothing -> 1.0
645+ widthAttr = maybe [] (\x -> [("width",
646+ show $ scaleDimension scale x)])
647+ $ lookup "width" fields >>=
648+ (lengthToDim . filter (not . isSpace))
649+ heightAttr = maybe [] (\x -> [("height",
650+ show $ scaleDimension scale x)])
651+ $ lookup "height" fields >>=
652+ (lengthToDim . filter (not . isSpace))
633653 case label of
634654 "table" -> tableDirective top fields body'
635655 "line-block" -> lineBlockDirective body'
test/command/2662.mdnew file+11
@@ -0,0 +1,11 @@
1+```
2+% pandoc -t html -f rst --wrap=none
3+.. image:: http://url.to.image/foo.png
4+ :align: left
5+ :height: 100px
6+ :width: 200 px
7+ :scale: 300 %
8+ :alt: alternate text
9+^D
10+<p><img src="http://url.to.image/foo.png" alt="alternate text" class="align-left" width="600" height="300" /></p>
11+```
test/rst-reader.native+1−1
@@ -223,7 +223,7 @@ Pandoc (Meta {unMeta = fromList [("author",MetaList [MetaInlines [Str "John",Spa
223223,Header 1 ("images",[],[]) [Str "Images"]
224224,Para [Str "From",Space,Quoted DoubleQuote [Str "Voyage",Space,Str "dans",Space,Str "la",Space,Str "Lune"],Space,Str "by",Space,Str "Georges",Space,Str "Melies",Space,Str "(1902):"]
225225,Para [Image ("",[],[]) [Str "image"] ("lalune.jpg","")]
226−,Para [Image ("",[],[("height","2343")]) [Str "Voyage dans la Lune"] ("lalune.jpg","")]
226+,Para [Image ("",[],[("height","2343px")]) [Str "Voyage dans la Lune"] ("lalune.jpg","")]
227227,Para [Str "Here",Space,Str "is",Space,Str "a",Space,Str "movie",Space,Image ("",[],[]) [Str "movie"] ("movie.jpg",""),Space,Str "icon."]
228228,Para [Str "And",Space,Str "an",Space,Link ("",[],[]) [Image ("",[],[]) [Str "A movie"] ("movie.jpg","")] ("/url",""),Str "."]
229229,Header 1 ("comments",[],[]) [Str "Comments"]