2020-08-03 13:34:21 +02:00
|
|
|
#!/usr/bin/env python3
|
2020-08-03 12:47:23 +02:00
|
|
|
|
|
|
|
import subprocess
|
|
|
|
import glob
|
|
|
|
import sys
|
|
|
|
import os.path as path
|
|
|
|
import os
|
2020-08-03 13:07:10 +02:00
|
|
|
import argparse
|
2021-09-06 21:59:32 +02:00
|
|
|
from multiprocessing import Pool
|
2020-08-03 12:47:23 +02:00
|
|
|
|
|
|
|
BAT_OPTIONS = [
|
|
|
|
"--no-config",
|
|
|
|
"--style=plain",
|
|
|
|
"--color=always",
|
2021-02-16 21:39:38 +01:00
|
|
|
"--theme=default",
|
2020-08-03 12:47:23 +02:00
|
|
|
"--italic-text=always",
|
|
|
|
]
|
|
|
|
|
2020-10-05 00:29:20 +02:00
|
|
|
SKIP_FILENAMES = [
|
|
|
|
"LICENSE.md",
|
2021-05-30 12:12:59 +02:00
|
|
|
"NOTICE",
|
2020-10-05 00:29:20 +02:00
|
|
|
"README.md",
|
|
|
|
"bat_options",
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2020-10-10 21:24:21 +02:00
|
|
|
def get_options(source):
|
|
|
|
options = BAT_OPTIONS.copy()
|
2020-10-11 21:36:42 +02:00
|
|
|
|
|
|
|
source_dirpath = path.dirname(source)
|
2020-10-10 21:25:45 +02:00
|
|
|
options_file = path.join(source_dirpath, "bat_options")
|
2020-10-11 21:36:42 +02:00
|
|
|
try:
|
2020-10-10 21:25:45 +02:00
|
|
|
with open(options_file, "r") as f:
|
2020-10-11 21:37:25 +02:00
|
|
|
options.extend(map(lambda x: x.rstrip(), f.readlines()))
|
2020-10-11 21:36:42 +02:00
|
|
|
except FileNotFoundError:
|
|
|
|
pass
|
2020-10-11 21:37:25 +02:00
|
|
|
|
2020-10-10 21:24:21 +02:00
|
|
|
return options
|
|
|
|
|
2020-10-05 00:29:20 +02:00
|
|
|
|
2021-09-06 21:59:32 +02:00
|
|
|
def create_highlighted_version(args):
|
|
|
|
output_basepath, source = args
|
|
|
|
env = os.environ.copy()
|
|
|
|
env.pop("BAT_CACHE_PATH", None)
|
|
|
|
env.pop("BAT_CONFIG_DIR", None)
|
|
|
|
env.pop("BAT_CONFIG_PATH", None)
|
|
|
|
env.pop("BAT_OPTS", None)
|
|
|
|
env.pop("BAT_PAGER", None)
|
|
|
|
env.pop("BAT_STYLE", None)
|
|
|
|
env.pop("BAT_TABS", None)
|
|
|
|
env.pop("BAT_THEME", None)
|
|
|
|
env.pop("NO_COLOR", None)
|
|
|
|
env.pop("PAGER", None)
|
|
|
|
env["COLORTERM"] = "truecolor" # make sure to output 24bit colors
|
|
|
|
|
|
|
|
source_dirname = path.basename(path.dirname(source))
|
|
|
|
source_filename = path.basename(source)
|
|
|
|
|
|
|
|
if source_filename in SKIP_FILENAMES:
|
|
|
|
return
|
|
|
|
|
|
|
|
bat_output = subprocess.check_output(
|
|
|
|
["bat"] + get_options(source) + [source],
|
|
|
|
stderr=subprocess.PIPE,
|
|
|
|
env=env,
|
|
|
|
)
|
|
|
|
|
|
|
|
output_dir = path.join(output_basepath, source_dirname)
|
|
|
|
output_path = path.join(output_dir, source_filename)
|
|
|
|
|
|
|
|
os.makedirs(output_dir, exist_ok=True)
|
|
|
|
|
|
|
|
with open(output_path, "wb") as output_file:
|
|
|
|
output_file.write(bat_output)
|
|
|
|
|
|
|
|
print("Created '{}'".format(output_path))
|
|
|
|
|
|
|
|
|
2020-08-03 13:07:10 +02:00
|
|
|
def create_highlighted_versions(output_basepath):
|
2020-08-03 12:47:23 +02:00
|
|
|
root = os.path.dirname(os.path.abspath(__file__))
|
2021-09-06 21:59:32 +02:00
|
|
|
source_paths = path.join(root, "source", "*")
|
2020-08-03 12:47:23 +02:00
|
|
|
|
2021-09-06 21:59:32 +02:00
|
|
|
sources = []
|
|
|
|
for source in glob.glob(path.join(source_paths, "*")) + glob.glob(
|
|
|
|
path.join(source_paths, ".*")
|
2021-02-16 21:39:38 +01:00
|
|
|
):
|
2021-09-06 21:59:32 +02:00
|
|
|
sources.append((output_basepath, source))
|
|
|
|
|
|
|
|
try:
|
|
|
|
with Pool() as p:
|
|
|
|
p.map(create_highlighted_version, sources)
|
|
|
|
except subprocess.CalledProcessError as err:
|
|
|
|
print(
|
2021-10-23 12:52:37 +02:00
|
|
|
"=== Error: Could not highlight source file:\n" + " ".join(err.cmd),
|
2021-09-06 21:59:32 +02:00
|
|
|
file=sys.stderr,
|
|
|
|
)
|
|
|
|
print(
|
|
|
|
"=== bat stdout:\n{}".format(err.stdout.decode("utf-8")),
|
|
|
|
file=sys.stderr,
|
|
|
|
)
|
|
|
|
print(
|
|
|
|
"=== bat stderr:\n{}".format(err.stderr.decode("utf-8")),
|
|
|
|
file=sys.stderr,
|
|
|
|
)
|
|
|
|
return False
|
|
|
|
except FileNotFoundError:
|
|
|
|
print(
|
|
|
|
"Error: Could not execute 'bat'. Please make sure that the executable "
|
|
|
|
"is available on the PATH."
|
|
|
|
)
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
2020-08-03 12:47:23 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2020-08-03 13:07:10 +02:00
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description="This script creates syntax-highlighted versions of all "
|
|
|
|
"files in the 'source' directory."
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--output",
|
|
|
|
"-O",
|
|
|
|
metavar="PATH",
|
|
|
|
help="Output directory",
|
|
|
|
required=True,
|
|
|
|
type=str,
|
|
|
|
)
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2021-09-06 21:59:32 +02:00
|
|
|
if not create_highlighted_versions(output_basepath=args.output):
|
|
|
|
sys.exit(1)
|