2015-07-15 06:40:12 +00:00
|
|
|
import re
|
|
|
|
|
import os
|
|
|
|
|
import sys
|
|
|
|
|
import importlib
|
|
|
|
|
import os.path
|
2016-04-22 03:18:05 +00:00
|
|
|
import shutil
|
2015-07-15 06:40:12 +00:00
|
|
|
|
|
|
|
|
class Exporter(object):
|
|
|
|
|
data = None
|
2016-04-21 22:32:47 +00:00
|
|
|
|
|
|
|
|
def __init__(self, data, target_directory):
|
2015-07-15 06:40:12 +00:00
|
|
|
self.data = data
|
2016-04-21 22:32:47 +00:00
|
|
|
self.target_directory = target_directory
|
|
|
|
|
|
|
|
|
|
def _path_visitor(self, arg, dirname, names):
|
|
|
|
|
for name in names:
|
|
|
|
|
mirrored_path = os.path.join(dirname, name)
|
|
|
|
|
relative_path = os.path.join(arg, mirrored_path.replace(self.target_directory + "/", ""))
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
if (os.path.isdir(mirrored_path)):
|
|
|
|
|
os.mkdir(relative_path)
|
|
|
|
|
except OSError:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def write(self, directory):
|
2015-07-15 06:40:12 +00:00
|
|
|
import jinja2
|
2016-04-21 22:32:47 +00:00
|
|
|
|
2015-07-15 06:40:12 +00:00
|
|
|
# Read the template files first
|
|
|
|
|
file_template = None
|
|
|
|
|
with open("data/filetempl.html", "r") as handle:
|
|
|
|
|
file_template = handle.read()
|
2016-04-21 22:32:47 +00:00
|
|
|
|
2015-07-15 06:40:12 +00:00
|
|
|
index_template = None
|
|
|
|
|
with open("data/indextempl.html", "r") as handle:
|
|
|
|
|
index_template = handle.read()
|
2016-04-21 22:32:47 +00:00
|
|
|
|
2015-07-15 06:40:12 +00:00
|
|
|
html_filenames = [ ]
|
2016-04-21 22:32:47 +00:00
|
|
|
|
|
|
|
|
# Recurse the target directory and recreate its structure
|
|
|
|
|
os.path.walk(self.target_directory, self._path_visitor, directory)
|
|
|
|
|
|
2015-07-15 06:40:12 +00:00
|
|
|
# For each file entry...
|
2016-04-21 22:32:47 +00:00
|
|
|
script_relative_paths = [ ]
|
|
|
|
|
for file in self.data["files"]:
|
2015-07-15 06:40:12 +00:00
|
|
|
if (len(file.global_functions) == 0 and len(file.bound_functions.keys()) == 0 and len(file.datablocks) == 0):
|
|
|
|
|
continue
|
2016-04-21 22:32:47 +00:00
|
|
|
|
|
|
|
|
# First, we collapse to a file path relative to our output dir
|
|
|
|
|
# FIXME: Dirty hack to make sure the os.path.join works
|
|
|
|
|
html_filename = file.path.replace(self.target_directory + "/", "")
|
|
|
|
|
script_relative = html_filename
|
2016-04-22 03:18:05 +00:00
|
|
|
file.mod_path = html_filename
|
2016-04-21 22:32:47 +00:00
|
|
|
script_relative_paths.append(script_relative)
|
|
|
|
|
|
|
|
|
|
# Next, we ensure that the subdirectories exist
|
|
|
|
|
#html_filename = html_filename.lstrip("./").replace("/", "-")
|
2015-07-15 06:40:12 +00:00
|
|
|
html_filename, oldextension = os.path.splitext(html_filename)
|
|
|
|
|
html_filename = "%s.html" % html_filename
|
|
|
|
|
html_filenames.append(html_filename)
|
2016-04-22 03:18:05 +00:00
|
|
|
file.web_path = html_filename
|
2016-04-21 22:32:47 +00:00
|
|
|
|
|
|
|
|
with open(os.path.join(directory, html_filename), "w") as handle:
|
2015-07-15 06:40:12 +00:00
|
|
|
template = jinja2.Template(file_template)
|
2016-04-22 03:18:05 +00:00
|
|
|
handle.write(template.render(file=file))
|
2016-04-21 22:32:47 +00:00
|
|
|
|
2015-07-15 06:40:12 +00:00
|
|
|
# Dump the index file
|
2016-04-21 22:32:47 +00:00
|
|
|
with open(os.path.join(directory, "index.html"), "w") as handle:
|
2015-07-15 06:40:12 +00:00
|
|
|
template = jinja2.Template(index_template)
|
2016-04-21 22:32:47 +00:00
|
|
|
|
2016-04-22 03:18:05 +00:00
|
|
|
handle.write(template.render(files=self.data["files"]))
|
|
|
|
|
|
|
|
|
|
# Puke bootstrap into the directory
|
|
|
|
|
try:
|
|
|
|
|
shutil.copytree("data/bootstrap", os.path.join(directory, "bootstrap"))
|
|
|
|
|
except OSError:
|
|
|
|
|
pass
|
2016-04-21 22:32:47 +00:00
|
|
|
|
2015-07-15 06:40:12 +00:00
|
|
|
print("Done processing.")
|