summaryrefslogtreecommitdiffstats
path: root/scripts/gen_mapfiles.py
blob: a5aed0ca85b19fc3cc71b1a9087509f7efe96432 (plain)
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
"""
    scripts/gen_mapfiles.py
    ~~~~~~~~~~~~~~~~~~~~~~~

    Regenerate mapping files.

    :copyright: Copyright 2006-2022 by the Pygments team, see AUTHORS.
    :license: BSD, see LICENSE for details.
"""

from importlib import import_module
from pathlib import Path
import re
import sys

top_src_dir = Path(__file__).parent.parent
pygments_package = top_src_dir / 'pygments'
sys.path.insert(0, str(pygments_package.parent.resolve()))

from pygments.util import docstring_headline

def main():
    for key in ['lexers', 'formatters']:
        lines = []
        for file in (pygments_package / key).glob('[!_]*.py'):
            module_name = '.'.join(file.relative_to(pygments_package.parent).with_suffix('').parts)
            print(module_name)
            module = import_module(module_name)
            for obj_name in module.__all__:
                obj = getattr(module, obj_name)
                desc = (module_name, obj.name, tuple(obj.aliases), tuple(obj.filenames))
                if key == 'lexers':
                    desc += (tuple(obj.mimetypes),)
                elif key == 'formatters':
                    desc += (docstring_headline(obj),)
                else:
                    assert False
                lines.append(f'    {obj_name!r}: {desc!r},')
        # Sort to make diffs minimal.
        lines.sort()
        new_dict = '\n'.join(lines)
        content = f'''# Automatically generated by scripts/gen_mapfiles.py.
# DO NOT EDIT BY HAND; run `make mapfiles` instead.

{key.upper()} = {{
{new_dict}
}}
'''
        (pygments_package / key / '_mapping.py').write_text(content, encoding='utf8')
        print(f'=== {len(lines)} {key} processed.')

if __name__ == '__main__':
    main()