summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlbert Krewinkel <albert+github@zeitkraut.de>2017-11-18 22:24:06 +0100
committerJohn MacFarlane <jgm@berkeley.edu>2017-11-18 13:24:06 -0800
commit53aafd66434d97f5e0e9209650581177e2c79a91 (patch)
treeaa703574e2b659a13c608f67cc35b93376da4fa2
parent6018a2324d4eddc3844aa4c00b17294e85003750 (diff)
Lua filters: preload text module (#4077)
The `text` module is preloaded in lua. The module contains some UTF-8 aware string functions, implemented in Haskell. The module is loaded on request only, e.g.: text = require 'text' function Str (s) s.text = text.upper(s.text) return s end
-rw-r--r--pandoc.cabal1
-rw-r--r--src/Text/Pandoc/Lua.hs2
-rw-r--r--stack.pkg.yaml1
-rw-r--r--stack.yaml1
-rw-r--r--test/Tests/Lua.hs12
-rw-r--r--test/lua/uppercase-header.lua9
6 files changed, 23 insertions, 3 deletions
diff --git a/pandoc.cabal b/pandoc.cabal
index 7e0790973..7dfc06ed5 100644
--- a/pandoc.cabal
+++ b/pandoc.cabal
@@ -327,6 +327,7 @@ library
scientific >= 0.2 && < 0.4,
vector >= 0.10 && < 0.13,
hslua >= 0.9 && < 0.10,
+ hslua-module-text >= 0.1.2 && < 0.2,
binary >= 0.5 && < 0.9,
SHA >= 1.6 && < 1.7,
haddock-library >= 1.1 && < 1.5,
diff --git a/src/Text/Pandoc/Lua.hs b/src/Text/Pandoc/Lua.hs
index 355a5baf1..148e7a23d 100644
--- a/src/Text/Pandoc/Lua.hs
+++ b/src/Text/Pandoc/Lua.hs
@@ -46,6 +46,7 @@ import Text.Pandoc.Lua.PandocModule (pushMediaBagModule, pushPandocModule)
import Text.Pandoc.Lua.Filter (LuaFilter, walkMWithLuaFilter)
import Text.Pandoc.MediaBag (MediaBag)
import qualified Foreign.Lua as Lua
+import qualified Foreign.Lua.Module.Text as Lua
runLuaFilter :: Maybe FilePath -> FilePath -> String
-> Pandoc -> PandocIO (Either LuaException Pandoc)
@@ -64,6 +65,7 @@ runLuaFilter' :: CommonState
-> Pandoc -> Lua Pandoc
runLuaFilter' commonState datadir filterPath format mbRef pd = do
Lua.openlibs
+ Lua.preloadTextModule "text"
-- store module in global "pandoc"
pushPandocModule datadir
Lua.setglobal "pandoc"
diff --git a/stack.pkg.yaml b/stack.pkg.yaml
index 83aed507b..55dfc337a 100644
--- a/stack.pkg.yaml
+++ b/stack.pkg.yaml
@@ -15,6 +15,7 @@ packages:
extra-deps:
- pandoc-types-1.17.3
- hslua-0.9.2
+- hslua-module-text-0.1.2
- skylighting-0.4.3.2
- texmath-0.10
- cmark-gfm-0.1.1
diff --git a/stack.yaml b/stack.yaml
index 5cfb430f6..8d53b0bde 100644
--- a/stack.yaml
+++ b/stack.yaml
@@ -9,6 +9,7 @@ packages:
extra-deps:
- pandoc-types-1.17.3
- hslua-0.9.2
+- hslua-module-text-0.1.2
- skylighting-0.4.3.2
- texmath-0.10
- cmark-gfm-0.1.1
diff --git a/test/Tests/Lua.hs b/test/Tests/Lua.hs
index f9f0e9753..e380be6bb 100644
--- a/test/Tests/Lua.hs
+++ b/test/Tests/Lua.hs
@@ -7,9 +7,9 @@ import Test.Tasty (TestTree, localOption)
import Test.Tasty.HUnit (Assertion, assertEqual, testCase)
import Test.Tasty.QuickCheck (QuickCheckTests (..), ioProperty, testProperty)
import Text.Pandoc.Arbitrary ()
-import Text.Pandoc.Builder (bulletList, doc, doubleQuoted, emph, linebreak,
- para, plain, rawBlock, singleQuoted, space, str,
- strong, (<>))
+import Text.Pandoc.Builder (bulletList, doc, doubleQuoted, emph, header,
+ linebreak, para, plain, rawBlock, singleQuoted,
+ space, str, strong, (<>))
import Text.Pandoc.Class (runIOorExplode)
import Text.Pandoc.Definition (Block, Inline, Meta, Pandoc)
import Text.Pandoc.Lua
@@ -77,6 +77,12 @@ tests = map (localOption (QuickCheckTests 20))
"block-count.lua"
(doc $ para "one" <> para "two")
(doc $ para "2")
+
+ , testCase "Convert header upper case" $
+ assertFilterConversion "converting header to upper case failed"
+ "uppercase-header.lua"
+ (doc $ header 1 "les états-unis" <> para "text")
+ (doc $ header 1 "LES ÉTATS-UNIS" <> para "text")
]
assertFilterConversion :: String -> FilePath -> Pandoc -> Pandoc -> Assertion
diff --git a/test/lua/uppercase-header.lua b/test/lua/uppercase-header.lua
new file mode 100644
index 000000000..8e86911c0
--- /dev/null
+++ b/test/lua/uppercase-header.lua
@@ -0,0 +1,9 @@
+local text = require 'text'
+
+local function str_to_uppercase (s)
+ return pandoc.Str(text.upper(s.text))
+end
+
+function Header (el)
+ return pandoc.walk_block(el, {Str = str_to_uppercase})
+end