mirror of
https://github.com/fmap/muflax65ngodyewp.onion
synced 2024-06-29 10:36:48 +02:00
speedup
This commit is contained in:
parent
a3892d53c3
commit
2521f01714
58
webifier.py
58
webifier.py
|
@ -33,14 +33,19 @@ class Webifier(object):
|
||||||
self.force = force
|
self.force = force
|
||||||
self.out = out
|
self.out = out
|
||||||
self.now = dt.datetime.now()
|
self.now = dt.datetime.now()
|
||||||
|
self.relist = re.compile("""
|
||||||
|
<li>
|
||||||
|
(?P<y>\d+) / (?P<m>\d+) / (?P<d>\d+):\
|
||||||
|
(?P<desc>.+?)
|
||||||
|
</li>
|
||||||
|
""", re.X|re.S)
|
||||||
|
self.recopy = re.compile("\.(yaml|pdc|swp)$")
|
||||||
|
|
||||||
def webify(self):
|
def webify(self):
|
||||||
"""wrapper for the whole process"""
|
"""wrapper for the whole process"""
|
||||||
|
|
||||||
self.make_html_files(self.src, self.out)
|
self.make_html_files(self.src, self.out)
|
||||||
#self.make_css(self.styles, op.join(self.out, self.styles))
|
|
||||||
self.tidy_up_html(self.out)
|
|
||||||
self.make_rss_feed(op.join(self.out, "changelog.html"))
|
self.make_rss_feed(op.join(self.out, "changelog.html"))
|
||||||
self.tidy_up_xml(self.out)
|
|
||||||
self.copy_media_files(self.src, self.out)
|
self.copy_media_files(self.src, self.out)
|
||||||
self.copy_media_files(self.styles, op.join(self.out, self.styles))
|
self.copy_media_files(self.styles, op.join(self.out, self.styles))
|
||||||
|
|
||||||
|
@ -49,7 +54,7 @@ class Webifier(object):
|
||||||
# again, we manually walk this shit... *sigh*
|
# again, we manually walk this shit... *sigh*
|
||||||
for f in [f for f in os.listdir(src)
|
for f in [f for f in os.listdir(src)
|
||||||
if (op.isfile(op.join(src, f))
|
if (op.isfile(op.join(src, f))
|
||||||
and not re.search("\.(yaml|pdc|swp)$", f))]:
|
and not self.recopy.search(f))]:
|
||||||
if not os.path.exists(out):
|
if not os.path.exists(out):
|
||||||
os.mkdir(out)
|
os.mkdir(out)
|
||||||
shutil.copy2(op.join(src, f), op.join(out, f))
|
shutil.copy2(op.join(src, f), op.join(out, f))
|
||||||
|
@ -70,7 +75,8 @@ class Webifier(object):
|
||||||
def make_breadcrumb(self, file, meta):
|
def make_breadcrumb(self, file, meta):
|
||||||
"""turn current path into breadcrumb navigation"""
|
"""turn current path into breadcrumb navigation"""
|
||||||
crumbs = []
|
crumbs = []
|
||||||
for i in range(len(meta["cats"])):
|
mod = -1 if op.basename(file) == "index.html" else 0
|
||||||
|
for i in range(len(meta["cats"]) + mod):
|
||||||
catname = meta["cats"][i]["name"]
|
catname = meta["cats"][i]["name"]
|
||||||
catfile = op.join(*[crumb["file"] for crumb in meta["cats"][:i+1]])
|
catfile = op.join(*[crumb["file"] for crumb in meta["cats"][:i+1]])
|
||||||
crumbs.append(self._breadcrumbtagify(catfile, catname))
|
crumbs.append(self._breadcrumbtagify(catfile, catname))
|
||||||
|
@ -95,6 +101,9 @@ class Webifier(object):
|
||||||
]
|
]
|
||||||
subprocess.call(pandoc)
|
subprocess.call(pandoc)
|
||||||
print("\t\tsaving as {}...".format(dest))
|
print("\t\tsaving as {}...".format(dest))
|
||||||
|
self.tidy_up_html(dest)
|
||||||
|
stat = os.stat(file)
|
||||||
|
os.utime(dest, (stat.st_mtime, stat.st_mtime))
|
||||||
|
|
||||||
def make_html_files(self, src, out, meta=None):
|
def make_html_files(self, src, out, meta=None):
|
||||||
"""turn all *.pdc in src into html files in out"""
|
"""turn all *.pdc in src into html files in out"""
|
||||||
|
@ -120,6 +129,10 @@ class Webifier(object):
|
||||||
if not op.exists(out):
|
if not op.exists(out):
|
||||||
os.mkdir(out)
|
os.mkdir(out)
|
||||||
for f in glob.glob(src+"/*.pdc"):
|
for f in glob.glob(src+"/*.pdc"):
|
||||||
|
dest_file = op.basename(f).replace(".pdc", ".html")
|
||||||
|
dest = op.join(out, dest_file )
|
||||||
|
mdst, msrc = os.stat(dest).st_mtime, os.stat(f).st_mtime
|
||||||
|
if self.force or msrc - mdst >= 1:
|
||||||
self.templatify(f, meta, out)
|
self.templatify(f, meta, out)
|
||||||
|
|
||||||
# do the same for all subdirectories
|
# do the same for all subdirectories
|
||||||
|
@ -148,18 +161,17 @@ class Webifier(object):
|
||||||
def make_rss_feed(self, changelog):
|
def make_rss_feed(self, changelog):
|
||||||
"""generate an RSS feed out of the Changelog"""
|
"""generate an RSS feed out of the Changelog"""
|
||||||
|
|
||||||
|
dest = op.join(self.out, "rss.xml")
|
||||||
|
mdst, msrc = os.stat(dest).st_mtime, os.stat(changelog).st_mtime
|
||||||
|
if not (self.force or msrc - mdst >= 1):
|
||||||
|
return
|
||||||
|
|
||||||
with open(changelog, "r") as f:
|
with open(changelog, "r") as f:
|
||||||
print("parsing {}...".format(changelog))
|
print("parsing {}...".format(changelog))
|
||||||
txt = f.read()
|
txt = f.read()
|
||||||
relist = re.compile("""
|
|
||||||
<li>
|
|
||||||
(?P<y>\d+) / (?P<m>\d+) / (?P<d>\d+):\
|
|
||||||
(?P<desc>.+?)
|
|
||||||
</li>
|
|
||||||
""", re.X|re.S)
|
|
||||||
|
|
||||||
items = []
|
items = []
|
||||||
for entry in relist.finditer(txt):
|
for entry in self.relist.finditer(txt):
|
||||||
items.append(
|
items.append(
|
||||||
RSS2.RSSItem(
|
RSS2.RSSItem(
|
||||||
title = "omg new stuff!!w!",
|
title = "omg new stuff!!w!",
|
||||||
|
@ -188,18 +200,16 @@ class Webifier(object):
|
||||||
items = items[:10]
|
items = items[:10]
|
||||||
)
|
)
|
||||||
|
|
||||||
with open("out/rss.xml", "w") as f:
|
with open(dest, "w") as f:
|
||||||
print("writing RSS feed...")
|
print("writing RSS feed...")
|
||||||
feed.write_xml(f, encoding="utf8")
|
feed.write_xml(f, encoding="utf8")
|
||||||
|
self.tidy_up_xml(dest)
|
||||||
|
os.utime(dest, (msrc, msrc))
|
||||||
|
|
||||||
def tidy_up_html(self, dir):
|
def tidy_up_html(self, f):
|
||||||
"""clean up all the html we generated earlier..."""
|
"""clean up the html we generated earlier..."""
|
||||||
|
|
||||||
for root, dirs, files in os.walk(dir):
|
print("cleaning up {}...".format(f))
|
||||||
for f in [op.join(root, f) for f in files
|
|
||||||
if re.match(".*\.html", f)]:
|
|
||||||
mtime = dt.datetime.fromtimestamp(os.stat(f).st_mtime)
|
|
||||||
if self.force or mtime > self.now:
|
|
||||||
subprocess.call(["tidy", "--tidy-mark", "f", "-i", "-m", "-q",
|
subprocess.call(["tidy", "--tidy-mark", "f", "-i", "-m", "-q",
|
||||||
"-utf8", f])
|
"-utf8", f])
|
||||||
# What? You got a problem with me using Perl inside Python
|
# What? You got a problem with me using Perl inside Python
|
||||||
|
@ -208,14 +218,10 @@ class Webifier(object):
|
||||||
subprocess.call(["perl", "-i", "-p", "-e",
|
subprocess.call(["perl", "-i", "-p", "-e",
|
||||||
"s,<br /></code>,</code>,g", f])
|
"s,<br /></code>,</code>,g", f])
|
||||||
|
|
||||||
def tidy_up_xml(self, dir):
|
def tidy_up_xml(self, f):
|
||||||
"""clean up all the xml we generated earlier..."""
|
"""clean up all the xml we generated earlier..."""
|
||||||
|
|
||||||
for root, dirs, files in os.walk(dir):
|
print("cleaning up {}...".format(f))
|
||||||
for f in [op.join(root, f) for f in files
|
|
||||||
if re.match(".*\.xml", f)]:
|
|
||||||
mtime = dt.datetime.fromtimestamp(os.stat(f).st_mtime)
|
|
||||||
if self.force or mtime > self.now:
|
|
||||||
subprocess.call(["tidy", "-xml", "-i", "-m", "-q", "-utf8", f])
|
subprocess.call(["tidy", "-xml", "-i", "-m", "-q", "-utf8", f])
|
||||||
|
|
||||||
def start_server(self, dir=""):
|
def start_server(self, dir=""):
|
||||||
|
|
Loading…
Reference in a new issue