git.iko.soy/typeable/pandoc
on master help sign in
Lua: add module "pandoc.path"

The module allows to work with file paths in a convenient and platform-independent manner. Closes: #6001 Closes: #6565

commit61b108d52789f20fb03c4f8a74719c1d53021c91 authorAlbert Krewinkel <albert@zeitkraut.de> dateWed 27 Jan 2021, 15:17 (6 years ago) parent2415b26
6 files changed+197−1
doc/lua-filters.md+172 pandoc.cabal+1 src/Text/Pandoc/Lua/Packages.hs+2 stack.yaml+3−1 test/Tests/Lua/Module.hs+2 test/lua/module/pandoc-path.lua+17
doc/lua-filters.md+172
@@ -3376,6 +3376,178 @@ methods and convenience functions.
33763376 `comp`:
33773377 : Comparison function as described above.
33783378
3379+# Module pandoc.path
3380+
3381+Module for file path manipulations.
3382+
3383+## Static Fields {#pandoc.path-fields}
3384+
3385+### separator {#pandoc.path.separator}
3386+
3387+The character that separates directories.
3388+
3389+### search_path_separator {#pandoc.path.search_path_separator}
3390+
3391+The character that is used to separate the entries in the `PATH`
3392+environment variable.
3393+
3394+## Functions {#pandoc.path-functions}
3395+
3396+### directory (filepath) {#pandoc.path.directory}
3397+
3398+Get the directory name; move up one level.
3399+
3400+Parameters:
3401+
3402+filepath
3403+: path (string)
3404+
3405+Returns:
3406+
3407+- The filepath up to the last directory separator. (string)
3408+
3409+### filename (filepath) {#pandoc.path.filename}
3410+
3411+Get the file name.
3412+
3413+Parameters:
3414+
3415+filepath
3416+: path (string)
3417+
3418+Returns:
3419+
3420+- File name part of the input path. (string)
3421+
3422+### is_absolute (filepath) {#pandoc.path.is_absolute}
3423+
3424+Checks whether a path is absolute, i.e. not fixed to a root.
3425+
3426+Parameters:
3427+
3428+filepath
3429+: path (string)
3430+
3431+Returns:
3432+
3433+- `true` iff `filepath` is an absolute path, `false` otherwise.
3434+ (boolean)
3435+
3436+### is_relative (filepath) {#pandoc.path.is_relative}
3437+
3438+Checks whether a path is relative or fixed to a root.
3439+
3440+Parameters:
3441+
3442+filepath
3443+: path (string)
3444+
3445+Returns:
3446+
3447+- `true` iff `filepath` is a relative path, `false` otherwise.
3448+ (boolean)
3449+
3450+### join (filepaths) {#pandoc.path.join}
3451+
3452+Join path elements back together by the directory separator.
3453+
3454+Parameters:
3455+
3456+filepaths
3457+: path components (list of strings)
3458+
3459+Returns:
3460+
3461+- The joined path. (string)
3462+
3463+### make_relative (path, root[, unsafe]) {#pandoc.path.make_relative}
3464+
3465+Contract a filename, based on a relative path. Note that the
3466+resulting path will usually not introduce `..` paths, as the
3467+presence of symlinks means `../b` may not reach `a/b` if it starts
3468+from `a/c`. For a worked example see [this blog
3469+post](http://neilmitchell.blogspot.co.uk/2015/10/filepaths-are-subtle-symlinks-are-hard.html).
3470+
3471+Set `unsafe` to a truthy value to a allow `..` in paths.
3472+
3473+Parameters:
3474+
3475+path
3476+: path to be made relative (string)
3477+
3478+root
3479+: root path (string)
3480+
3481+unsafe
3482+: whether to allow `..` in the result. (boolean)
3483+
3484+Returns:
3485+
3486+- contracted filename (string)
3487+
3488+### normalize (filepath) {#pandoc.path.normalize}
3489+
3490+Normalizes a path.
3491+
3492+- `//` outside of the drive can be made blank
3493+- `/` becomes the `path.separator`
3494+- `./` -\> ''
3495+- an empty path becomes `.`
3496+
3497+Parameters:
3498+
3499+filepath
3500+: path (string)
3501+
3502+Returns:
3503+
3504+- The normalized path. (string)
3505+
3506+### split (filepath) {#pandoc.path.split}
3507+
3508+Splits a path by the directory separator.
3509+
3510+Parameters:
3511+
3512+filepath
3513+: path (string)
3514+
3515+Returns:
3516+
3517+- List of all path components. (list of strings)
3518+
3519+### split_extension (filepath) {#pandoc.path.split_extension}
3520+
3521+Splits the last extension from a file path and returns the parts. The
3522+extension, if present, includes the leading separator; if the path has
3523+no extension, then the empty string is returned as the extension.
3524+
3525+Parameters:
3526+
3527+filepath
3528+: path (string)
3529+
3530+Returns:
3531+
3532+- filepath without extension (string)
3533+
3534+- extension or empty string (string)
3535+
3536+### split_search_path (search_path) {#pandoc.path.split_search_path}
3537+
3538+Takes a string and splits it on the `search_path_separator` character.
3539+Blank items are ignored on Windows, and converted to `.` on Posix. On
3540+Windows path elements are stripped of quotes.
3541+
3542+Parameters:
3543+
3544+search_path
3545+: platform-specific search path (string)
3546+
3547+Returns:
3548+
3549+- list of directories in search path (list of strings)
3550+
33793551# Module pandoc.system
33803552
33813553Access to system information and functionality.
pandoc.cabal+1
@@ -429,6 +429,7 @@ library
429429 filepath >= 1.1 && < 1.5,
430430 haddock-library >= 1.8 && < 1.10,
431431 hslua >= 1.1 && < 1.4,
432+ hslua-module-path >= 0.0.1 && < 0.1.0,
432433 hslua-module-system >= 0.2 && < 0.3,
433434 hslua-module-text >= 0.2.1 && < 0.4,
434435 http-client >= 0.4.30 && < 0.8,
src/Text/Pandoc/Lua/Packages.hs+2
@@ -17,6 +17,7 @@ import Foreign.Lua (NumResults)
1717import Text.Pandoc.Lua.PandocLua (PandocLua, liftPandocLua, loadDefaultModule)
1818
1919import qualified Foreign.Lua as Lua
20+import qualified Foreign.Lua.Module.Path as Path
2021import qualified Foreign.Lua.Module.Text as Text
2122import qualified Text.Pandoc.Lua.Module.Pandoc as Pandoc
2223import qualified Text.Pandoc.Lua.Module.MediaBag as MediaBag
@@ -43,6 +44,7 @@ pandocPackageSearcher pkgName =
4344 case pkgName of
4445 "pandoc" -> pushWrappedHsFun Pandoc.pushModule
4546 "pandoc.mediabag" -> pushWrappedHsFun MediaBag.pushModule
47+ "pandoc.path" -> pushWrappedHsFun Path.pushModule
4648 "pandoc.system" -> pushWrappedHsFun System.pushModule
4749 "pandoc.types" -> pushWrappedHsFun Types.pushModule
4850 "pandoc.utils" -> pushWrappedHsFun Utils.pushModule
stack.yaml+3−1
@@ -10,7 +10,9 @@ extra-deps:
1010- haddock-library-1.9.0
1111- skylighting-0.10.2
1212- skylighting-core-0.10.2
13−- hslua-1.1.2
13+- hslua-1.3.0
14+- hslua-module-path-0.0.1
15+- hslua-module-text-0.3.0.1
1416- jira-wiki-markup-1.3.2
1517- HsYAML-aeson-0.2.0.0
1618- commonmark-0.1.1.3
test/Tests/Lua/Module.hs+2
@@ -25,6 +25,8 @@ tests =
2525 ("lua" </> "module" </> "pandoc-list.lua")
2626 , testPandocLua "pandoc.mediabag"
2727 ("lua" </> "module" </> "pandoc-mediabag.lua")
28+ , testPandocLua "pandoc.path"
29+ ("lua" </> "module" </> "pandoc-path.lua")
2830 , testPandocLua "pandoc.types"
2931 ("lua" </> "module" </> "pandoc-types.lua")
3032 , testPandocLua "pandoc.util"
test/lua/module/pandoc-path.luanew file+17
@@ -0,0 +1,17 @@
1+local tasty = require 'tasty'
2+local path = require 'pandoc.path'
3+
4+local assert = tasty.assert
5+local test = tasty.test_case
6+local group = tasty.test_group
7+
8+return {
9+ group 'path separator' {
10+ test('is string', function ()
11+ assert.are_same(type(path.separator), 'string')
12+ end),
13+ test('is slash or backslash', function ()
14+ assert.is_truthy(path.separator:match '^[/\\]$')
15+ end),
16+ },
17+}