git.iko.soy/typeable/pandoc
on master help sign in
Lua: simply mediabag module.
17583cdJohn MacFarlane
doc/lua-filters.md+12−4
@@ -1130,14 +1130,22 @@ storage. The "media bag" is used when pandoc is called with the
1130 local filename = "media/diagram.png"
1130 local filename = "media/diagram.png"
1131 local mt, contents = pandoc.mediabag.lookup(filename)
1131 local mt, contents = pandoc.mediabag.lookup(filename)
1132
1132
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+
1133[`fetch (source, base_url)`]{#mediabag-fetch}
1142[`fetch (source, base_url)`]{#mediabag-fetch}
1134
1143
1135−: Fetches the given source and inserts it into the media bag
1144+: Fetches the given source from a URL or local file.
1136− using a SHA1 hash of the content as filename. Returns two
1137− values: the filename (based on SHA1 hash) and the mime
1145+ Returns two values: the contents of the file and the mime
1138 type (or an empty string).
1146 type (or an empty string).
1139
1147
1140 Usage:
1148 Usage:
1141
1149
1142 local diagram_url = "https://pandoc.org/diagram.jpg"
1150 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
41import Data.Maybe (fromMaybe)
41import Data.Maybe (fromMaybe)
42import Data.Text (pack)
42import Data.Text (pack)
43import Foreign.Lua (Lua, FromLuaStack, ToLuaStack, NumResults, liftIO)
43import Foreign.Lua (Lua, FromLuaStack, ToLuaStack, NumResults, liftIO)
44−import Text.Pandoc.Class (fetchMediaResource, readDataFile, runIO,
44+import Text.Pandoc.Class (readDataFile, runIO,
45 runIOorExplode, setUserDataDir, CommonState(..),
45 runIOorExplode, setUserDataDir, CommonState(..),
46− putCommonState)
46+ putCommonState, fetchItem, setMediaBag)
47import Text.Pandoc.Options (ReaderOptions(readerExtensions))
47import Text.Pandoc.Options (ReaderOptions(readerExtensions))
48import Text.Pandoc.Lua.StackInstances ()
48import Text.Pandoc.Lua.StackInstances ()
49import Text.Pandoc.Readers (Reader (..), getReader)
49import 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)
51
52
52import qualified Foreign.Lua as Lua
53import qualified Foreign.Lua as Lua
53import qualified Data.ByteString.Lazy as BL
54import qualified Data.ByteString.Lazy as BL
@@ -91,7 +92,8 @@ pushMediaBagModule commonState mediaBagRef = do
91 addFunction "insert" (insertMediaFn mediaBagRef)
92 addFunction "insert" (insertMediaFn mediaBagRef)
92 addFunction "lookup" (lookupMediaFn mediaBagRef)
93 addFunction "lookup" (lookupMediaFn mediaBagRef)
93 addFunction "list" (mediaDirectoryFn mediaBagRef)
94 addFunction "list" (mediaDirectoryFn mediaBagRef)
94− addFunction "fetch" (insertResource commonState mediaBagRef)
95+ addFunction "fetch" (fetch commonState mediaBagRef)
96+ addFunction "hashname" hashnameFn
95 return ()
97 return ()
96 where
98 where
97 addFunction name fn = do
99 addFunction name fn = do
@@ -99,6 +101,20 @@ pushMediaBagModule commonState mediaBagRef = do
99 Lua.pushHaskellFunction fn
101 Lua.pushHaskellFunction fn
100 Lua.rawset (-3)
102 Lua.rawset (-3)
101
103
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+
102insertMediaFn :: IORef MB.MediaBag
118insertMediaFn :: IORef MB.MediaBag
103 -> FilePath
119 -> FilePath
104 -> OrNil MimeType
120 -> OrNil MimeType
@@ -137,19 +153,19 @@ mediaDirectoryFn mbRef = do
137 Lua.push "length" *> Lua.push contentLength *> Lua.rawset (-3)
153 Lua.push "length" *> Lua.push contentLength *> Lua.rawset (-3)
138 Lua.rawseti (-2) idx
154 Lua.rawseti (-2) idx
139
155
140−insertResource :: CommonState
156+fetch :: CommonState
141− -> IORef MB.MediaBag
157+ -> IORef MB.MediaBag
142− -> String
158+ -> String
143− -> Lua NumResults
159+ -> Lua NumResults
144−insertResource commonState mbRef src = do
160+fetch commonState mbRef src = do
161+ mediaBag <- liftIO $ readIORef mbRef
145− (fp, mimeType, bs) <- liftIO . runIOorExplode $ do
162+ (bs, mimeType) <- liftIO . runIOorExplode $ do
146 putCommonState commonState
163 putCommonState commonState
147− fetchMediaResource src
164+ setMediaBag mediaBag
148− liftIO $ print (fp, mimeType) -- TODO DEBUG
165+ fetchItem src
149− insertMediaFn mbRef fp (OrNil mimeType) bs
150− Lua.push fp
166+ Lua.push bs
151 Lua.push $ fromMaybe "" mimeType
167 Lua.push $ fromMaybe "" mimeType
152− return 2 -- returns 2 values: name in mediabag, mimetype
168+ return 2 -- returns 2 values: contents, mimetype
153
169
154--
170--
155-- Helper types and orphan instances
171-- Helper types and orphan instances