You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
177 lines
5.9 KiB
177 lines
5.9 KiB
import json
|
|
import os
|
|
import re
|
|
import requests
|
|
import toml
|
|
from shutil import copytree, rmtree
|
|
|
|
|
|
def parse_for_toc(content):
|
|
# TODO: ADD .TableofContent directly to the page (see hugo theme ?)
|
|
content = content.replace("[TOC]", "")
|
|
return content
|
|
|
|
|
|
def parse_for_notice(content):
|
|
result = re.findall(":::warning.*?:::", content, re.MULTILINE | re.DOTALL)
|
|
for notice in result:
|
|
old = notice
|
|
content = content.replace(
|
|
old,
|
|
notice.replace(":::warning", '{{% alert context="warning" %}}').replace(
|
|
":::", "{{% /alert %}}"
|
|
),
|
|
)
|
|
result = re.findall(":::info.*?:::", content, re.MULTILINE | re.DOTALL)
|
|
for notice in result:
|
|
old = notice
|
|
content = content.replace(
|
|
old,
|
|
notice.replace(":::info", '{{% alert context="info" %}}').replace(
|
|
":::", "{{% /alert %}}"
|
|
),
|
|
)
|
|
result = re.findall(":::success.*?:::", content, re.MULTILINE | re.DOTALL)
|
|
for notice in result:
|
|
old = notice
|
|
content = content.replace(
|
|
old,
|
|
notice.replace(":::success", '{{% alert context="success" %}}').replace(
|
|
":::", "{{% /alert %}}"
|
|
),
|
|
)
|
|
result = re.findall(":::danger.*?:::", content, re.MULTILINE | re.DOTALL)
|
|
for notice in result:
|
|
old = notice
|
|
content = content.replace(
|
|
old,
|
|
notice.replace(":::danger", '{{% alert context="danger" %}}').replace(
|
|
":::", "{{% /alert %}}"
|
|
),
|
|
)
|
|
# print(content)
|
|
return content
|
|
|
|
|
|
def create_index_file(dir_path, dir_name):
|
|
#add _index.md for page organization
|
|
with open(dir_path + "/_index.md", "wb") as f:
|
|
print("Created", dir_path + "/_index.md")
|
|
index = """
|
|
+++
|
|
title = "{dir_name}"
|
|
chapter = true
|
|
weight = 5
|
|
+++
|
|
|
|
# {dir_name}
|
|
|
|
Discover what this Hugo theme is all about and the core-concepts behind it.
|
|
""".format(
|
|
dir_name=dir_name
|
|
)
|
|
f.write(b"%s" % index.encode("utf-8"))
|
|
|
|
|
|
def generate_directory_structure_and_files(node, parent_path="./content/"):
|
|
if "children" in node:
|
|
# If the node has children, it's a directory
|
|
dir_path = os.path.join(parent_path, node["technical_name"])
|
|
os.makedirs(dir_path, exist_ok=True) # Create directory
|
|
create_index_file(dir_path, node["name"])
|
|
for child in node["children"]:
|
|
generate_directory_structure_and_files(child, parent_path=dir_path)
|
|
elif "url" in node:
|
|
# If the node has a URL, it's a document
|
|
file_path = os.path.join(parent_path, node["technical_name"] + ".md")
|
|
link_url = node["url"]
|
|
if link_url:
|
|
with open(file_path, "wb") as f:
|
|
doc_link = link_url + "/download"
|
|
response = requests.get(doc_link)
|
|
hugo_header = '---\ntitle: "' + node["name"] + '"\n---\n\n'
|
|
content = parse_for_notice(
|
|
response.content.decode("utf-8")
|
|
).encode("utf-8")
|
|
content = parse_for_toc(content.decode("utf-8")).encode("utf-8")
|
|
f.write(hugo_header.encode("utf-8"))
|
|
f.write(content.replace(b"---", b""))
|
|
print("Downloaded", doc_link, "to", file_path)
|
|
|
|
|
|
def generate_book_layout(node):
|
|
"""Copy the defaults layout repository 'docs' for the new/other books"""
|
|
layout_dir = "./themes/lotusdocs/layouts/"
|
|
layout_docs_dir = os.path.join(layout_dir, "docs")
|
|
layout_book_dir = os.path.join(layout_dir, node["technical_name"])
|
|
# copy book layout folder from "layouts/docs" if it doesn't exist
|
|
if not os.path.exists(layout_book_dir):
|
|
# create the layout for the book
|
|
copytree(layout_docs_dir, layout_book_dir)
|
|
|
|
|
|
def generate_book_menu(node, weight):
|
|
"""Generate the menu displayed in the home page"""
|
|
book_config = {
|
|
"name": node["name"],
|
|
"url": node["technical_name"] + "/",
|
|
"identifier": node["technical_name"],
|
|
"weight": weight,
|
|
}
|
|
return book_config
|
|
|
|
def generate_book_config(node):
|
|
"""Generate configuration for each book"""
|
|
book_config = {
|
|
"title": node["name"],
|
|
"darkMode": True
|
|
}
|
|
return book_config
|
|
|
|
|
|
def parse_json_file(json_file_path):
|
|
content_dir = "./content/"
|
|
config_path ="./hugo.toml"
|
|
# new_config_path = "./config_new.toml" # TO DELETE WHEN SCRIPT FINISHED
|
|
config_data_dict = toml.load(config_path)
|
|
|
|
# Retrieve the init json data
|
|
with open(json_file_path, 'r') as json_file:
|
|
json_data = json.load(json_file)
|
|
|
|
|
|
if not os.path.exists(content_dir):
|
|
# Create content directory if it doesn't exist
|
|
os.makedirs(content_dir)
|
|
else:
|
|
# Delete the content directories files and sub-directories
|
|
rmtree(content_dir)
|
|
|
|
nb_books = 0
|
|
primary_menu = []
|
|
books_config = []
|
|
config_data_dict.update({"params": {}})
|
|
# Loop on the json structure to build repositories and files
|
|
for book_node in json_data["books"]:
|
|
nb_books +=1
|
|
generate_directory_structure_and_files(book_node)
|
|
generate_book_layout(book_node)
|
|
primary_menu.append(generate_book_menu(book_node, nb_books))
|
|
# books_config.append(generate_book_config(book_node))
|
|
config_data_dict["params"]["%s" % book_node["technical_name"]] = generate_book_config(book_node)
|
|
|
|
|
|
# Add the primary menu data to the whole config
|
|
config_data_dict.update({"menu": {"primary": primary_menu}})
|
|
#config_data_dict.update({"params": {"primary": primary_menu}})
|
|
print("WHOLE CONFIG")
|
|
print(config_data_dict)
|
|
|
|
# Replace the config.toml content with the new params
|
|
with open(config_path, 'w') as toml_file:
|
|
toml.dump(config_data_dict, toml_file)
|
|
|
|
|
|
|
|
parse_json_file("structure.json")
|
|
|