From aff2e760210a428bf4e1957a667b1f7e08ae74e8 Mon Sep 17 00:00:00 2001 From: MaxVerevkin <79253435110@ya.ru> Date: Thu, 9 Jul 2020 13:20:09 +0300 Subject: [PATCH] create_manpage_completions.py: introduce TypeScdocManParser which is capable of parsing scdoc manpages This greatly improves generated completions for scdoc man pages, see #7187. --- share/tools/create_manpage_completions.py | 51 +++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/share/tools/create_manpage_completions.py b/share/tools/create_manpage_completions.py index c6749b71f..97e48e49a 100755 --- a/share/tools/create_manpage_completions.py +++ b/share/tools/create_manpage_completions.py @@ -502,6 +502,56 @@ class Type4ManParser(ManParser): options_section = options_section[options_matched.end() - 3 :] options_matched = re.search(options_parts_regex, options_section) +class TypeScdocManParser(ManParser): + def is_my_type(self, manpage): + return compile_and_search(r"\.(\\)(\") Generated by scdoc(.*?)\.SH OPTIONS(.*?)", manpage) != None + + def parse_man_page(self, manpage): + options_section_regex = re.compile("\.SH OPTIONS(.*?)\.SH", re.DOTALL) + options_section_matched = re.search(options_section_regex, manpage) + if options_section_matched == None: + return False + options_section = options_section_matched.group(1) + + options_parts_regex = re.compile("(.*?).RE", re.DOTALL) + options_matched = re.match(options_parts_regex, options_section) + add_diagnostic("Command is %r" % CMDNAME) + + if options_matched == None: + add_diagnostic("%r: Unable to find options" % self) + return False + + while options_matched != None: + # Get first option and move options_section + option = options_matched.group(1) + options_section = options_section[options_matched.end()::] + options_matched = re.match(options_parts_regex, options_section) + + option = remove_groff_formatting(option) + option = option.split("\n") + + # Crean option list + option_clean = [] + for line in option: + if line not in ["", ".P", ".RS 4"]: + option_clean.append(line) + + # Shold be at least two lines + if len(option_clean) < 2: + add_diagnostic("Unable to split option from description") + continue + + # Name and description, others lines are ignored + option_name = option_clean[0] + option_description = option_clean[1] + + if "-" not in option_name: + add_diagnostic("%r doesn't contain '-'" % option_name) + + option_name = unquote_double_quotes(option_name) + option_name = unquote_single_quotes(option_name) + built_command(option_name, option_description) + return True @@ -780,6 +830,7 @@ def parse_manpage_at_path(manpage_path, output_directory): parsers = [TypeDeroffManParser()] else: parsers = [ + TypeScdocManParser(), Type1ManParser(), Type2ManParser(), Type4ManParser(),