Skip to content

Commit 0fcbf8a

Browse files
author
Julia Maria Jodczyk
committed
Merge branch 'plugin' into 'main'
Plugin See merge request mwasilew/2023-ZPRP!21
2 parents c5b8ec1 + 013c6bc commit 0fcbf8a

34 files changed

+398
-179
lines changed

.gitignore

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
venv/
22
.venv/
33
__pycache__/
4-
.vscode/*
4+
.vscode
55
.pytest_cache/
66
.tox/
7-
.hypothesis/
7+
.hypothesis/
8+
site
9+
demo/site

Makefile

+4
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,8 @@ clean:
2626
rm -rf .tox
2727
rm -rf .hypothesis
2828

29+
plugin_demo:
30+
cd demo
31+
mkdocs serve
32+
2933
.PHONY: tests clean pre_commit

README.md

+42-17
Original file line numberDiff line numberDiff line change
@@ -12,43 +12,68 @@
1212

1313
The plugin allows you to define image size categories and use them as tags in your documentation. The images annotated with tags will be resized to the size specified in the configuration file.
1414

15-
### (Planned) Usage:
15+
### Installation:
16+
You can install the plugin using pip: `pip install mkdocs-image-formatter-plugin`
17+
18+
19+
### Usage:
20+
21+
#### Prerequisites
22+
Plugin works by replacing your tags with attribute list that is later interpreted by `attr_list` markdown extension. Make sure you add it to `mkdocs.yaml` like so:
23+
```
24+
markdown_extensions:
25+
- attr_list
26+
```
27+
1628

1729
#### Step 1.
1830

1931
To configuration file add:
2032

2133
```
22-
- image_sizes:
23-
- <tag_name>:
24-
width: <size>
25-
height: <size>
26-
- <tag_name>:
27-
width: <size>
28-
height: <size>
34+
- image_formatter:
35+
image_size:
36+
<tag_name>:
37+
width: <size>
38+
height: <size>
39+
<tag_name>:
40+
width: <size>
41+
height: <size>
2942
```
3043

3144
where different `tag_names` are the names of image size categories. Remember about measurement units (`px, %, etc.`) when specifying width and height.
3245

3346
Example of correct configuration:
3447

3548
```
36-
- image_sizes:
37-
- large:
38-
width: 100px
39-
height: 50px
40-
- small:
41-
width: 80px
42-
height: 40px
49+
markdown_extensions:
50+
- attr_list
51+
52+
plugins:
53+
- image-formatter:
54+
image_size:
55+
small:
56+
height: 100px
57+
width: 100px
58+
big:
59+
height: 200px
60+
width: 200px
4361
```
4462

63+
4564
#### Step 2:
4665

4766
Annotate images in your documents with desired tags.
4867

4968
Example:
5069

5170
```
52-
@small
53-
(../images/b.png)
71+
![MyImage]@small(../images/b.png)
5472
```
73+
74+
#### Info:
75+
If you accidentally use a tag that is not specified in the documentation, the tag will be removed and the image will be displayed with no additional formatting.
76+
77+
78+
### Demo:
79+
You can see the plugin in action after you clone this project to your local repository by executing `make plugin_demo` in the main directory.

demo/docs/img/Maine_coon.jpg

26.8 KB
Loading

demo/docs/img/Persialainen.jpg

102 KB
Loading

demo/docs/img/Siam_lilacpoint.jpg

632 KB
Loading

demo/docs/index.md

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Cats
2+
3+
## Introduction
4+
5+
Cats are small, carnivorous mammals that are often kept as pets. They are known for their playful behavior, independent nature, and distinctive vocalizations. Here are some interesting facts about cats.
6+
7+
## Breeds
8+
9+
### 1. Persian Cat
10+
11+
Persian cats are known for their long, luxurious fur and distinctive flat face. They are calm and enjoy lounging around the house.
12+
13+
![Persian Cat]@small(./img/Persialainen.jpg)
14+
15+
### 2. Siamese Cat
16+
17+
Siamese cats are known for their striking blue almond-shaped eyes and short coat with color points. They are social and vocal.
18+
19+
![Siamese Cat]@large(./img/Siam_lilacpoint.jpg)
20+
21+
### 3. Maine Coon
22+
23+
Maine Coon cats are one of the largest domestic cat breeds. They have tufted ears, bushy tails, and a thick, water-resistant coat.
24+
25+
![Maine Coon]@small(./img/Maine_coon.jpg)
26+
27+
## Behavior
28+
29+
Cats exhibit various behaviors that make them unique and endearing as pets.
30+
31+
### 1. Purring
32+
33+
Cats purr when content or relaxed, and it is often associated with positive feelings.
34+
35+
### 2. Kneading
36+
37+
Kneading is a behavior where cats push their paws in and out against a soft surface. It's usually a sign of contentment.
38+
39+
### 3. Hunting Instinct
40+
41+
Cats are natural hunters, and even domesticated cats may display hunting behavior, such as stalking and pouncing.
42+
43+
## Health
44+
45+
### 1. Common Health Issues
46+
47+
- **Hairballs:** Cats may develop hairballs from grooming themselves. Regular brushing can help reduce this issue.
48+
49+
- **Dental Problems:** Dental care is crucial for cats. Regular teeth cleaning and dental check-ups are recommended.
50+
51+
- **Obesity:** Overweight cats are prone to various health issues. A balanced diet and regular exercise are essential.
52+
53+
## Conclusion
54+
55+
Cats make wonderful companions, bringing joy and warmth to households worldwide. Understanding their behavior and providing proper care ensures a happy and healthy relationship with these fascinating creatures.

demo/mkdocs.yml

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
site_name: Test with cats
2+
3+
dev_addr: 127.0.0.1:8002
4+
5+
theme:
6+
name: "material"
7+
8+
markdown_extensions:
9+
- attr_list
10+
11+
plugins:
12+
- mkdocstrings
13+
- image-formatter:
14+
image_size:
15+
small:
16+
height: 100px
17+
width: 100px
18+
big:
19+
height: 200px
20+
width: 200px
21+
22+
nav:
23+
- Home: index.md

docs/DesignProposal.md

+9-7
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,15 @@ Plug-in do MkDocs ułatwiający ustawianie rozmiarów obrazów w dokumentacji.
3737
- możliwość konfiguracji wymiarów obrazów:
3838
```
3939
# Dodanie odpowiednich wpisów konfiguracyjnych:
40-
image_sizes:
41-
large:
42-
- width: 100px
43-
height: 50px
44-
small:
45-
- width: 80px
46-
height: 40px
40+
plugins:
41+
- image_formatter:
42+
image_size:
43+
<tag_name>:
44+
width: <size>
45+
height: <size>
46+
<tag_name>:
47+
width: <size>
48+
height: <size>
4749
```
4850
- automatyczne ustawianie wymiarów obrazów poprzez zdefiniowane tagi
4951
```

docs/index.md

+36-17
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,50 @@
1212

1313
The plugin allows you to define image size categories and use them as tags in your documentation. The images annotated with tags will be resized to the size specified in the configuration file.
1414

15-
### (Planned) Usage:
15+
### Installation:
16+
You can install the plugin using pip: `pip install mkdocs-image-formatter-plugin`
17+
18+
### Usage:
19+
20+
#### Prerequisites
21+
Plugin works by replacing your tags with attribute list that is later interpreted by `attr_list` markdown extension. Make sure you add it to `mkdocs.yaml` like so:
22+
```
23+
markdown_extensions:
24+
- attr_list
25+
```
1626

1727
#### Step 1.
1828

1929
To configuration file add:
2030

2131
```
22-
- image_sizes:
23-
- <tag_name>:
24-
width: <size>
25-
height: <size>
26-
- <tag_name>:
27-
width: <size>
28-
height: <size>
32+
- image_formatter:
33+
image_size:
34+
<tag_name>:
35+
width: <size>
36+
height: <size>
37+
<tag_name>:
38+
width: <size>
39+
height: <size>
2940
```
3041

3142
where different `tag_names` are the names of image size categories. Remember about measurement units (`px, %, etc.`) when specifying width and height.
3243

3344
Example of correct configuration:
3445

3546
```
36-
- image_sizes:
37-
- large:
38-
width: 100px
39-
height: 50px
40-
- small:
41-
width: 80px
42-
height: 40px
47+
markdown_extensions:
48+
- attr_list
49+
50+
plugins:
51+
- image-formatter:
52+
image_size:
53+
small:
54+
height: 100px
55+
width: 100px
56+
big:
57+
height: 200px
58+
width: 200px
4359
```
4460

4561
#### Step 2:
@@ -49,6 +65,9 @@ Annotate images in your documents with desired tags.
4965
Example:
5066

5167
```
52-
@small
53-
(../images/b.png)
68+
![MyImage]@small(../images/b.png)
5469
```
70+
71+
#### Info:
72+
If you accidentally use a tag that is not specified in the documentation, the tag will be removed and the image will be displayed with no additional formatting.
73+

image_formatter/image_formatter_plugin/image_formatter_plugin.py

+11-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
import cssutils
1212
import logging
1313
from typing import Tuple
14+
from image_formatter.lexer.lexer import Lexer
15+
from image_formatter.image_properties_tag_replacer.image_properties_tag_replacer import ImagePropertiesTagReplacer
16+
from image_formatter.token_to_string_converter.token_to_string_converter import TokenToStringConverter
1417

1518
WIDTH = "width"
1619
HEIGHT = "height"
@@ -44,7 +47,7 @@ def on_config(self, config: MkDocsConfig) -> MkDocsConfig or None:
4447
"""
4548
Verifies if tags are defined correctly. Each tag should specify width and height in valid CSS form.
4649
"""
47-
size_tags = config["image_formatter"]
50+
size_tags = self.config["image_size"]
4851
for tag, options in size_tags.items():
4952
if WIDTH not in options or HEIGHT not in options:
5053
raise mkdocs.config.base.ValidationError(f"width or height is missing from {tag} tag configuration")
@@ -56,6 +59,10 @@ def on_config(self, config: MkDocsConfig) -> MkDocsConfig or None:
5659
return config
5760

5861
def on_page_read_source(self, page: Page, config: MkDocsConfig) -> str or None:
59-
# todo: using lexer, image_properties_tag_replacer and interpreter read user's docs and apply sizes specified in tags
60-
61-
pass
62+
src_path = page.file.abs_src_path
63+
with open(src_path, "r") as fp:
64+
lexer = Lexer(fp) # noqa
65+
image_tag_replacer = ImagePropertiesTagReplacer(lexer, self.config["image_size"])
66+
converter = TokenToStringConverter(image_tag_replacer)
67+
result = converter.to_text()
68+
return result

image_formatter/image_properties_tag_replacer/image_properties_tag_replacer.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def parse_image_link_url(self, tag_token: Token) -> Token:
5656
self.next_token()
5757
return Token(TokenType.T_IMAGE_URL_WITH_PROPERTIES, url_token.position, formatted_url)
5858
else:
59-
log.error(f"{ImagePropertiesTagReplacer.name()}: Failed to parse image link url.")
59+
log.info(f"{ImagePropertiesTagReplacer.name()}: Failed to parse image link url.")
6060
self.error_handler.handle(UnexpectedTagException(TokenType.T_IMAGE_URL, self.curr_token.type))
6161
return tag_token
6262

@@ -70,6 +70,9 @@ def add_tag_properties_to_url(self, tag_token: Token) -> str:
7070
Returns:
7171
str: image tag properties formatted to CSS
7272
"""
73+
if tag_token.string not in self.image_tags_properties:
74+
log.info(f"{ImagePropertiesTagReplacer.name()}: {tag_token.string} is an unknown tag, removing formatting")
75+
return self.curr_token.string
7376
properties = '{: style="'
7477
pairs = ";".join([f"{key}:{value}" for key, value in self.image_tags_properties[tag_token.string].items()])
7578
properties = properties + pairs + '"}'

image_formatter/lexer/lexer.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def __init__(
5151
self.max_int = max_int
5252
self.tag = tag
5353
self.special_signs = special_signs
54-
self.newline_characters = newline_characters # @TODO add hypothesis tests
54+
self.newline_characters = newline_characters
5555
self.additional_path_signs = additional_path_signs
5656

5757
@classmethod
@@ -305,7 +305,7 @@ def get_url_ending(self, string: str) -> str or None:
305305
return None
306306
string += self.current_char
307307
self.next_char()
308-
while self.is_character() or self.current_char in self.additional_path_signs: # @TODO add hypothesis tests
308+
while self.is_character() or self.current_char in self.additional_path_signs:
309309
string += self.current_char
310310
self.next_char()
311311
log.info(f"{Lexer.name()}: Url ending built successfully.")

0 commit comments

Comments
 (0)