git.iko.soy/typeable/pandoc
on master help sign in
Lua modules: added pandoc.utils module

A new module `pandoc.utils` has been created. It holds utility functions like `sha1`, which was moved from the main `pandoc` module.

commitbd3ea723717b54e3853487bee7a48947fb73b68a authorAlbert Krewinkel <albert@zeitkraut.de> dateThu 21 Dec 2017, 22:30 (9 years ago) parent5ad719c
6 files changed+79−22
data/pandoc.lua+5 doc/lua-filters.md+21−14 pandoc.cabal+1 src/Text/Pandoc/Lua/Module/Pandoc.hs−8 src/Text/Pandoc/Lua/Module/Utils.hs+50 src/Text/Pandoc/Lua/Packages.hs+2
data/pandoc.lua+5
@@ -903,4 +903,9 @@ function M.global_filter()
903903 return res
904904end
905905
906+------------------------------------------------------------------------
907+-- Functions which have moved to different modules
908+local utils = require 'pandoc.utils'
909+M.sha1 = utils.sha1
910+
906911return M
doc/lua-filters.md+21−14
@@ -1405,18 +1405,6 @@ Lua functions for pandoc scripts.
14051405 -- the above is equivallent to
14061406 -- return {{Str = Str}}
14071407
1408−[`sha1 (contents)`]{#mediabag-sha1}
1409−
1410−: Returns the SHA1 has of the contents.
1411−
1412− Returns:
1413−
1414− - SHA1 hash of the contents.
1415−
1416− Usage:
1417−
1418− local fp = pandoc.mediabag.sha1("foobar")
1419−
14201408[`pipe (command, args, input)`]{#mediabag-sha1}
14211409
14221410: Runs command with arguments, passing it some input,
@@ -1436,9 +1424,28 @@ Lua functions for pandoc scripts.
14361424
14371425 local output = pandoc.pipe("sed", {"-e","s/a/b/"}, "abc")
14381426
1439−# Submodule mediabag
14401427
1441−The submodule `mediabag` allows accessing pandoc's media
1428+# Module pandoc.utils
1429+
1430+This module exposes internal pandoc functions and utility
1431+functions.
1432+
1433+[`sha1 (contents)`]{#utils-sha1}
1434+
1435+: Returns the SHA1 has of the contents.
1436+
1437+ Returns:
1438+
1439+ - SHA1 hash of the contents.
1440+
1441+ Usage:
1442+
1443+ local fp = pandoc.utils.sha1("foobar")
1444+
1445+
1446+# Module pandoc.mediabag
1447+
1448+The `pandoc.mediabag` module allows accessing pandoc's media
14421449storage. The "media bag" is used when pandoc is called with the
14431450`--extract-media` or `--standalone`/`-s` option.
14441451
pandoc.cabal+1
@@ -525,6 +525,7 @@ library
525525 Text.Pandoc.Lua.Init,
526526 Text.Pandoc.Lua.Module.MediaBag,
527527 Text.Pandoc.Lua.Module.Pandoc,
528+ Text.Pandoc.Lua.Module.Utils,
528529 Text.Pandoc.Lua.Packages,
529530 Text.Pandoc.Lua.StackInstances,
530531 Text.Pandoc.Lua.Util,
src/Text/Pandoc/Lua/Module/Pandoc.hs−8
@@ -32,7 +32,6 @@ module Text.Pandoc.Lua.Module.Pandoc
3232
3333import Control.Monad (when)
3434import Data.Default (Default (..))
35−import Data.Digest.Pure.SHA (sha1, showDigest)
3635import Data.Maybe (fromMaybe)
3736import Data.Text (pack)
3837import Foreign.Lua (ToLuaStack, FromLuaStack, Lua, NumResults, liftIO)
@@ -59,7 +58,6 @@ pushModule datadir = do
5958 loadScriptFromDataDir datadir "pandoc.lua"
6059 addFunction "read" readDoc
6160 addFunction "pipe" pipeFn
62− addFunction "sha1" sha1HashFn
6361 addFunction "walk_block" walkBlock
6462 addFunction "walk_inline" walkInline
6563 return 1
@@ -88,12 +86,6 @@ readDoc content formatSpecOrNil = do
8886 Left s -> raiseError (show s) -- error while reading
8987 _ -> raiseError "Only string formats are supported at the moment."
9088
91−sha1HashFn :: BL.ByteString
92− -> Lua NumResults
93−sha1HashFn contents = do
94− Lua.push $ showDigest (sha1 contents)
95− return 1
96−
9789-- | Pipes input through a command.
9890pipeFn :: String
9991 -> [String]
src/Text/Pandoc/Lua/Module/Utils.hsnew file+50
@@ -0,0 +1,50 @@
1+{-
2+Copyright © 2017 Albert Krewinkel <tarleb+pandoc@moltkeplatz.de>
3+
4+This program is free software; you can redistribute it and/or modify
5+it under the terms of the GNU General Public License as published by
6+the Free Software Foundation; either version 2 of the License, or
7+(at your option) any later version.
8+
9+This program is distributed in the hope that it will be useful,
10+but WITHOUT ANY WARRANTY; without even the implied warranty of
11+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+GNU General Public License for more details.
13+
14+You should have received a copy of the GNU General Public License
15+along with this program; if not, write to the Free Software
16+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17+-}
18+{- |
19+ Module : Text.Pandoc.Lua.Module.Utils
20+ Copyright : Copyright © 2017 Albert Krewinkel
21+ License : GNU GPL, version 2 or above
22+
23+ Maintainer : Albert Krewinkel <tarleb+pandoc@moltkeplatz.de>
24+ Stability : alpha
25+
26+Utility module for lua, exposing internal helper functions.
27+-}
28+module Text.Pandoc.Lua.Module.Utils
29+ ( pushModule
30+ ) where
31+
32+import Data.Digest.Pure.SHA (sha1, showDigest)
33+import Foreign.Lua (Lua, NumResults)
34+import Text.Pandoc.Lua.StackInstances ()
35+import Text.Pandoc.Lua.Util (addFunction)
36+
37+import qualified Data.ByteString.Lazy as BSL
38+import qualified Foreign.Lua as Lua
39+
40+-- | Push the "pandoc.utils" module to the lua stack.
41+pushModule :: Lua NumResults
42+pushModule = do
43+ Lua.newtable
44+ addFunction "sha1" sha1HashFn
45+ return 1
46+
47+-- | Calculate the hash of the given contents.
48+sha1HashFn :: BSL.ByteString
49+ -> Lua String
50+sha1HashFn = return . showDigest . sha1
src/Text/Pandoc/Lua/Packages.hs+2
@@ -43,6 +43,7 @@ import Text.Pandoc.Lua.Util (dostring')
4343import qualified Foreign.Lua as Lua
4444import Text.Pandoc.Lua.Module.Pandoc as Pandoc
4545import Text.Pandoc.Lua.Module.MediaBag as MediaBag
46+import Text.Pandoc.Lua.Module.Utils as Utils
4647
4748-- | Parameters used to create lua packages/modules.
4849data LuaPackageParams = LuaPackageParams
@@ -77,6 +78,7 @@ pandocPackageSearcher luaPkgParams pkgName =
7778 "pandoc.mediabag" -> let st = luaPkgCommonState luaPkgParams
7879 mbRef = luaPkgMediaBag luaPkgParams
7980 in pushWrappedHsFun (MediaBag.pushModule st mbRef)
81+ "pandoc.utils" -> pushWrappedHsFun Utils.pushModule
8082 _ -> searchPureLuaLoader
8183 where
8284 pushWrappedHsFun f = do