<commit_before>using System;

using RazorEngine;

namespace Mulder.Base.Filters
{
	public class RazorFilter : IFilter
	{
		public RazorFilter()
		{
		}

		public string Execute(string source, dynamic model)
		{
			return Razor.Parse(source, model);
		}
	}
}
<commit_msg>Supporting includes in Razor filter.<commit_after>using Microsoft.CSharp.RuntimeBinder;
using System;
using System.IO;

using RazorEngine;
using RazorEngine.Configuration;
using RazorEngine.Templating;

namespace Mulder.Base.Filters
{
	public class RazorFilter : IFilter
	{
		public RazorFilter()
		{
		}

		public string Execute(string source, dynamic model)
		{
			string includesPath = GetIncludesPath(model);

			var serviceConfig = new TemplateServiceConfiguration {
				Resolver = new IncludesResolver(includesPath)
			};

			Razor.SetTemplateService(new TemplateService(serviceConfig));

			return Razor.Parse(source, model);
		}

		string GetIncludesPath(dynamic model)
		{
			string includesPath = string.Empty;

			try {
				var layoutMeta = model.Layout;
				
				if (layoutMeta.ContainsKey("includes_path"))
					includesPath = layoutMeta["includes_path"].ToString();
			}
			catch (RuntimeBinderException e) {
				// Just ignore the exception if it doesn't exist
			}
			
			return includesPath;
		}
	}

	class IncludesResolver : ITemplateResolver
	{
		string root;
		
		public IncludesResolver(string root)
		{
			this.root = root;
		}
		
		public string Resolve(string name)
		{
			string includesPath = Path.Combine(root, name);
			bool exists = false;

			foreach (var ext in new[] { ".cshtml", ".html", ".htm" }) {
				var testPath = String.Concat(includesPath, ext);
				exists = File.Exists(testPath);
				if (exists) {
					includesPath = testPath;
					break;
				}
			}

			return exists ? File.ReadAllText(includesPath) : String.Empty;
		}
	}
}

{'fraction_non_alphanumeric': 0.0989668297988037, 'fraction_numerical': 0.0, 'mean_word_length': 3.56575682382134, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7305632', 'n_tokens_mistral': 684, 'n_tokens_neox': 565, 'n_words': 137}