aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn MacFarlane <[email protected]>2025-12-24 10:13:03 -0700
committerJohn MacFarlane <[email protected]>2025-12-24 11:00:25 -0700
commit83692bbc50d46e157126099da84233deb6dc6574 (patch)
tree30ee8f569121a989a3fa97d5331b06f3f4bcbdc2
parenteef6cde22f868c82618bf9517f5281f28ed10ab0 (diff)
Add http cabal flag.
This allows pandoc to be compiled without support for making HTTP requests, which is useful when WASM is the target. Closes #10980.
-rw-r--r--pandoc.cabal19
-rw-r--r--src/Text/Pandoc/Class/CommonState.hs7
-rw-r--r--src/Text/Pandoc/Class/IO.hs23
3 files changed, 36 insertions, 13 deletions
diff --git a/pandoc.cabal b/pandoc.cabal
index 393451b35..dcad3fffb 100644
--- a/pandoc.cabal
+++ b/pandoc.cabal
@@ -455,6 +455,10 @@ flag embed_data_files
Description: Embed data files in binary for relocatable executable.
Default: False
+flag http
+ Description: Support for fetching resources using HTTP.
+ Default: True
+
common common-options
default-language: Haskell2010
build-depends: base >= 4.18 && < 5
@@ -517,7 +521,6 @@ library
commonmark-pandoc >= 0.2.3 && < 0.3,
containers >= 0.6.0.1 && < 0.9,
crypton >= 0.30 && < 1.1,
- crypton-connection >= 0.3.1 && < 0.5,
data-default >= 0.4 && < 0.9,
deepseq >= 1.3 && < 1.6,
directory >= 1.2.3 && < 1.4,
@@ -529,14 +532,11 @@ library
filepath >= 1.1 && < 1.6,
gridtables >= 0.1 && < 0.2,
haddock-library >= 1.10 && < 1.12,
- http-client >= 0.4.30 && < 0.8,
- http-client-tls >= 0.2.4 && < 0.4,
http-types >= 0.8 && < 0.13,
ipynb >= 0.2 && < 0.3,
jira-wiki-markup >= 1.5.1 && < 1.6,
mime-types >= 0.1.1 && < 0.2,
mtl >= 2.2 && < 2.4,
- network >= 2.6 && < 3.3,
network-uri >= 2.6 && < 2.8,
pandoc-types >= 1.23.1 && < 1.24,
parsec >= 3.1 && < 3.2,
@@ -568,14 +568,21 @@ library
vector >= 0.12 && < 0.14,
djot >= 0.1.2.4 && < 0.2,
asciidoc >= 0.1 && < 0.2,
- tls >= 2.0.1 && < 2.2,
- crypton-x509-system >= 1.6.7 && < 1.7
if !os(windows)
build-depends: unix >= 2.4 && < 2.9
if flag(embed_data_files)
cpp-options: -DEMBED_DATA_FILES
other-modules: Text.Pandoc.Data.BakedIn
+ if flag(http)
+ cpp-options: -DPANDOC_HTTP_SUPPORT
+ build-depends:
+ crypton-connection >= 0.3.1 && < 0.5,
+ crypton-x509-system >= 1.6.7 && < 1.7,
+ http-client >= 0.4.30 && < 0.8,
+ http-client-tls >= 0.2.4 && < 0.4,
+ network >= 2.6 && < 3.3,
+ tls >= 2.0.1 && < 2.2
hs-source-dirs: src
exposed-modules: Text.Pandoc,
diff --git a/src/Text/Pandoc/Class/CommonState.hs b/src/Text/Pandoc/Class/CommonState.hs
index e3c4efce9..27f9c7f16 100644
--- a/src/Text/Pandoc/Class/CommonState.hs
+++ b/src/Text/Pandoc/Class/CommonState.hs
@@ -1,3 +1,4 @@
+{-# LANGUAGE CPP #-}
{- |
Module : Text.Pandoc.Class.CommonState
Copyright : Copyright (C) 2016-2020 Jesse Rosenthal, John MacFarlane
@@ -23,7 +24,9 @@ import Text.Collate.Lang (Lang)
import Text.Pandoc.MediaBag (MediaBag)
import Text.Pandoc.Logging (LogMessage, Verbosity (WARNING))
import Text.Pandoc.Translations.Types (Translations)
+#ifdef PANDOC_HTTP_SUPPORT
import Network.HTTP.Client (Manager)
+#endif
-- | 'CommonState' represents state that is used by all
-- instances of 'PandocMonad'. Normally users should not
@@ -51,9 +54,11 @@ data CommonState = CommonState
, stResourcePath :: [FilePath]
-- ^ Path to search for resources like
-- included images
+#ifdef PANDOC_HTTP_SUPPORT
, stManager :: Maybe Manager
-- ^ Manager for HTTP client; this needs to persist across many requests
-- for efficiency.
+#endif
, stVerbosity :: Verbosity
-- ^ Verbosity level
, stTrace :: Bool
@@ -79,7 +84,9 @@ defaultCommonState = CommonState
, stInputFiles = []
, stOutputFile = Nothing
, stResourcePath = ["."]
+#ifdef PANDOC_HTTP_SUPPORT
, stManager = Nothing
+#endif
, stVerbosity = WARNING
, stTrace = False
}
diff --git a/src/Text/Pandoc/Class/IO.hs b/src/Text/Pandoc/Class/IO.hs
index 1a9dc2eae..4c0d7b1ab 100644
--- a/src/Text/Pandoc/Class/IO.hs
+++ b/src/Text/Pandoc/Class/IO.hs
@@ -38,13 +38,17 @@ module Text.Pandoc.Class.IO
import Control.Monad.Except (throwError)
import Control.Monad.IO.Class (MonadIO, liftIO)
-import Data.ByteString.Lazy (toChunks)
import Data.Text (Text, pack, unpack)
import Data.Time (TimeZone, UTCTime)
import Data.Unique (hashUnique)
+#ifdef PANDOC_HTTP_SUPPORT
+import Data.ByteString.Lazy (toChunks)
+import System.Environment (getEnv)
+import Data.Default (def)
import Network.Connection (TLSSettings(..))
import qualified Network.TLS as TLS
import qualified Network.TLS.Extra as TLS
+import System.X509 (getSystemCertificateStore)
import Network.HTTP.Client
(httpLbs, Manager, responseBody, responseHeaders,
Request(port, host, requestHeaders), parseUrlThrow, newManager, HttpException)
@@ -52,17 +56,19 @@ import Network.HTTP.Client.Internal (addProxy)
import Network.HTTP.Client.TLS (mkManagerSettings)
import Network.HTTP.Types.Header ( hContentType )
import Network.Socket (withSocketsDo)
+import Text.Pandoc.Class.CommonState (CommonState (..))
+import Text.Pandoc.Class.PandocMonad ( getsCommonState, modifyCommonState )
+import qualified Data.CaseInsensitive as CI
+#endif
import Network.URI (URI(..), parseURI, unEscapeString)
import System.Directory (createDirectoryIfMissing)
-import System.Environment (getEnv)
import System.FilePath ((</>), takeDirectory, normalise)
import qualified System.FilePath.Posix as Posix
import System.IO (stderr)
import System.IO.Error
import System.Random (StdGen)
-import Text.Pandoc.Class.CommonState (CommonState (..))
import Text.Pandoc.Class.PandocMonad
- (PandocMonad, getsCommonState, modifyCommonState,
+ (PandocMonad,
getMediaBag, report, extractURIData)
import Text.Pandoc.Definition (Pandoc, Inline (Image))
import Text.Pandoc.Error (PandocError (..))
@@ -73,7 +79,6 @@ import Text.Pandoc.Walk (walk)
import qualified Control.Exception as E
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as BL
-import qualified Data.CaseInsensitive as CI
import qualified Data.Text as T
import qualified Data.Time
import qualified Data.Time.LocalTime
@@ -83,8 +88,6 @@ import qualified System.Environment as Env
import qualified System.FilePath.Glob
import qualified System.Random
import qualified Text.Pandoc.UTF8 as UTF8
-import Data.Default (def)
-import System.X509 (getSystemCertificateStore)
#ifndef EMBED_DATA_FILES
import qualified Paths_pandoc as Paths
#endif
@@ -125,6 +128,7 @@ newStdGen = liftIO System.Random.newStdGen
newUniqueHash :: MonadIO m => m Int
newUniqueHash = hashUnique <$> liftIO Data.Unique.newUnique
+#ifdef PANDOC_HTTP_SUPPORT
getManager :: (PandocMonad m, MonadIO m) => m Manager
getManager = do
mbManager <- getsCommonState stManager
@@ -154,12 +158,14 @@ getManager = do
newManager tlsManagerSettings
modifyCommonState $ \st -> st{ stManager = Just manager }
pure manager
+#endif
openURL :: (PandocMonad m, MonadIO m) => Text -> m (B.ByteString, Maybe MimeType)
openURL u
| Just (URI{ uriScheme = "data:",
uriPath = upath }) <- parseURI (T.unpack u)
= pure $ extractURIData upath
+#ifdef PANDOC_HTTP_SUPPORT
| otherwise = do
let toReqHeader (n, v) = (CI.mk (UTF8.fromText n), UTF8.fromText v)
customHeaders <- map toReqHeader <$> getsCommonState stRequestHeaders
@@ -181,6 +187,9 @@ openURL u
Right r -> return r
Left (e :: HttpException)
-> throwError $ PandocHttpError u (T.pack (show e))
+#else
+ | otherwise = error "Text.Pandoc.Class.IO.openURL"
+#endif
-- | Read the lazy ByteString contents from a file path, raising an error on
-- failure.