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

support {attr} and fix img path error when use_directory_url=True #6

Open
wants to merge 2 commits into
base: develop
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.vscode/
dist/
*.egg-info/
__pycache__/
Expand Down
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,30 @@

This [MkDocs](https://www.mkdocs.org) plugin converts markdown encoded images like

```
![An image caption](\assets\images\my-image.png)
```markdown
![An image caption](../images/my-image.png){align="right" width="20%"}
```

into

```html
<figure class="figure-image">
<img src="\assets\images\my-image.png" alt="An image caption">
<img src="../../images/my-image.png" alt="An image caption" align="left" width="20%" >
<figcaption>An image caption</figcaption>
</figure>
```

- of course, you could leave out `{align="right" width="20%"}`
- support attr : https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/img
- unfortunately, `align:center` is not supported. if you want to center image, use

```markdown
![An image caption](../images/my-image.png){width="20%" style="display:block; margin:0 auto;"}
```

## Requirements

This package requires Python >=3.5 and MkDocs version 1.0 or higher.
This package requires Python >=3.6 and MkDocs version 1.0 or higher.

## Installation

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def read_file(fname):
author='Antonio Cambule',
author_email='[email protected]',
license='MIT',
python_requires='>=3.5',
python_requires='>=3.6',
install_requires=[
'mkdocs'
],
Expand Down
35 changes: 23 additions & 12 deletions src/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,30 @@

import re

from pathlib import Path
from mkdocs.plugins import BasePlugin

class Image2FigurePlugin(BasePlugin):

def modify_match(match):
caption, image_link, attr_list = match.groups()
image_link = ('..' / Path(image_link)).as_posix()
if attr_list:
attr_list = attr_list.replace('{', '').replace('}', '')
else:
attr_list = ''

return (
r'<figure class="figure-image">'
rf' <img src="{image_link}" alt="{caption}" {attr_list}>'
rf' <figcaption>{caption}</figcaption>'
r'</figure>'
)


class Image2FigurePlugin(BasePlugin):
def on_page_markdown(self, markdown, **kwargs):

pattern = re.compile(r'!\[(.*?)\]\((.*?)\)', flags=re.IGNORECASE)

markdown = re.sub(pattern,
r'<figure class="figure-image">\n' + \
r' <img src="\2" alt="\1">\n' + \
r' <figcaption>\1</figcaption>\n' + \
r'</figure>',
markdown)

return markdown

pattern = re.compile(r'!\[(.*?)\]\((.*?)\)(\{[^\}]*\})?', flags=re.IGNORECASE)
markdown = re.sub(pattern, modify_match, markdown)

return markdown
4 changes: 3 additions & 1 deletion tests/fixtures/docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

Our image:

![our image caption](github-octocat.png)
![attr caption](github-octocat.png){align="right" width="20%"}

![our image caption](github-octocat.png)
3 changes: 3 additions & 0 deletions tests/fixtures/mkdocs.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
site_name: test img2fig page

markdown_extensions:
- attr_list

plugins:
- search
- img2fig
4 changes: 4 additions & 0 deletions tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,8 @@ def test_img2fig_output(tmp_path):
assert os.path.exists(index_file), "%s does not exist" % index_file

contents = pathlib.Path(index_file).read_text()
print(contents)
assert "<figcaption>our image caption</figcaption>" in contents
assert '<img src="../github-octocat.png" alt="attr caption" align="right" width="20%">' in contents