8000 Search for metadata files in $DATADIR/metadata by evenbrenden · Pull Request #7851 · jgm/pandoc · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Search for metadata files in $DATADIR/metadata #7851

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion MANUAL.txt
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,9 @@ header when requesting a document from a URL:
files specified later on the command line will be preferred
over those specified in earlier files. Metadata values
specified inside the document, or by using `-M`, overwrite
values specified with this option.
values specified with this option. The file will be searched
for first in the working directory, and then in the `metadata`
subdirectory of the user data directory (see `--data-dir`).

`-p`, `--preserve-tabs`

Expand Down Expand Up @@ -1512,6 +1514,7 @@ Nonzero exit codes have the following meanings:
93 PandocIpynbDecodingError
94 PandocUnsupportedCharsetError
97 PandocCouldNotFindDataFileError
98 PandocCouldNotFindMetadataFileError
99 PandocResourceNotFound
----- ------------------------------------

Expand Down
3 changes: 3 additions & 0 deletions pandoc.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,9 @@ extra-source-files:
test/command/3510-export.latex
test/command/3510-src.hs
test/command/3971b.tex
test/command/5876.yaml
test/command/5876/metadata/5876.yaml
test/command/5876/metadata/command/5876.yaml
test/docbook-chapter.docbook
test/docbook-reader.docbook
test/docbook-xref.docbook
Expand Down
2 changes: 1 addition & 1 deletion src/Text/Pandoc/App.hs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ convertWithOpts opts = do
case optMetadataFiles opts of
[] -> return mempty
paths -> mconcat <$>
mapM (\path -> do raw <- readFileStrict path
mapM (\path -> do raw <- readMetadataFile path
yamlToMeta readerOpts (Just path) raw) paths

let transforms = (case optShiftHeadingLevelBy opts of
Expand Down
20 changes: 20 additions & 0 deletions src/Text/Pandoc/Class/PandocMonad.hs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ module Text.Pandoc.Class.PandocMonad
, getResourcePath
, readDefaultDataFile
, readDataFile
, readMetadataFile
, fillMediaBag
, toLang
, setTranslations
Expand Down Expand Up @@ -586,6 +587,25 @@ readDataFile fname = do
then readFileStrict (userDir </> fname)
else readDefaultDataFile fname

-- | Read metadata file from the working directory or, if not found there, from
-- the metadata subdirectory of the user data directory.
readMetadataFile :: PandocMonad m => FilePath -> m B.ByteString
readMetadataFile fname = do
existsInWorkingDir <- fileExists fname
if existsInWorkingDir
then readFileStrict fname
else do
dataDir <- getUserDataDir
case dataDir of
Nothing ->
throwError $ PandocCouldNotFindMetadataFileError $ T.pack fname
Just userDir -> do
let path = userDir </> "metadata" </> fname
existsInUserDir <- fileExists path
if existsInUserDir
then readFileStrict path
else throwError $ PandocCouldNotFindMetadataFileError $ T.pack fname

-- | Read file from from the default data files.
readDefaultDataFile :: PandocMonad m => FilePath -> m B.ByteString
readDefaultDataFile "reference.docx" =
Expand Down
4 changes: 4 additions & 0 deletions src/Text/Pandoc/Error.hs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ data PandocError = PandocIOError Text IOError
| PandocFilterError Text Text
| PandocLuaError Text
| PandocCouldNotFindDataFileError Text
| PandocCouldNotFindMetadataFileError Text
| PandocResourceNotFound Text
| PandocTemplateError Text
| PandocAppError Text
Expand Down Expand Up @@ -118,6 +119,8 @@ renderError e =
PandocLuaError msg -> "Error running Lua:\n" <> msg
PandocCouldNotFindDataFileError fn ->
"Could not find data file " <> fn
PandocCouldNotFindMetadataFileError fn ->
"Could not find metadata file " <> fn
PandocResourceNotFound fn ->
"File " <> fn <> " not found in resource path"
PandocTemplateError s -> "Error compiling template " <> s
Expand Down Expand Up @@ -198,6 +201,7 @@ handleError (Left e) =
PandocIpynbDecodingError{} -> 93
PandocUnsupportedCharsetError{} -> 94
PandocCouldNotFindDataFileError{} -> 97
PandocCouldNotFindMetadataFileError{} -> 98
PandocResourceNotFound{} -> 99

err :: Int -> Text -> IO a
Expand Down
58 changes: 58 additions & 0 deletions test/command/5876.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
```
% pandoc -s -t native --data-dir=command/5876 --metadata-file=5876.yaml
Hello 6D47
^D
Pandoc
Meta
{ unMeta =
fromList
[ ( "desc"
, MetaInlines
[ Str "Found"
, Space
, Str "in"
, Space
, Str "metadata"
, Space
, Str "directory."
]
)
]
}
[ Para [ Str "Hello" ] ]
```
```
% pandoc -s -t native --data-dir=command/5876 --metadata-file=command/5876.yaml
Hello
^D
Pandoc
Meta
{ unMeta =
fromList
[ ( "desc"
, MetaInlines
[ Str "Found"
, Space
, Str "in"
, Space
, Str "working"
, Space
, Str "directory."
]
)
]
}
[ Para [ Str "Hello" ] ]
```
```
% pandoc -s -t native --data-dir=command/5876 --metadata-file=does-not-exist.yaml
Hello
^D
Could not find metadata file does-not-exist.yaml
```
```
% pandoc -s -t native --metadata-file=does-not-exist.yaml
Hello
^D
Could not find metadata file does-not-exist.yaml
```
3 changes: 3 additions & 0 deletions test/command/5876.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
desc: Found in working directory.
---
3 changes: 3 additions & 0 deletions test/command/5876/metadata/5876.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
desc: Found in metadata directory.
---
3 changes: 3 additions & 0 deletions test/command/5876/metadata/command/5876.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
desc: Should not be loaded.
---
0