Coverage for src/django_resume/markdown.py: 100%

17 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-10-13 13:17 +0200

1import re 

2 

3 

4def textarea_input_to_markdown(text): 

5 # <br> to \n 

6 text = text.replace("<br>", "\n") 

7 

8 # remove <div> and </div> 

9 text = text.replace("<div>", "") 

10 print("remove: ", text) 

11 text = text.replace("</div>", "") 

12 return text 

13 

14 

15def markdown_to_textarea_input(text): 

16 # \n to <br> 

17 text = text.replace("\n", "<br>") 

18 

19 return text 

20 

21 

22def markdown_to_html(text): 

23 # Headings 

24 text = re.sub( 

25 r"^(#{1,6})\s*(.*)", 

26 lambda m: f"<h{len(m.group(1))}>{m.group(2)}</h{len(m.group(1))}>", 

27 text, 

28 flags=re.MULTILINE, 

29 ) 

30 

31 # Bold 

32 text = re.sub(r"\*\*(.*?)\*\*", r"<strong>\1</strong>", text) 

33 

34 # Italic 

35 text = re.sub(r"\*(.*?)\*", r"<em>\1</em>", text) 

36 

37 # Links [text](url) 

38 text = re.sub(r"\[(.*?)\]\((.*?)\)", r'<a href="\2">\1</a>', text) 

39 

40 # Paragraphs 

41 text = re.sub(r"(^|\n)([^\n]+)\n", r"\1<p>\2</p>\n", text) 

42 

43 return text