Skip to content

Commit 609d9bc

Browse files
Refactor and enhance practice mode handling.
Replaced PracticeFactory with PracticeHandler for improved clarity and maintainability. Added Markdown support for processing problem descriptions and converting them to HTML before handling plain text. Updated requirements to include the markdown library.
1 parent cdeb48f commit 609d9bc

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

handlers/PracticeHandler.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import random
22
import html2text
3+
import markdown
34

45
from typing import List, Dict, Any, Optional
56

@@ -35,16 +36,19 @@ def log_problem_details(self, problem, difficulty_label, url):
3536
if ac_rate is not None:
3637
log(f"📈 Acceptance Rate: {ac_rate:.2f}%", LogLevel.INFO)
3738

39+
# Assuming `content` is the Markdown content
3840
content = problem.get("content")
3941
if content:
4042
try:
43+
html_content = markdown.markdown(content)
44+
4145
text_maker = html2text.HTML2Text()
4246
text_maker.ignore_links = True
43-
plain_text = text_maker.handle(content)
44-
log(f"📝 Problem Description:", LogLevel.INFO)
45-
log(f"{plain_text}", LogLevel.INFO)
47+
plain_text = text_maker.handle(html_content)
48+
49+
log(plain_text, LogLevel.INFO)
4650
except Exception as e:
47-
log(f"⚠️ Failed to process problem description: {e}", LogLevel.WARN)
51+
log(f"Failed to convert problem content: {str(e)}", LogLevel.ERROR)
4852

4953
def open_in_browser(self, url, open_flag):
5054
if open_flag:

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,5 @@ zipp==3.19.2
5151

5252
python-dotenv~=1.0.1
5353
html2text~=2024.2.26
54+
55+
Markdown~=3.7

services/PracticeModeManager.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
from PracticeFactory import PracticeModeFactory
1+
from handlers.PracticeHandler import PracticeModeHandler
22

33

44
def handle(inputs):
5-
mode_handler = PracticeModeFactory.get_mode(inputs["practice_mode"])
5+
mode_handler = PracticeModeHandler.get_mode(inputs["practice_mode"])
66
mode_handler.handle(inputs)

0 commit comments

Comments
 (0)