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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
#! /usr/bin/env python
#################################################
# Convert each worldflags symbol to a PDF file. #
# #
# Author: Scott Pakin <scott.clsl@pakin.org> #
#################################################
import concurrent.futures
import glob
import os
import re
import subprocess
import sys
def kpsewhich(fname):
'Find a filename in the TeX tree.'
proc = subprocess.run(['kpsewhich', fname], capture_output=True,
check=True, encoding='utf-8')
return proc.stdout.strip()
# Extract the package date from worldflags.sty.
wfdate_re = re.compile(r'(\d\d\d\d)-(\d\d)-(\d\d)')
sty = kpsewhich('worldflags.sty')
with open(sty) as r:
for ln in r:
match = wfdate_re.search(ln)
if match is not None:
wfdate = match[1] + '/' + match[2] + '/' + match[3]
break
# Acquire a list of all defined symbols.
wfdir = os.path.dirname(kpsewhich('worldflag_US.tex'))
tex_files = glob.glob(os.path.join(wfdir, '*.tex'))
flags = [os.path.splitext(os.path.basename(fn))[0][10:] for fn in tex_files]
flags.sort()
# Create a fakeworldflags.sty file.
with open('fakeworldflags.sty', 'w') as w:
w.write('''\
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This is a generated file. DO NOT EDIT. %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
''')
w.write(r'''
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{fakeworldflags}[%s v0.0 Flags of the world]
\RequirePackage{graphicx}
\newcommand*{\worldflag}[1]{\includegraphics{worldflags/flag_#1}}
\endinput
'''[1:] % wfdate)
# Create and switch to a worldflags subdirectory.
try:
os.mkdir('worldflags')
except FileExistsError:
pass
os.chdir('worldflags')
def run_and_return_output(cmd_line):
'Run a command line and return its combined stdout + stderr.'
stdout = 'RUN: ' + ' '.join(cmd_line) + '\n'
proc = subprocess.run(cmd_line, check=True,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
stdout += proc.stdout.decode('utf-8')
return stdout
# For speed, dump a LaTeX format file with worldflags preloaded.
with open('preloaded.tex', 'w') as w:
w.write(r'''
\documentclass{minimal}
\usepackage{worldflags}
\begin{document}
\end{document}
'''[1:])
subprocess.run([
'pdflatex',
'--ini',
'&pdflatex',
'mylatex.ltx',
'preloaded.tex'
],
check=True)
def generate_graphic(flag):
'Generate a PDF file for a given worldflags flag.'
stdout = f'*** PROCESSING {flag} ***\n\n'
# Create a LaTeX file.
fbase = 'flag_' + flag
stdout += f'CREATE: {fbase}.tex\n'
with open(fbase + '.tex', 'w') as w:
w.write(r'''
\documentclass{minimal}
\usepackage{worldflags}
\begin{document}
\worldflag[width=9pt]{%s}
\end{document}
''' % flag)
# Compile the LaTeX file to PDF.
stdout += run_and_return_output(['pdflatex', '&mylatex', fbase + '.tex'])
# Crop the PDF file.
stdout += run_and_return_output(['pdfcrop', fbase + '.pdf'])
# Overwrite the original PDF file with the cropped version.
stdout += f'RUN: mv {fbase}-crop.pdf {fbase}.pdf\n'
os.rename(f'{fbase}-crop.pdf', f'{fbase}.pdf')
# Output the buffered output.
stdout += '\n'
print(stdout)
# Concurrently process all symbols.
with concurrent.futures.ProcessPoolExecutor() as executor:
executor.map(generate_graphic, flags)
|