-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Parse base64-encoded data URIs more efficiently #10434
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
{-# LANGUAGE FlexibleContexts #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{- | | ||
Module : Text.Pandoc.Parsing.Base64 | ||
Copyright : © 2024 Evan Silberman | ||
License : GPL-2.0-or-later | ||
Maintainer : John MacFarlane <jgm@berkeley.edu> | ||
|
||
Parse large base64 strings efficiently within Pandoc's | ||
normal parsing environment | ||
-} | ||
|
||
module Text.Pandoc.Parsing.Base64 | ||
( parseBase64String ) | ||
|
||
where | ||
|
||
import Data.Text as T | ||
import Data.Attoparsec.Text as A | ||
import Text.Parsec (ParsecT, getInput, setInput, incSourceColumn) | ||
import Text.Pandoc.Sources | ||
import Control.Monad (mzero) | ||
|
||
parseBase64String :: Monad m => ParsecT Sources u m Text | ||
parseBase64String = do | ||
Sources ((pos, txt):rest) <- getInput | ||
let r = A.parse pBase64 txt | ||
case r of | ||
Done remaining consumed -> do | ||
let pos' = incSourceColumn pos (T.length consumed) | ||
setInput $ Sources ((pos', remaining):rest) | ||
return consumed | ||
_ -> mzero | ||
|
||
pBase64 :: A.Parser Text | ||
pBase64 = do | ||
most <- A.takeWhile1 (A.inClass "A-Za-z0-9+/") | ||
rest <- A.takeWhile (== '=') | ||
return $ most <> rest | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1822,13 +1822,35 @@ parenthesizedChars = do | |
result <- charsInBalanced '(' ')' litChar | ||
return $ "(" <> result <> ")" | ||
|
||
pBase64DataURI :: PandocMonad m => MarkdownParser m Text | ||
pBase64DataURI = mconcat <$> sequence | ||
[ (textStr "data:") | ||
, (T.singleton <$> (letter <|> digit)) | ||
, restrictedName | ||
, (T.singleton <$> char '/') | ||
, restrictedName | ||
, textStr ";" <* trace "cool" | ||
, (mconcat <$> many mediaParam) | ||
, textStr "base64," <* trace "fine" | ||
, parseBase64String | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if we replace l.1835 with
and get rid of the special T.P.Parsing.Base64 module. What is the impact on performance on the sorts of files that were problematic before? I'd like to try the simplest possible thing before worrying about possible optimizations. |
||
] | ||
where | ||
restrictedName = manyChar (alphaNum <|> oneOf "!#$&^_.+-") | ||
mediaParam = mconcat <$> sequence | ||
[ notFollowedBy (textStr "base64,") *> mempty -- XXX ??? | ||
, restrictedName | ||
, textStr "=" | ||
, manyChar (noneOf ";") | ||
, textStr ";" | ||
] | ||
|
||
-- source for a link, with optional title | ||
source :: PandocMonad m => MarkdownParser m (Text, Text) | ||
source = do | ||
char '(' | ||
skipSpaces | ||
let urlChunk = | ||
try parenthesizedChars | ||
let urlChunk = try pBase64DataURI | ||
<|> try parenthesizedChars | ||
<|> (notFollowedBy (oneOf " )") >> litChar) | ||
<|> try (many1Char spaceChar <* notFollowedBy (oneOf "\"')")) | ||
let sourceURL = T.unwords . T.words . T.concat <$> many urlChunk | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two thoughts on this:
Data.Text.takeWhile
?getInput
andsetInput
, are problematic in parsec because parsec doesn't realize that input has been consumed. I've had to use a regular parsec parser somewhere in there to make it realize this. One option is just something likecount charsConsumed anyChar
, and then you don't need to compute the end position manually...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
inClass
function from attoparsec and use it with regular texttakeWhile
, will have to fiddle with it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's probably fine to use attoparsec, but there might be a slight speedup if you can avoid it.
On 2: you could try putting this parser under
many
and see if parsec complains.