Skip to content

Commit 336228b

Browse files
committed
Make it build with wasm backend
1 parent 31e4fab commit 336228b

File tree

8 files changed

+192
-20
lines changed

8 files changed

+192
-20
lines changed

cabal.project

Lines changed: 141 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,147 @@ packages: .
22
pandoc-lua-engine
33
pandoc-server
44
pandoc-cli
5-
tests: True
6-
flags: +embed_data_files
5+
6+
tests: False
7+
78
constraints: skylighting-format-blaze-html >= 0.1.1.2,
89
skylighting-format-context >= 0.1.0.2
910

11+
index-state: 2024-11-15T08:25:42Z
12+
13+
allow-newer: all:zlib
14+
15+
package aeson
16+
flags: -ordered-keymap
17+
18+
package crypton
19+
ghc-options: -optc-DARGON2_NO_THREADS
20+
21+
package digest
22+
flags: -pkg-config
23+
24+
package pandoc
25+
flags: +embed_data_files
26+
27+
package pandoc-cli
28+
flags: -lua -server
29+
30+
allow-newer:
31+
all:Cabal,
32+
all:Cabal-syntax,
33+
all:array,
34+
all:base,
35+
all:binary,
36+
all:bytestring,
37+
all:containers,
38+
all:deepseq,
39+
all:directory,
40+
all:exceptions,
41+
all:filepath,
42+
all:ghc,
43+
all:ghc-bignum,
44+
all:ghc-boot,
45+
all:ghc-boot-th,
46+
all:ghc-compact,
47+
all:ghc-experimental,
48+
all:ghc-heap,
49+
all:ghc-internal,
50+
all:ghc-platform,
51+
all:ghc-prim,
52+
all:ghc-toolchain,
53+
all:ghci,
54+
all:haskeline,
55+
all:hpc,
56+
all:integer-gmp,
57+
all:mtl,
58+
all:os-string,
59+
all:parsec,
60+
all:pretty,
61+
all:process,
62+
all:rts,
63+
all:semaphore-compat,
64+
all:stm,
65+
all:system-cxx-std-lib,
66+
all:template-haskell,
67+
all:text,
68+
all:time,
69+
all:transformers,
70+
all:unix,
71+
all:xhtml
72+
73+
constraints:
74+
Cabal installed,
75+
Cabal-syntax installed,
76+
array installed,
77+
base installed,
78+
binary installed,
79+
bytestring installed,
80+
containers installed,
81+
deepseq installed,
82+
directory installed,
83+
exceptions installed,
84+
filepath installed,
85+
ghc installed,
86+
ghc-bignum installed,
87+
ghc-boot installed,
88+
ghc-boot-th installed,
89+
ghc-compact installed,
90+
ghc-experimental installed,
91+
ghc-heap installed,
92+
ghc-internal installed,
93+
ghc-platform installed,
94+
ghc-prim installed,
95+
ghc-toolchain installed,
96+
ghci installed,
97+
haskeline installed,
98+
hpc installed,
99+
integer-gmp installed,
100+
mtl installed,
101+
os-string installed,
102+
parsec installed,
103+
pretty installed,
104+
process installed,
105+
rts installed,
106+
semaphore-compat installed,
107+
stm installed,
108+
system-cxx-std-lib installed,
109+
template-haskell installed,
110+
text installed,
111+
time installed,
112+
transformers installed,
113+
unix installed,
114+
xhtml installed
115+
116+
source-repository-package
117+
type: git
118+
location: https://github.com/haskell-wasm/conduit.git
119+
tag: ff33329247f2ef321dcab836e98c1bcfaff2bd13
120+
subdir: conduit-extra
121+
122+
source-repository-package
123+
type: git
124+
location: https://github.com/haskell-wasm/foundation.git
125+
tag: 8e6dd48527fb429c1922083a5030ef88e3d58dd3
126+
subdir: basement
127+
128+
source-repository-package
129+
type: git
130+
location: https://github.com/haskell-wasm/hs-memory.git
131+
tag: a198a76c584dc2cfdcde6b431968de92a5fed65e
132+
133+
source-repository-package
134+
type: git
135+
location: https://github.com/haskell-wasm/streaming-commons.git
136+
tag: 7e9c38b2fd55ce50d3f74fe708ca47db8c9bb315
137+
138+
source-repository-package
139+
type: git
140+
location: https://github.com/haskell-wasm/xml.git
141+
tag: bc793dc9bc29c92245d3482a54d326abd3ae1403
142+
subdir: xml-conduit
143+
144+
-- https://github.com/haskellari/splitmix/pull/73
145+
source-repository-package
146+
type: git
147+
location: https://github.com/amesgen/splitmix
148+
tag: 5f5b766d97dc735ac228215d240a3bb90bc2ff75

pandoc-cli/pandoc-cli.cabal

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ common common-options
6161

6262
common common-executable
6363
import: common-options
64-
ghc-options: -rtsopts -with-rtsopts=-A8m -threaded
64+
ghc-options: -rtsopts -with-rtsopts=-H64m
6565

6666
executable pandoc
6767
import: common-executable
@@ -74,6 +74,10 @@ executable pandoc
7474
text
7575
other-modules: PandocCLI.Lua
7676
, PandocCLI.Server
77+
78+
if arch(wasm32)
79+
ghc-options: -optl-Wl,--export=__wasm_call_ctors,--export=hs_init_with_rtsopts,--export=malloc,--export=wasm_main
80+
7781
if flag(nightly)
7882
cpp-options: -DNIGHTLY
7983
build-depends: template-haskell,

pandoc-cli/src/pandoc.hs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
{-# LANGUAGE CPP #-}
2+
{-# LANGUAGE ScopedTypeVariables #-}
23
{-# LANGUAGE TemplateHaskell #-}
4+
35
{- |
46
Module : Main
57
Copyright : Copyright (C) 2006-2024 John MacFarlane
@@ -34,6 +36,13 @@ import qualified Language.Haskell.TH as TH
3436
import Data.Time
3537
#endif
3638

39+
#if defined(wasm32_HOST_ARCH)
40+
import Control.Exception
41+
import Foreign
42+
import Foreign.C
43+
import System.IO
44+
#endif
45+
3746
#ifdef NIGHTLY
3847
versionSuffix :: String
3948
versionSuffix = "-nightly-" ++
@@ -44,6 +53,24 @@ versionSuffix :: String
4453
versionSuffix = ""
4554
#endif
4655

56+
#if defined(wasm32_HOST_ARCH)
57+
58+
foreign export ccall "wasm_main" wasm_main :: Ptr CChar -> Int -> IO ()
59+
60+
wasm_main :: Ptr CChar -> Int -> IO ()
61+
wasm_main raw_args_ptr raw_args_len =
62+
catch act (\(err :: SomeException) -> hPrint stderr err)
63+
where
64+
act = do
65+
args <- words <$> peekCStringLen (raw_args_ptr, raw_args_len)
66+
free raw_args_ptr
67+
engine <- getEngine
68+
res <- parseOptionsFromArgs options defaultOpts "pandoc.wasm" $ args <> ["/in", "-o", "/out"]
69+
case res of
70+
Left e -> handleOptInfo engine e
71+
Right opts -> convertWithOpts engine opts
72+
#endif
73+
4774
main :: IO ()
4875
main = E.handle (handleError . Left) $ do
4976
prg <- getProgName

pandoc.cabal

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,6 @@ library
495495
commonmark-pandoc >= 0.2.2.2 && < 0.3,
496496
containers >= 0.6.0.1 && < 0.8,
497497
crypton >= 0.30 && < 1.1,
498-
crypton-connection >= 0.3.1 && < 0.5,
499498
data-default >= 0.4 && < 0.8,
500499
deepseq >= 1.3 && < 1.6,
501500
directory >= 1.2.3 && < 1.4,
@@ -507,14 +506,11 @@ library
507506
filepath >= 1.1 && < 1.6,
508507
gridtables >= 0.1 && < 0.2,
509508
haddock-library >= 1.10 && < 1.12,
510-
http-client >= 0.4.30 && < 0.8,
511-
http-client-tls >= 0.2.4 && < 0.4,
512509
http-types >= 0.8 && < 0.13,
513510
ipynb >= 0.2 && < 0.3,
514511
jira-wiki-markup >= 1.5.1 && < 1.6,
515512
mime-types >= 0.1.1 && < 0.2,
516513
mtl >= 2.2 && < 2.4,
517-
network >= 2.6 && < 3.3,
518514
network-uri >= 2.6 && < 2.8,
519515
pandoc-types >= 1.23.1 && < 1.24,
520516
parsec >= 3.1 && < 3.2,
@@ -542,9 +538,7 @@ library
542538
xml >= 1.3.12 && < 1.4,
543539
typst >= 0.6 && < 0.6.1,
544540
vector >= 0.12 && < 0.14,
545-
djot >= 0.1.2.2 && < 0.2,
546-
tls >= 2.0.1 && < 2.2,
547-
crypton-x509-system >= 1.6.7 && < 1.7
541+
djot >= 0.1.2.2 && < 0.2
548542

549543
if !os(windows)
550544
build-depends: unix >= 2.4 && < 2.9

src/Text/Pandoc/Class/IO.hs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,7 @@ import Data.ByteString.Lazy (toChunks)
4141
import Data.Text (Text, pack, unpack)
4242
import Data.Time (TimeZone, UTCTime)
4343
import Data.Unique (hashUnique)
44-
import Network.Connection (TLSSettings(..))
45-
import qualified Network.TLS as TLS
46-
import qualified Network.TLS.Extra as TLS
47-
import Network.HTTP.Client
48-
(httpLbs, responseBody, responseHeaders,
49-
Request(port, host, requestHeaders), parseRequest, newManager)
50-
import Network.HTTP.Client.Internal (addProxy)
51-
import Network.HTTP.Client.TLS (mkManagerSettings)
5244
import Network.HTTP.Types.Header ( hContentType )
53-
import Network.Socket (withSocketsDo)
5445
import Network.URI (URI(..), parseURI, unEscapeString)
5546
import System.Directory (createDirectoryIfMissing)
5647
import System.Environment (getEnv)
@@ -83,7 +74,6 @@ import qualified System.FilePath.Glob
8374
import qualified System.Random
8475
import qualified Text.Pandoc.UTF8 as UTF8
8576
import Data.Default (def)
86-
import System.X509 (getSystemCertificateStore)
8777
#ifndef EMBED_DATA_FILES
8878
import qualified Paths_pandoc as Paths
8979
#endif
@@ -129,6 +119,9 @@ openURL u
129119
| Just (URI{ uriScheme = "data:",
130120
uriPath = upath }) <- parseURI (T.unpack u)
131121
= pure $ extractURIData upath
122+
#if defined(wasm32_HOST_ARCH)
123+
| otherwise = error "Text.Pandoc.Class.IO.openURL"
124+
#else
132125
| otherwise = do
133126
let toReqHeader (n, v) = (CI.mk (UTF8.fromText n), UTF8.fromText v)
134127
customHeaders <- map toReqHeader <$> getsCommonState stRequestHeaders
@@ -168,6 +161,7 @@ openURL u
168161
case res of
169162
Right r -> return r
170163
Left e -> throwError $ PandocHttpError u e
164+
#endif
171165

172166
-- | Read the lazy ByteString contents from a file path, raising an error on
173167
-- failure.

src/Text/Pandoc/Class/PandocMonad.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,11 +491,13 @@ fillMediaBag d = walkM handleImage d
491491
"replacing image with description"
492492
-- emit alt text
493493
return $ replacementSpan attr src tit lab
494+
#if !defined(wasm32_HOST_ARCH)
494495
PandocHttpError u er -> do
495496
report $ CouldNotFetchResource u
496497
(T.pack $ show er ++ "\rReplacing image with description.")
497498
-- emit alt text
498499
return $ replacementSpan attr src tit lab
500+
#endif
499501
_ -> throwError e)
500502
handleImage x = return x
501503

src/Text/Pandoc/Error.hs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE CPP #-}
12
{-# LANGUAGE DeriveDataTypeable #-}
23
{-# LANGUAGE DeriveGeneric #-}
34
{-# LANGUAGE OverloadedStrings #-}
@@ -25,7 +26,9 @@ import Data.Word (Word8)
2526
import Data.Text (Text)
2627
import qualified Data.Text as T
2728
import GHC.Generics (Generic)
29+
#if !defined(wasm32_HOST_ARCH)
2830
import Network.HTTP.Client (HttpException)
31+
#endif
2932
import System.Exit (ExitCode (..), exitWith)
3033
import System.IO (stderr)
3134
import qualified Text.Pandoc.UTF8 as UTF8
@@ -34,7 +37,9 @@ import Text.Pandoc.Shared (tshow)
3437
import Citeproc (CiteprocError, prettyCiteprocError)
3538

3639
data PandocError = PandocIOError Text IOError
40+
#if !defined(wasm32_HOST_ARCH)
3741
| PandocHttpError Text HttpException
42+
#endif
3843
| PandocShouldNeverHappenError Text
3944
| PandocSomeError Text
4045
| PandocParseError Text
@@ -73,8 +78,10 @@ renderError :: PandocError -> Text
7378
renderError e =
7479
case e of
7580
PandocIOError _ err' -> T.pack $ displayException err'
81+
#if !defined(wasm32_HOST_ARCH)
7682
PandocHttpError u err' ->
7783
"Could not fetch " <> u <> "\n" <> tshow err'
84+
#endif
7885
PandocShouldNeverHappenError s ->
7986
"Something we thought was impossible happened!\n" <>
8087
"Please report this to pandoc's developers: " <> s
@@ -169,7 +176,9 @@ handleError (Left e) =
169176
PandocPDFError{} -> 43
170177
PandocXMLError{} -> 44
171178
PandocPDFProgramNotFoundError{} -> 47
179+
#if !defined(wasm32_HOST_ARCH)
172180
PandocHttpError{} -> 61
181+
#endif
173182
PandocShouldNeverHappenError{} -> 62
174183
PandocSomeError{} -> 63
175184
PandocParseError{} -> 64

src/Text/Pandoc/SelfContained.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE CPP #-}
12
{-# LANGUAGE ScopedTypeVariables #-}
23
{-# LANGUAGE OverloadedStrings #-}
34
{-# LANGUAGE LambdaCase #-}
@@ -422,9 +423,11 @@ getData mimetype src
422423
PandocResourceNotFound r -> do
423424
report $ CouldNotFetchResource r ""
424425
return $ CouldNotFetch e
426+
#if !defined(wasm32_HOST_ARCH)
425427
PandocHttpError u er -> do
426428
report $ CouldNotFetchResource u (tshow er)
427429
return $ CouldNotFetch e
430+
#endif
428431
_ -> throwError e
429432

430433

0 commit comments

Comments
 (0)