Last active
May 2, 2022 02:28
-
-
Save shiracamus/3163d408500294e088f9bfde6b36636b to your computer and use it in GitHub Desktop.
download a Qiita article and print program in code blocks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
download a Qiita article and print program in code blocks | |
example: | |
$ python3 qiitacode.py https://qiita.com/shiracamus/items/556ff8d916712a9f7055 | |
$ python3 qiitacode.py https://qiita.com/shiracamus/items/556ff8d916712a9f7055 c | |
$ python3 qiitacode.py https://qiita.com/shiracamus/items/556ff8d916712a9f7055 py | |
""" | |
import sys | |
import re | |
import requests | |
def load_markdown(url): | |
return requests.get(f'{url}.md').text.splitlines() | |
def markdown_code(lines, lang=''): | |
lang_pattern = rf'(\s*{lang}(:.*)?|.*:.*\.{lang})' if lang else '' | |
code_start = re.compile(rf'^\s*(```|~~~){lang_pattern}\s*$') | |
code_end = re.compile(r'^\s*(```|~~~)\s*$') | |
in_code_block = False | |
for line in lines: | |
if in_code_block: | |
if code_end.match(line): | |
in_code_block = False | |
yield '\n' | |
else: | |
yield line | |
else: | |
if code_start.match(line): | |
in_code_block = True | |
def main(): | |
if not 2 <= len(sys.argv) <= 3: | |
print(f'Usage: {sys.argv[0]} QIITA_ARTICLE_URL LANGUAGE') | |
exit(1) | |
markdown = load_markdown(url=sys.argv[1]) | |
lang = sys.argv[2] if len(sys.argv) > 2 else '' | |
for code in markdown_code(markdown, lang): | |
print(code) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment