Bladeren bron

UglifyJS; cache background information.

pull/24/head
Ben Kurtovic 12 jaren geleden
bovenliggende
commit
cfb4cad519
4 gewijzigde bestanden met toevoegingen van 45 en 4 verwijderingen
  1. +1
    -0
      README.md
  2. +10
    -0
      build.py
  3. +13
    -4
      static/js/cookies.js
  4. +21
    -0
      static/js/potd.js

+ 1
- 0
README.md Bestand weergeven

@@ -10,3 +10,4 @@ Dependencies
* [earwigbot](https://github.com/earwig/earwigbot)
* [mako](http://www.makotemplates.org/)
* [oursql](http://packages.python.org/oursql/)
* [uglifyjs](https://github.com/mishoo/UglifyJS/)

+ 10
- 0
build.py Bestand weergeven

@@ -4,6 +4,7 @@
import logging
import os
import shutil
import subprocess

page_src = """#! /usr/bin/env python
# -*- coding: utf-8 -*-
@@ -95,6 +96,15 @@ class Builder(object):

logger.debug("copytree {0} -> {1}".format(self.static_dir, dest))
shutil.copytree(self.static_dir, dest)
for dirpath, dirnames, filenames in os.walk(dest):
for filename in filenames:
if filename.endswith(".js"):
name = os.path.join(dirpath, filename)
logger.debug("uglifyjs {0}".format(name))
uglified = subprocess.check_output(["uglifyjs", name])
os.remove(name)
with open(name, "w") as fp:
fp.write(uglified)

def gen_pages(self):
logger = self.root.getChild("pages")


+ 13
- 4
static/js/cookies.js Bestand weergeven

@@ -18,18 +18,27 @@ function get_cookie(name) {
return null;
}

function set_cookie(name, value, days) {
function set_cookie_with_date(name, value, date) {
value = window.btoa("--ets1" + value);
var path = window.location.pathname.split("/", 2)[1];
if (date) {
var expires = "; expires=" + date.toUTCString();
}
else {
var expires = "";
}
document.cookie = name + "=" + value + expires + "; path=/" + path;
}

function set_cookie(name, value, days) {
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
var expires = "; expires=" + date.toGMTString();
set_cookie_with_date(name, value, date);
}
else {
var expires = "";
set_cookie_with_date(name, value);
}
document.cookie = name + "=" + value + expires + "; path=/" + path;
}

function delete_cookie(name) {


+ 21
- 0
static/js/potd.js Bestand weergeven

@@ -1,4 +1,20 @@
function potd_set_background() {
var cookie = get_cookie("EarwigBackgroundCache");
if (cookie) {
try {
data = JSON.parse(cookie);
var url = data.url;
var descurl = data.descurl;
var imgwidth = data.imgwidth;
var imgheight = data.imgheight;
if (url && descurl && imgwidth && imgheight) {
set_background(url, descurl, imgwidth, imgheight);
return;
}
}
catch (SyntaxError) {}
}

var d = new Date();
var callback = "earwigpotd1";
var date = (d.getUTCFullYear()) + "-" + zero_pad(d.getUTCMonth() + 1, 2) + "-" + zero_pad(d.getUTCDate(), 2);
@@ -55,7 +71,12 @@ function parse_file_url(data, filename) {
imgwidth = r["width"];
imgheight = r["height"];
}

set_background(url, descurl, imgwidth, imgheight);
var data = {"url": url, "descurl": descurl, "imgwidth": imgwidth, "imgheight": imgheight};
var now = new Date();
var expires = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate() + 1);
set_cookie_with_date("EarwigBackgroundCache", JSON.stringify(data), expires);
}

function set_background(url, descurl, imgwidth, imgheight) {


Laden…
Annuleren
Opslaan