Readmes in subdirectories with svg previews (#2)

This commit is contained in:
cieplik 2021-09-24 14:50:35 -07:00 committed by GitHub
parent e703051a9d
commit 0036a354e9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
235 changed files with 21193 additions and 3 deletions

View file

@ -0,0 +1,91 @@
import yaml
from typing import Dict, List, Optional
import argparse
import os
from pathlib import Path
def get_all_input_files(input_dir: str) -> List[str]:
filenames = next(os.walk(input_dir), (None, None, []))[2]
files = filter(lambda f: (f.endswith("yaml") or f.endswith("yml")), filenames)
return list(files)
def ensure_output_dir(output_dir: str):
if not os.path.exists(output_dir):
os.makedirs(output_dir)
def add_color_to_dict(output: Dict[str, str], obj: Dict[str, str], key: str, prefix: Optional[str] = None):
if not prefix:
prefix = ""
output[f"{prefix}{key}"] = obj[key]
def get_color_dict(input_dir: str, file_name: str) -> Dict[str, str]:
file = open(os.path.join(input_dir, file_name), 'r')
loaded_theme = yaml.safe_load(file)
output = {}
add_color_to_dict(output, loaded_theme, "accent")
add_color_to_dict(output, loaded_theme, "foreground")
add_color_to_dict(output, loaded_theme, "background")
normal_colors = loaded_theme["terminal_colors"]["normal"]
for color in normal_colors.keys():
add_color_to_dict(output, normal_colors, color)
bright_colors = loaded_theme["terminal_colors"]["bright"]
for color in bright_colors.keys():
add_color_to_dict(output, bright_colors, color, "br")
return output
def file_name_to_display(file_name: str) -> str:
file_name = Path(file_name).with_suffix('').name
split = file_name.split("_")
output = []
for s in split:
output.append(s.capitalize())
return " ".join(output)
def gen_svg_for_theme(color_dict: Dict[str, str], svg_template: str) -> str:
output = svg_template
for key, value in color_dict.items():
output = output.replace(f"{{{key}}}", value)
return output
def main():
parser = argparse.ArgumentParser(description='Generate README.md with embedded SVG previews.')
parser.add_argument('input_dir', type=str, help='Directory from which to read in all Warp themes.')
parser.add_argument('output_dir', type=str, help='Where to save README.md')
parser.add_argument("svg_path", type=str, help="Path to svg template file.")
parser.add_argument("intro_file", type=str, help="What should go on top of README.md.")
args = parser.parse_args()
ensure_output_dir(args.output_dir)
filenames = get_all_input_files(args.input_dir)
markdown = []
markdown.append("|Theme name | Preview|")
markdown.append("| --- | --- |")
svg = open(args.svg_path, 'r').read()
svg_dir = os.path.join(args.output_dir, "previews")
os.makedirs(svg_dir, exist_ok = True)
intro = open(args.intro_file, 'r').read()
for input_file in sorted(filenames):
print(f"Generating for {input_file}")
cell = f"|**[{file_name_to_display(input_file)}]({input_file})**:|"
color_dict = get_color_dict(args.input_dir, input_file)
theme_svg = gen_svg_for_theme(color_dict, svg)
theme_svg_path = os.path.join(svg_dir, f"{input_file}.svg")
with open(theme_svg_path, 'w') as svg_out:
svg_out.write(theme_svg)
cell += f"<img src='previews/{input_file}.svg' width='300'>|"
markdown.append(cell)
output_str = intro + "\n".join(markdown)
with open(os.path.join(args.output_dir,"README.md"), 'w') as output:
output.write(output_str)
if __name__ == "__main__":
main()

91
scripts/preview.svg Normal file
View file

@ -0,0 +1,91 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 300 145">
<rect width="300" height="145" class="Dynamic" fill="{background}" rx="5" />
<!-- first command -->
<text x="15" y="15" fill="{foreground}" font-size="0.6em" font-family="monospace" class="Dynamic">ls</text>
<text x="15" y="30" fill="{blue}" font-size="0.6em" font-family="monospace" class="Dynamic">
dir
</text>
<text x="65" y="30" fill="{red}" font-size="0.6em" font-family="monospace" class="Dynamic">
executable
</text>
<text x="175" y="30" fill="{foreground}" font-size="0.6em" font-family="monospace" class="Dynamic">
file
</text>
<line x1="0" y1="40" x2="300" y2="40" style="stroke-width:0.2" class="Dynamic" stroke="{foreground}"/>
<!-- second command -->
<text x="15" y="55" fill="{foreground}" font-size="0.6em" font-family="monospace" class="Dynamic">bash ~/colors.sh</text>
<text x="15" y="70" fill="{foreground}" font-size="0.6em" font-family="monospace" class="Dynamic">
normal:
</text>
<text x="55" y="70" fill="{black}" font-size="0.6em" font-family="monospace" class="Dynamic">
black
</text>
<text x="85" y="70" fill="{red}" font-size="0.6em" font-family="monospace" class="Dynamic">
red
</text>
<text x="105" y="70" fill="{green}" font-size="0.6em" font-family="monospace" class="Dynamic">
green
</text>
<text x="135" y="70" fill="{yellow}" font-size="0.6em" font-family="monospace" class="Dynamic">
yellow
</text>
<text x="170" y="70" fill="{blue}" font-size="0.6em" font-family="monospace" class="Dynamic">
blue
</text>
<text x="195" y="70" fill="{magenta}" font-size="0.6em" font-family="monospace" class="Dynamic">
magenta
</text>
<text x="235" y="70" fill="{cyan}" font-size="0.6em" font-family="monospace" class="Dynamic">
cyan
</text>
<text x="260" y="70" fill="{white}" font-size="0.6em" font-family="monospace" class="Dynamic">
white
</text>
<text x="15" y="85" fill="{foreground}" font-size="0.6em" font-family="monospace" class='Dynamic'>
bright:
</text>
<text x="55" y="85" fill="{brblack}" font-size="0.6em" font-family="monospace" class="Dynamic">
black
</text>
<text x="85" y="85" fill="{brred}" font-size="0.6em" font-family="monospace" class="Dynamic">
red
</text>
<text x="105" y="85" fill="{brgreen}" font-size="0.6em" font-family="monospace" class="Dynamic">
green
</text>
<text x="135" y="85" fill="{bryellow}" font-size="0.6em" font-family="monospace" class="Dynamic">
yellow
</text>
<text x="170" y="85" fill="{brblue}" font-size="0.6em" font-family="monospace" class="Dynamic">
blue
</text>
<text x="195" y="85" fill="{brmagenta}" font-size="0.6em" font-family="monospace" class="Dynamic">
magenta
</text>
<text x="235" y="85" fill="{brcyan}" font-size="0.6em" font-family="monospace" class="Dynamic">
cyan
</text>
<text x="260" y="85" fill="{brwhite}" font-size="0.6em" font-family="monospace" class="Dynamic">
white
</text>
<line x1="0" y1="95" x2="300" y2="95" style="stroke-width:0.2" class="Dynamic" stroke="{foreground}" />
<!-- prompt -->
<text x="15" y="110" fill="{magenta}" font-size="0.6em" font-family="monospace" class="Dynamic">
~/project
</text>
<text x="65" y="110" fill="{green}" font-size="0.6em" font-family="monospace" class="Dynamic">
git(
</text>
<text x="85" y="110" fill="{yellow}" font-size="0.6em" font-family="monospace" class="Dynamic">
main
</text>
<text x="113" y="110" fill="{green}" font-size="0.6em" font-family="monospace" class="Dynamic">
)
</text>
<!-- cursor -->
<line x1="15" y1="120" x2="15" y2="130" style="stroke-width:2" class="Dynamic" stroke="{accent}" />
</svg>

After

Width:  |  Height:  |  Size: 3.5 KiB