Merge branch 'master' of gitlab.com:xpub/xpub2017

master
Michael Murtaugh 8 years ago
commit 42d9e42efd

@ -0,0 +1,14 @@
Installation
=============
Make sure your virtualenv is up to date (had troubles with html5lib).
Create and activate your venv.
virtualenv venv
source venv/bin/activate
Install python dependencies with pip
pip install pillow html5lib mwclient

@ -18,10 +18,32 @@
<script>
(function() {
// warning CHANGES TO THIS CODE NEED TO BE ROLLED BACK INTO leaflet.py
var cell_layout, expandzoom, fourup, layoutxyz, render, split4, tiler, tiles_wrapper, zoom;
var expandzoom, cell_layout, layoutxyz, tiler, tiles_wrapper, zoom;
window.tiler = tiler = {};
cell_layout = function(items) {
return [
{
y: 0,
x: 0,
item: items[0]
}, {
y: 0,
x: 1,
item: items[1]
}, {
y: 1,
x: 0,
item: items[2]
}, {
y: 1,
x: 1,
item: items[3]
}
];
};
tiler.tiles_wrapper = tiles_wrapper = function(path, ext) {
if (ext == null) { ext = "jpg"; }
var ret = {};
@ -70,107 +92,6 @@
return node;
};
split4 = function(items) {
var c, el, i, l, len, p, ref, results, x;
l = items.length;
p = Math.ceil(Math.log(l) / Math.log(4));
c = Math.max(1, Math.pow(4, p) / 4);
el = function(x, c) {
while (x.length < c) {
x.push(null);
}
return x;
};
ref = [items.slice(0, c), items.slice(c, c * 2), items.slice(c * 2, c * 3), items.slice(c * 3)];
results = [];
for (i = 0, len = ref.length; i < len; i++) {
x = ref[i];
results.push(el(x, c));
}
return results;
};
cell_layout = function(items) {
return [
{
y: 0,
x: 0,
item: items[0]
}, {
y: 0,
x: 1,
item: items[1]
}, {
y: 1,
x: 0,
item: items[2]
}, {
y: 1,
x: 1,
item: items[3]
}
];
};
tiler.render = render = function(items, tilewidth, tileheight, z, y, x) {
var g, i, j, kids, len, node, ref;
if (tilewidth == null) {
tilewidth = 256;
}
if (tileheight == null) {
tileheight = 256;
}
if (z == null) {
z = 0;
}
if (y == null) {
y = 0;
}
if (x == null) {
x = 0;
}
if (items.length === 1) {
x = items[0];
if (x === null) {
return null;
}
return zoom(x, '');
} else {
node = {};
node['text'] = '';
kids = [];
ref = cell_layout(split4(items));
for (i = 0, len = ref.length; i < len; i++) {
g = ref[i];
kids.push(render(g.item, tilewidth, tileheight, z + 1, (y * 2) + g.y, (x * 2) + g.x));
}
node.children = (function() {
var k, len1, results;
results = [];
for (k = 0, len1 = kids.length; k < len1; k++) {
j = kids[k];
if (j !== null) {
results.push(j);
}
}
return results;
})();
node.image = fourup((function() {
var k, len1, ref1, results;
ref1 = node.children;
results = [];
for (k = 0, len1 = ref1.length; k < len1; k++) {
j = ref1[k];
if (j !== null) {
results.push(j.image);
}
}
return results;
})(), tilewidth, tileheight);
return node;
}
};
tiler.layoutxyz = layoutxyz = function(n, x, y, z, outnode) {
var g, i, len, ref;
if (x == null) {
@ -198,66 +119,6 @@
return outnode;
};
tiler.fourup = fourup = function(images, tilewidth, tileheight) {
if (tilewidth == null) {
tilewidth = 256;
}
if (tileheight == null) {
tileheight = 256;
}
return function(done) {
var i, img, imgelts, len, loadcount, results, src, x;
loadcount = 0;
images = (function() {
var i, len, results;
results = [];
for (i = 0, len = images.length; i < len; i++) {
x = images[i];
if (x !== null) {
results.push(x);
}
}
return results;
})();
imgelts = [];
results = [];
for (i = 0, len = images.length; i < len; i++) {
src = images[i];
img = new Image;
imgelts.push(img);
img.addEventListener("load", function() {
var canvas, ctx, g, hh, hw, k, len1, ref;
if (++loadcount >= images.length) {
canvas = document.createElement("canvas");
canvas.width = tilewidth;
canvas.height = tileheight;
ctx = canvas.getContext("2d");
hw = tilewidth / 2;
hh = tileheight / 2;
ref = cell_layout(imgelts);
for (k = 0, len1 = ref.length; k < len1; k++) {
g = ref[k];
if (g.item) {
ctx.drawImage(g.item, g.x * hw, g.y * hh, hw, hh);
}
}
return done(null, canvas.toDataURL());
}
}, false);
if (typeof src === "function") {
console.log("inside 4up, deferring");
results.push(src(function(err, data) {
console.log(" inside 4up, GOT DATA");
return img.src = data;
}));
} else {
results.push(img.src = src);
}
}
return results;
};
};
tiler.expandzoom = expandzoom = function(node) {
var c, ret, tilespath;
if (node.zoomable) {
@ -377,6 +238,7 @@ function getjson (url, callback) {
request.send();
}
var TILESIZE = 256;
var map = L.map('map', {
editable: true,
maxZoom: 100,
@ -390,6 +252,7 @@ getjson("index.json", function (err, data) {
var nodes = (tiler.layoutxyz(tiler.expandzoom(data)));
map.addLayer( L.gridLayer.dynamicTiles({
minZoom: 0,
tileSize: TILESIZE,
nodes: nodes
}) );
@ -425,9 +288,9 @@ xpath = absolutize(xpath, 10.3, 87, -128.5);
// flip it
xpath = xpath.map(function (x) { return [x[1], x[0]] });
// x = x.map(function (x) { return yx(x) });
var polyline = L.polyline(xpath, {color: '#ed4e47'}).addTo(map);
var polyline = L.polyline(xpath, {color: '#dd2e57'}).addTo(map);
map.setView(xy(0.5 * 256, -0.5 * 256), 0);
map.setView(xy(0.5 * TILESIZE, -0.5 * TILESIZE), 0);
})();
</script>

@ -12,7 +12,7 @@ def fitbox (boxw, boxh, w, h):
return rw, rh
def tile_image (im, maxz=0, tilew=256, tileh=256, base=".", template="z{0[z]}y{0[y]}x{0[x]}.jpg", bgcolor=(0,0,0)):
def tile_image (im, maxz=0, tilew=256, tileh=256, base=".", template="z{0[z]}y{0[y]}x{0[x]}.jpg", bgcolor=(0,0,0), margin_right=0, margin_bottom=0):
z = 0
boxw, boxh = tilew, tileh
@ -20,7 +20,7 @@ def tile_image (im, maxz=0, tilew=256, tileh=256, base=".", template="z{0[z]}y{0
while True:
rw, rh = fitbox(boxw, boxh, im.size[0], im.size[1])
rim = im.resize((rw, rh), Image.ANTIALIAS)
rim = im.resize((rw-margin_right, rh-margin_bottom), Image.ANTIALIAS)
if bgcolor:
tim = Image.new("RGB", (boxw, boxh), bgcolor)
tim.paste(rim, (0, 0))

@ -111,7 +111,7 @@ def split4(items):
ret = [items[0:c],items[c:c*2],items[c*2:c*3],items[c*3:]]
return tuple([el(x, c) for x in ret])
def gridrender (items, basename, tilewidth=256, tileheight=256, z=0, y=0, x=0):
def gridrender (items, basename, tilewidth=320, tileheight=320, z=0, y=0, x=0):
""" items are now nodes proper """
""" Takes a list of nodes and returns a new node where items are arranged in a cascade of nodes such that
all items appear at the same (z) level -- side by side
@ -141,7 +141,7 @@ def gridrender (items, basename, tilewidth=256, tileheight=256, z=0, y=0, x=0):
return node
def recursiverender (items, basename, tilewidth=256, tileheight=256, direction=3, z=0):
def recursiverender (items, basename, tilewidth=320, tileheight=320, direction=3, z=0):
node = {}
node['text'] = ''
# if len(items) >=1 and 'date' in items[0].item:

@ -74,7 +74,7 @@ def wiki_title_to_url (wiki, title):
ret += "/" + p.normalize_title(p.name)
return ret
def ensure_wiki_image_tiles (wiki, imagepageurl, text='', basepath="tiles", force=False, bgcolor=None, tilewidth=256, tileheight=256, zoom=3):
def ensure_wiki_image_tiles (wiki, imagepageurl, text='', basepath="tiles", force=False, bgcolor=None, tilewidth=256, tileheight=256, zoom=3, margin_right=0, margin_bottom=0):
print ("ensure_wiki_image_tiles", imagepageurl, file=sys.stderr)
page, imageurl = mwfilepage_to_url(wiki, imagepageurl)
path = os.path.join(basepath, url_to_path(imageurl))
@ -89,7 +89,7 @@ def ensure_wiki_image_tiles (wiki, imagepageurl, text='', basepath="tiles", forc
except OSError:
pass
im = Image.open(urlopen(imageurl))
tile_image(im, zoom, tilewidth, tileheight, path+"/", ret.tilename, bgcolor)
tile_image(im, zoom, tilewidth, tileheight, path+"/", ret.tilename, bgcolor, margin_right, margin_bottom)
return ret
def textcell (paras):
@ -104,7 +104,7 @@ def name_to_path (name):
return name.replace("/", "_")
def render_article (wiki, ref, basepath="tiles", depth=0, maxdepth=3):
def render_article (wiki, ref, basepath="tiles", depth=0, maxdepth=3, tilewidth=256, tileheight=256):
print ("render_article", ref, file=sys.stderr)
if type(ref) == Page:
page = ref
@ -150,7 +150,7 @@ def render_article (wiki, ref, basepath="tiles", depth=0, maxdepth=3):
# article = wiki_absurl(wiki, article)
# images.append(render_article(wiki, article, caption, basepath, depth+1, maxdepth))
# else:
images.append(ensure_wiki_image_tiles(wiki, src, caption, basepath).zoom())
images.append(ensure_wiki_image_tiles(wiki, src, caption, basepath, tilewidth=tilewidth, tileheight=tileheight).zoom())
for a in body.findall('.//a[@class="image"]'):
caption = a.attrib.get("title", '')
@ -162,7 +162,7 @@ def render_article (wiki, ref, basepath="tiles", depth=0, maxdepth=3):
if src in imgsrcs:
continue
imgsrcs[src] = True
images.append(ensure_wiki_image_tiles(wiki, src, caption, basepath).zoom())
images.append(ensure_wiki_image_tiles(wiki, src, caption, basepath, tilewidth=tilewidth, tileheight=tileheight).zoom())
print ("{0} paras, {1} images".format(len(paras), len(images)), file=sys.stderr)
@ -179,7 +179,7 @@ def render_article (wiki, ref, basepath="tiles", depth=0, maxdepth=3):
cells.append(textcell(paras))
cells.extend(images)
ret = recursiverender(cells, basename)
ret = recursiverender(cells, basename, tilewidth=tilewidth, tileheight=tileheight)
ret['text'] = u"""<p class="caption"><span class="text">{0}</span><a class="url" href="{1}">WIKI</a></p>""".format(title, ref)
if images:
ret['image'] = images[0]['image']
@ -200,7 +200,7 @@ def render_article (wiki, ref, basepath="tiles", depth=0, maxdepth=3):
# children.append(gridrender(restimages, basename))
# return article
def render_category (wiki, cat, output="tiles"):
def render_category (wiki, cat, output="tiles", tilewidth=256, tileheight=256):
print ("Render Category", cat, file=sys.stderr)
# if type(cat) == Page:
# page = ref
@ -218,10 +218,10 @@ def render_category (wiki, cat, output="tiles"):
for m in cat.members():
pages.append(m)
pages.sort(key=lambda x: x.name)
pagenodes = [render_article(wiki, x.name) for x in pages]
pagenodes = [render_article(wiki, x.name, tilewidth=tilewidth, tileheight=tileheight) for x in pages]
for page, node in zip(pages, pagenodes):
node['text'] = u"""<p class="caption"><span class="text">{0}</span><a class="url" href="{1}">WIKI</a></p>""".format(page.name, wiki_title_to_url(wiki, page.name))
ret = gridrender(pagenodes, output+"/"+cat.name.replace(":", "_"))
ret = gridrender(pagenodes, output+"/"+cat.name.replace(":", "_"), tilewidth=tilewidth, tileheight=tileheight)
ret['text'] = u"""<p class="caption"><a class="url" href="{0}">{1}</a></p>""".format(wiki_title_to_url(wiki, cat.name), cat.name)
return ret
# for p in pages:
@ -238,7 +238,7 @@ def make_category (args):
def make_article (args):
wiki = Site((args.wikiprotocol, args.wikihost), path=args.wikipath)
root_node = render_article(wiki, args.wikipage)
root_node = render_article(wiki, args.wikipage, tilewidth=args.tilewidth, tileheight=args.tileheight)
if args.html:
print (html(root_node, ""))
else:
@ -248,7 +248,7 @@ def make_gallery(args):
wiki = Site((args.wikiprotocol, args.wikihost), path=args.wikipath)
# apiurl = args.wikiprotocol+"://"+args.wikihost+args.wikipath+"api.php"
if len(args.wikipage) == 1:
root_node = render_article(wiki, args.wikipage[0])
root_node = render_article(wiki, args.wikipage[0], tilewidth=args.tilewidth, tileheight=args.tileheight)
else:
children = []
for wikipage in args.wikipage:
@ -257,12 +257,12 @@ def make_gallery(args):
print ("rendering", wikipage, file=sys.stderr)
cnode = render_category(wiki, wikipage, args.output)
else:
cnode = render_article(wiki, wikipage)
cnode = render_article(wiki, wikipage, tilewidth=args.tilewidth, tileheight=args.tileheight)
children.append(cnode)
if args.recursive:
root_node = recursiverender(children, args.output+"/"+args.name, direction=1)
root_node = recursiverender(children, args.output+"/"+args.name, direction=1, tilewidth=args.tilewidth, tileheight=args.tileheight)
else:
root_node = gridrender(children, args.output+"/"+args.name, direction=1)
root_node = gridrender(children, args.output+"/"+args.name, direction=1, tilewidth=args.tilewidth, tileheight=args.tileheight)
if args.html:
print (html(root_node, ""))
@ -273,6 +273,7 @@ def make_gallery(args):
def testwiki (args):
return Site((args.wikiprotocol, args.wikihost), path=args.wikipath)
if __name__ == "__main__":
ap = ArgumentParser("")

Loading…
Cancel
Save