git.iko.soy/typeable/pandoc
on master help sign in
App: added --request-header option.
commitd8804f4747b0214a3aca45ecdf6cb2f6a9d09646 authorJohn MacFarlane <jgm@berkeley.edu> dateSun 15 Oct 2017, 22:11 (9 years ago) parent2f66d57
2 files changed+28−11
MANUAL.txt+11−3 src/Text/Pandoc/App.hs+17−8
MANUAL.txt+11−3
@@ -130,10 +130,11 @@ pandoc will fetch the content using HTTP:
130130
131131 pandoc -f html -t markdown http://www.fsf.org
132132
133−It is possible to supply a custom User-Agent string when requesting a
134−document from a URL, by setting an environment variable:
133+It is possible to supply a custom User-Agent string or other
134+header when requesting a document from a URL:
135135
136− USER_AGENT="Mozilla/5.0" pandoc -f html -t markdown http://www.fsf.org
136+ pandoc -f html -t markdown --request-header User-Agent:"Mozilla/5.0" \
137+ http://www.fsf.org
137138
138139If multiple input files are given, `pandoc` will concatenate them all (with
139140blank lines between them) before parsing. This feature is disabled for
@@ -728,6 +729,13 @@ General writer options
728729 `--resource-path=.:test` will search the working directory
729730 and the `test` subdirectory, in that order.
730731
732+`--request-header=`*NAME*`:`*VAL*
733+
734+: Set the request header *NAME* to the value *VAL* when making
735+ HTTP requests (for example, when a URL is given on the
736+ command line, or when resources used in a document must be
737+ downloaded).
738+
731739Options affecting specific writers
732740----------------------------------
733741
src/Text/Pandoc/App.hs+17−8
@@ -75,9 +75,9 @@ import qualified System.IO as IO (Newline (..))
7575import Text.Pandoc
7676import Text.Pandoc.Builder (setMeta)
7777import Text.Pandoc.Class (PandocIO, extractMedia, fillMediaBag, getLog,
78− setResourcePath, setTrace, report,
78+ setResourcePath, setTrace, report, setRequestHeader,
7979 setUserDataDir, readFileStrict, readDataFile,
80− readDefaultDataFile, setTranslations,
80+ readDefaultDataFile, setTranslations, openURL,
8181 setInputFiles, setOutputFile)
8282import Text.Pandoc.Highlighting (highlightingStyles)
8383import Text.Pandoc.BCP47 (parseBCP47, Lang(..))
@@ -86,7 +86,7 @@ import Text.Pandoc.Writers.Math (defaultMathJaxURL, defaultKaTeXURL)
8686import Text.Pandoc.PDF (makePDF)
8787import Text.Pandoc.Process (pipeProcess)
8888import Text.Pandoc.SelfContained (makeDataURI, makeSelfContained)
89−import Text.Pandoc.Shared (headerShift, isURI, openURL, ordNub,
89+import Text.Pandoc.Shared (headerShift, isURI, ordNub,
9090 safeRead, tabFilter, eastAsianLineBreakFilter)
9191import qualified Text.Pandoc.UTF8 as UTF8
9292import Text.Pandoc.XML (toEntities)
@@ -491,7 +491,10 @@ convertWithOpts opts = do
491491 when (readerName == "markdown_github" ||
492492 writerName == "markdown_github") $
493493 report $ Deprecated "markdown_github" "Use gfm instead."
494+
494495 setResourcePath (optResourcePath opts)
496+ mapM_ (\(n,v) -> setRequestHeader n v) (optRequestHeaders opts)
497+
495498 doc <- sourceToDoc sources >>=
496499 ( (if isJust (optExtractMedia opts)
497500 then fillMediaBag
@@ -641,6 +644,7 @@ data Opt = Opt
641644 , optIncludeAfterBody :: [FilePath] -- ^ Files to include after body
642645 , optIncludeInHeader :: [FilePath] -- ^ Files to include in header
643646 , optResourcePath :: [FilePath] -- ^ Path to search for images etc
647+ , optRequestHeaders :: [(String, String)] -- ^ Headers for HTTP requests
644648 , optEol :: LineEnding -- ^ Style of line-endings to use
645649 , optStripComments :: Bool -- ^ Skip HTML comments
646650 } deriving (Generic, Show)
@@ -716,6 +720,7 @@ defaultOpts = Opt
716720 , optIncludeAfterBody = []
717721 , optIncludeInHeader = []
718722 , optResourcePath = ["."]
723+ , optRequestHeaders = []
719724 , optEol = Native
720725 , optStripComments = False
721726 }
@@ -863,11 +868,7 @@ readSource src = case parseURI src of
863868 BS.readFile src
864869
865870readURI :: FilePath -> PandocIO Text
866−readURI src = do
867− res <- liftIO $ openURL src
868− case res of
869− Left e -> throwError $ PandocHttpError src e
870− Right (contents, _) -> return $ UTF8.toText contents
871+readURI src = UTF8.toText . fst <$> openURL src
871872
872873readFile' :: MonadIO m => FilePath -> m B.ByteString
873874readFile' "-" = liftIO B.getContents
@@ -1161,6 +1162,14 @@ options =
11611162 "SEARCHPATH")
11621163 "" -- "Paths to search for images and other resources"
11631164
1165+ , Option "" ["request-header"]
1166+ (ReqArg
1167+ (\arg opt -> do
1168+ let (key, val) = splitField arg
1169+ return opt{ optRequestHeaders =
1170+ (key, val) : optRequestHeaders opt })
1171+ "NAME:VALUE")
1172+ ""
11641173
11651174 , Option "" ["self-contained"]
11661175 (NoArg