Skip to content

use image paths relative to md file #45

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
25 changes: 20 additions & 5 deletions sphinx_mdinclude/render.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import re
import textwrap
from functools import partial
Expand Down Expand Up @@ -31,7 +32,8 @@ class RestRenderer(BaseRenderer):
6: "#",
}

def __init__(self, *args, **kwargs):
def __init__(self, mdinclude_path, *args, **kwargs):
self.mdinclude_path = mdinclude_path
self._indent_block = partial(textwrap.indent, prefix=self.indent)
super().__init__(*args, **kwargs)

Expand Down Expand Up @@ -225,11 +227,16 @@ def image(self, src, alt, title):
"""
# rst does not support title option
# and I couldn't find title attribute in HTML standard

# generate the path to the image file relative to the rst file
image_path = os.path.join(os.path.dirname(self.mdinclude_path), src).replace(
"\\", "/"
)
Comment on lines +232 to +234
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to see this using pathlib and proper separator normalization rather than string replacements.

return "\n".join(
[
"",
".. image:: {}".format(src),
" :target: {}".format(src),
".. image:: {}".format(image_path),
" :target: {}".format(image_path),
" :alt: {}".format(alt),
"",
]
Expand Down Expand Up @@ -307,8 +314,16 @@ def rest_code_block(self, text):


class RestMarkdown(Markdown):
def __init__(self, renderer=None, block=None, inline=None, plugins=None, **kwargs):
renderer = renderer or RestRenderer()
def __init__(
self,
mdinclude_path=".",
renderer=None,
block=None,
inline=None,
plugins=None,
**kwargs,
):
renderer = renderer or RestRenderer(mdinclude_path)
block = block or RestBlockParser()
inline = inline or RestInlineParser(renderer)
plugins = plugins or [PLUGINS[p] for p in DEFAULT_PLUGINS]
Expand Down
1 change: 1 addition & 0 deletions sphinx_mdinclude/sphinx.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ def run(self):

config = self.state.document.settings.env.config
converter = RestMarkdown(
mdinclude_path=rst_directives.path(self.arguments[0]),
no_underscore_emphasis=config.no_underscore_emphasis,
parse_relative_links=config.md_parse_relative_links,
anonymous_references=config.md_anonymous_references,
Expand Down