| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 1 | // Copyright 2012 Google Inc. All Rights Reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package com.google.gitiles; |
| 16 | |
| 17 | import com.google.common.base.Function; |
| 18 | import com.google.common.collect.ImmutableMap; |
| 19 | import com.google.common.io.Resources; |
| 20 | import com.google.template.soy.SoyFileSet; |
| 21 | import com.google.template.soy.tofu.SoyTofu; |
| 22 | |
| 23 | import java.net.URL; |
| 24 | import java.util.Map; |
| 25 | |
| 26 | /** Renderer that precompiles Soy and uses static precompiled CSS. */ |
| 27 | public class DefaultRenderer extends Renderer { |
| 28 | private final SoyTofu tofu; |
| 29 | |
| 30 | DefaultRenderer() { |
| Chad Horohoe | 2a28d62 | 2012-11-12 11:56:59 -0800 | [diff] [blame] | 31 | this("", null, ""); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 32 | } |
| 33 | |
| Chad Horohoe | 2a28d62 | 2012-11-12 11:56:59 -0800 | [diff] [blame] | 34 | public DefaultRenderer(String staticPrefix, URL customTemplates, String siteTitle) { |
| 35 | this(ImmutableMap.<String, String> of(), staticPrefix, customTemplates, siteTitle); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| Chad Horohoe | 2a28d62 | 2012-11-12 11:56:59 -0800 | [diff] [blame] | 38 | public DefaultRenderer(Map<String, String> globals, String staticPrefix, URL customTemplates, |
| 39 | String siteTitle) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 40 | super( |
| 41 | new Function<String, URL>() { |
| 42 | @Override |
| 43 | public URL apply(String name) { |
| 44 | return Resources.getResource(Renderer.class, "templates/" + name); |
| 45 | } |
| 46 | }, |
| Chad Horohoe | 2a28d62 | 2012-11-12 11:56:59 -0800 | [diff] [blame] | 47 | globals, staticPrefix, customTemplates, siteTitle); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 48 | SoyFileSet.Builder builder = new SoyFileSet.Builder() |
| 49 | .setCompileTimeGlobals(this.globals); |
| 50 | for (URL template : templates) { |
| 51 | builder.add(template); |
| 52 | } |
| 53 | tofu = builder.build().compileToTofu(); |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | protected SoyTofu getTofu() { |
| 58 | return tofu; |
| 59 | } |
| 60 | } |