-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreate_catalog.py
80 lines (68 loc) · 2.83 KB
/
create_catalog.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
#!/usr/bin/env python3
import subprocess
import os
import sys
from pathlib import Path
from natsort import natsorted
def get_font_metadata(font_file):
"""Extract metadata from a font file using fc-scan."""
try:
cmd = ['fc-scan', '--format', '%{family}|%{fullname}|%{postscriptname}\n', str(font_file)]
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode == 0 and result.stdout.strip():
# Process each line of output
metadata_list = []
for line in result.stdout.strip().split('\n'):
family, fullname, postscript = line.split('|')
# Create set of names (fullname + postscript)
names = set()
if fullname.strip():
names.update(name.strip() for name in fullname.split(','))
if postscript.strip():
names.add(postscript.strip())
metadata_list.append({
'family': family.strip(),
'names': names
})
return metadata_list
except Exception as e:
print(f"Error processing {font_file}: {e}", file=sys.stderr)
return None
def format_markdown_table(fonts_data):
"""Format the font data as a markdown table."""
# Table header
table = "| File | Family | Names |\n"
table += "|------|--------|-------|\n"
# Table rows
for font in fonts_data:
if font['metadata']:
# Escape pipe characters in all fields
file_path = font['path'].replace('|', '\\|')
# Create a row for each metadata entry (multiple faces in font file)
for metadata in font['metadata']:
family = metadata['family'].replace('|', '\\|')
names = ', '.join(sorted(metadata['names'])).replace('|', '\\|')
table += f"| {file_path} | {family} | {names} |\n"
else:
file_path = font['path'].replace('|', '\\|')
table += f"| {file_path} | Error | Error |\n"
return table
def main():
# Get all font files in current directory and subdirectories
font_extensions = ('.ttf', '.otf', '.ttc')
fonts_data = []
for font_file in Path('.').rglob('*'):
if font_file.suffix.lower() in font_extensions:
# Get relative path
rel_path = os.path.relpath(font_file)
metadata = get_font_metadata(font_file)
fonts_data.append({
'path': rel_path,
'metadata': metadata
})
# Sort by file path for consistent output
fonts_data = natsorted(fonts_data, key=lambda x: x['path'].lower())
# Generate and print the markdown table
print(format_markdown_table(fonts_data))
if __name__ == "__main__":
main()