Skip to content

Commit 4a36d0e

Browse files
committed
feat (day 136): parser markdown image syntax into HTML img tag using regex
1 parent 10eb7fb commit 4a36d0e

1 file changed

Lines changed: 86 additions & 0 deletions

File tree

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
"""
2+
3+
Markdown Image Parser
4+
Given a string of an image in Markdown, return the equivalent HTML string.
5+
6+
A Markdown image has the following format: "![alt text](image_url)". Where:
7+
8+
alt text is the description of the image (the alt attribute value).
9+
image_url is the source URL of the image (the src attribute value).
10+
Return a string of the HTML img tag with the src set to the image URL and the alt set to the alt text.
11+
12+
For example, given "![Cute cat](cat.png)" return '<img src="cat.png" alt="Cute cat">';
13+
14+
Make sure the tag, order of attributes, spacing, and quote usage is the same as above.
15+
Note: The console may not display HTML tags in strings when logging messages — check the browser console to see logs with tags included.
16+
"""
17+
18+
import unittest
19+
20+
class MarkdownImageParserTest(unittest.TestCase):
21+
22+
def test1(self):
23+
self.assertEqual(parse_image("![Cute cat](cat.png)"),'<img src="cat.png" alt="Cute cat">')
24+
25+
def test2(self):
26+
self.assertEqual(parse_image("![Rocket Ship](https://freecodecamp.org/cdn/rocket-ship.jpg)"),'<img src="https://freecodecamp.org/cdn/rocket-ship.jpg" alt="Rocket Ship">')
27+
28+
def test3(self):
29+
self.assertEqual(parse_image("![Cute cats!](https://freecodecamp.org/cats.jpeg)"),'<img src="https://freecodecamp.org/cats.jpeg" alt="Cute cats!">')
30+
31+
32+
import re
33+
34+
def parse_image(markdown):
35+
36+
match = re.match(r"!(\[.+\])(\(.+\))",markdown)
37+
"""
38+
1. Regex grouping
39+
=> The above pattern r"!(\[.+\])(\(.+\))" captures:
40+
==> Group 1: "[alt text]" (including the square brackets).
41+
==> Group 2: "(url)" (including the parentheses).
42+
=> So alt_info will look like "[Cute cat]" and url_info will look like "(cat.png)".
43+
-> You need to strip the brackets/ parantheses.
44+
45+
46+
2. Incorrect use of groups() :
47+
=> match.groups(0) is not valid.
48+
=> groups() returns all groups at once as a tuple. You should unpack them directly.
49+
50+
Key Takeaway:
51+
52+
-=> Use !\[(.+)\]\((.+)\) so the groups capture inside the brackets/parentheses.
53+
- => Unpack with alt_info, url_info = match.groups().
54+
- => Build the HTML string with those values.
55+
56+
"""
57+
58+
# alt_info = match.groups(0)
59+
# url_info = match.groups(1)
60+
alt_info , url_info = match.groups()
61+
62+
63+
return f'<img src="{url_info}" alt="{alt_info}">'
64+
65+
def parse_image(markdown):
66+
67+
match = re.fullmatch(r'!\[(.*?)\]\((.*?)\)',markdown)
68+
69+
if not match:
70+
raise ValueError("Invalid markdown image format")
71+
72+
alt, src = match.groups()
73+
74+
return f'<img src="{src}" alt="{alt}">'
75+
76+
77+
"""
78+
Pattern: !\[(.*?)\]\((.*?)\) extracts alt text and image URL.
79+
Output format: Exactly
80+
81+
<img src="image_url" alt="alt text"> with the same attribute order, spacing, and double quotes.
82+
"""
83+
84+
if __name__ == "__main__":
85+
print(parse_image("![Cute cat](cat.png)"))
86+
unittest.main()

0 commit comments

Comments
 (0)