Skip to content

Commit c2c7e9a

Browse files
committed
fix: dev
1 parent 2295587 commit c2c7e9a

File tree

2 files changed

+52
-37
lines changed

2 files changed

+52
-37
lines changed

leetcode_py/tools/generator.py

Lines changed: 50 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -60,43 +60,58 @@ def __init__(self, file_ops: FileOperations | None = None):
6060

6161
def check_and_prompt_tags(self, data: Dict[str, Any]) -> Dict[str, Any]:
6262
"""Check and prompt for tags if empty."""
63-
if "tags" in data and (not data["tags"] or data["tags"] == []):
64-
if sys.stdin.isatty(): # Interactive terminal
65-
typer.echo("\n📋 No tags specified. Would you like to add any common tags?")
66-
typer.echo("Available options:")
67-
for i, tag in enumerate(self.common_tags, 1):
68-
typer.echo(f" {i}. {tag}")
69-
typer.echo(" 0. Skip (no tags)")
70-
71-
choices_input = typer.prompt(
72-
"Select options (comma-separated, e.g. '1,2' or '0' to skip)"
73-
)
74-
75-
try:
76-
choices = [int(x.strip()) for x in choices_input.split(",")]
77-
selected_tags: list[str] = []
78-
79-
for choice in choices:
80-
if choice == 0:
81-
selected_tags = []
82-
break
83-
elif 1 <= choice <= len(self.common_tags):
84-
tag = self.common_tags[choice - 1]
85-
if tag not in selected_tags:
86-
selected_tags.append(tag)
87-
88-
data["tags"] = selected_tags
89-
if selected_tags:
90-
typer.echo(f"✅ Added tags: {', '.join(selected_tags)}")
91-
else:
92-
typer.echo("✅ No tags added")
93-
94-
except ValueError:
95-
typer.echo("⚠️ Invalid input, skipping tags")
96-
data["tags"] = []
97-
63+
if self._should_prompt_for_tags(data) and sys.stdin.isatty():
64+
selected_tags = self._prompt_for_tags()
65+
data["tags"] = selected_tags
66+
self._display_tags_result(selected_tags)
9867
return data
9968

69+
def _should_prompt_for_tags(self, data: Dict[str, Any]) -> bool:
70+
"""Check if we should prompt for tags."""
71+
return "tags" in data and (not data["tags"] or data["tags"] == [])
72+
73+
def _prompt_for_tags(self) -> list[str]:
74+
"""Prompt user for tag selection."""
75+
self._display_tag_options()
76+
choices_input = typer.prompt("Select options (comma-separated, e.g. '1,2' or '0' to skip)")
77+
return self._process_tag_choices(choices_input)
78+
79+
def _display_tag_options(self) -> None:
80+
"""Display available tag options."""
81+
typer.echo("\n📋 No tags specified. Would you like to add any common tags?")
82+
typer.echo("Available options:")
83+
for i, tag in enumerate(self.common_tags, 1):
84+
typer.echo(f" {i}. {tag}")
85+
typer.echo(" 0. Skip (no tags)")
86+
87+
def _process_tag_choices(self, choices_input: str) -> list[str]:
88+
"""Process user's tag choices."""
89+
try:
90+
choices = [int(x.strip()) for x in choices_input.split(",")]
91+
return self._build_selected_tags(choices)
92+
except ValueError:
93+
typer.echo("⚠️ Invalid input, skipping tags")
94+
return []
95+
96+
def _build_selected_tags(self, choices: list[int]) -> list[str]:
97+
"""Build list of selected tags from choices."""
98+
selected_tags: list[str] = []
99+
for choice in choices:
100+
if choice == 0:
101+
return []
102+
if 1 <= choice <= len(self.common_tags):
103+
tag = self.common_tags[choice - 1]
104+
if tag not in selected_tags:
105+
selected_tags.append(tag)
106+
return selected_tags
107+
108+
def _display_tags_result(self, selected_tags: list[str]) -> None:
109+
"""Display the result of tag selection."""
110+
if selected_tags:
111+
typer.echo(f"✅ Added tags: {', '.join(selected_tags)}")
112+
else:
113+
typer.echo("✅ No tags added")
114+
100115
def auto_set_dummy_return(self, data: Dict[str, Any]) -> Dict[str, Any]:
101116
"""Auto-set dummy_return based on return_type."""
102117
if "dummy_return" not in data and "return_type" in data:

leetcode_py/tools/parser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ def parse_content(html_content: str) -> Dict[str, Any]:
1717
"""Parse HTML content to extract description, examples, and constraints."""
1818
# Extract description (everything before first example)
1919
desc_match = re.search(
20-
r'<p>(.*?)(?=<p><strong class="example">|<p><strong>Constraints:|$)', html_content, re.DOTALL
20+
r'<p>(.*)(?=<p><strong class="example">|<p><strong>Constraints:|$)', html_content, re.DOTALL
2121
)
2222
description = HTMLParser.clean_html(desc_match.group(1)) if desc_match else ""
2323

2424
# Extract examples
2525
examples = []
2626
example_pattern = (
27-
r'<p><strong class="example">Example (\d+):</strong></p>\s*<pre>\s*(.*?)\s*</pre>'
27+
r'<p><strong class="example">Example (\d+):</strong></p>\s*<pre>\s*(.*)\s*</pre>'
2828
)
2929
for match in re.finditer(example_pattern, html_content, re.DOTALL):
3030
example_num = match.group(1)

0 commit comments

Comments
 (0)