Skip to content

Commit

Permalink
Support directory compilation
Browse files Browse the repository at this point in the history
Allow a directory to be passed instead of an individual file, which
will then make the compiler compile all files within the directory.
  • Loading branch information
Lyrenhex committed Jan 16, 2017
1 parent f5c2c3b commit 8694cb9
Showing 1 changed file with 81 additions and 69 deletions.
150 changes: 81 additions & 69 deletions main.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env python3
"""
Compound HTML Transclusion Compiler
Copyright (C) 2016 Damian Heaton <dh64784@gmail.com>
Expand All @@ -18,6 +19,7 @@

import bs4
import sys
import os

minify = False
nocomm = False
Expand All @@ -40,72 +42,82 @@
print("""Compound should be used with syntax thus:
python(3) main.py <CHTML file> [any optional parameters (see docs)]""")
else:
output = ".".join(sys.argv[1].split(".")[:-2]) + ".html"
if "-o" in sys.argv:
output = sys.argv[sys.argv.index("-o") + 1]
elif "--output" in sys.argv:
output = sys.argv[sys.argv.index("--output") + 1]

if "-b" in sys.argv:
output = sys.argv[sys.argv.index("-b") + 1] + output
elif "--build-dir" in sys.argv:
output = sys.argv[sys.argv.index("-b") + 1] + output

try:
if(".c.html" not in sys.argv[1]):
print("WARNING: CHTML input file does not contain '.c.html' in its name; are you sure that it's the correct format?")
chtml = bs4.BeautifulSoup(open(sys.argv[1]), "html.parser")

# index all of the tags that we deal with
transcludes = chtml.find_all("link", rel="transclusion")
csslinks = chtml.find_all("link", rel="stylesheet")
scriptlinks = chtml.find_all("script", src=True)

for transclusion in transcludes:
try:
transclusion.replaceWith(bs4.BeautifulSoup(open(transclusion["href"]), "html.parser"))
except IOError as e:
print("Transclusion failure: file %s could not be found -- perhaps it's misspelt?" % transclusion["href"])
del transclusion
except Exception as e:
print("Transclusion failure:", e)

if "-v" in sys.argv or "--verbose" in sys.argv:
# they passed the verbose flag; embed script and css files

# css files
for cssfile in csslinks:
# create a new <style> tag to contain embedded css
csstag = chtml.new_tag("style")

# fill the <style> tag with the css
csstag.append(bs4.BeautifulSoup(open(cssfile["href"]), "html.parser"))

# replace the link tag with the <style> tag
cssfile.replaceWith(csstag)

# script files
for scriptfile in scriptlinks:
# create a new <script> tag to contain embedded js
scripttag = chtml.new_tag("script")

# fill the <script> tag with the js
scripttag.append(bs4.BeautifulSoup(open(scriptfile["src"]), "html.parser"))

# replace the link tag with the <script> tag
scriptfile.replaceWith(scripttag)

if minify:
chtml = htmlmin.minify(str(chtml), remove_empty_space=True, remove_comments=nocomm)

# TODO: if minify flag set, and -d (deep) flag is set, then also transclude JS and CSS files into the base HTML file. (we really don't like external references ;))

# save the new file
f = open(output, "w")
f.write(str(chtml))
f.close()
print("SUCCESS: Compiled to %s!" % output)
except IOError as e:
print("File %s could not be found -- are you sure you spelt it right?" % sys.argv[1])
except Exception as e:
print("Whoops, we couldn't compile the CHTML file. See error:", e)
files = []
if ".c.html" in sys.argv[1]:
files[0] = sys.argv[1]
else:
print("WARNING: CHTML input file does not contain '.c.html' in its name; are you sure that it's the correct format?")
# no .c.html; user likely specified a directory to build
if os.path.isdir(sys.argv[1]):
# it is a dir; we should go through the files and compile them all.
for f in os.listdir(sys.argv[1]):
if os.path.isfile(os.path.join(sys.argv[1], f)):
files.append(os.path.join(sys.argv[1], f))
for f in files:
output = ".".join(f.split(".")[:-2]).split("/")[-1] + ".html"
if "-o" in sys.argv:
output = sys.argv[sys.argv.index("-o") + 1]
elif "--output" in sys.argv:
output = sys.argv[sys.argv.index("--output") + 1]

if "-b" in sys.argv:
output = sys.argv[sys.argv.index("-b") + 1] + "/" + output
elif "--build-dir" in sys.argv:
output = sys.argv[sys.argv.index("-b") + 1] + "/" + output

try:
chtml = bs4.BeautifulSoup(open(f), "html.parser")

# index all of the tags that we deal with
transcludes = chtml.find_all("link", rel="transclusion")
csslinks = chtml.find_all("link", rel="stylesheet")
scriptlinks = chtml.find_all("script", src=True)

for transclusion in transcludes:
try:
transclusion.replaceWith(bs4.BeautifulSoup(open(transclusion["href"]), "html.parser"))
except IOError as e:
print("Transclusion failure: file %s could not be found -- perhaps it's misspelt?" % transclusion["href"])
del transclusion
except Exception as e:
print("Transclusion failure:", e)

if "-v" in sys.argv or "--verbose" in sys.argv:
# they passed the verbose flag; embed script and css files

# css files
for cssfile in csslinks:
# create a new <style> tag to contain embedded css
csstag = chtml.new_tag("style")

# fill the <style> tag with the css
csstag.append(bs4.BeautifulSoup(open(cssfile["href"]), "html.parser"))

# replace the link tag with the <style> tag
cssfile.replaceWith(csstag)

# script files
for scriptfile in scriptlinks:
# create a new <script> tag to contain embedded js
scripttag = chtml.new_tag("script")

# fill the <script> tag with the js
scripttag.append(bs4.BeautifulSoup(open(scriptfile["src"]), "html.parser"))

# replace the link tag with the <script> tag
scriptfile.replaceWith(scripttag)

if minify:
chtml = htmlmin.minify(str(chtml), remove_empty_space=True, remove_comments=nocomm)

# TODO: if minify flag set, and -d (deep) flag is set, then also transclude JS and CSS files into the base HTML file. (we really don't like external references ;))

# save the new file
f = open(output, "w")
f.write(str(chtml))
f.close()
print("SUCCESS: Compiled to %s!" % output)
except IOError as e:
print("File %s could not be found -- are you sure you spelt it right?" % f)
except Exception as e:
print("Whoops, we couldn't compile the CHTML file. See error:", e)

0 comments on commit 8694cb9

Please sign in to comment.