Skip to content

Commit

Permalink
Merge branch 'razor-feature' into unstable. Fixes #1
Browse files Browse the repository at this point in the history
  • Loading branch information
dragan committed Sep 22, 2012
2 parents 588a46a + 4c79dfd commit 5fd7bf3
Show file tree
Hide file tree
Showing 26 changed files with 430 additions and 106 deletions.
2 changes: 1 addition & 1 deletion src/Mulder.Acceptance.Tests/Resources/DefaultSite/Rules
Expand Up @@ -30,7 +30,7 @@ Route("/stylesheet/", (context) => {
Route("*", (context) => {
if (context.Item.IsBinary) {
// Write item with identifier /foo/ to /foo.ext
return context.Item.Identifier.Chop() + context.Item.Meta["extension"];
return context.Item.Identifier.Chop() + context.Item.Meta.Extension;
} else {
// Write item with identifier /foo/ to /foo/index.html
return context.Item.Identifier + "index.html";
Expand Down
27 changes: 15 additions & 12 deletions src/Mulder.Base/Compilation/Compiler.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;

using Mulder.Base.Domain;
Expand Down Expand Up @@ -91,12 +92,13 @@ void ExecuteFilters(StaticFile staticFile, Site site)
IFilter filter = filterFactory.CreateFilter(filterName);

string source = staticFile.GetLastSnapShot();
var arguments = new Dictionary<string, object> {
{ "configuration", site.Configuration as IDictionary<string, object> },
{ "item", staticFile.Item.Meta }

var filterContext = new FilterContext {
Configuration = site.Configuration,
Item = staticFile.Item.Meta
};

string result = filter.Execute(source, arguments);
string result = filter.Execute(source, filterContext);

staticFile.CreateSnapShot(result);
}
Expand All @@ -108,15 +110,16 @@ void ExecuteLayoutFilter(StaticFile staticFile, Site site)
var layoutRule = site.GetLayoutRuleFor(staticFile.Layout);

IFilter filter = filterFactory.CreateFilter(layoutRule.FilterName);
var arguments = new Dictionary<string, object> {
{ "configuration", site.Configuration as IDictionary<string, object> },
{ "layout", staticFile.Layout.Meta },
{ "item", staticFile.Item.Meta },
{ "content", staticFile.GetLastSnapShot() }

var filterContext = new FilterContext {
Configuration = site.Configuration,
Layout = staticFile.Layout.Meta,
Item = staticFile.Item.Meta,
Content = staticFile.GetLastSnapShot()
};
string result = filter.Execute(staticFile.Layout.Content, arguments);


string result = filter.Execute(staticFile.Layout.Content, filterContext);
staticFile.CreateSnapShot(result);
}
}
Expand Down
14 changes: 14 additions & 0 deletions src/Mulder.Base/Compilation/FilterContext.cs
@@ -0,0 +1,14 @@
using System;

using Mulder.Base.Domain;

namespace Mulder.Base.Compilation
{
public class FilterContext
{
public IConfiguration Configuration { get; set; }
public dynamic Layout { get; set; }
public dynamic Item { get; set; }
public string Content { get; set; }
}
}
2 changes: 2 additions & 0 deletions src/Mulder.Base/Compilation/FilterFactory.cs
Expand Up @@ -25,6 +25,8 @@ public IFilter CreateFilter(string filterName)
return new MarkdownFilter();
case Filters.LESS:
return new LessFilter(fileSystem);
case Filters.RAZOR:
return new RazorFilter();
default:
return null;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Mulder.Base/Compilation/Filters.cs
Expand Up @@ -6,6 +6,7 @@ public enum Filters
{
LIQUID,
MARKDOWN,
LESS
LESS,
RAZOR
}
}
23 changes: 12 additions & 11 deletions src/Mulder.Base/DataSources/FileSystemUnified.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.IO;
using System.Linq;
using System.Text;
Expand Down Expand Up @@ -153,13 +154,12 @@ List<FileObject> GetFileObjects(string path)
bool isBinary = IsBinary(contentFileName);

ParsedFileData parsedFileData = !isBinary
? ParseFiles(contentFileName, metaFileName) : new ParsedFileData { Content = "", Meta = new Dictionary<string, object>() };
? ParseFiles(contentFileName, metaFileName) : new ParsedFileData { Content = "", Meta = new ExpandoObject() };

IDictionary<string, object> meta = new Dictionary<string, object>(parsedFileData.Meta) {
{ "filename", contentFileName },
{ "meta_filename", metaFileName},
{ "extension", contentFileExtension }
};
dynamic meta = parsedFileData.Meta;
meta.Filename = contentFileName;
meta.MetaFilename = metaFileName;
meta.Extension = contentFileExtension;

string contentFileNameWithoutPath = contentFileName.RemovePathFromFileName(path);
string identifier = GetIdentifierForFileName(contentFileNameWithoutPath);
Expand Down Expand Up @@ -234,17 +234,18 @@ bool IsBinary(string fileName)

ParsedFileData ParseFiles(string rawContentFileName, string metaFileName)
{
IDictionary<string, object> meta = null;
dynamic meta = null;
string fileContent = string.Empty;

// If we have a stand alone meta file
if (!string.IsNullOrEmpty(metaFileName)) {
fileContent = !string.IsNullOrEmpty(rawContentFileName) ? fileSystem.ReadStringFromFile(rawContentFileName) : string.Empty;
meta = fileSystem.ReadStringFromFile(metaFileName).DeserializeYaml()[0] as IDictionary<string, object>;
var yaml = fileSystem.ReadStringFromFile(metaFileName).DeserializeYaml()[0] as IDictionary<string, object>;
meta = yaml.ToExpando();
}
else {
string rawFileContent = !string.IsNullOrEmpty(rawContentFileName) ? fileSystem.ReadStringFromFile(rawContentFileName) : string.Empty;
meta = rawFileContent.DeserializeYamlHeader();
meta = rawFileContent.DeserializeYamlHeader().ToExpando();
fileContent = rawFileContent.ExcludeYamlHeader();
}

Expand Down Expand Up @@ -301,14 +302,14 @@ class FileObject
public string Identifier { get; set; }
public bool IsBinary { get; set; }
public string Content { get; set; }
public IDictionary<string, object> Meta { get; set; }
public dynamic Meta { get; set; }
public DateTime ModificationTime { get; set; }
}

class ParsedFileData
{
public string Content { get; set; }
public IDictionary<string, object> Meta { get; set; }
public dynamic Meta { get; set; }
}
}
}
2 changes: 1 addition & 1 deletion src/Mulder.Base/Domain/ISourceFile.cs
Expand Up @@ -7,7 +7,7 @@ public interface ISourceFile
{
string Identifier { get; }
string Content { get; }
IDictionary<string, object> Meta { get; }
dynamic Meta { get; }
DateTime ModificationTime { get; }
}
}
16 changes: 9 additions & 7 deletions src/Mulder.Base/Domain/Item.cs
Expand Up @@ -8,21 +8,21 @@ public class Item : ISourceFile, IEquatable<Item>
readonly string identifier;
readonly bool isBinary;
readonly string content;
readonly IDictionary<string, object> meta;
readonly dynamic meta;
readonly DateTime modificationTime;
readonly List<Item> children;
readonly List<StaticFile> staticFiles;

public string Identifier { get { return identifier; } }
public bool IsBinary { get { return isBinary; } }
public string Content { get { return content; } }
public IDictionary<string, object> Meta { get { return meta; } }
public dynamic Meta { get { return meta; } }
public DateTime ModificationTime { get { return modificationTime; } }
public Item Parent { get; set; }
public IEnumerable<Item> Children { get { return children; } }
public IEnumerable<StaticFile> StaticFiles { get { return staticFiles; } }

public Item(string identifier, bool isBinary, string content, IDictionary<string, object> meta, DateTime modificationTime)
public Item(string identifier, bool isBinary, string content, dynamic meta, DateTime modificationTime)
{
this.identifier = identifier;
this.isBinary = isBinary;
Expand Down Expand Up @@ -53,7 +53,7 @@ public override string ToString()
stringBuilder.AppendLine("IsBinary: \"" + isBinary + "\"");
stringBuilder.AppendLine("Content: \"" + content + "\"");
stringBuilder.AppendLine("Meta:");
foreach (var kvp in meta) {
foreach (var kvp in meta as IDictionary<string, object>) {
stringBuilder.AppendLine(" " + kvp.Key + ": \"" + kvp.Value.ToString() + "\"");
}

Expand Down Expand Up @@ -88,9 +88,11 @@ public bool Equals(Item other)
return false;

bool metasAreEqual = true;
if (meta.Count == other.meta.Count) {
foreach (var kvp in meta) {
if (other.meta[kvp.Key].ToString() == kvp.Value.ToString())
var metaDictionary = meta as IDictionary<string, object>;
var otherMetaDictionary = other.meta as IDictionary<string, object>;
if (metaDictionary.Count == otherMetaDictionary.Count) {
foreach (var kvp in metaDictionary) {
if (otherMetaDictionary[kvp.Key].ToString() == kvp.Value.ToString())
continue;

metasAreEqual = false;
Expand Down
16 changes: 9 additions & 7 deletions src/Mulder.Base/Domain/Layout.cs
Expand Up @@ -7,15 +7,15 @@ public class Layout : ISourceFile, IEquatable<Layout>
{
readonly string identifier;
readonly string content;
readonly IDictionary<string, object> meta;
readonly dynamic meta;
readonly DateTime modificationTime;

public string Identifier { get { return identifier; } }
public string Content { get { return content; } }
public IDictionary<string, object> Meta { get { return meta; } }
public dynamic Meta { get { return meta; } }
public DateTime ModificationTime { get { return modificationTime; } }

public Layout(string identifier, string content, IDictionary<string, object> meta, DateTime modificationTime)
public Layout(string identifier, string content, dynamic meta, DateTime modificationTime)
{
this.identifier = identifier;
this.content = content;
Expand All @@ -31,7 +31,7 @@ public override string ToString()
stringBuilder.AppendLine("Identifier: \"" + identifier + "\"");
stringBuilder.AppendLine("Content: \"" + content + "\"");
stringBuilder.AppendLine("Meta:");
foreach (var kvp in meta) {
foreach (var kvp in meta as IDictionary<string, object>) {
stringBuilder.AppendLine(" " + kvp.Key + ": \"" + kvp.Value.ToString() + "\"");
}

Expand Down Expand Up @@ -64,9 +64,11 @@ public bool Equals(Layout other)
return false;

bool metasAreEqual = true;
if (meta.Count == other.meta.Count) {
foreach (var kvp in meta) {
if (other.meta[kvp.Key].ToString() == kvp.Value.ToString())
var metaDictionary = meta as IDictionary<string, object>;
var otherMetaDictionary = other.meta as IDictionary<string, object>;
if (metaDictionary.Count == otherMetaDictionary.Count) {
foreach (var kvp in metaDictionary) {
if (otherMetaDictionary[kvp.Key].ToString() == kvp.Value.ToString())
continue;

metasAreEqual = false;
Expand Down
42 changes: 42 additions & 0 deletions src/Mulder.Base/Extensions/DictionaryExtensions.cs
@@ -0,0 +1,42 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Dynamic;

namespace Mulder.Base.Extensions
{
public static class DictionaryExtensions
{
public static ExpandoObject ToExpando(this IDictionary<string, object> dictionary)
{
var expando = new ExpandoObject();
var expandoDictionary = (IDictionary<string, object>)expando;

foreach (var kvp in dictionary) {
if (kvp.Value is IDictionary<string, object>) {
var expandoValue = ((IDictionary<string, object>)kvp.Value).ToExpando();
expandoDictionary.Add(kvp.Key, expandoValue);
}
else if (kvp.Value is ICollection) {
var itemList = new List<object>();
foreach (var item in (ICollection)kvp.Value) {
if (item is IDictionary<string, object>) {
var expandoItem = ((IDictionary<string, object>) item).ToExpando();
itemList.Add(expandoItem);
}
else {
itemList.Add(item);
}
}

expandoDictionary.Add(kvp.Key, itemList);
}
else {
expandoDictionary.Add(kvp);
}
}

return expando;
}
}
}
5 changes: 3 additions & 2 deletions src/Mulder.Base/Filters/IFilter.cs
@@ -1,10 +1,11 @@
using System;
using System.Collections.Generic;

using Mulder.Base.Compilation;

namespace Mulder.Base.Filters
{
public interface IFilter
{
string Execute(string source, IDictionary<string, object> arguments);
string Execute(string source, FilterContext filterContext);
}
}
5 changes: 3 additions & 2 deletions src/Mulder.Base/Filters/LessFilter.cs
Expand Up @@ -8,6 +8,7 @@
using dotless.Core.Parser;
using dotless.Core.Stylizers;

using Mulder.Base.Compilation;
using Mulder.Base.IO;

namespace Mulder.Base.Filters
Expand All @@ -21,9 +22,9 @@ public LessFilter(IFileSystem fileSystem)
this.fileSystem = fileSystem;
}

public string Execute(string source, IDictionary<string, object> arguments)
public string Execute(string source, FilterContext filterContext)
{
var item = arguments["item"] as IDictionary<string, object>;
var item = filterContext.Item as IDictionary<string, object>;
string path = Path.GetDirectoryName(item["filename"].ToString());

string result = Transform(source, path);
Expand Down

0 comments on commit 5fd7bf3

Please sign in to comment.