Repository navigation
10BC8 Expand file tree
/
Copy pathrecipes
More file actions
executable file
·100 lines (70 loc) · 2.49 KB
/
Copy pathrecipes
File metadata and controls
executable file
·100 lines (70 loc) · 2.49 KB
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
#!/usr/bin/env python3
import sys
import os
import re
import shutil
import subprocess
SRC_PATH = "src/"
SUMMARY_PATH = "src/SUMMARY.md"
TEMPLATE_PATH = "src/template_blank.md"
cmds = [ "", "", "", "", "", "" ]
def get_dirty_title_from_url( url_str ):
def gen_recipe_title( input ):
# Replace any hyphens or underscores with spaces
buf1 = re.sub( '-', ' ', input )
buf2 = re.sub( '_', ' ', buf1 )
# Change to title case
buf3 = buf2.title()
# Change 'And' to 'and'
buf4 = re.sub( 'And', 'and', buf3 )
# Change 'With' to 'with'
return re.sub( 'With', 'with', buf4 )
def gen_filename_from_title( ):
pass
def process_link( link ):
print("processing", link )
# Get recipe name:
recipe_name = input("Recipe title: ")
# Make target filename
filename = SRC_PATH + re.sub(' ', '-', recipe_name.lower() ) + ".md"
print(filename)
shutil.copy(TEMPLATE_PATH, filename)
# Read in the file
with open(filename, 'r') as file :
filedata = file.read()
# Replace the target string # REPLACE TEMPLATE VARS WITH SIMPLER STRINGS
filedata = filedata.replace('Recipe Name', recipe_name)
filedata = filedata.replace('Link Text', recipe_name)
filedata = filedata.replace('https://www.website.com/Recipes/recipe/', link.strip())
# Write the file out again
with open(filename, 'w') as file:
file.write(filedata)
return recipe_name, filename
def run_batch_from_file( filepath ):
with open(filepath) as f:
links = f.readlines()
recipe_list = list()
file_list = list()
# For each link, get a recipe name, copy the template using the recipe name, and fill in other meaningful data
for link in links:
recipe, recipe_file = process_link( link )
recipe_list.append( recipe )
file_list.append( recipe_file )
print("")
for recipe in recipe_list:
print("- Adding", recipe )
print("")
for idx, title in enumerate(recipe_list, start=0):
print(" - [" + title + "](./" + os.path.basename( file_list[idx] ) + ")" )
print("")
def workerFunction():
if len(sys.argv) <= 1 or sys.argv[1] not in cmds:
print("Please use one of the available commands:", cmds)
sys.exit()
arg = sys.argv[1]
if arg == cmds[0]:
print("Running batch job..")
# cmd[1] is the batch file of jobs
run_batch_from_file( sys.argv[2] )
if __name__ == "__main__":
workerFunction()