def create(self): """ This method creates a SVG chart. :return: (string) contents of the SVG file """ # variables for drawing grid = self.__grid_coordinates() objs = { 'grid': grid, 'labels': self.__txt_coordinates(grid), 'reference': self.area_coordinates(self.reference, grid), 'whisky': self.area_coordinates(self.comparison, grid), 'center_x': self.center_x, 'center_y': self.center_y } # generate the svg basedir = app.config['BASEDIR'] template = basedir.child('whiskyton', 'templates', 'chart.svg') with open(template, 'r') as file_handler: # create SVG svg_template = Template(file_handler.read()) return xhtml_slimmer(svg_template.render(**objs))
def process_response(self, request, response): if 'text/html' in response['Content-Type']: #from django.utils.html import strip_spaces_between_tags #response.content = strip_spaces_between_tags(response.content) from slimmer import xhtml_slimmer response.content = xhtml_slimmer(response.content) return response
def process_response(self, request, response): if "text/html" in response["Content-Type"]: # from django.utils.html import strip_spaces_between_tags # response.content = strip_spaces_between_tags(response.content) from slimmer import xhtml_slimmer response.content = xhtml_slimmer(response.content) return response
def get_code(self): import slimmer f = codecs.open(self.path, 'r', 'utf-8') code = f.read() f.close() if self.filetype == '.css': return slimmer.css_slimmer(code) if self.filetype == '.js': return slimmer.js_slimmer(code) if self.filetype == '.html': return slimmer.xhtml_slimmer(code) return code
def get_code(self): import slimmer f = open(self.path) code = f.read() f.close() if self.filetype == '.css': return slimmer.css_slimmer(code) if self.filetype == '.js': return slimmer.js_slimmer(code) if self.filetype == '.html': return slimmer.xhtml_slimmer(code) return code
def _get_code(self): import slimmer f = open(self.path) code = f.read() f.close() if self.filetype == '.css': return slimmer.css_slimmer(code) if self.filetype == '.js': return slimmer.js_slimmer(code) if self.filetype == '.html': return slimmer.xhtml_slimmer(code) return code
def process_response(self, request, response): if 'text/html' in response['Content-Type']: from slimmer import xhtml_slimmer content = response.content.replace("</a> <a", "</a> <a") pre_tokens = {} while "<pre" in content: pre = content[content.find("<pre"):content.find("</pre>") + 6] token = str(uuid4()) content = content.replace(pre, token) pre_tokens[token] = pre content = xhtml_slimmer(content).replace("</a> <a", "</a> <a") for token, pre in pre_tokens.items(): content = content.replace(token, pre) response.content = content return response
def render(self, context): code = self.nodelist.render(context) if slimmer is None: return code if self.format not in ('css','js','html','xhtml'): self.format = guessSyntax(code) if self.format == 'css': return css_slimmer(code) elif self.format in ('js', 'javascript'): return js_slimmer(code) elif self.format == 'xhtml': return xhtml_slimmer(code) elif self.format == 'html': return html_slimmer(code) return code
def render(self, context): code = self.nodelist.render(context) if slimmer is None: return code if self.format not in ('css','js','html','xhtml'): self.format = slimmer.guessSyntax(code) if self.format == 'css': return slimmer.css_slimmer(code) elif self.format in ('js', 'javascript'): return slimmer.js_slimmer(code) elif self.format == 'xhtml': return slimmer.xhtml_slimmer(code) elif self.format == 'html': return slimmer.html_slimmer(code) else: raise TemplateSyntaxError("Unrecognized format for slimming content") return code
def render(self, context): code = self.nodelist.render(context) if slimmer is None: return code if self.format not in ("css", "js", "html", "xhtml"): self.format = slimmer.guessSyntax(code) if self.format == "css": return slimmer.css_slimmer(code) elif self.format in ("js", "javascript"): return slimmer.js_slimmer(code) elif self.format == "xhtml": return slimmer.xhtml_slimmer(code) elif self.format == "html": return slimmer.html_slimmer(code) else: raise TemplateSyntaxError("Unrecognized format for slimming content") return code
def _wrapped_view_func(request, *args, **kwargs): response = view_func(request, *args, **kwargs) if isinstance(response, HttpResponse) \ and response.get('Content-Type', '').find('text/html;') == 0: response.content = slimmer.xhtml_slimmer(response.content) return response
for tr in soup.find_all("tr"): del tr['id'] # Make attributes shorter for tag in soup.find_all(re.compile(".*")): try: tag['class'] = tag['class'][0] except KeyError: pass body = soup.find("body") body['style'] = "background: transparent;" # Remove the unwanted decoration. [x.decompose() for x in soup.find_all("div", {"class": "navigation"})] [x.decompose() for x in soup.find_all("div", {"class": "generatedby"})] [x.decompose() for x in soup.find_all("h1")] [x.decompose() for x in soup.find_all("div", {"class": "searchbox"})] # Reverse the table rows. table = soup.find("table") new_order = [] for row in list(table.children): new_order.insert(0, row.extract()) for row in new_order[:40]: table.append(row) table['style'] = "font-size: 8pt;" # print str(soup) print slimmer.xhtml_slimmer(str(soup))
def _wrapped_view_func(request, *args, **kwargs): response = view_func(request, *args, **kwargs) if isinstance(response, HttpResponse) and response.get("Content-Type", None).find("text/html;") == 0: response.content = slimmer.xhtml_slimmer(response.content) return response
def process_response(self, request, response): if isinstance(response, HttpResponse) \ and response.get('Content-Type', '').find('text/html;') == 0: response.content = slimmer.xhtml_slimmer(response.content) return response
def create_chart(reference_slug, whisky_slug): # URL check cond1 = whisky_slug != whisky_slug.lower() cond2 = reference_slug != reference_slug.lower() if cond1 or cond2: return redirect('/' + whisky_slug.lower()) reference_obj = models.Whisky.query.filter_by(slug=reference_slug).first() whisky_obj = models.Whisky.query.filter_by(slug=whisky_slug).first() # error page if whisky doesn't exist if reference_obj is None or whisky_obj is None: return abort(404) # get whisky data tastes_labels = ( 'spicy', 'honey', 'tobacco', 'medicinal', 'smoky', 'sweetness', 'body', 'floral', 'fruity', 'malty', 'nutty', 'winey') reference = [] comparison = [] for taste in tastes_labels: reference.append(eval('reference_obj.' + taste)) comparison.append(eval('whisky_obj.' + taste)) # name for caching filename = '' for taste in reference: filename += str(taste) filename += 'x' for taste in comparison: filename += str(taste) filename += '.svg' # check if file exists basedir = os.path.abspath(os.path.dirname(__file__)) charts_dir = basedir + '/static/charts/' try: os.stat(charts_dir) except: os.mkdir(charts_dir) filepath = charts_dir + filename # if file does not exists, create it if not os.path.isfile(filepath): # basic variables for the chart sides = 12 width = 330 height = 260 margin = 60 scales = 4 # calc other basic values for the chart radius = (width-(2*margin))/2 angle_adjust = ((2*math.pi/sides))/2 interval = radius / scales # variables for drawing polygon_coordinates = [] text_coordinates = [] reference_coordinates = [] whisky_coordinates = [] center_x = width / 2 center_y = height / 2 # calculate the coordinates of the grid for scale in range(0, scales): output = [] for x in range(0, sides): angle = ((2*math.pi/sides)*x) - angle_adjust r = radius - (scale * interval) a = center_x + (math.sin(angle)*r) b = center_y + (math.cos(angle)*r) output.append([int(a), int(b)]) polygon_coordinates.append(output) # calculate the position of text text_count = 0 text_line_height = 11 for coordinates in polygon_coordinates[0]: a = coordinates[0] b = coordinates[1] top = [6, 7] right = [2, 3, 4, 5] bottom = [0, 1] left = [8, 9, 10, 11] diagonal_down = [2, 11] diagonal_up = [5, 8] sub_diagonal_down = [3, 10] if text_count in top: b -= text_line_height * 0.75 if text_count in right: a += text_line_height * 0.75 if text_count in bottom: b += text_line_height * 1.5 if text_count in left: a -= text_line_height * 0.75 if text_count in diagonal_up: b -= text_line_height * 0.5 if text_count in diagonal_down: b += text_line_height * 0.75 if text_count in sub_diagonal_down: b += text_line_height * 0.25 text_anchor = 'start' if text_count in top or text_count in bottom: text_anchor = 'middle' if text_count in left: text_anchor = 'end' taste = tastes_labels[text_count] taste = taste[0].upper() + taste[1:] text_coordinates.append([int(a), int(b), text_anchor, taste]) text_count += 1 # calculate the coordinate of the reference data taste_count = 0 for taste in reference: taste = abs(taste - 4) if taste == 4: reference_coordinates.append([center_x, center_y]) else: polygon = polygon_coordinates[taste] point = polygon[taste_count] reference_coordinates.append(point) taste_count += 1 # calculate the coordinate of the comparison data taste_count = 0 for taste in comparison: taste = abs(taste - 4) if taste == 4: whisky_coordinates.append([center_x, center_y]) else: polygon = polygon_coordinates[taste] point = polygon[taste_count] whisky_coordinates.append(point) taste_count += 1 # generate the svg svg_image = render_template( 'chart.svg', polygon_coordinates=polygon_coordinates, text_coordinates=text_coordinates, reference_coordinates=reference_coordinates, whisky_coordinates=whisky_coordinates, center_x=center_x, center_y=center_y) svg_compressed = xhtml_slimmer(svg_image) # save the file new_chart = open(filepath, 'w+') new_chart.write(svg_compressed) new_chart.close() # return the chart to the user content = open(filepath).read() return Response(content, mimetype='image/svg+xml')
def optimize(forig, self, **kwargs): return xhtml_slimmer(forig(self, **kwargs))
def compress(self, string): if SLIMMER: return xhtml_slimmer(string) return string