Пример #1
0
 def link_css(self, content, old_path, new_path):
     """Replace URL links in CSS content and return result
     
     """
     # put both path in output dir
     old_path = os.path.join(self.output_dir, old_path)
     new_path = os.path.join(self.output_dir, new_path)
     old_dir = os.path.dirname(old_path)
     new_dir = os.path.dirname(new_path)
     rel_dir = os.path.relpath(old_dir, new_dir)
     #rel_dir = url_path(rel_dir, old_dir)
     rel_dir = rel_dir.replace('\\', '/')
     if rel_dir:
         rel_dir = rel_dir + '/'
     
     def map_func(url):
         parsed = urlparse.urlparse(url)
         # we don't want to map absolute URLs
         if parsed.scheme:
             return
         if parsed.path.startswith('/'):
             return
         return urlparse.urljoin(rel_dir, url)
     
     output = replace_css_links(content, map_func, self.logger)
     return output
Пример #2
0
    def test_replace_fonts(self):
        from kagin.link_css import replace_css_links
        file_map = {
            'fontawesome-webfont.eot': 'hashed.eot',
            'fontawesome-webfont.woff': 'hashed.woff',
            'fontawesome-webfont.ttf': 'hashed.ttf',
        }

        def map_func(path):
            return file_map.get(path)

        css = """
        @font-face {
          font-family: 'FontAwesome';
          src: url('fontawesome-webfont.eot?v=3.0.1');
          src: url('fontawesome-webfont.eot?#iefix&v=3.0.1') format('embedded-opentype'),
            url('fontawesome-webfont.woff?v=3.0.1') format('woff'),
            url('fontawesome-webfont.ttf?v=3.0.1') format('truetype');
          font-weight: normal;
          font-style: normal;
        }"""

        result = replace_css_links(css, map_func)

        expected = """
        @font-face {
          font-family: 'FontAwesome';
          src: url('hashed.eot?v=3.0.1');
          src: url('hashed.eot?#iefix&v=3.0.1') format('embedded-opentype'),
            url('hashed.woff?v=3.0.1') format('woff'),
            url('hashed.ttf?v=3.0.1') format('truetype');
          font-weight: normal;
          font-style: normal;
        }"""
        self.assertMultiLineEqual(result, expected)
Пример #3
0
    def test_replace_fonts(self):
        from kagin.link_css import replace_css_links
        file_map = {
            'fontawesome-webfont.eot': 'hashed.eot',
            'fontawesome-webfont.woff': 'hashed.woff',
            'fontawesome-webfont.ttf': 'hashed.ttf',
        }
        
        def map_func(path):
            return file_map.get(path)

        css = """
        @font-face {
          font-family: 'FontAwesome';
          src: url('fontawesome-webfont.eot?v=3.0.1');
          src: url('fontawesome-webfont.eot?#iefix&v=3.0.1') format('embedded-opentype'),
            url('fontawesome-webfont.woff?v=3.0.1') format('woff'),
            url('fontawesome-webfont.ttf?v=3.0.1') format('truetype');
          font-weight: normal;
          font-style: normal;
        }"""

        result = replace_css_links(css, map_func)

        expected = """
        @font-face {
          font-family: 'FontAwesome';
          src: url('hashed.eot?v=3.0.1');
          src: url('hashed.eot?#iefix&v=3.0.1') format('embedded-opentype'),
            url('hashed.woff?v=3.0.1') format('woff'),
            url('hashed.ttf?v=3.0.1') format('truetype');
          font-weight: normal;
          font-style: normal;
        }"""
        self.assertMultiLineEqual(result, expected)
Пример #4
0
    def link_css(self, content, old_path, new_path):
        """Replace URL links in CSS content and return result
        
        """
        # put both path in output dir
        old_path = os.path.join(self.output_dir, old_path)
        new_path = os.path.join(self.output_dir, new_path)
        old_dir = os.path.dirname(old_path)
        new_dir = os.path.dirname(new_path)
        rel_dir = os.path.relpath(old_dir, new_dir)
        #rel_dir = url_path(rel_dir, old_dir)
        rel_dir = rel_dir.replace('\\', '/')
        if rel_dir:
            rel_dir = rel_dir + '/'

        def map_func(url):
            parsed = urlparse.urlparse(url)
            # we don't want to map absolute URLs
            if parsed.scheme:
                return
            if parsed.path.startswith('/'):
                return
            return urlparse.urljoin(rel_dir, url)

        output = replace_css_links(content, map_func, self.logger)
        return output
Пример #5
0
 def run_linking(self, file_map, route_url):
     """Run linking process
     
     """
     for root, _, filenames in os.walk(self.input_dir):
         for filename in filenames:
             file_path = os.path.join(root, filename)
             _, ext = os.path.splitext(file_path)
             if ext.lower() != '.css':
                 continue
             
             # directory path of CSS file
             css_dir = os.path.dirname(file_path)
             if css_dir:
                 css_dir = css_dir + '/'
             input_url = url_path(file_path, self.input_dir)
             
             def map_func(url):
                 parsed = urlparse.urlparse(url)
                 # we don't want to map absolute URLs
                 if parsed.scheme:
                     return
                 if parsed.path.startswith('/'):
                     return
                 
                 # get CSS path in file system
                 css_path = os.path.join(css_dir, url)
                 css_path = os.path.normpath(css_path)
                 css_url = url_path(css_path, self.input_dir)
                 new_url = file_map.get(css_url)
                 if new_url is None:
                     return
                 return route_url(new_url)
             
             # output filename
             output_filename = file_map.get(input_url)
             output_filename = os.path.join(self.output_dir, output_filename)
             with open(output_filename, 'rt') as file:
                 css_content = file.read()
             output = replace_css_links(css_content, map_func, self.logger)
             with open(output_filename, 'wt') as file:
                 file.write(output)
Пример #6
0
 def test_replace(input, expected):
     result = replace_css_links(input, map_func)
     self.assertEqual(result, expected)
Пример #7
0
 def test_replace(input, expected):
     result = replace_css_links(input, map_func)
     self.assertEqual(result, expected)