-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathmarkdown_tool.py
executable file
·155 lines (130 loc) · 5.51 KB
/
markdown_tool.py
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
#!/usr/bin/python3
"""
Tool for the downloading Markdown articles, replace image links and save the article in the selected format.
"""
import argparse
from argparse import RawDescriptionHelpFormatter, SUPPRESS, ZERO_OR_MORE, OPTIONAL
import logging
from mimetypes import types_map
from pathlib import Path
from markdown_toolset.article_processor import ArticleProcessor, DeduplicationVariant, IN_FORMATS_LIST, OUT_FORMATS_LIST
from markdown_toolset.__version__ import __version__
del types_map['.jpe']
class CustomArgumentDefaultsHelpFormatter(RawDescriptionHelpFormatter):
"""Help message formatter which adds default values to argument help.
Only the name of this class is considered a public API. All the methods
provided by the class are considered an implementation detail.
"""
def _get_help_string(self, action):
help_ = action.help
if '%(default)' not in action.help:
if action.default is not SUPPRESS:
defaulting_nargs = [OPTIONAL, ZERO_OR_MORE]
if action.option_strings or action.nargs in defaulting_nargs:
help_ += ' (default: %(default)s)'
return help_
def main(arguments):
"""
Entrypoint.
"""
logging.basicConfig(
format='%(asctime)s %(message)s', datefmt='%d.%m.%Y %H:%M:%S', level='DEBUG' if arguments.verbose else 'INFO'
)
print(f'Markdown tool version {__version__} started...')
if arguments.process_local_images:
print('--process_local_images is deprecated and will be disabled in the next version!')
processor = ArticleProcessor(
article_file_path_or_url=arguments.article_file_path_or_url,
skip_list=arguments.skip_list,
downloading_timeout=arguments.downloading_timeout,
output_format=arguments.output_format,
output_path=getattr(arguments, 'output_path', Path.cwd()),
remove_source=arguments.remove_source,
images_public_path=getattr(arguments, 'images_public_path', ''),
input_formats=arguments.input_format.split('+'),
skip_all_incorrect=arguments.skip_all_incorrect,
download_incorrect_mime=arguments.download_incorrect_mime,
deduplication_type=getattr(DeduplicationVariant, arguments.deduplication_type.upper()),
images_dirname=arguments.images_dirname,
save_hierarchy=arguments.prepend_images_with_path,
replace_image_names=arguments.replace_image_names,
)
processor.process()
print('Processing finished successfully...')
if __name__ == '__main__':
parser = argparse.ArgumentParser(
prog='markdown_tool',
epilog='Use tool at your own risk!',
description=f'{__doc__}Version: {__version__}',
formatter_class=CustomArgumentDefaultsHelpFormatter,
)
parser.add_argument('article_file_path_or_url', type=str, help='path to the article file in the Markdown format')
parser.add_argument(
'-D',
'--deduplication-type',
choices=[i.name.lower() for i in DeduplicationVariant],
default='disabled',
help='Deduplicate images, using content hash or SHA1(image_name)',
)
parser.add_argument(
'-d',
'--images-dirname',
default='images',
help='Folder in which to download images ' '(possible variables: $article_name, $time, $date, $dt, $base_url)',
)
parser.add_argument(
'-a', '--skip-all-incorrect', default=False, action='store_true', help='skip all incorrect images'
)
parser.add_argument(
'-E',
'--download-incorrect-mime',
default=False,
action='store_true',
help='download "images" with unrecognized MIME type',
)
parser.add_argument(
'-s',
'--skip-list',
default=None,
help='skip URL\'s from the comma-separated list (or file with a leading \'@\')',
)
parser.add_argument('-i', '--input-format', default='md', choices=IN_FORMATS_LIST, help='input format')
parser.add_argument(
'-l', '--process-local-images', default=False, action='store_true', help='[DEPRECATED] Process local images'
)
parser.add_argument(
'-n',
'--replace-image-names',
default=False,
action='store_true',
help='Replace image names, using content hash',
)
parser.add_argument(
'-o', '--output-format', default=OUT_FORMATS_LIST[0], choices=OUT_FORMATS_LIST, help='output format'
)
parser.add_argument(
'-p',
'--images-public-path',
default=SUPPRESS,
help='Public path to the folder of downloaded images '
'(possible variables: $article_name, $time, $date, $dt, $base_url)',
)
# TODO: Replace this with variables.
parser.add_argument(
'-P', '--prepend-images-with-path', default=False, action='store_true', help='Save relative images paths'
)
parser.add_argument(
'-R', '--remove-source', default=False, action='store_true', help='Remove or replace source file'
)
parser.add_argument(
'-t',
'--downloading-timeout',
type=float,
default=-1,
help='how many seconds to wait before downloading will be failed',
)
parser.add_argument('-O', '--output-path', type=str, help='article output file name or path', default=SUPPRESS)
parser.add_argument('--verbose', '-v', default=False, action='store_true', help='More verbose logging')
parser.add_argument('--version', action='version', version=f'%(prog)s {__version__}', help='return version number')
args = parser.parse_args()
main(args)