git.iko.soy/typeable/pandoc
on master help sign in
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

commit53aafd66434d97f5e0e9209650581177e2c79a91 authorAlbert Krewinkel <albert+github@zeitkraut.de> dateSat 18 Nov 2017, 22:24 (9 years ago) parent6018a23
6 files changed+23−3
pandoc.cabal+1 src/Text/Pandoc/Lua.hs+2 stack.pkg.yaml+1 stack.yaml+1 test/Tests/Lua.hs+9−3 test/lua/uppercase-header.lua+9
pandoc.cabal+1
@@ -327,6 +327,7 @@ library
327327 scientific >= 0.2 && < 0.4,
328328 vector >= 0.10 && < 0.13,
329329 hslua >= 0.9 && < 0.10,
330+ hslua-module-text >= 0.1.2 && < 0.2,
330331 binary >= 0.5 && < 0.9,
331332 SHA >= 1.6 && < 1.7,
332333 haddock-library >= 1.1 && < 1.5,
src/Text/Pandoc/Lua.hs+2
@@ -46,6 +46,7 @@ import Text.Pandoc.Lua.PandocModule (pushMediaBagModule, pushPandocModule)
4646import Text.Pandoc.Lua.Filter (LuaFilter, walkMWithLuaFilter)
4747import Text.Pandoc.MediaBag (MediaBag)
4848import qualified Foreign.Lua as Lua
49+import qualified Foreign.Lua.Module.Text as Lua
4950
5051runLuaFilter :: Maybe FilePath -> FilePath -> String
5152 -> Pandoc -> PandocIO (Either LuaException Pandoc)
@@ -64,6 +65,7 @@ runLuaFilter' :: CommonState
6465 -> Pandoc -> Lua Pandoc
6566runLuaFilter' commonState datadir filterPath format mbRef pd = do
6667 Lua.openlibs
68+ Lua.preloadTextModule "text"
6769 -- store module in global "pandoc"
6870 pushPandocModule datadir
6971 Lua.setglobal "pandoc"
stack.pkg.yaml+1
@@ -15,6 +15,7 @@ packages:
1515extra-deps:
1616- pandoc-types-1.17.3
1717- hslua-0.9.2
18+- hslua-module-text-0.1.2
1819- skylighting-0.4.3.2
1920- texmath-0.10
2021- cmark-gfm-0.1.1
stack.yaml+1
@@ -9,6 +9,7 @@ packages:
99extra-deps:
1010- pandoc-types-1.17.3
1111- hslua-0.9.2
12+- hslua-module-text-0.1.2
1213- skylighting-0.4.3.2
1314- texmath-0.10
1415- cmark-gfm-0.1.1
test/Tests/Lua.hs+9−3
@@ -7,9 +7,9 @@ import Test.Tasty (TestTree, localOption)
77import Test.Tasty.HUnit (Assertion, assertEqual, testCase)
88import Test.Tasty.QuickCheck (QuickCheckTests (..), ioProperty, testProperty)
99import Text.Pandoc.Arbitrary ()
10−import Text.Pandoc.Builder (bulletList, doc, doubleQuoted, emph, linebreak,
11− para, plain, rawBlock, singleQuoted, space, str,
12− strong, (<>))
10+import Text.Pandoc.Builder (bulletList, doc, doubleQuoted, emph, header,
11+ linebreak, para, plain, rawBlock, singleQuoted,
12+ space, str, strong, (<>))
1313import Text.Pandoc.Class (runIOorExplode)
1414import Text.Pandoc.Definition (Block, Inline, Meta, Pandoc)
1515import Text.Pandoc.Lua
@@ -77,6 +77,12 @@ tests = map (localOption (QuickCheckTests 20))
7777 "block-count.lua"
7878 (doc $ para "one" <> para "two")
7979 (doc $ para "2")
80+
81+ , testCase "Convert header upper case" $
82+ assertFilterConversion "converting header to upper case failed"
83+ "uppercase-header.lua"
84+ (doc $ header 1 "les états-unis" <> para "text")
85+ (doc $ header 1 "LES ÉTATS-UNIS" <> para "text")
8086 ]
8187
8288assertFilterConversion :: String -> FilePath -> Pandoc -> Pandoc -> Assertion
test/lua/uppercase-header.luanew file+9
@@ -0,0 +1,9 @@
1+local text = require 'text'
2+
3+local function str_to_uppercase (s)
4+ return pandoc.Str(text.upper(s.text))
5+end
6+
7+function Header (el)
8+ return pandoc.walk_block(el, {Str = str_to_uppercase})
9+end