-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-template.sh
executable file
·199 lines (167 loc) · 4.89 KB
/
create-template.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/bin/bash
show_help() {
cat <<EOF
Usage: $(basename "$0") [options]
Options:
-p, --problem Problem Slug (required)
-c, --code Problem Code (required)
-t, --template Template directory (optional)
-i, --ide IDE (e.g. code, rider) (optional)
-o, --output Solutions directory (optional, default: Solutions or ../Solutions)
-h, --help Display this help message
Example:
$(basename "$0") -p two-sum -c 1 -t /path/to/template -i code
EOF
exit 0
}
# Functions:
exit_if_failed() {
if [ "$1" -ne 0 ]; then
echo "Error! Operation failed with code $1: $2"
exit "$1"
fi
}
warn_if_failed() {
if [ "$1" -ne 0 ]; then
echo "Warning! Operation failed with code $1: $2"
fi
}
create_dir_if_missing() {
[ ! -d "$1" ] && mkdir -p "$1"
}
ensure_command_exists() {
command -v "$1" &>/dev/null || {
echo "Command not found: $1"
exit 1
}
}
ask_ignore_error() {
if [ "$?" -ne 0 ]; then
read -rp "Do you want to ignore this error? (Y/n) " ignore_error
[[ "$ignore_error" =~ ^[nN]$ ]] && exit 1
fi
}
get_default_solution_dir() {
for dir in "Solutions" "../Solutions"; do
[ -d "$dir" ] && echo "$dir" && return
done
echo "" # Not Found
}
# Function to retrieve HTML, extract an element with XPath, and convert to Markdown
html_to_markdown() {
local url="$1"
local output_path="$2"
# Step 1: Retrieve the HTML page with curl
local html_content
html_content=$(curl -s "$url")
if [ $? -ne 0 ]; then
echo "Error: Failed to fetch the HTML page."
return 1
fi
# Step 2: Extract the element with XPath using xmllint
local extracted_html
extracted_html=$(echo "$html_content" | xmllint --html --xpath '/html/body/div[5]' - 2>/dev/null)
if [ $? -ne 0 ]; then
echo "Error: XPath extraction failed. Element not found."
return 1
fi
# Step 3: Convert the extracted HTML to Markdown using pandoc
echo "$extracted_html" | pandoc -f html -t markdown -o "$output_path"
if [ $? -ne 0 ]; then
echo "Error: Failed to convert HTML to Markdown."
return 1
fi
echo "Markdown content saved to $output_path"
return 0
}
checkout_branch() {
local branch_name="$1"
# Check if the branch name is provided
if [[ -z "$branch_name" ]]; then
echo "Error: No branch name provided."
return 1
fi
# Check if the branch exists
if git rev-parse --verify "$branch_name" &>/dev/null; then
echo "Switching to branch '$branch_name'..."
git checkout "$branch_name"
else
echo "Branch '$branch_name' does not exist. Creating it..."
git checkout -b "$branch_name"
fi
}
#===========================================================
# Parse Command-Line Options
while [[ $# -gt 0 ]]; do
case "$1" in
-p|--problem)
problem_slug="$2"
shift 2
;;
-c|--code)
problem_code="$2"
shift 2
;;
-t|--template)
template_dir="$2"
shift 2
;;
-o|--output)
solutions_dir="$2"
shift 2
;;
-i|--ide)
ide="$2"
shift 2
;;
-h|--help)
show_help
;;
*)
echo "Unknown option: $1"
show_help
;;
esac
done
#===========================================================
# Validate Inputs
[ -z "$problem_slug" ] && { echo "Error: Problem slug is required."; show_help; }
if [ -n "$template_dir" ] && [ ! -d "$template_dir" ]; then
echo "Invalid template path: $template_dir"
exit 1;
fi
solutions_dir="${solutions_dir:-$(get_default_solution_dir)}"
if [ -z "$solutions_dir" ] || [ ! -d "$solutions_dir" ]; then
echo "Solutions directory not found: $solutions_dir"
exit 1
fi
if [ -n "$ide" ]; then
ensure_command_exists "$ide"
fi
#===========================================================
target_solution_dir="$solutions_dir/$problem_slug"
problem_description="$target_solution_dir/README.md"
[ -z "$problem_code" ] && [ ! -f "$problem_description" ] && { echo "Error: Problem code is required."; show_help; }
# Checkout a new branch
checkout_branch "$problem_slug"
exit_if_failed "$?" "Unable to checkout to branch $problem_slug"
create_dir_if_missing "$target_solution_dir"
# Copy template to solution directory
if [ -n "$template_dir" ]; then
cp -r "$template_dir" "$target_solution_dir"
exit_if_failed "$?" "Failed to copy template to $target_solution_dir"
fi
#Create README file for question text
if [ ! -f "$problem_description" ]; then
html_to_markdown "https://leetcode.ca/all/$problem_code.html" "$problem_description"
fi
echo "Directory is ready: $target_solution_dir"
if [ -n "$ide" ]; then
"$ide" "$target_solution_dir"
warn_if_failed "$?" "Failed to open IDE for $target_solution_dir"
fi
# Push to master branch (optional)
read -rp "Do you want to push to the master branch (with merge flag)? (y/N) " push_confirm
if [[ "$push_confirm" =~ ^[yY]$ ]]; then
git pull --no-rebase && git push
fi