예제 #1
0
파일: generator.py 프로젝트: bemre/cdap
    def render(self):
        """ Returns generated html code.
        """
        template_src = codecs.open(self.template_file, encoding=self.encoding)
        template = jinja2.Template(template_src.read())
        slides = self.fetch_contents(self.source)
        context = self.get_template_vars(slides)

        html = template.render(context)

        if self.embed:
            pat = re.compile(r"""
                            \s+         # leading whitespace
                            background  # 'background'
                            (?:-image)  # ':-image', if present
                            ?:          # 1 or zero ':'
                            \s          # single space
                            url         # 'url'
                            \(          # opening paren
                            (.+?)       # group anything one-or-more, not-greedy
                            \)          # closing paren
                            .*?;        # anything zero-or-more until ';', not-greedy
                            """, re.DOTALL | re.UNICODE | re.VERBOSE)
            
            images = pat.findall(html)
            self.log("Embedding %d theme image(s)" % len(images))
            
            for img_url in images:
                self.log("Embeding theme image %s" % img_url)
                img_url = img_url.replace('"', '').replace("'", '')
                if self.theme_dir:
                    source = os.path.join(self.theme_dir, 'css')
                else:
                    source = os.path.join(THEMES_DIR, self.theme, 'css')

                encoded_url = utils.encode_image_from_url(img_url, source)
                if encoded_url:
                    html = html.replace(img_url, encoded_url, 1)
                    self.log("Embedded theme image %s from theme directory %s" % (img_url, source))
                else:
                    # Missing file in theme directory. Try user_css folders
                    found = False
                    for css_entry in context['user_css']:
                        directory = os.path.dirname(css_entry['path_url'])
                        if not directory: 
                            directory = "."

                        encoded_url = utils.encode_image_from_url(img_url, directory)
                            
                        if encoded_url:
                            found = True
                            html = html.replace(img_url, encoded_url, 1)
                            self.log("Embedded theme image %s from directory %s" % (img_url, directory))
                    
                    if not found:
                        #Missing image file, etc...
                        self.log(u"Failed to embed theme image %s" % img_url)

        return html
예제 #2
0
    def render(self):
        """ Returns generated html code.
        """
        template_src = codecs.open(self.template_file, encoding=self.encoding)
        template = jinja2.Template(template_src.read())
        slides = self.fetch_contents(self.source)
        context = self.get_template_vars(slides)

        html = template.render(context)

        if self.embed:
            images = re.findall(r'\s+background(?:-image)?:\surl\((.+?)\).+;',
                                html, re.DOTALL | re.UNICODE)

            for img_url in images:
                img_url = img_url.replace('"', '').replace("'", '')
                if self.theme_dir:
                    source = os.path.join(self.theme_dir, 'css')
                else:
                    source = os.path.join(THEMES_DIR, self.theme, 'css')

                encoded_url = utils.encode_image_from_url(img_url, source)
                if encoded_url:
                    html = html.replace(img_url, encoded_url, 1)
                    self.log(
                        "Embedded theme image %s from theme directory %s" %
                        (img_url, source))
                else:
                    # Missing file in theme directory. Try user_css folders
                    found = False
                    for css_entry in context['user_css']:
                        directory = os.path.dirname(css_entry['path_url'])
                        if not directory:
                            directory = "."

                        encoded_url = utils.encode_image_from_url(
                            img_url, directory)

                        if encoded_url:
                            found = True
                            html = html.replace(img_url, encoded_url, 1)
                            self.log(
                                "Embedded theme image %s from directory %s" %
                                (img_url, directory))

                    if not found:
                        #Missing image file, etc...
                        self.log(u"Failed to embed theme image %s" % img_url)

        return html
예제 #3
0
    def process(self, content, source=None):
        classes = []

        if not self.embed:
            return content, classes

        images = re.findall(r'<img\s.*?src="(.+?)"\s?.*?/?>', content,
                            re.DOTALL | re.UNICODE)

        source_dir = os.path.dirname(source)

        for image_url in images:
            encoded_url = utils.encode_image_from_url(image_url, source_dir)

            if not encoded_url:
                self.logger(u"Failed to embed image \"%s\"" % image_url,
                            'warning')
                return content, classes

            content = content.replace(u"src=\"" + image_url,
                                      u"src=\"" + encoded_url, 1)

            self.logger(u"Embedded image %s" % image_url, 'notice')

        return content, classes
예제 #4
0
    def render(self):
        """ Returns generated html code.
        """
        template_src = codecs.open(self.template_file, encoding=self.encoding)
        template = jinja2.Template(template_src.read())
        slides = self.fetch_contents(self.source)
        context = self.get_template_vars(slides)

        html = template.render(context)

        if self.embed:
            images = re.findall(r'\s+background(?:-image)?:\surl\((.+?)\).+;',
                            html, re.DOTALL | re.UNICODE)

            for img_url in images:
                img_url = img_url.replace('"', '').replace("'", '')

                source = os.path.join(THEMES_DIR, self.theme, 'css')

                encoded_url = utils.encode_image_from_url(img_url, source)

                if encoded_url:
                    html = html.replace(img_url, encoded_url, 1)

        return html
예제 #5
0
    def render(self):
        """ Returns generated html code.
        """
        template_src = codecs.open(self.template_file, encoding=self.encoding)
        template = jinja2.Template(template_src.read())
        slides = self.fetch_contents(self.source)
        context = self.get_template_vars(slides)

        html = template.render(context)

        if self.embed:
            images = re.findall(r"\s+background(?:-image)?:\surl\((.+?)\).+;", html, re.DOTALL | re.UNICODE)

            for img_url in images:
                img_url = img_url.replace('"', "").replace("'", "")

                if self.theme_dir:
                    source = os.path.join(self.theme_dir, "css")
                else:
                    source = os.path.join(THEMES_DIR, self.theme, "css")

                encoded_url = utils.encode_image_from_url(img_url, source)
                if encoded_url:
                    html = html.replace(img_url, encoded_url, 1)
                    self.log("Embedded theme image %s from theme directory %s" % (img_url, source))
                else:
                    # Missing image file, etc...
                    self.log(u"Failed to embed theme image %s" % img_url)

        return html
예제 #6
0
    def render(self):
        """ Returns generated html code.
        """
        template_src = codecs.open(self.template_file, encoding=self.encoding)
        template = jinja2.Template(template_src.read())
        slides = self.fetch_contents(self.source)
        context = self.get_template_vars(slides)

        html = template.render(context)

        if self.embed:
            images = re.findall(r'\s+background(?:-image)?:\surl\((.+?)\).+;',
                            html, re.DOTALL | re.UNICODE)

            for img_url in images:
                img_url = img_url.replace('"', '').replace("'", '')
                if self.theme_dir:
                    source = os.path.join(self.theme_dir, 'css')
                else:
                    source = os.path.join(THEMES_DIR, self.theme, 'css')

                encoded_url = utils.encode_image_from_url(img_url, source)
                if encoded_url:
                    html = html.replace(img_url, encoded_url, 1)
                    self.log("Embedded theme image %s from theme directory %s" % (img_url, source))
                else:
                    # Missing file in theme directory. Try user_css folders
                    found = False
                    for css_entry in context['user_css']:
                        directory = os.path.dirname(css_entry['path_url'])
                        if not directory: 
                            directory = "."

                        encoded_url = utils.encode_image_from_url(img_url, directory)
                            
                        if encoded_url:
                            found = True
                            html = html.replace(img_url, encoded_url, 1)
                            self.log("Embedded theme image %s from directory %s" % (img_url, directory))
                    
                    if not found:
                        #Missing image file, etc...
                        self.log(u"Failed to embed theme image %s" % img_url)



        return html
예제 #7
0
    def css_contents(self, css_path):
        """ Returns stylesheet content by optionally embedding images and fonts.
        """
        contents = codecs.open(css_path, encoding=self.encoding).read()
        if self.embed:
            urls = re.findall(r'\burl\((.+?)\)', contents, re.UNICODE)
            source_path = os.path.dirname(css_path)
            for url in urls:
                url = url.replace('"', '').replace("'", '')
                encoded_url = utils.encode_image_from_url(url, source_path)
                if encoded_url is not False:
                    self.logger(u"Embedded image %s in %s" %
                                (url, css_path), 'notice')
                    contents = contents.replace(url, encoded_url, 1)

        return contents
예제 #8
0
    def css_contents(self, css_path):
        """ Returns stylesheet content by optionally embedding images and fonts.
        """
        contents = codecs.open(css_path, encoding=self.encoding).read()
        if self.embed:
            urls = re.findall(r'\burl\((.+?)\)', contents, re.UNICODE)
            source_path = os.path.dirname(css_path)
            for url in urls:
                url = url.replace('"', '').replace("'", '')
                encoded_url = utils.encode_image_from_url(url, source_path)
                if encoded_url is not False:
                    self.logger(u"Embedded image %s in %s" % (url, css_path),
                                'notice')
                    contents = contents.replace(url, encoded_url, 1)

        return contents
예제 #9
0
    def process(self, content, source=None):
        classes = []

        if not self.embed:
            return content, classes

        images = re.findall(r'<img\s.*?src="(.+?)"\s?.*?/?>', content,
                            re.DOTALL | re.UNICODE)

        source_dir = os.path.dirname(source)

        for image_url in images:
            encoded_url = utils.encode_image_from_url(image_url, source_dir)

            if not encoded_url:
                return content, classes

            content = content.replace(u"src=\"" + image_url,
                                      u"src=\"" + encoded_url, 1)

            self.logger(u"Embedded image %s" % image_url, 'notice')

        return content, classes
예제 #10
0
    def render(self):
        """ Returns generated html code.
        """
        template_src = codecs.open(self.template_file, encoding=self.encoding)
        template = jinja2.Template(template_src.read())
        slides = self.fetch_contents(self.source)
        context = self.get_template_vars(slides)

        html = template.render(context)

        if self.embed:
            images = re.findall(r'\s+background(?:-image)?:\surl\((.+?)\).+;',
                            html, re.DOTALL | re.UNICODE)

            for img_url in images:
                img_url = img_url.replace('"', '').replace("'", '')

                source = os.path.join(THEMES_DIR, self.theme, 'css')

                encoded_url = utils.encode_image_from_url(img_url, source)

                html = html.replace(img_url, encoded_url, 1)

        return html