Skip to content

Commit fe2a8e5

Browse files
committed
Add problem content template definition as program argument
1 parent 83333dd commit fe2a8e5

File tree

4 files changed

+33
-13
lines changed

4 files changed

+33
-13
lines changed

.github/workflows/publish-docker-image.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ on:
99

1010
jobs:
1111
publish-image:
12+
name: Publish Image
1213
runs-on: ubuntu-latest
1314
steps:
1415
- name: Checkout

README.md

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ The problems hosted on leetcode.com are intellectual propriety of LeetCode LLC u
88
UPLOAD THE DESCRIPTION OF LEETCODE PROBLEMS ON GITHUB OR ON ANY OTHER WEBSITE** or you might receive ad DMCA Takedown
99
notice.
1010

11-
Before using this script make read [LeetCode Terms of Service](https://leetcode.com/terms/).
12-
13-
To avoid committing the problem description on git, you can add `*.txt` to your `.gitignore` file.
11+
Before using this script read the [LeetCode Terms of Service](https://leetcode.com/terms/).
1412

1513
## How it works
1614

@@ -97,6 +95,7 @@ The script accepts the following arguments:
9795
```
9896
usage: leetcode-export [-h] [--cookies COOKIES] [--folder FOLDER]
9997
[--problem-filename PROBLEM_FILENAME]
98+
[--problem-content PROBLEM_CONTENT]
10099
[--submission-filename SUBMISSION_FILENAME] [-v] [-vv]
101100
[-V]
102101
@@ -108,6 +107,8 @@ optional arguments:
108107
--folder FOLDER set output folder
109108
--problem-filename PROBLEM_FILENAME
110109
problem description filename format
110+
--problem-content PROBLEM_CONTENT
111+
problem description content format
111112
--submission-filename SUBMISSION_FILENAME
112113
submission filename format
113114
-v, --verbose enable verbose logging details
@@ -139,7 +140,26 @@ title: str
139140
title_slug: str
140141
```
141142

142-
Default problem description filename template: `${question_id} - ${title_slug}.txt`
143+
Default problem description filename template: `${question_id} - ${title_slug}.html`
144+
145+
### Problem description content template
146+
147+
To change the format of the problem description content, you can provide a template as a string when lunching the
148+
script.
149+
150+
```bash
151+
python leetcode-export --problem-content PROBLEM_CONTENT
152+
```
153+
154+
The template can contain parameters that will later be replaced based on the LeetCode problem information. The available
155+
parameters are the ones used in [problem description filename template](#problem-description-filename-template) plus:
156+
157+
```python
158+
content: str
159+
```
160+
161+
Default problem description content
162+
template: `<h1>${question_id} - ${title}</h1><h2>Difficulty: ${difficulty} - <a href="https://leetcode.com/problems/${title_slug}/">${title_slug}</a></h2>${content}`
143163

144164
### Submission filename template
145165

leetcode_export/__main__.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,17 @@
77
from leetcode_export._version import __version__
88
from leetcode_export.leetcode import LeetCode
99

10-
PROBLEM_CONTENT_TEMPLATE = Template('''${question_id} - ${title}
11-
${difficulty} - https://leetcode.com/problems/${title_slug}/
12-
13-
${content}
14-
''')
15-
1610

1711
def parse_args():
1812
parser = argparse.ArgumentParser(description='Export LeetCode solutions')
1913
parser.add_argument('--cookies', type=str, help='set LeetCode cookies')
2014
parser.add_argument('--folder', type=str, default='.', help='set output folder')
21-
parser.add_argument('--problem-filename', type=str, default='${question_id} - ${title_slug}.txt',
15+
parser.add_argument('--problem-filename', type=str, default='${question_id} - ${title_slug}.html',
2216
help='problem description filename format')
17+
parser.add_argument('--problem-content', type=str,
18+
default='<h1>${question_id} - ${title}</h1><h2>Difficulty: ${difficulty} - ' +
19+
'<a href="https://leetcode.com/problems/${title_slug}/">${title_slug}</a></h2>${content}',
20+
help='problem description content format')
2321
parser.add_argument('--submission-filename', type=str,
2422
default='${date_formatted} - ${status_display} - runtime ${runtime} - memory ${memory}.${extension}',
2523
help='submission filename format')
@@ -53,6 +51,7 @@ def main():
5351
)
5452

5553
problem_template = Template(args.problem_filename)
54+
problem_content_template = Template(args.problem_content)
5655
submission_template = Template(args.submission_filename)
5756

5857
leetcode = LeetCode()
@@ -83,7 +82,7 @@ def main():
8382
info_filename = problem_template.substitute(**problem.__dict__)
8483
if not os.path.exists(info_filename):
8584
info_file = open(info_filename, 'w+')
86-
info_file.write(PROBLEM_CONTENT_TEMPLATE.substitute(**problem.__dict__))
85+
info_file.write(problem_content_template.substitute(**problem.__dict__))
8786
info_file.close()
8887
written_problems.add(submission.title_slug)
8988

leetcode_export/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '1.1.1'
1+
__version__ = '1.2.0'

0 commit comments

Comments
 (0)