git.iko.soy/typeable/pandoc
on master help sign in
Lua: simply mediabag module.

Now 'fetch' simply fetches content and mime type. A new 'hashname' function is provided to get a filename based on the sha1 hash of the contents and the mime type.

commit17583cd99d0ea2cb4a5dcb3eecf2735395ebc3d6 authorJohn MacFarlane <jgm@berkeley.edu> dateSat 30 Sep 2017, 22:54 (9 years ago) parent73c47a4
2 files changed+43−19
doc/lua-filters.md+12−4 src/Text/Pandoc/Lua/PandocModule.hs+31−15
doc/lua-filters.md+12−4
@@ -1130,14 +1130,22 @@ storage. The "media bag" is used when pandoc is called with the
11301130 local filename = "media/diagram.png"
11311131 local mt, contents = pandoc.mediabag.lookup(filename)
11321132
1133+[`hashname (mime_type, contents)`]{#mediabag-hashname}
1134+
1135+: Returns a filename with a basename based on the SHA1 has of the
1136+ contents and an extension based on the mime type.
1137+
1138+ Usage:
1139+
1140+ local fp = pandoc.mediabag.hashname("plain/text", "foobar")
1141+
11331142[`fetch (source, base_url)`]{#mediabag-fetch}
11341143
1135−: Fetches the given source and inserts it into the media bag
1136− using a SHA1 hash of the content as filename. Returns two
1137− values: the filename (based on SHA1 hash) and the mime
1144+: Fetches the given source from a URL or local file.
1145+ Returns two values: the contents of the file and the mime
11381146 type (or an empty string).
11391147
11401148 Usage:
11411149
11421150 local diagram_url = "https://pandoc.org/diagram.jpg"
1143− pandoc.mediabag.fetch(diagram_url, ".")
1151+ local contents = pandoc.mediabag.fetch(diagram_url, ".")
src/Text/Pandoc/Lua/PandocModule.hs+31−15
@@ -41,13 +41,14 @@ import Data.IORef
4141import Data.Maybe (fromMaybe)
4242import Data.Text (pack)
4343import Foreign.Lua (Lua, FromLuaStack, ToLuaStack, NumResults, liftIO)
44−import Text.Pandoc.Class (fetchMediaResource, readDataFile, runIO,
44+import Text.Pandoc.Class (readDataFile, runIO,
4545 runIOorExplode, setUserDataDir, CommonState(..),
46− putCommonState)
46+ putCommonState, fetchItem, setMediaBag)
4747import Text.Pandoc.Options (ReaderOptions(readerExtensions))
4848import Text.Pandoc.Lua.StackInstances ()
4949import Text.Pandoc.Readers (Reader (..), getReader)
50−import Text.Pandoc.MIME (MimeType)
50+import Text.Pandoc.MIME (MimeType, extensionFromMimeType)
51+import Data.Digest.Pure.SHA (sha1, showDigest)
5152
5253import qualified Foreign.Lua as Lua
5354import qualified Data.ByteString.Lazy as BL
@@ -91,7 +92,8 @@ pushMediaBagModule commonState mediaBagRef = do
9192 addFunction "insert" (insertMediaFn mediaBagRef)
9293 addFunction "lookup" (lookupMediaFn mediaBagRef)
9394 addFunction "list" (mediaDirectoryFn mediaBagRef)
94− addFunction "fetch" (insertResource commonState mediaBagRef)
95+ addFunction "fetch" (fetch commonState mediaBagRef)
96+ addFunction "hashname" hashnameFn
9597 return ()
9698 where
9799 addFunction name fn = do
@@ -99,6 +101,20 @@ pushMediaBagModule commonState mediaBagRef = do
99101 Lua.pushHaskellFunction fn
100102 Lua.rawset (-3)
101103
104+hashnameFn :: OrNil MimeType
105+ -> BL.ByteString
106+ -> Lua NumResults
107+hashnameFn nilOrMime contents = do
108+ Lua.push (getHashname (toMaybe nilOrMime) contents)
109+ return 1
110+
111+getHashname :: Maybe MimeType -> BL.ByteString -> String
112+getHashname mbMime bs =
113+ let ext = fromMaybe ""
114+ (('.':) <$> (mbMime >>= extensionFromMimeType))
115+ basename = showDigest $ sha1 bs
116+ in basename ++ ext
117+
102118insertMediaFn :: IORef MB.MediaBag
103119 -> FilePath
104120 -> OrNil MimeType
@@ -137,19 +153,19 @@ mediaDirectoryFn mbRef = do
137153 Lua.push "length" *> Lua.push contentLength *> Lua.rawset (-3)
138154 Lua.rawseti (-2) idx
139155
140−insertResource :: CommonState
141− -> IORef MB.MediaBag
142− -> String
143− -> Lua NumResults
144−insertResource commonState mbRef src = do
145− (fp, mimeType, bs) <- liftIO . runIOorExplode $ do
156+fetch :: CommonState
157+ -> IORef MB.MediaBag
158+ -> String
159+ -> Lua NumResults
160+fetch commonState mbRef src = do
161+ mediaBag <- liftIO $ readIORef mbRef
162+ (bs, mimeType) <- liftIO . runIOorExplode $ do
146163 putCommonState commonState
147− fetchMediaResource src
148− liftIO $ print (fp, mimeType) -- TODO DEBUG
149− insertMediaFn mbRef fp (OrNil mimeType) bs
150− Lua.push fp
164+ setMediaBag mediaBag
165+ fetchItem src
166+ Lua.push bs
151167 Lua.push $ fromMaybe "" mimeType
152− return 2 -- returns 2 values: name in mediabag, mimetype
168+ return 2 -- returns 2 values: contents, mimetype
153169
154170--
155171-- Helper types and orphan instances