Skip to content
Snippets Groups Projects
script_docs_to_cpp.py 1.91 KiB
Newer Older
  • Learn to ignore specific revisions
  • hluk's avatar
    hluk committed
    #!/usr/bin/python
    '''
    
    hluk's avatar
    hluk committed
    Parses API for C++ from Scriptable API documentation.
    
    hluk's avatar
    hluk committed
    '''
    
    import re
    
    
    hluk's avatar
    hluk committed
    readme_path = 'docs/scripting-api.rst'
    
    output_path = 'src/gui/commandcompleterdocumentation.h'
    
    
    hluk's avatar
    hluk committed
    header = '''// Generated by "utils/script_docs_to_cpp.py" from "%s".
    
    template <typename AddDocumentationCallback>
    void addDocumentation(AddDocumentationCallback addDocumentation)
    {
    '''
    
    footer = '}'
    
    
    hluk's avatar
    hluk committed
    # Regex for function/variable/type name in documentation
    
    re_title = re.compile(r'''
      (?:
    
    hluk's avatar
    hluk committed
        ^\.\.\s*js:function::\s*
    
        (?P<function_api>
          # function return value
          .*?
          # function name
          (?P<function_name>\w+)
          # arguments
          \(.*
        )
    
        |
    
    
    hluk's avatar
    hluk committed
        ^\.\.\s*js:data::\s*
    
        # variable name
        (?P<variable_name>\w+)
    
    hluk's avatar
    hluk committed
        \s*
    
        # followed by opening parenthesis
        (?P<variable_api>\(.*)
    
        |
    
    
    hluk's avatar
    hluk committed
        ^\.\.\s*js:class::\s*
    
        # type name
        (?P<type_name>\w+)$
      )
      ''', re.VERBOSE)
    
    hluk's avatar
    hluk committed
    
    def main():
    
        with open(output_path, mode='w', encoding='utf-8') as output_file:
    
    hluk's avatar
    hluk committed
            output_file.write((header % readme_path) + '\n')
    
    
            with open(readme_path, mode='r', encoding='utf-8') as readme_file:
                match = None
                for line in readme_file:
    
    hluk's avatar
    hluk committed
                    line = line.strip().replace('``', '`')
    
                    if line:
                        if match:
                            name = match.group('function_name') or match.group('variable_name') or match.group('type_name')
                            api = match.group('function_api') or match.group('variable_api') or name
    
    hluk's avatar
    hluk committed
                            description = line.strip()
    
                            output = '    addDocumentation("{}", "{}", "{}");\n'\
    
    hluk's avatar
    hluk committed
                                .format(name, api, description)
    
                            output_file.write(output)
                            match = None
                        else:
                            match = re.match(re_title, line)
    
            output_file.write(footer + '\n')
    
    hluk's avatar
    hluk committed
    
    if __name__ == "__main__":
        main()