Skip to content

Commit

Permalink
add - read pair of parenthesis inside url
Browse files Browse the repository at this point in the history
  • Loading branch information
jackusay committed May 1, 2023
1 parent da00c84 commit 0787fb5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 12 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@


* work only local image
* can't use right parenthesis in url
* `![aaa](bbb)` - In `bbb` part, if you put some behind url, it needs to be *behind* double quate. ex: `![alt text](a.jpg "title a1")`
* if you use parenthesis inside bbb, the parenthesis must be paired in order to work.

```
#local image, ok
Expand All @@ -12,14 +12,12 @@
![alt text](C:\aa bb\2023-12-30.png "title a1") #absolute path
![alt text](中/a c 文.jpg "title a1") #non ascii
![alt text](file:///C:\Current Work\Service\12-30-34.png)
![alt text](New folder/a002 (1) (2).jpg?key&ddd "title")
#online image, no work
![alt text](https://example.com/img)
![alt text](http://example.com/img.jpg)
#fail, duo to markdown image syntax limit, you can't use right parenthesis in url:
![alt text](a (2).jpg "title a1") #wrong!
#???
![alt text](//aa/bb/12-30-34.png)
![alt text](abc/a b.jpg =205x251)
Expand Down
46 changes: 38 additions & 8 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,51 @@
def log(s):
#print(s)
pass


def right_parenthesis_index(txt):
""" get index of markdown image syntax's right parenthesis ) """
i = 0
right_parenthesis_index = 0 # ) index of markdown image syntax; ![ ]( )
# ^

#In order to read: folder/img (2).jpg this type of url,
#![aaa](bbb), I assume bbb part **must** has multiple **pair** of parenthesis.
#if the number of right parenthesis != left parenthesis, right_parenthesis_index return 0
for index in range(0, len(txt)):
if txt[index] == "(":
i += 1
elif txt[index] == ")":
i -= 1
if i == 0:
right_parenthesis_index = index
break
return right_parenthesis_index

def get_url(txt):
"""input line_text, return url"""
#url can't mix with ), otherwise it become unsure ) is url or part of image syntax.
"""input line_text, return url
The parenthesis must be paired in order to work."""
#In markdown's syntax, url can't mix with ), otherwise it become unsure ) is url or part of image syntax.
#But we can **assume** the parenthesis must be paired.

x = re.findall("!\[[^\]]+\]\([^\)]+\)", txt)
#get image syntax ex: ![Stormtroopocat](https://octodex.github.com/images/stormtroopocat.jpg "The Stormtroopocat")
log(f"image syntax: {x}")
#log(f"image syntax: {x}")
if not x:
log("Can't find image syntax.")
#log("Can't find image syntax.")
return

x = txt[re.search("!\[[^\]]+\]\(", txt).end()-1:] #strip ![xxx] part
rp = right_parenthesis_index(x)
if not rp:
log("The parenthesis must be paired in order to work.")
return
p = x[:rp] #strip anything after right parenthesis), including itself
pp = p[1:] #strip prefix (

#q = re.search("!\[[^\]]+\]", x[0]) #get title ex: ![sdff]
#log(q.group()[2:-1])
p = re.search("\([^\)]+", x[0]) #get (... part ex: (https://octodex.github.com/images/stormtroopocat.jpg "The Stormtroopocat"
pp = p.group()[1:] #strip prefix (
#p = re.search("\([^\)]+", x[0]) #get (... part ex: (https://octodex.github.com/images/stormtroopocat.jpg "The Stormtroopocat"
#pp = p.group()[1:] #strip prefix (
url = pp.split("\"")[0].strip() #get url
url = url.split("?")[0] #strip query string ex: cat.img?key&value > cat.img
log(f"url: {url}")
Expand Down Expand Up @@ -109,7 +140,6 @@ def insert_file(self, ed_self, txt, nline):
filepath = ed_self.get_filename()
fn = os.path.join(os.path.dirname(filepath), url)


if not os.path.isfile(fn):
ed_self.gap(GAP_DELETE, nline, nline)
msg_status(PRE + _('Cannot find picture'))
Expand Down

0 comments on commit 0787fb5

Please sign in to comment.