git.iko.soy/typeable/pandoc
on master help sign in
SelfContained: handle @import with quoted string.
commit93eaf33e6e7fbb364c83e6bde66f253a8b14297b authorJohn MacFarlane <jgm@berkeley.edu> dateSat 20 May 2017, 17:27 (9 years ago) parent8d4fbe6
1 file changed+25−11
src/Text/Pandoc/SelfContained.hs+25−11
src/Text/Pandoc/SelfContained.hs+25−11
@@ -159,9 +159,10 @@ pCSSImport :: PandocMonad m => Maybe String -> FilePath
159159pCSSImport sourceURL d = P.try $ do
160160 P.string "@import"
161161 P.spaces
162− res <- pCSSUrl' sourceURL d
162+ res <- (pQuoted <|> pUrl) >>= handleCSSUrl sourceURL d
163+ P.spaces
164+ P.char ';'
163165 P.spaces
164− P.optional $ P.char ';' >> P.spaces
165166 case res of
166167 Left b -> return $ B.pack "@import " <> b
167168 Right (_, b) -> return b
@@ -185,31 +186,44 @@ pCSSOther = do
185186pCSSUrl :: PandocMonad m
186187 => Maybe String -> FilePath -> ParsecT ByteString () m ByteString
187188pCSSUrl sourceURL d = P.try $ do
188− res <- pCSSUrl' sourceURL d
189+ res <- pUrl >>= handleCSSUrl sourceURL d
189190 case res of
190191 Left b -> return b
191192 Right (mt,b) -> do
192193 let enc = makeDataURI (mt, b)
193194 return (B.pack $ "url(" ++ enc ++ ")")
194195
195−pCSSUrl' :: PandocMonad m
196− => Maybe String -> FilePath
197− -> ParsecT ByteString () m (Either ByteString (MimeType, ByteString))
198−pCSSUrl' sourceURL d = P.try $ do
196+pQuoted :: PandocMonad m
197+ => ParsecT ByteString () m (String, ByteString)
198+pQuoted = P.try $ do
199+ quote <- P.oneOf "\"'"
200+ url <- P.manyTill P.anyChar (P.char quote)
201+ let fallback = B.pack ([quote] ++ trim url ++ [quote])
202+ return (url, fallback)
203+
204+pUrl :: PandocMonad m
205+ => ParsecT ByteString () m (String, ByteString)
206+pUrl = P.try $ do
199207 P.string "url("
200208 P.spaces
201209 quote <- P.option Nothing (Just <$> P.oneOf "\"'")
202210 url <- P.manyTill P.anyChar (maybe (P.lookAhead (P.char ')')) P.char quote)
203211 P.spaces
204212 P.char ')'
205− let fallback = Left $
206− B.pack ("url(" ++ maybe "" (:[]) quote ++ trim url ++
213+ let fallback = B.pack ("url(" ++ maybe "" (:[]) quote ++ trim url ++
207214 maybe "" (:[]) quote ++ ")")
215+ return (url, fallback)
216+
217+handleCSSUrl :: PandocMonad m
218+ => Maybe String -> FilePath -> (String, ByteString)
219+ -> ParsecT ByteString () m
220+ (Either ByteString (MimeType, ByteString))
221+handleCSSUrl sourceURL d (url, fallback) = do
208222 -- pipes are used in URLs provided by Google Code fonts
209223 -- but parseURI doesn't like them, so we escape them:
210224 case escapeURIString (/='|') (trim url) of
211− '#':_ -> return fallback
212− 'd':'a':'t':'a':':':_ -> return fallback
225+ '#':_ -> return $ Left fallback
226+ 'd':'a':'t':'a':':':_ -> return $ Left fallback
213227 u -> do let url' = if isURI u then u else d </> u
214228 res <- lift $ getData sourceURL "" url'
215229 case res of