Skip to content

Commit

Permalink
Merge pull request #39 from solidify-project/feature/model
Browse files Browse the repository at this point in the history
Feature/model
  • Loading branch information
boyko-ant authored Jul 11, 2018
2 parents c6a739a + a92a828 commit 87e9afc
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 3 deletions.
84 changes: 84 additions & 0 deletions src/SolidifyProject.Engine.Infrastructure/Models/PageModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public sealed class PageModel : TextContentModel

private static readonly string[] CUSTOM_ATTRIBUTE_PREFIX_SEPARATOR = {"."};
private static readonly string[] CUSTOM_ATTRIBUTE_PREFIX = {"Custom"};

private static readonly string[] MODEL_ATTRIBUTE_PREFIX = {"Model"};

public string Title { get; set; }
public string Url { get; set; }
Expand All @@ -39,10 +41,13 @@ public sealed class PageModel : TextContentModel
/// Raw content after separator
/// </summary>
public string Content { get; set; }

public dynamic Model { get; set; }

public override void Parse()
{
Custom = new ExpandoObject();
Model = new ExpandoObject();

var lines = ContentRaw.Split(END_OF_LINE, StringSplitOptions.None);

Expand All @@ -60,6 +65,11 @@ public override void Parse()

}

public void MapDataToModel(ExpandoObject data)
{
mapDataToPageModel(Model, data);
}

private void ParseAttributeLine(string line)
{
var attribute = line.Split(ATTRIBUTE_SEPARATORS, StringSplitOptions.None);
Expand Down Expand Up @@ -108,6 +118,30 @@ private void ParseAttributeLine(string line)
ParseCustomAttribute(Custom, customAttributeNames.Skip(1), attributeValue);
}
else
{
if (customAttributeNames.Length >= 2 && MODEL_ATTRIBUTE_PREFIX.Any(x =>
x.Equals(customAttributeNames[0], StringComparison.InvariantCultureIgnoreCase)))
{
ParseCustomAttribute(Model, customAttributeNames.Skip(1), attributeValue);
}
else
{
throw new ArgumentException(
$"Unknown name format of custom attribute \"{attributeName}\" at line \"{line}\"");
}
}

return;
}

if (CUSTOM_ATTRIBUTE_PREFIX_SEPARATOR.Any(x => attributeName.Contains(x)))
{
var modelAttributeNames = attributeName.Split(CUSTOM_ATTRIBUTE_PREFIX_SEPARATOR, StringSplitOptions.RemoveEmptyEntries);
if (modelAttributeNames.Length >= 2 && MODEL_ATTRIBUTE_PREFIX.Any(x => x.Equals(modelAttributeNames[0], StringComparison.InvariantCultureIgnoreCase)))
{
ParseCustomAttribute(Model, modelAttributeNames.Skip(1), attributeValue);
}
else
{
throw new ArgumentException($"Unknown name format of custom attribute \"{attributeName}\" at line \"{line}\"");
}
Expand Down Expand Up @@ -142,5 +176,55 @@ private void ParseContent(IEnumerable<string> lines)
{
Content = string.Join("\r\n", lines);
}

private void mapDataToPageModel(ExpandoObject model, ExpandoObject data)
{
IDictionary<string, object> modelDict = model;
foreach (var keyValuePair in model)
{
if (keyValuePair.Value is ExpandoObject expObject)
{
mapDataToPageModel(expObject, data);
}
else
{
modelDict[keyValuePair.Key] = getValueFromDataObject(keyValuePair.Value as string, data);
}
}
}

private object getValueFromDataObject(string path, ExpandoObject data)
{
var attributeNames = path.Split('.');
if (attributeNames.Length == 0)
{
return null;
}

if (attributeNames.First() == "Data")
{
if (attributeNames.Length == 1)
{
return null;
}

attributeNames = attributeNames.Skip(1).ToArray();
}

object value = data;
foreach (var attribute in attributeNames)
{
if (value is IDictionary<string,object> dict)
{
value = dict[attribute];
}
else
{
return null;
}
}

return value;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ private async Task ProcessPageByIdAsync(string pageId, ExpandoObject dataModel)
await LoggerService.WriteLogMessage($"{DateTime.Now.ToLongTimeString()}: [Page:Started] \"{pageId}\"");

var page = await PageModelReaderService.LoadContentByIdAsync(pageId).ConfigureAwait(false);


page.MapDataToModel(dataModel);

var html = await TemplateService.RenderTemplateAsync(page.Content, page, dataModel);
html = await MarkupService.RenderMarkupAsync(html).ConfigureAwait(false);
page.Content = html;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Nustache.Core;
using SolidifyProject.Engine.Infrastructure.Interfaces;
Expand Down Expand Up @@ -30,7 +32,8 @@ public Task<string> RenderTemplateAsync(string template, PageModel pageModel, Ex
throw new ArgumentNullException(nameof(pageModel));
}

var model = new { Page = pageModel, Data = dataModel };

var model = new { Page = pageModel, Data = dataModel, Model = pageModel.Model };

var result = Render.StringToString(template, model, getTemplate, new RenderContextBehaviour
{
Expand Down Expand Up @@ -62,5 +65,6 @@ private Template getTemplate(string key)

return task.Result;
}

}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<div>
<i>{{ Data.disclaimer }}</i>
<i>{{ Model.disclaimer }}</i>
</div>
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
url: pages/about-me.html
template: default.hjs
Model.disclaimer: Data.disclaimer

title: Solidify Project - About ME

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
url: index.html
template: default.hjs
Model.disclaimer: Data.disclaimer

title: Static Site Generator - Home

Expand Down

0 comments on commit 87e9afc

Please sign in to comment.