Ejemplo n.º 1
0
        /// <summary>
        /// Translates a code of assets written on Mustache to JS code
        /// </summary>
        /// <param name="assets">Set of assets with code written on Mustache</param>
        /// <returns>Set of assets with translated code</returns>
        public override IList <IAsset> Translate(IList <IAsset> assets)
        {
            if (assets == null)
            {
                throw new ArgumentException(CoreStrings.Common_ValueIsEmpty, "assets");
            }

            if (assets.Count == 0)
            {
                return(assets);
            }

            var assetsToProcessing = assets.Where(a => a.AssetTypeCode == Constants.AssetTypeCode.Mustache ||
                                                  a.AssetTypeCode == Constants.AssetTypeCode.Hogan).ToList();

            if (assetsToProcessing.Count == 0)
            {
                return(assets);
            }

            bool enableNativeMinification = NativeMinificationEnabled;
            CompilationOptions options    = CreateCompilationOptions(enableNativeMinification);

            using (var hoganCompiler = new HoganCompiler(_createJsEngineInstance, options))
            {
                foreach (var asset in assetsToProcessing)
                {
                    InnerTranslate(asset, hoganCompiler, enableNativeMinification);
                }
            }

            return(assets);
        }
Ejemplo n.º 2
0
        private void InnerTranslate(IAsset asset, HoganCompiler hoganCompiler, bool enableNativeMinification)
        {
            string newContent;
            string assetUrl = asset.Url;

            try
            {
                newContent = hoganCompiler.Compile(asset.Content, assetUrl);
            }
            catch (HoganCompilationException e)
            {
                throw new AssetTranslationException(
                          string.Format(CoreStrings.Translators_TranslationSyntaxError,
                                        INPUT_CODE_TYPE, OUTPUT_CODE_TYPE, assetUrl, e.Message));
            }
            catch (Exception e)
            {
                throw new AssetTranslationException(
                          string.Format(CoreStrings.Translators_TranslationFailed,
                                        INPUT_CODE_TYPE, OUTPUT_CODE_TYPE, assetUrl, e.Message));
            }

            asset.Content  = newContent;
            asset.Minified = enableNativeMinification;
        }
Ejemplo n.º 3
0
        public void CompileFailsGracefullyOnMono()
        {
            var compiler  = new HoganCompiler();
            var exception = Assert.Throws(typeof(NotSupportedException), () => compiler.Compile(""));

            Assert.AreEqual("Hogan not yet supported for mono.", exception.Message);
        }
Ejemplo n.º 4
0
        public override IProcessResult Process(string filePath, string content)
        {
            var    compiler     = new HoganCompiler();
            string template     = compiler.Compile(content);
            string templateName = Path.GetFileName(filePath).Split('.').First();
            var    sb           = new StringBuilder();

            sb.AppendLine("var JST = JST || {};");
            sb.AppendLine("JST['" + templateName + "'] = new Hogan.Template(" + template + ");");
            return(new ProcessResult(sb.ToString()));
        }
Ejemplo n.º 5
0
        public override IProcessResult Process(string filePath, string content)
        {
            var    compiler     = new HoganCompiler();
            string renderFunc   = compiler.Compile(content);
            string templateName = Path.GetFileName(filePath).Split('.').First();
            string templateHtml = string.Join("\\r\"+\"\\n",
                                              content.Replace("\"", "\\\"").Split(new[] { Environment.NewLine }, StringSplitOptions.None));
            var sb = new StringBuilder();

            sb.AppendLine("var JST = JST || {};");
            sb.AppendLine("JST['" + templateName + "'] = new Hogan.Template(" + renderFunc + ",\"" + templateHtml + "\",Hogan,{});");
            return(new ProcessResult(sb.ToString()));
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Translates a code of asset written on Mustache to JS code
        /// </summary>
        /// <param name="asset">Asset with code written on Mustache</param>
        /// <returns>Asset with translated code</returns>
        public override IAsset Translate(IAsset asset)
        {
            if (asset == null)
            {
                throw new ArgumentException(CoreStrings.Common_ValueIsEmpty, "asset");
            }

            bool enableNativeMinification = NativeMinificationEnabled;
            CompilationOptions options    = CreateCompilationOptions(enableNativeMinification);

            using (var hoganCompiler = new HoganCompiler(_createJsEngineInstance, options))
            {
                InnerTranslate(asset, hoganCompiler, enableNativeMinification);
            }

            return(asset);
        }
        public void CompileHoganTemplate()
        {
            const string template = @"<div class=""entry"">
  <h1>{{title}}</h1>

  {{#author}}
  <h2>By {{firstName}} {{lastName}}</h2>
  {{/author}}
</div>";
            var embeddedResourceLoader = new EmbeddedResourceLoader();
            var hoganCompiler = new HoganCompiler(embeddedResourceLoader);

            const string expectedCompiledTemplate = @"{code: function (c,p,i) { var t=this;t.b(i=i||"""");t.b(""<div class=\""entry\"">"");t.b(""\n"" + i);t.b(""  <h1>"");t.b(t.v(t.f(""title"",c,p,0)));t.b(""</h1>"");t.b(""\n"");t.b(""\n"" + i);if(t.s(t.f(""author"",c,p,1),c,p,0,55,99,""{{ }}"")){t.rs(c,p,function(c,p,t){t.b(""  <h2>By "");t.b(t.v(t.f(""firstName"",c,p,0)));t.b("" "");t.b(t.v(t.f(""lastName"",c,p,0)));t.b(""</h2>"");t.b(""\n"" + i);});c.pop();}t.b(""</div>"");return t.fl(); },partials: {}, subs: {  }}";

            var compiledTemplate = hoganCompiler.Compile(template);

            Assert.AreEqual(expectedCompiledTemplate, compiledTemplate);
        }
        public void CompileHoganTemplate()
        {
            const string template = @"<div class=""entry"">
  <h1>{{title}}</h1>

  {{#author}}
  <h2>By {{firstName}} {{lastName}}</h2>
  {{/author}}
</div>";
            var          embeddedResourceLoader = new EmbeddedResourceLoader();
            var          hoganCompiler          = new HoganCompiler(embeddedResourceLoader);

            const string expectedCompiledTemplate = @"{code: function (c,p,i) { var t=this;t.b(i=i||"""");t.b(""<div class=\""entry\"">"");t.b(""\n"" + i);t.b(""  <h1>"");t.b(t.v(t.f(""title"",c,p,0)));t.b(""</h1>"");t.b(""\n"");t.b(""\n"" + i);if(t.s(t.f(""author"",c,p,1),c,p,0,55,99,""{{ }}"")){t.rs(c,p,function(c,p,t){t.b(""  <h2>By "");t.b(t.v(t.f(""firstName"",c,p,0)));t.b("" "");t.b(t.v(t.f(""lastName"",c,p,0)));t.b(""</h2>"");t.b(""\n"" + i);});c.pop();}t.b(""</div>"");return t.fl(); },partials: {}, subs: {  }}";

            var compiledTemplate = hoganCompiler.Compile(template);

            Assert.AreEqual(expectedCompiledTemplate, compiledTemplate);
        }
        public void RenderHoganTemplate()
        {
            const string template               = @"<div class=""entry"">
  <h1>{{title}}</h1>

  {{#author}}
  <h2>By {{firstName}} {{lastName}}</h2>
  {{/author}}
</div>";
            const string templateName           = "Authors.mustache";
            var          embeddedResourceLoader = new EmbeddedResourceLoader();
            var          hoganCompiler          = new HoganCompiler(embeddedResourceLoader);
            var          compiledTemplate       = hoganCompiler.Compile(template);

            hoganCompiler.AddTemplate(templateName, compiledTemplate);
            var model = new
            {
                title  = "The Title",
                author = new []
                {
                    new
                    {
                        firstName = "FirstName",
                        lastName  = "LastName"
                    },
                    new
                    {
                        firstName = "2ndFirstName",
                        lastName  = "2ndLastName"
                    },
                }
            };

            const string expectedTemplateOuput = @"<div class=""entry"">
  <h1>The Title</h1>

  <h2>By FirstName LastName</h2>
  <h2>By 2ndFirstName 2ndLastName</h2>
</div>";

            var templateOutput = hoganCompiler.Render(templateName, model);

            Assert.AreEqual(expectedTemplateOuput, templateOutput);
        }
        public void RenderHoganTemplate()
        {
            const string template = @"<div class=""entry"">
  <h1>{{title}}</h1>

  {{#author}}
  <h2>By {{firstName}} {{lastName}}</h2>
  {{/author}}
</div>";
            const string templateName = "Authors.mustache";
            var embeddedResourceLoader = new EmbeddedResourceLoader();
            var hoganCompiler = new HoganCompiler(embeddedResourceLoader);
            var compiledTemplate = hoganCompiler.Compile(template);
            hoganCompiler.AddTemplate(templateName, compiledTemplate);
            var model = new
            {
                title = "The Title",
                author = new []
                {
                    new
                    {
                        firstName = "FirstName",
                        lastName = "LastName"
                    },
                                        new
                    {
                        firstName = "2ndFirstName",
                        lastName = "2ndLastName"
                    },
                }
            };

            const string expectedTemplateOuput = @"<div class=""entry"">
  <h1>The Title</h1>

  <h2>By FirstName LastName</h2>
  <h2>By 2ndFirstName 2ndLastName</h2>
</div>";

            var templateOutput = hoganCompiler.Render(templateName, model);

            Assert.AreEqual(expectedTemplateOuput, templateOutput);
        }
        /// <summary>
        /// Translates a code of asset written on Mustache to JS code
        /// </summary>
        /// <param name="asset">Asset with code written on Mustache</param>
        /// <returns>Asset with translated code</returns>
        public override IAsset Translate(IAsset asset)
        {
            if (asset == null)
            {
                throw new ArgumentNullException(
                          nameof(asset),
                          string.Format(CoreStrings.Common_ArgumentIsNull, nameof(asset))
                          );
            }

            bool enableNativeMinification = NativeMinificationEnabled;
            CompilationOptions options    = CreateCompilationOptions(enableNativeMinification);

            using (var hoganCompiler = new HoganCompiler(_createJsEngineInstance, options))
            {
                InnerTranslate(asset, hoganCompiler, enableNativeMinification);
            }

            return(asset);
        }
Ejemplo n.º 12
0
		private void InnerTranslate(IAsset asset, HoganCompiler hoganCompiler, bool enableNativeMinification)
		{
			string newContent;
			string assetVirtualPath = asset.VirtualPath;

			try
			{
				newContent = hoganCompiler.Compile(asset.Content, assetVirtualPath);
			}
			catch (HoganCompilingException e)
			{
				throw new AssetTranslationException(
					string.Format(CoreStrings.Translators_TranslationSyntaxError,
						INPUT_CODE_TYPE, OUTPUT_CODE_TYPE, assetVirtualPath, e.Message));
			}
			catch (Exception e)
			{
				throw new AssetTranslationException(
					string.Format(CoreStrings.Translators_TranslationFailed,
						INPUT_CODE_TYPE, OUTPUT_CODE_TYPE, assetVirtualPath, e.Message));
			}

			asset.Content = newContent;
			asset.Minified = enableNativeMinification;
		}
Ejemplo n.º 13
0
		/// <summary>
		/// Translates a code of assets written on Mustache to JS-code
		/// </summary>
		/// <param name="assets">Set of assets with code written on Mustache</param>
		/// <returns>Set of assets with translated code</returns>
		public override IList<IAsset> Translate(IList<IAsset> assets)
		{
			if (assets == null)
			{
				throw new ArgumentException(CoreStrings.Common_ValueIsEmpty, "assets");
			}

			if (assets.Count == 0)
			{
				return assets;
			}

			var assetsToProcessing = assets.Where(a => a.AssetTypeCode == Constants.AssetTypeCode.Mustache
				|| a.AssetTypeCode == Constants.AssetTypeCode.Hogan).ToList();
			if (assetsToProcessing.Count == 0)
			{
				return assets;
			}

			bool enableNativeMinification = NativeMinificationEnabled;
			CompilationOptions options = CreateCompilationOptions(enableNativeMinification);

			using (var hoganCompiler = new HoganCompiler(_createJsEngineInstance, options))
			{
				foreach (var asset in assetsToProcessing)
				{
					InnerTranslate(asset, hoganCompiler, enableNativeMinification);
				}
			}

			return assets;
		}
Ejemplo n.º 14
0
		/// <summary>
		/// Translates a code of asset written on Mustache to JS-code
		/// </summary>
		/// <param name="asset">Asset with code written on Mustache</param>
		/// <returns>Asset with translated code</returns>
		public override IAsset Translate(IAsset asset)
		{
			if (asset == null)
			{
				throw new ArgumentException(CoreStrings.Common_ValueIsEmpty, "asset");
			}

			bool enableNativeMinification = NativeMinificationEnabled;
			CompilationOptions options = CreateCompilationOptions(enableNativeMinification);

			using (var hoganCompiler = new HoganCompiler(_createJsEngineInstance, options))
			{
				InnerTranslate(asset, hoganCompiler, enableNativeMinification);
			}

			return asset;
		}