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

Side by Side Diff: cms/utils.py

Issue 29590620: Noissue - Refactor Converter.__init__ and __call__ (Closed)
Patch Set: Address comments on PS1 Created Nov. 7, 2017, 5:01 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « cms/converters.py ('k') | tests/expected_output/en/includer » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 from cms.converters import converters, TemplateConverter 16 import re
17
18 __all__ = [
19 'get_page_params',
20 'process_page',
21 'split_head_body',
22 'extract_page_metadata'
23 ]
24
25
26 def split_head_body(html):
27 """Split HTML page into head and remaining content.
28
29 This is used to pass head and body of the page to the template as two
30 separate variables.
31
32 Parameters
33 ----------
34 html: str
35 Source HTML to split.
36
37 Returns
38 -------
39 (str, str)
40 Everything inside of <head> tags found in the page,
41 the rest of the page text with <head> tags removed.
42
43 """
44 head = []
45
46 def add_to_head(match):
47 head.append(match.group(1))
48 return ''
49
50 body = re.sub(r'<head>(.*?)</head>', add_to_head, html, flags=re.S)
51 return ''.join(head), body
52
53
54 def extract_page_metadata(source):
55 """Extract metadata variables from source text of the page.
56
57 Parameters
58 ----------
59 source: str
60 Source text of the page.
61
62 Returns
63 -------
64 (dict, str)
65 Metadata of the page, remaining source text without metadata.
66
67 """
68 metadata = {}
69 lines = source.splitlines(True)
70 for i, line in enumerate(lines):
71 if line.strip() in {'<!--', '-->'}:
72 lines[i] = ''
73 continue
74 if not re.search(r'^\s*[\w\-]+\s*=', line):
75 break
76 name, value = line.split('=', 1)
77 value = value.strip()
78 if value.startswith('[') and value.endswith(']'):
79 value = [element.strip() for element in value[1:-1].split(',')]
80 lines[i] = '\n'
81 metadata[name.strip()] = value
82 return metadata, ''.join(lines)
17 83
18 84
19 def get_page_params(source, locale, page, format=None, site_url_override=None, 85 def get_page_params(source, locale, page, format=None, site_url_override=None,
20 localized_string_callback=None): 86 localized_string_callback=None):
87 from cms.converters import converters
88
21 # Guess page format if omitted, but default to Markdown for friendlier excep tions 89 # Guess page format if omitted, but default to Markdown for friendlier excep tions
22 if format is None: 90 if format is None:
23 for format in converters.iterkeys(): 91 for format in converters.iterkeys():
24 if source.has_page(page, format): 92 if source.has_page(page, format):
25 break 93 break
26 else: 94 else:
27 format = 'md' 95 format = 'md'
28 96
29 params = { 97 params = {
30 'source': source, 98 'source': source,
31 'template': 'default', 99 'template': 'default',
32 'locale': locale, 100 'locale': locale,
33 'page': page, 101 'page': page,
34 'pagedata': source.read_page(page, format),
35 'config': source.read_config(), 102 'config': source.read_config(),
36 'localized_string_callback': localized_string_callback, 103 'localized_string_callback': localized_string_callback,
37 } 104 }
38 105
39 localefile = page 106 localefile = page
40 if params['config'].has_option('locale_overrides', page): 107 if params['config'].has_option('locale_overrides', page):
41 localefile = params['config'].get('locale_overrides', page) 108 localefile = params['config'].get('locale_overrides', page)
42 params['localedata'] = source.read_locale(params['locale'], localefile) 109 params['localedata'] = source.read_locale(params['locale'], localefile)
43 110
44 if params['config'].has_option('general', 'siteurl'): 111 if params['config'].has_option('general', 'siteurl'):
45 if site_url_override: 112 if site_url_override:
46 params['site_url'] = site_url_override 113 params['site_url'] = site_url_override
47 else: 114 else:
48 params['site_url'] = params['config'].get('general', 'siteurl') 115 params['site_url'] = params['config'].get('general', 'siteurl')
49 116
50 try: 117 data, filename = source.read_page(page, format)
51 converter_class = converters[format] 118 metadata, body = extract_page_metadata(data)
52 except KeyError: 119 params['pagedata'] = body, filename
53 raise Exception('Page %s uses unknown format %s' % (page, format)) 120 params.update(metadata)
54 121
55 converter = converter_class(params)
56
57 # Note: The converter might change some parameters so we can only read in
58 # template data here.
59 params['templatedata'] = source.read_template(params['template']) 122 params['templatedata'] = source.read_template(params['template'])
60 123
61 defaultlocale = params['config'].get('general', 'defaultlocale') 124 defaultlocale = params['config'].get('general', 'defaultlocale')
62 params['defaultlocale'] = defaultlocale 125 params['defaultlocale'] = defaultlocale
63 126
64 locales = [ 127 locales = [
65 l 128 l
66 for l in source.list_locales() 129 for l in source.list_locales()
67 if source.has_locale(l, localefile) 130 if source.has_locale(l, localefile)
68 ] 131 ]
69 if defaultlocale not in locales: 132 if defaultlocale not in locales:
70 locales.append(defaultlocale) 133 locales.append(defaultlocale)
71 locales.sort() 134 locales.sort()
72 params['available_locales'] = locales 135 params['available_locales'] = locales
73 136
74 params['head'], params['body'] = converter() 137 try:
138 converter_class = converters[format]
139 except KeyError:
140 raise Exception('Page %s uses unknown format %s' % (page, format))
141
142 converter = converter_class(body, filename, params)
143 converted = converter()
144 params['head'], params['body'] = split_head_body(converted)
145
75 if converter.total_translations > 0: 146 if converter.total_translations > 0:
76 params['translation_ratio'] = ( 147 params['translation_ratio'] = (
77 1 - float(converter.missing_translations) / converter.total_translat ions 148 1 - float(converter.missing_translations) / converter.total_translat ions
78 ) 149 )
79 else: 150 else:
80 params['translation_ratio'] = 1 151 params['translation_ratio'] = 1
81 152
82 return params 153 return params
83 154
84 155
85 def process_page(source, locale, page, format=None, site_url_override=None, 156 def process_page(source, locale, page, format=None, site_url_override=None,
86 localized_string_callback=None): 157 localized_string_callback=None):
87 return TemplateConverter( 158 from cms.converters import TemplateConverter
88 get_page_params(source, locale, page, format, 159
89 site_url_override, localized_string_callback), 160 params = get_page_params(source, locale, page, format, site_url_override,
90 key='templatedata' 161 localized_string_callback)
91 )() 162 return TemplateConverter(*params['templatedata'], params=params)()
OLDNEW
« no previous file with comments | « cms/converters.py ('k') | tests/expected_output/en/includer » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld