1 # Copyright (c) 2014 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
12 from tvcm import js_utils
13 from tvcm import module as module_module
14 from tvcm import html_generation_controller
17 srcdir = os.path.abspath(os.path.join(os.path.dirname(__file__),
18 "..", "..", "..", "src"))
20 html_warning_message = """
24 WARNING: This file is auto generated.
30 js_warning_message = """
31 // Copyright (c) 2014 The Chromium Authors. All rights reserved.
32 // Use of this source code is governed by a BSD-style license that can be
33 // found in the LICENSE file.
35 /* WARNING: This file is auto generated.
37 * Do not edit directly.
41 css_warning_message = """
42 /* Copyright (c) 2014 The Chromium Authors. All rights reserved.
43 * Use of this source code is governed by a BSD-style license that can be
44 * found in the LICENSE file. */
46 /* WARNING: This file is auto-generated.
48 * Do not edit directly.
52 def GenerateJS(load_sequence,
53 use_include_tags_for_scripts=False,
54 dir_for_include_tag_root=None):
55 f = StringIO.StringIO()
58 use_include_tags_for_scripts,
59 dir_for_include_tag_root)
62 def GenerateJSToFile(f,
64 use_include_tags_for_scripts=False,
65 dir_for_include_tag_root=None):
66 if use_include_tags_for_scripts and dir_for_include_tag_root == None:
67 raise Exception('Must provide dir_for_include_tag_root')
69 f.write(js_warning_message)
72 loader = load_sequence[0].loader
74 polymer_script = loader.LoadRawScript('polymer.js')
75 f.write(polymer_script.contents)
78 f.write("window._TVCM_IS_COMPILED = true;\n")
80 for module in load_sequence:
81 module.AppendJSContentsToFile(f,
82 use_include_tags_for_scripts,
83 dir_for_include_tag_root)
85 class ExtraScript(object):
86 def __init__(self, script_id=None, text_content=None, content_type=None):
88 assert script_id[0] != '#'
89 self.script_id = script_id
90 self.text_content = text_content
91 self.content_type = content_type
93 def WriteToFile(self, output_file):
96 attrs.append('id="%s"' % self.script_id)
98 attrs.append('content-type="%s"' % self.content_type)
101 output_file.write('<script %s>\n' % ' '.join(attrs))
103 output_file.write('<script>\n')
104 if self.text_content:
105 output_file.write(self.text_content)
106 output_file.write('</script>\n')
109 def GenerateStandaloneHTMLAsString(*args, **kwargs):
110 f = StringIO.StringIO()
111 GenerateStandaloneHTMLToFile(f, *args, **kwargs)
114 def GenerateStandaloneHTMLToFile(output_file,
117 flattened_js_url=None,
119 extra_scripts = extra_scripts or []
121 output_file.write("""<!DOCTYPE HTML>
123 <head i18n-values="dir:textdirection;">
124 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
128 loader = load_sequence[0].loader
130 written_style_sheets = set()
132 class HTMLGenerationController(html_generation_controller.HTMLGenerationController):
133 def __init__(self, module):
135 def GetHTMLForStylesheetHRef(self, href):
136 resource = self.module.HRefToResource(
137 href, '<link rel="stylesheet" href="%s">' % href)
138 style_sheet = loader.LoadStyleSheet(resource.name)
140 if style_sheet in written_style_sheets:
142 written_style_sheets.add(style_sheet)
144 return "<style>\n%s\n</style>" % style_sheet.contents_with_inlined_images
146 for module in load_sequence:
147 ctl = HTMLGenerationController(module)
148 module.AppendHTMLContentsToFile(output_file, ctl)
151 output_file.write('<script src="%s"></script>\n' % flattened_js_url)
153 output_file.write('<script>\n')
154 output_file.write(GenerateJS(load_sequence))
155 output_file.write('</script>\n')
157 for extra_script in extra_scripts:
158 extra_script.WriteToFile(output_file)
160 output_file.write("""</head>
164 output_file.write("""</body>