Skip to content
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

Better ReST: Function to resolve external includes? #6632

Closed
saurabhnanda opened this issue Aug 22, 2020 · 5 comments
Closed

Better ReST: Function to resolve external includes? #6632

saurabhnanda opened this issue Aug 22, 2020 · 5 comments

Comments

@saurabhnanda
Copy link

Following up from #223 (comment)

The motivation for this feature-request is partially given at #6631

The filename in .. include directives is treated as relative to the entire process's current-directory. However, I'd expect it to be relative to the file being parsed. So, if I move an RST file (along with its external includes) up/down a directory level, I don't need to change all include paths within the file.

The generalization of this behaviour would be a loadExternalInclude :: FilePath -> IO String type of function that can be overridden by the library user.

@saurabhnanda
Copy link
Author

I'm not sure what you mean here; relative paths should work, but pandoc interprets them relative to the working directory (or any --resource-paths)

@jgm I completely missed this part! What is the Haskell equivalent of --resource-paths command-line switch?

@mb21
Copy link
Collaborator

mb21 commented Aug 22, 2020

What is the Haskell equivalent of --resource-paths command-line switch?

I think setResourcePath...

@saurabhnanda
Copy link
Author

I think setResourcePath...

Thanks. It looks like what I was looking for!

@saurabhnanda
Copy link
Author

It doesn't seem to be working as expected. Should the include directive be using fetchItem instead of readFileFromDirs ["."] -

mbContents <- readFileFromDirs ["."] $ T.unpack f
?

@saurabhnanda saurabhnanda reopened this Aug 23, 2020
@jgm
Copy link
Owner

jgm commented Aug 23, 2020

"as expected" here means what? Our aim would be to have it work the way docutils tools work, to the extent possible. So see how it is processed by docutils' rsthtml.py.

Unfortunately we can't reliably go relative to the directory containing the input file. Multiple input files can be specified on the command line; pandoc concatenates them before passing them to the RST reader, which therefore doesn't know which file corresponds to a particular part of the input.

We could use fetchItem, which would at least allow you to work around this with resource-path, but it would also introduce some non-reST behavior (e.g. you could specify a URL). Maybe worth it though.

jgm added a commit that referenced this issue May 9, 2021
Previously, when multiple file arguments were provided, pandoc
simply concatenated them and passed the contents to the readers,
which took a Text argument.

As a result, the readers had no way of knowing which file
was the source of any particular bit of text.  This meant that
we couldn't report accurate source positions on errors or
include accurate source positions as attributes in the AST.
More seriously, it meant that we couldn't resolve resource
paths relative to the files containing them
(see e.g. #5501, #6632, #6384, #3752).

Add Text.Pandoc.Sources (exported module), with a `Sources` type
and a `ToSources` class.  A `Sources` wraps a list of `(SourcePos,
Text)` pairs. [API change] A parsec `Stream` instance is provided for
`Sources`.  The module also exports versions of parsec's `satisfy` and
other Char parsers that track source positions accurately from a
`Sources` stream (or any instance of the new `UpdateSourcePos` class).

Text.Pandoc.Parsing now exports these modified Char parsers instead of
the ones parsec provides.  Modified parsers to use a `Sources` as stream
[API change].

The readers that previously took a `Text` argument have been
modified to take any instance of `ToSources`. So, they may still
be used with a `Text`, but they can also be used with a `Sources`
object.

In Text.Pandoc.Error, modified the constructor PandocParsecError
to take a `Sources` rather than a `Text` as first argument,
so parse error locations can be accurately reported.

T.P.Error: showPos, do not print "-" as source name.
jgm added a commit that referenced this issue May 10, 2021
Previously, when multiple file arguments were provided, pandoc
simply concatenated them and passed the contents to the readers,
which took a Text argument.

As a result, the readers had no way of knowing which file
was the source of any particular bit of text.  This meant that
we couldn't report accurate source positions on errors or
include accurate source positions as attributes in the AST.
More seriously, it meant that we couldn't resolve resource
paths relative to the files containing them
(see e.g. #5501, #6632, #6384, #3752).

Add Text.Pandoc.Sources (exported module), with a `Sources` type
and a `ToSources` class.  A `Sources` wraps a list of `(SourcePos,
Text)` pairs. [API change] A parsec `Stream` instance is provided for
`Sources`.  The module also exports versions of parsec's `satisfy` and
other Char parsers that track source positions accurately from a
`Sources` stream (or any instance of the new `UpdateSourcePos` class).

Text.Pandoc.Parsing now exports these modified Char parsers instead of
the ones parsec provides.  Modified parsers to use a `Sources` as stream
[API change].

The readers that previously took a `Text` argument have been
modified to take any instance of `ToSources`. So, they may still
be used with a `Text`, but they can also be used with a `Sources`
object.

In Text.Pandoc.Error, modified the constructor PandocParsecError
to take a `Sources` rather than a `Text` as first argument,
so parse error locations can be accurately reported.

T.P.Error: showPos, do not print "-" as source name.
@jgm jgm closed this as completed in 2a2e08d May 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants