7 ROOT_DIR
= os
.path
.dirname(os
.path
.dirname(os
.path
.abspath(__file__
)))
8 README_FILE
= os
.path
.join(ROOT_DIR
, 'README.md')
10 PREFIX
= r
'''%yt-dlp(1)
14 yt\-dlp \- A youtube-dl fork with additional features and patches
18 **yt-dlp** \[OPTIONS\] URL [URL...]
26 parser
= optparse
.OptionParser(usage
='%prog OUTFILE.md')
27 _
, args
= parser
.parse_args()
29 parser
.error('Expected an output filename')
33 with open(README_FILE
, encoding
='utf-8') as f
:
36 readme
= filter_excluded_sections(readme
)
37 readme
= move_sections(readme
)
38 readme
= filter_options(readme
)
40 with open(outfile
, 'w', encoding
='utf-8') as outf
:
41 outf
.write(PREFIX
+ readme
)
44 def filter_excluded_sections(readme
):
45 EXCLUDED_SECTION_BEGIN_STRING
= re
.escape('<!-- MANPAGE: BEGIN EXCLUDED SECTION -->')
46 EXCLUDED_SECTION_END_STRING
= re
.escape('<!-- MANPAGE: END EXCLUDED SECTION -->')
48 rf
'(?s){EXCLUDED_SECTION_BEGIN_STRING}.+?{EXCLUDED_SECTION_END_STRING}\n',
52 def move_sections(readme
):
53 MOVE_TAG_TEMPLATE
= '<!-- MANPAGE: MOVE "%s" SECTION HERE -->'
54 sections
= re
.findall(r
'(?m)^%s$' % (
55 re
.escape(MOVE_TAG_TEMPLATE
).replace(r
'\%', '%') % '(.+)'), readme
)
57 for section_name
in sections
:
58 move_tag
= MOVE_TAG_TEMPLATE
% section_name
59 if readme
.count(move_tag
) > 1:
60 raise Exception(f
'There is more than one occurrence of "{move_tag}". This is unexpected')
62 sections
= re
.findall(rf
'(?sm)(^# {re.escape(section_name)}.+?)(?=^# )', readme
)
64 raise Exception(f
'The section {section_name} does not exist')
65 elif len(sections
) > 1:
66 raise Exception(f
'There are multiple occurrences of section {section_name}, this is unhandled')
68 readme
= readme
.replace(sections
[0], '', 1).replace(move_tag
, sections
[0], 1)
72 def filter_options(readme
):
73 section
= re
.search(r
'(?sm)^# USAGE AND OPTIONS\n.+?(?=^# )', readme
).group(0)
74 options
= '# OPTIONS\n'
75 for line
in section
.split('\n')[1:]:
76 mobj
= re
.fullmatch(r
'''(?x)
77 \s{4}(?P<opt>-(?:,\s|[^\s])+)
78 (?:\s(?P<meta>(?:[^\s]|\s(?!\s))+))?
82 options
+= f
'{line.lstrip()}\n'
84 option
, metavar
, description
= mobj
.group('opt', 'meta', 'desc')
86 # Pandoc's definition_lists. See http://pandoc.org/README.html
87 option
= f
'{option} *{metavar}*' if metavar
else option
88 description
= f
'{description}\n' if description
else ''
89 options
+= f
'\n{option}\n: {description}'
92 return readme
.replace(section
, options
, 1)
95 if __name__
== '__main__':