-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The module provides basic querying functions for image properties.
- Loading branch information
Showing
6 changed files
with
195 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
pandoc-lua-engine/src/Text/Pandoc/Lua/Marshal/ImageSize.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{-# LANGUAGE LambdaCase #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{- | | ||
Module : Text.Pandoc.Lua.Marshal.ImageSize | ||
Copyright : © 2024 Albert Krewinkel | ||
License : GPL-2.0-or-later | ||
Maintainer : Albert Krewinkel <[email protected]> | ||
Marshaling image properties. | ||
-} | ||
module Text.Pandoc.Lua.Marshal.ImageSize | ||
( pushImageType | ||
, pushImageSize | ||
) where | ||
|
||
import Data.Char (toLower) | ||
import HsLua | ||
import Text.Pandoc.ImageSize | ||
|
||
-- | Pushes an 'ImageType' as a string value. | ||
pushImageType :: LuaError e => Pusher e ImageType | ||
pushImageType = pushString . map toLower . show | ||
|
||
-- | Pushes a dimensional value. | ||
pushImageSize :: LuaError e => Pusher e ImageSize | ||
pushImageSize = pushAsTable | ||
[ ("width", pushIntegral . pxX) | ||
, ("height", pushIntegral . pxY) | ||
, ("dpi_horz", pushIntegral . dpiX) | ||
, ("dpi_vert", pushIntegral . dpiY) | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-| | ||
Module : Text.Pandoc.Lua.Module.Image | ||
Copyright : © 2024 Albert Krewinkel | ||
License : MIT | ||
Maintainer : Albert Krewinkel <[email protected]> | ||
Lua module for basic image operations. | ||
-} | ||
module Text.Pandoc.Lua.Module.Image ( | ||
-- * Module | ||
documentedModule | ||
|
||
-- ** Functions | ||
, size | ||
, format | ||
) | ||
where | ||
|
||
import Prelude hiding (null) | ||
import Data.Default (Default (def)) | ||
import Data.Maybe (fromMaybe) | ||
import Data.Version (makeVersion) | ||
import HsLua.Core | ||
import HsLua.Marshalling | ||
import HsLua.Packaging | ||
import Text.Pandoc.Error (PandocError) | ||
import Text.Pandoc.ImageSize (imageType, imageSize) | ||
import Text.Pandoc.Lua.PandocLua () | ||
import Text.Pandoc.Lua.Marshal.ImageSize (pushImageType, pushImageSize) | ||
import Text.Pandoc.Lua.Marshal.WriterOptions (peekWriterOptions) | ||
|
||
import qualified Data.Text as T | ||
|
||
-- | The @aeson@ module specification. | ||
documentedModule :: Module PandocError | ||
documentedModule = Module | ||
{ moduleName = "pandoc.image" | ||
, moduleDescription = "Basic image querying functions." | ||
, moduleFields = fields | ||
, moduleFunctions = functions | ||
, moduleOperations = [] | ||
, moduleTypeInitializers = [] | ||
} | ||
|
||
-- | ||
-- Fields | ||
-- | ||
|
||
-- | Exported fields. | ||
fields :: LuaError e => [Field e] | ||
fields = [] | ||
|
||
-- | ||
-- Functions | ||
-- | ||
|
||
functions :: [DocumentedFunction PandocError] | ||
functions = | ||
[ size `since` makeVersion [3, 2, 0] | ||
, format `since` makeVersion [3, 2, 0] | ||
] | ||
|
||
-- | Decode a JSON string into a Lua object. | ||
size :: DocumentedFunction PandocError | ||
size = defun "size" | ||
### liftPure2 | ||
(\img mwriterOpts -> imageSize (fromMaybe def mwriterOpts) img) | ||
<#> parameter peekByteString "string" "image" "image data" | ||
<#> opt (parameter peekWriterOptions "WriterOptions" "writer_options" | ||
"writer options") | ||
=#> functionResult (either (failLua . T.unpack) pushImageSize) "string|table" | ||
"image size object or error message" | ||
-- #? T.unlines | ||
-- [ "Creates a Lua object from a JSON string. The function returns an" | ||
-- , "[[Inline]], [[Block]], [[Pandoc]], [[Inlines]], or [[Blocks]] element" | ||
-- , "if the input can be decoded into represent any of those types." | ||
-- , "Otherwise the default decoding is applied, using tables, booleans," | ||
-- , "numbers, and [null](#pandoc.json.null) to represent the JSON value." | ||
-- , "" | ||
-- , "The special handling of AST elements can be disabled by setting" | ||
-- , "`pandoc_types` to `false`." | ||
-- ] | ||
|
||
-- | Encode a Lua object as JSON. | ||
format :: LuaError e => DocumentedFunction e | ||
format = defun "format" | ||
### liftPure imageType | ||
<#> parameter peekByteString "string" "image" "binary image data" | ||
=#> functionResult (maybe pushnil pushImageType) "string|nil" | ||
"image format, or nil if the format cannot be determined" | ||
-- #? T.unlines | ||
-- ["Encodes a Lua object as JSON string." | ||
-- , "" | ||
-- , "If the object has a metamethod with name `__tojson`, then the" | ||
-- , "result is that of a call to that method with `object` passed as" | ||
-- , "the sole argument. The result of that call is expected to be a" | ||
-- , "valid JSON string, but this not checked." | ||
-- ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
-- | ||
-- Tests for the system module | ||
-- | ||
local image = require 'pandoc.image' | ||
local tasty = require 'tasty' | ||
|
||
local group = tasty.test_group | ||
local test = tasty.test_case | ||
local assert = tasty.assert | ||
|
||
local svg_image = [==[<?xml version="1.0"?> | ||
<svg xmlns="http://www.w3.org/2000/svg" | ||
xmlns:xlink="http://www.w3.org/1999/xlink" | ||
height="70" width="70" | ||
viewBox="-35 -35 70 70"> | ||
<title>test</title> | ||
<!-- document shape --> | ||
<polygon points="-10,-31.53 -10,-3.25 0,0 10,-3.25 10,-23.53 2,-31.53" /> | ||
</svg> | ||
]==] | ||
|
||
return { | ||
-- Check existence of static fields | ||
group 'static fields' { | ||
}, | ||
|
||
group 'size' { | ||
test('string', function () | ||
local imgsize = { | ||
width = 70, | ||
height = 70, | ||
dpi_horz = 96, | ||
dpi_vert = 96, | ||
} | ||
assert.are_same(image.size(svg_image), imgsize) | ||
end), | ||
test('fails on faulty eps', function () | ||
assert.error_matches( | ||
function () image.size('%!PS EPSF') end, | ||
'could not determine EPS size' | ||
) | ||
end), | ||
test('fails if input is not an image', function () | ||
assert.error_matches( | ||
function () image.size('not an image') end, | ||
'could not determine image type' | ||
) | ||
end), | ||
}, | ||
|
||
group 'format' { | ||
test('SVG', function () | ||
assert.are_equal(image.format(svg_image), 'svg') | ||
end), | ||
test('returns nil if input is not an image', function () | ||
assert.is_nil(image.format('not an image')) | ||
end), | ||
}, | ||
} |