2020-08-03 11:34:21 +00:00
|
|
|
#!/usr/bin/env python3
|
2020-08-03 10:47:23 +00:00
|
|
|
|
|
|
|
import subprocess
|
|
|
|
import glob
|
|
|
|
import sys
|
|
|
|
import os.path as path
|
|
|
|
import os
|
2020-08-03 11:07:10 +00:00
|
|
|
import argparse
|
2020-08-03 10:47:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
BAT_OPTIONS = [
|
|
|
|
"--no-config",
|
|
|
|
"--style=plain",
|
|
|
|
"--color=always",
|
2021-02-16 20:39:38 +00:00
|
|
|
"--theme=default",
|
2020-08-03 10:47:23 +00:00
|
|
|
"--italic-text=always",
|
|
|
|
]
|
|
|
|
|
2020-10-04 22:29:20 +00:00
|
|
|
SKIP_FILENAMES = [
|
|
|
|
"LICENSE.md",
|
2021-05-30 10:12:59 +00:00
|
|
|
"NOTICE",
|
2020-10-04 22:29:20 +00:00
|
|
|
"README.md",
|
|
|
|
"bat_options",
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2020-10-10 19:24:21 +00:00
|
|
|
def get_options(source):
|
|
|
|
options = BAT_OPTIONS.copy()
|
2020-10-11 19:36:42 +00:00
|
|
|
|
|
|
|
source_dirpath = path.dirname(source)
|
2020-10-10 19:25:45 +00:00
|
|
|
options_file = path.join(source_dirpath, "bat_options")
|
2020-10-11 19:36:42 +00:00
|
|
|
try:
|
2020-10-10 19:25:45 +00:00
|
|
|
with open(options_file, "r") as f:
|
2020-10-11 19:37:25 +00:00
|
|
|
options.extend(map(lambda x: x.rstrip(), f.readlines()))
|
2020-10-11 19:36:42 +00:00
|
|
|
except FileNotFoundError:
|
|
|
|
pass
|
2020-10-11 19:37:25 +00:00
|
|
|
|
2020-10-10 19:24:21 +00:00
|
|
|
return options
|
|
|
|
|
2020-10-04 22:29:20 +00:00
|
|
|
|
2020-08-03 11:07:10 +00:00
|
|
|
def create_highlighted_versions(output_basepath):
|
2020-08-03 10:47:23 +00:00
|
|
|
root = os.path.dirname(os.path.abspath(__file__))
|
2020-10-17 15:20:24 +00:00
|
|
|
sources = path.join(root, "source", "*")
|
2020-08-03 10:47:23 +00:00
|
|
|
|
2021-02-16 20:39:38 +00:00
|
|
|
for source in glob.glob(path.join(sources, "*")) + glob.glob(
|
|
|
|
path.join(sources, ".*")
|
|
|
|
):
|
2020-08-03 10:47:23 +00:00
|
|
|
try:
|
2020-08-03 13:10:26 +00:00
|
|
|
env = os.environ.copy()
|
2021-09-06 19:37:02 +00:00
|
|
|
env.pop("BAT_CACHE_PATH", None)
|
|
|
|
env.pop("BAT_CONFIG_DIR", None)
|
2020-08-03 13:10:26 +00:00
|
|
|
env.pop("BAT_CONFIG_PATH", None)
|
2021-09-06 19:37:02 +00:00
|
|
|
env.pop("BAT_OPTS", None)
|
|
|
|
env.pop("BAT_PAGER", None)
|
2020-08-03 13:10:26 +00:00
|
|
|
env.pop("BAT_STYLE", None)
|
|
|
|
env.pop("BAT_TABS", None)
|
2021-09-06 19:37:02 +00:00
|
|
|
env.pop("BAT_THEME", None)
|
|
|
|
env.pop("NO_COLOR", None)
|
|
|
|
env.pop("PAGER", None)
|
2020-08-03 13:10:26 +00:00
|
|
|
env["COLORTERM"] = "truecolor" # make sure to output 24bit colors
|
|
|
|
|
2020-10-10 19:24:21 +00:00
|
|
|
source_dirname = path.basename(path.dirname(source))
|
2020-08-03 10:47:23 +00:00
|
|
|
source_filename = path.basename(source)
|
|
|
|
|
2020-10-04 22:29:20 +00:00
|
|
|
if source_filename in SKIP_FILENAMES:
|
2020-09-07 20:23:26 +00:00
|
|
|
continue
|
|
|
|
|
2020-10-04 22:29:20 +00:00
|
|
|
bat_output = subprocess.check_output(
|
2020-10-10 19:24:21 +00:00
|
|
|
["bat"] + get_options(source) + [source],
|
2020-10-11 19:33:37 +00:00
|
|
|
stderr=subprocess.PIPE,
|
|
|
|
env=env,
|
2020-10-04 22:29:20 +00:00
|
|
|
)
|
|
|
|
|
2020-08-03 11:07:10 +00:00
|
|
|
output_dir = path.join(output_basepath, source_dirname)
|
2020-08-03 10:47:23 +00:00
|
|
|
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)
|
|
|
|
|
2020-08-03 11:34:21 +00:00
|
|
|
print("Created '{}'".format(output_path))
|
2020-08-03 10:47:23 +00:00
|
|
|
except subprocess.CalledProcessError as err:
|
|
|
|
print(
|
|
|
|
"=== Error: Could not highlight source file '{}".format(source),
|
|
|
|
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,
|
|
|
|
)
|
|
|
|
sys.exit(1)
|
2020-10-04 22:47:28 +00:00
|
|
|
except FileNotFoundError:
|
2020-10-11 19:33:37 +00:00
|
|
|
print(
|
|
|
|
"Error: Could not execute 'bat'. Please make sure that the executable "
|
|
|
|
"is available on the PATH."
|
|
|
|
)
|
2020-10-04 22:47:28 +00:00
|
|
|
sys.exit(1)
|
2020-08-03 10:47:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2020-08-03 11:07:10 +00: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()
|
|
|
|
|
|
|
|
create_highlighted_versions(output_basepath=args.output)
|