 Issue 29592634:
  Issue 5333 - Allow cms to generate relative pages
    
  
    Issue 29592634:
  Issue 5333 - Allow cms to generate relative pages 
  | Left: | ||
| Right: | 
| OLD | NEW | 
|---|---|
| 1 # This file is part of the Adblock Plus web scripts, | 1 # This file is part of the Adblock Plus web scripts, | 
| 2 # Copyright (C) 2006-present eyeo GmbH | 2 # Copyright (C) 2006-present eyeo GmbH | 
| 3 # | 3 # | 
| 4 # Adblock Plus is free software: you can redistribute it and/or modify | 4 # Adblock Plus is free software: you can redistribute it and/or modify | 
| 5 # it under the terms of the GNU General Public License version 3 as | 5 # it under the terms of the GNU General Public License version 3 as | 
| 6 # published by the Free Software Foundation. | 6 # published by the Free Software Foundation. | 
| 7 # | 7 # | 
| 8 # Adblock Plus is distributed in the hope that it will be useful, | 8 # Adblock Plus is distributed in the hope that it will be useful, | 
| 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of | 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of | 
| 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 
| 11 # GNU General Public License for more details. | 11 # GNU General Public License for more details. | 
| 12 # | 12 # | 
| 13 # You should have received a copy of the GNU General Public License | 13 # You should have received a copy of the GNU General Public License | 
| 14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 
| 15 | 15 | 
| 16 import os | 16 import os | 
| 17 import re | 17 import re | 
| 18 import errno | 18 import errno | 
| 19 import codecs | 19 import codecs | 
| 20 import ConfigParser | 20 import ConfigParser | 
| 21 import logging | 21 import logging | 
| 22 import lxml.html | |
| 23 | |
| 22 from argparse import ArgumentParser | 24 from argparse import ArgumentParser | 
| 23 | 25 | 
| 24 from cms.utils import get_page_params, process_page | 26 from cms.utils import get_page_params, process_page | 
| 25 from cms.sources import create_source | 27 from cms.sources import create_source | 
| 26 | 28 | 
| 27 MIN_TRANSLATED = 0.3 | 29 MIN_TRANSLATED = 0.3 | 
| 28 | 30 | 
| 29 | 31 | 
| 30 def generate_pages(repo, output_dir, revision): | 32 def generate_pages(repo, output_dir, revision, relative): | 
| 31 known_files = set() | 33 known_files = set() | 
| 32 | 34 | 
| 33 def write_file(path_parts, contents, binary=False): | 35 def write_file(path_parts, contents, binary=False): | 
| 34 encoding = None if binary else 'utf-8' | 36 encoding = None if binary else 'utf-8' | 
| 35 outfile = os.path.join(output_dir, *path_parts) | 37 outfile = os.path.join(output_dir, *path_parts) | 
| 36 if outfile in known_files: | 38 if outfile in known_files: | 
| 37 logging.warning('File %s has multiple sources', outfile) | 39 logging.warning('File %s has multiple sources', outfile) | 
| 38 return | 40 return | 
| 39 known_files.add(outfile) | 41 known_files.add(outfile) | 
| 40 | 42 | 
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 83 orig_has_locale = source.has_locale | 85 orig_has_locale = source.has_locale | 
| 84 | 86 | 
| 85 def has_locale(locale, page): | 87 def has_locale(locale, page): | 
| 86 page = get_locale_file(page) | 88 page = get_locale_file(page) | 
| 87 if (locale, page) in blacklist: | 89 if (locale, page) in blacklist: | 
| 88 return False | 90 return False | 
| 89 return orig_has_locale(locale, page) | 91 return orig_has_locale(locale, page) | 
| 90 source.has_locale = has_locale | 92 source.has_locale = has_locale | 
| 91 source.resolve_link.cache_clear() | 93 source.resolve_link.cache_clear() | 
| 92 | 94 | 
| 95 def rewrite_link(link): | |
| 
Vasily Kuznetsov
2017/11/14 17:30:27
This seems like it would work but the links would
 | |
| 96 if link.startswith('/'): | |
| 97 if source.version: | |
| 98 link += '?' + source.version | |
| 99 if relative: | |
| 100 depth = len(page.split('/')) | |
| 101 link = '/'.join(['..'] * depth) + link | |
| 102 return link | |
| 103 | |
| 93 # Second pass: actually generate pages this time | 104 # Second pass: actually generate pages this time | 
| 94 for locale, page in pagelist: | 105 for locale, page in pagelist: | 
| 95 pagedata = process_page(source, locale, page) | 106 pagedata = process_page(source, locale, page) | 
| 96 | 107 | 
| 97 # Make sure links to static files are versioned | 108 root = lxml.html.fromstring(pagedata).getroottree() | 
| 98 pagedata = re.sub(r'(<script\s[^<>]*\bsrc="/[^"<>]+)', r'\1?%s' % so urce.version, pagedata) | |
| 99 pagedata = re.sub(r'(<link\s[^<>]*\bhref="/[^"<>]+)', r'\1?%s' % sou rce.version, pagedata) | |
| 100 pagedata = re.sub(r'(<img\s[^<>]*\bsrc="/[^"<>]+)', r'\1?%s' % sourc e.version, pagedata) | |
| 101 | 109 | 
| 110 expr = '//img[@src|@srcset]|//script[@src]|//link[@href]|//a[@href]' | |
| 111 for elem in root.xpath(expr): | |
| 112 if 'srcset' in elem.attrib: | |
| 113 srcset = [] | |
| 114 for src_zoom in elem.attrib['srcset'].split(', '): | |
| 
Vasily Kuznetsov
2017/11/14 17:30:28
Is the space after the comma mandatory? Otherwise
 | |
| 115 try: | |
| 116 src, zoom = src_zoom.split(' ') | |
| 117 except ValueError: | |
| 118 src = src_zoom | |
| 119 zoom = None | |
| 120 res = rewrite_link(src) | |
| 121 if zoom: | |
| 122 res = res + ' ' + zoom | |
| 123 srcset.append(res) | |
| 124 | |
| 125 elem.attrib['srcset'] = ', '.join(srcset) | |
| 126 | |
| 127 for key in ['src', 'href']: | |
| 128 if key in elem.attrib: | |
| 129 elem.attrib[key] = rewrite_link(elem.attrib[key]) | |
| 130 | |
| 131 pagedata = lxml.html.tostring(root, encoding='unicode') | |
| 102 write_file([locale] + page.split('/'), pagedata) | 132 write_file([locale] + page.split('/'), pagedata) | 
| 103 | 133 | 
| 104 for filename in source.list_localizable_files(): | 134 for filename in source.list_localizable_files(): | 
| 105 for locale in locales: | 135 for locale in locales: | 
| 106 if source.has_localizable_file(locale, filename): | 136 if source.has_localizable_file(locale, filename): | 
| 107 filedata = source.read_localizable_file(locale, filename) | 137 filedata = source.read_localizable_file(locale, filename) | 
| 108 write_file([locale] + filename.split('/'), filedata, binary= True) | 138 write_file([locale] + filename.split('/'), filedata, binary= True) | 
| 109 | 139 | 
| 110 for filename in source.list_static(): | 140 for filename in source.list_static(): | 
| 111 write_file(filename.split('/'), source.read_static(filename), binary =True) | 141 write_file(filename.split('/'), source.read_static(filename), binary =True) | 
| (...skipping 10 matching lines...) Expand all Loading... | |
| 122 os.rmdir(path) | 152 os.rmdir(path) | 
| 123 remove_unknown(output_dir) | 153 remove_unknown(output_dir) | 
| 124 | 154 | 
| 125 | 155 | 
| 126 if __name__ == '__main__': | 156 if __name__ == '__main__': | 
| 127 parser = ArgumentParser('Convert website source to static website') | 157 parser = ArgumentParser('Convert website source to static website') | 
| 128 parser.add_argument('-r', '--rev', | 158 parser.add_argument('-r', '--rev', | 
| 129 help=('Specify which revision to generate from. ' | 159 help=('Specify which revision to generate from. ' | 
| 130 'See "hg help revisions" for details.'), | 160 'See "hg help revisions" for details.'), | 
| 131 default='default') | 161 default='default') | 
| 162 parser.add_argument('--relative', action='store_true', default=False) | |
| 132 parser.add_argument('source', help="Path to website's repository") | 163 parser.add_argument('source', help="Path to website's repository") | 
| 133 parser.add_argument('output', help='Path to desired output directory') | 164 parser.add_argument('output', help='Path to desired output directory') | 
| 134 args = parser.parse_args() | 165 args = parser.parse_args() | 
| 135 generate_pages(args.source, args.output, args.rev) | 166 generate_pages(args.source, args.output, args.rev, args.relative) | 
| OLD | NEW |