You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
SI20/web/generate-booklet.py

81 lines
2.2 KiB
Python

from glob import glob
import os
import subprocess
import jinja2
# https://devdocs.io/python~3.9/library/glob#glob.glob
1 year ago
# files = glob("content/**", recursive=True)
# print(files)
print("---------")
all_html = ""
all_md = ""
html=[]
1 year ago
folders = glob("./*")
1 year ago
print(folders)
1 year ago
for folder in folders:
files = glob(folder + "/*")
print(files)
print("************")
1 year ago
for file in files:
#print(file)
if file.endswith(".md"):
print("===========")
file = file.replace(" ", "\ ")
print(file)
1 year ago
# pandoc2 = "pandoc -f markdown -t html " + file
# html_data = subprocess.run(pandoc2, capture_output=True, text=True).stdout
# print("html_data: ", html_data)
1 year ago
# #editing the html before creatings the main file
# # filename = os.path.basename(file)
# # print("filename", filename)
# # html_data = html_data.replace('<img src="','<img src="' + folder + '/')
1 year ago
# print("This is the html data: ", html_data)
# # and then collect all that content into a variable called "all_html"
# # add this content to all_html
# all_html = all_html + html_data
1 year ago
# html.append(html_data)
1 year ago
md_data = open(file).read()
all_md += "\n"+md_data
1 year ago
1 year ago
#create booklet.md
output = open("00-booklet/booklet.md", "w")
output.write(all_md)
output.close()
print("booklet md files saved!")
1 year ago
# use pandoc to convert booklet.md to html
pandoc_command = "pandoc --toc -f markdown -t html 00-booklet/booklet.md"
#html_data = subprocess.run(pandoc_command, capture_output=True, text=True).stdout
html_data = subprocess.check_output(pandoc_command, shell=True, text=True,encoding="utf-8")
print("html has been generated!")
#print("html data!", html_data)
#html to template environment
env = jinja2.Environment(loader=jinja2.FileSystemLoader("00-booklet"))
template = env.get_template("booklet.template.html")
1 year ago
booklet_html = template.render(content=html_data)
print("this is the html: ", booklet_html)
1 year ago
output = open("00-booklet/booklet.html", "w")
output.write(booklet_html)
output.close()
print("booklet html files saved!")
1 year ago
#take all the css files and write to main css
# Now open this page in the browser, remember: paged.js needs to be accessed through a webserver.
# To run a webserver locally, you can use:
# cd SI20/booklet/
# python3 -m http.server
# localhost:8000/booklet.html