Lua: use sha1 instead of hashname.

Better to leave control over the extension to the user.
This commit is contained in:
John MacFarlane 2017-09-30 23:15:43 -07:00
parent 9451d83058
commit 8768f7e5b0
2 changed files with 9 additions and 18 deletions

View file

@ -1130,18 +1130,17 @@ storage. The "media bag" is used when pandoc is called with the
local filename = "media/diagram.png"
local mt, contents = pandoc.mediabag.lookup(filename)
[`hashname (mime_type, contents)`]{#mediabag-hashname}
[`sha1 (contents)`]{#mediabag-sha1}
: Returns a filename with a basename based on the SHA1 has of the
contents and an extension based on the mime type.
: Returns the SHA1 has of the contents.
Returns:
- Filename based on SHA1 hash.
- SHA1 hash of the contents.
Usage:
local fp = pandoc.mediabag.hashname("plain/text", "foobar")
local fp = pandoc.mediabag.sha1("foobar")
[`fetch (source, base_url)`]{#mediabag-fetch}

View file

@ -47,7 +47,7 @@ import Text.Pandoc.Class (readDataFile, runIO,
import Text.Pandoc.Options (ReaderOptions(readerExtensions))
import Text.Pandoc.Lua.StackInstances ()
import Text.Pandoc.Readers (Reader (..), getReader)
import Text.Pandoc.MIME (MimeType, extensionFromMimeType)
import Text.Pandoc.MIME (MimeType)
import Data.Digest.Pure.SHA (sha1, showDigest)
import qualified Foreign.Lua as Lua
@ -93,7 +93,7 @@ pushMediaBagModule commonState mediaBagRef = do
addFunction "lookup" (lookupMediaFn mediaBagRef)
addFunction "list" (mediaDirectoryFn mediaBagRef)
addFunction "fetch" (fetch commonState mediaBagRef)
addFunction "hashname" hashnameFn
addFunction "sha1" sha1HashFn
return ()
where
addFunction name fn = do
@ -101,20 +101,12 @@ pushMediaBagModule commonState mediaBagRef = do
Lua.pushHaskellFunction fn
Lua.rawset (-3)
hashnameFn :: OrNil MimeType
-> BL.ByteString
sha1HashFn :: BL.ByteString
-> Lua NumResults
hashnameFn nilOrMime contents = do
Lua.push (getHashname (toMaybe nilOrMime) contents)
sha1HashFn contents = do
Lua.push $ showDigest (sha1 contents)
return 1
getHashname :: Maybe MimeType -> BL.ByteString -> String
getHashname mbMime bs =
let ext = fromMaybe ""
(('.':) <$> (mbMime >>= extensionFromMimeType))
basename = showDigest $ sha1 bs
in basename ++ ext
insertMediaFn :: IORef MB.MediaBag
-> FilePath
-> OrNil MimeType