Skip to content

Commit 9c311a1

Browse files
committed
fix: error in mkdir
1 parent b4c769f commit 9c311a1

File tree

4 files changed

+33
-12
lines changed

4 files changed

+33
-12
lines changed

README_zh.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ if __name__ == "__main__":
281281
- [x] 支持pypi简化操作步骤
282282
- [ ] 提供文件自定义命名
283283
- [ ] 提供图片自定义格式化命名方式
284+
- [ ] 构建PDF转换器
284285

285286
## FAQ
286287

imarkdown/converter.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def _download_img(image_local_storage_directory: str, image_url: str) -> Optiona
5555
now_time = time.strftime("%Y%m%d_%H%M%S", time.localtime(time.time()))
5656
image_local_storage_directory = polish_path(image_local_storage_directory)
5757
if not os.path.exists(image_local_storage_directory):
58-
os.mkdir(image_local_storage_directory)
58+
os.makedirs(image_local_storage_directory, exist_ok=True)
5959

6060
images_path = f"{image_local_storage_directory}{now_time}{random.randint(1000, 10000)}.png"
6161
with open(images_path, "wb") as f:
@@ -148,7 +148,9 @@ def set_converted_md_file_name(
148148
if enable_rename:
149149
if new_name != "":
150150
if name_prefix or name_prefix:
151-
raise ValueError("You can not set `name_prefix` and `name_prefix` if you set `new_name`.")
151+
raise ValueError(
152+
"You can not set `name_prefix` and `name_prefix` if you set `new_name`."
153+
)
152154
self.converted_md_file_name = new_name
153155
else:
154156
self.converted_md_file_name = f"{name_prefix}{md_name}{name_suffix}.md"
@@ -220,18 +222,26 @@ def _find_img_and_replace(self, md_str: str) -> str:
220222
Returns:
221223
Markdown data for the image url has been changed.
222224
"""
223-
images = list(
224-
map(
225-
lambda item: item[1],
226-
re.findall(
227-
r"(?:!\[(.*?)\]\((.*?)\))|<img.*?src=[\'\"](.*?)[\'\"].*?>", md_str
228-
),
229-
)
225+
_images = re.findall(
226+
r"(?:!\[(.*?)\]\((.*?)\))|<img.*?src=[\'\"](.*?)[\'\"].*?>", md_str
230227
)
231228

229+
images = []
230+
for image in _images:
231+
if image[1] == "":
232+
continue
233+
# If current image link is local path URL and you need to web URL to a local path,
234+
# the local path url will not be converted.
235+
if not self.is_local_images and not image[1].startswith("http"):
236+
continue
237+
images.append(image[1])
238+
232239
for image in images:
233240
converted_image_url = self._get_converted_image_url(image)
234241
md_str = md_str.replace(image, converted_image_url)
242+
logger.info(
243+
f"[imarkdown] All images conversion for this md file have been completed, ready to save to file."
244+
)
235245
return md_str
236246

237247
def _get_converted_image_url(self, original_image_url: str) -> str:

imarkdown/schema.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ def variables_check(cls, values: Dict[str, Any]) -> Optional[Dict[str, Any]]:
6565
values["name"][-3:] == ".md"
6666
), f'<{values["name"][-3:]}> is not markdown file.'
6767

68+
values["name"] = values["absolute_path_name"].split("/")[-1]
6869
values["absolute_path"] = "/".join(values["absolute_path_name"].split("/")[:-1])
6970
if "image_type" in values and values["image_type"] == "local":
7071
assert (
@@ -75,7 +76,6 @@ def variables_check(cls, values: Dict[str, Any]) -> Optional[Dict[str, Any]]:
7576
)
7677
values["is_default_image_directory"] = False
7778
else:
78-
# set default image_directory if image_type == remote
7979
if "image_directory" not in values or not values["image_directory"]:
8080
values["image_directory"] = f"{values['absolute_path']}/images"
8181
values["is_default_image_directory"] = True
@@ -98,7 +98,7 @@ def update_enable_save_images(
9898
@property
9999
def to_convert_params(self) -> Dict[str, Any]:
100100
params = {
101-
"md_file_path": self.name,
101+
"md_file_path": self.absolute_path_name,
102102
"enable_rename": self.enable_rename,
103103
"output_md_directory": self.output_directory,
104104
"image_local_storage_directory": self.image_directory,
@@ -199,6 +199,7 @@ def update_config(
199199
output_directory: Optional[str] = None,
200200
enable_save_images: bool = True,
201201
):
202+
"""Update every md_file basic parameters."""
202203
if not self._md_files:
203204
ValueError("Please run generate_md_files firstly.")
204205
for md_file in self._md_files:
@@ -215,6 +216,15 @@ def init_md_files(
215216
self,
216217
md_mediums: List[Union[MdFile, MdFolder]],
217218
) -> List[MdFile]:
219+
"""MdFolder may contain several MdFile.This method can convert all
220+
MdFolders and MdFiles to MdFiles list.
221+
222+
Args:
223+
md_mediums(List[Union[MdFile, MdFolder]]): a list of MdFile and MdFolder
224+
225+
Returns:
226+
A list of all MdFile.
227+
"""
218228
def find_sub_files(md_folder: MdFolder):
219229
for sub_node in md_folder.sub_nodes:
220230
if isinstance(sub_node, MdFile):

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
setuptools.setup(
2323
name="imarkdown",
24-
version="1.1.1",
24+
version="1.1.2",
2525
author="Zeeland",
2626
author_email="[email protected]",
2727
description="A practical Markdown image url converter",

0 commit comments

Comments
 (0)