Skip to content

Commit

Permalink
Lua filters: preload text module (#4077)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
tarleb authored and jgm committed Nov 18, 2017
1 parent 6018a23 commit 53aafd6
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 3 deletions.
1 change: 1 addition & 0 deletions pandoc.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions src/Text/Pandoc/Lua.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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"
Expand Down
1 change: 1 addition & 0 deletions stack.pkg.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions stack.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 9 additions & 3 deletions test/Tests/Lua.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions test/lua/uppercase-header.lua
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 53aafd6

Please sign in to comment.