Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
Blog
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Container registry
Monitor
Service Desk
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
TheJoeCoder
Blog
Commits
a25de5a1
Verified
Commit
a25de5a1
authored
9 months ago
by
TheJoeCoder
Browse files
Options
Downloads
Patches
Plain Diff
Change urlencode to quote (didn't read the docs) and fix replace bug
parent
7bf5923d
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
updatecontent.py
+9
-5
9 additions, 5 deletions
updatecontent.py
with
9 additions
and
5 deletions
updatecontent.py
+
9
−
5
View file @
a25de5a1
...
...
@@ -3,10 +3,10 @@ import os
import
logging
import
shutil
import
re
from
urllib.parse
import
urlencod
e
from
urllib.parse
import
quot
e
# MD_IMAGE_REGEX = r'!\[(?<altText>.*)\]\s*\((?<imageURL>.+)\)|img\s*src="(?<imageURL1>[^"]*)"\s*alt="(?<altText1>[^"]*)" \/>|img\s*alt="(?<altText2>[^"]*)"\s*src="(?<imageURL2>[^"]*)" \/>'
OBSIDIAN_IMAGE_REGEX
=
r
'
!\[\[(.+\.(png|jpe?g|avif|bmp|gif|svg|webp))(?:\|[0-9]+(?:x[0-9]+)?)?\]\]
'
OBSIDIAN_IMAGE_REGEX
=
r
'
!\[\[(
(
.+\.(png|jpe?g|avif|bmp|gif|svg|webp))(?:\|[0-9]+(?:x[0-9]+)?)?
)
\]\]
'
# Initialise logging
logging
.
basicConfig
(
level
=
logging
.
INFO
)
...
...
@@ -56,8 +56,12 @@ for filename in os.listdir(posts_target_dir):
images
=
re
.
findall
(
OBSIDIAN_IMAGE_REGEX
,
content
)
for
image
in
images
:
# image[0] is the full match (e.g. filename.png|100x100)
# image[1] is the image name (e.g. filename.png)
# image[2] is the image extension (e.g. png)
# Get the image name
image_name
=
image
[
0
]
image_name
=
image
[
1
]
image_path
=
os
.
path
.
join
(
images_source_dir
,
image_name
)
logger
.
debug
(
f
"
Processing image
{
image_name
}
"
)
...
...
@@ -73,9 +77,9 @@ for filename in os.listdir(posts_target_dir):
shutil
.
copy
(
image_path
,
target_image_path
)
# Replace the image path in the content
image_url_name
=
urlencod
e
(
image_name
)
image_url_name
=
quot
e
(
image_name
)
md_link
=
f
"

"
content
=
content
.
replace
(
f
"
![[
{
image
_name
}
]]
"
,
md_link
)
content
=
content
.
replace
(
f
"
![[
{
image
[
0
]
}
]]
"
,
md_link
)
# Write the updated content back to the file
with
open
(
filepath
,
"
w
"
)
as
file
:
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
sign in
to comment