Skip to content

Commit

Permalink
Add verbose transclusion, supporting auto-insertion of css/js
Browse files Browse the repository at this point in the history
-v or --verbose
  • Loading branch information
Lyrenhex committed Dec 19, 2016
1 parent d988ec4 commit f5c2c3b
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 11 deletions.
38 changes: 37 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,56 @@
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"])))
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)

Expand Down
1 change: 1 addition & 0 deletions script-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("yoyoyo, how do you do?")
3 changes: 3 additions & 0 deletions style-test.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
h1 {
color: blue;
}
5 changes: 0 additions & 5 deletions test-meta.html
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
<meta name="author" content="Damian Heaton" />
<style>
h1 {
color: blue;
}
</style>
2 changes: 2 additions & 0 deletions test.c.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
<head>
<title>Test CHTML Document</title>
<link rel="transclusion" href="test-meta.html" />
<link rel="stylesheet" type="text/css" href="style-test.css" />
<script src="script-test.js"></script>
</head>

<body>
Expand Down
10 changes: 5 additions & 5 deletions test.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!DOCTYPE html>
<html><head><title>Test CHTML Document</title><html><head><meta content="Damian Heaton" name=author><style>
h1 {
color: blue;
}
</style></head></html></head><body><h1>This is a test document.</h1><p>This document is a test CHTML file, for the particular purpose of testing the CHTML » HTML compiler.</p></body></html>
<html><head><title>Test CHTML Document</title><meta content="Damian Heaton" name=author><style>h1 {
color: blue;
}
</style><script>console.log("yoyoyo, how do you do?")
</script></head><body><h1>This is a test document.</h1><p>This document is a test CHTML file, for the particular purpose of testing the CHTML » HTML compiler.</p></body></html>

0 comments on commit f5c2c3b

Please sign in to comment.