diff options
Diffstat (limited to 'mensa/frontends')
-rw-r--r-- | mensa/frontends/html.py | 53 | ||||
-rw-r--r-- | mensa/frontends/plain-text.py | 32 |
2 files changed, 38 insertions, 47 deletions
diff --git a/mensa/frontends/html.py b/mensa/frontends/html.py index c23d27c..f3bf065 100644 --- a/mensa/frontends/html.py +++ b/mensa/frontends/html.py @@ -3,38 +3,33 @@ from yapsy.IPlugin import IPlugin from xml.sax.saxutils import escape as esc class HTMLRenderer(IPlugin) : + def format_line(self, item) : + vegkeys = [ "", "Vegetarian", "Vegan" ] + if self.options.get("only_veggie",0) > item.veggie : + return "" + if item.desc : + desc = "<div class=\"description\">"+esc(item.desc)+"</div>\n" + else : + desc = "" + return"<li class=\"fooditem\" ><span class=\"name\">" + esc(item.name) + "</span><span class=\"price\">"+ esc(item.price) + "</span><span class=\"veggie\">"+ esc(vegkeys[item.veggie])+"</span>\n"+ desc + + def format_category(self, category, items) : + return "<h4>%s</h4></h4><ul class=\"food-by-cat\">\n%s</ul>" % (esc(category), "".join([self.format_line(i) for i in items])) + def format_restaurant(self, restaurant, food) : + if not food : + return "" + st = "<div class=\"restaurant\"><h3>%s</h3>\n" % esc(restaurant.human_name) + dist = "" + if "pos" in self.options and restaurant.pos and self.options.get("dist") : + dist = "<div class=\"distance\">Distance: %.2f km</div>\n" % base.dist(options["pos"], restaurant.pos) + body = "\n".join([self.format_category(cat, it) for cat,it in food.items()]) + return st+dist+body+"</div>" def render (self, foods, **options) : ## Expects list of tuples with (Restaurant, Foodlist) - r = "" - vegkeys = [ "", "Vegetarian", "Vegan" ] - for restaurant, food in foods : - cat = [] - if not food : - continue - r = r+"<div class=\"restaurant\"><h3>"+esc(restaurant.human_name)+"</h3>"+"\n"#+base.formt(food) - if "pos" in options and restaurant.pos and "dist" in options : - ## display distance to restaurant - r=r+"<div class=\"distance\">Distance: %.2f km</div>\n" % base.dist(options["pos"], restaurant.pos) + self.options = options + + r = "\n".join([self.format_restaurant(restaurant, food) for restaurant, food in foods ]) - food.sort(key=lambda foo: foo.category) - for i in food: - if options["only_student_prices"] : - price = base.only_student_prices(i.price) - else: - price = i.price - if options["only_veggie"] and options["only_veggie"] > i.veggie : - continue - if not i.category in cat : - if cat : - r = r + "</ul>" - cat.append(i.category) - if not i.category == None : - r=r+ "<h4>"+esc(i.category)+"</h4><ul class=\"food-by-cat\">\n" - r=r+"<li class=\"fooditem\" ><span class=\"name\">" + esc(i.name) + "</span><span class=\"price\">"+ esc(price) + "</span><span class=\"veggie\">"+ esc(vegkeys[i.veggie])+"</span>\n" - if i.desc : - r = r+"<div class=\"description\">"+esc(i.desc)+"</div>\n" - r = r+"</ul></div>" - r = r print(r) def register_renderer(self) : diff --git a/mensa/frontends/plain-text.py b/mensa/frontends/plain-text.py index 36b2a34..8f3f736 100644 --- a/mensa/frontends/plain-text.py +++ b/mensa/frontends/plain-text.py @@ -1,33 +1,29 @@ from mensa import base from yapsy.IPlugin import IPlugin class TextRenderer(IPlugin) : + def render_line(self, item) : + vegkeys = [ "", "Vegetarian", "Vegan" ] + if self.options.get("only_veggie",0) > item.veggie : + return "" + desc = "" + if item.desc : + desc = "\t "+item.desc+"\n" + return "\t" + item.name.ljust(80) + "\t"+ item.price.ljust(20) + vegkeys[item.veggie]+"\n"+desc + def render (self, foods, **options) : + self.options = options ## Expects list of tuples with (Restaurant, Foodlist) r = "" - vegkeys = [ "", "Vegetarian", "Vegan" ] for restaurant, food in foods : cat = [] if not food : continue - r = r+"*"*20+restaurant.human_name+"*"*20+"\n"#+base.formt(food) - if "pos" in options and restaurant.pos and "dist" in options : + r = r+"*"*20+restaurant.human_name+"*"*20+"\n" + if "pos" in options and restaurant.pos and options.get("dist") : ## display distance to restaurant r=r+"Distance: %.2f km\n" % base.dist(options["pos"], restaurant.pos) - food.sort(key=lambda foo: foo.category) - for i in food: - if options["only_student_prices"] : - price = base.only_student_prices(i.price) - else: - price = i.price - if options["only_veggie"] and options["only_veggie"] > i.veggie : - continue - if not i.category in cat : - cat.append(i.category) - if not i.category == None : - r=r+ i.category+"\n" - r=r+"\t" + i.name.ljust(80) + "\t"+ price.ljust(20) + vegkeys[i.veggie]+"\n" - if i.desc : - r = r+"\t "+i.desc+"\n" + + r= r + "".join([category+"\n" + "".join([self.render_line(i) for i in items]) for category,items in food.items()]) print(r) def register_renderer(self) : |