Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: cms/bin/generate_static_pages.py

Issue 29592634: Issue 5333 - Allow cms to generate relative pages
Patch Set: Created Oct. 30, 2017, 4:31 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: cms/bin/generate_static_pages.py
diff --git a/cms/bin/generate_static_pages.py b/cms/bin/generate_static_pages.py
index 242dc0128fbeeacac98ff5897144823285c82bae..f0fccc22055fe0540a772e05671446a44f5f3b03 100644
--- a/cms/bin/generate_static_pages.py
+++ b/cms/bin/generate_static_pages.py
@@ -19,6 +19,8 @@ import errno
import codecs
import ConfigParser
import logging
+import lxml.html
+
from argparse import ArgumentParser
from cms.utils import get_page_params, process_page
@@ -27,7 +29,7 @@ from cms.sources import create_source
MIN_TRANSLATED = 0.3
-def generate_pages(repo, output_dir, revision):
+def generate_pages(repo, output_dir, revision, relative):
known_files = set()
def write_file(path_parts, contents, binary=False):
@@ -90,15 +92,43 @@ def generate_pages(repo, output_dir, revision):
source.has_locale = has_locale
source.resolve_link.cache_clear()
+ def rewrite_link(link):
Vasily Kuznetsov 2017/11/14 17:30:27 This seems like it would work but the links would
+ if link.startswith('/'):
+ if source.version:
+ link += '?' + source.version
+ if relative:
+ depth = len(page.split('/'))
+ link = '/'.join(['..'] * depth) + link
+ return link
+
# Second pass: actually generate pages this time
for locale, page in pagelist:
pagedata = process_page(source, locale, page)
- # Make sure links to static files are versioned
- pagedata = re.sub(r'(<script\s[^<>]*\bsrc="/[^"<>]+)', r'\1?%s' % source.version, pagedata)
- pagedata = re.sub(r'(<link\s[^<>]*\bhref="/[^"<>]+)', r'\1?%s' % source.version, pagedata)
- pagedata = re.sub(r'(<img\s[^<>]*\bsrc="/[^"<>]+)', r'\1?%s' % source.version, pagedata)
-
+ root = lxml.html.fromstring(pagedata).getroottree()
+
+ expr = '//img[@src|@srcset]|//script[@src]|//link[@href]|//a[@href]'
+ for elem in root.xpath(expr):
+ if 'srcset' in elem.attrib:
+ srcset = []
+ for src_zoom in elem.attrib['srcset'].split(', '):
Vasily Kuznetsov 2017/11/14 17:30:28 Is the space after the comma mandatory? Otherwise
+ try:
+ src, zoom = src_zoom.split(' ')
+ except ValueError:
+ src = src_zoom
+ zoom = None
+ res = rewrite_link(src)
+ if zoom:
+ res = res + ' ' + zoom
+ srcset.append(res)
+
+ elem.attrib['srcset'] = ', '.join(srcset)
+
+ for key in ['src', 'href']:
+ if key in elem.attrib:
+ elem.attrib[key] = rewrite_link(elem.attrib[key])
+
+ pagedata = lxml.html.tostring(root, encoding='unicode')
write_file([locale] + page.split('/'), pagedata)
for filename in source.list_localizable_files():
@@ -129,7 +159,8 @@ if __name__ == '__main__':
help=('Specify which revision to generate from. '
'See "hg help revisions" for details.'),
default='default')
+ parser.add_argument('--relative', action='store_true', default=False)
parser.add_argument('source', help="Path to website's repository")
parser.add_argument('output', help='Path to desired output directory')
args = parser.parse_args()
- generate_pages(args.source, args.output, args.rev)
+ generate_pages(args.source, args.output, args.rev, args.relative)
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld