Skip to content

Commit 87e9afc

Browse files
authored
Merge pull request #39 from solidify-project/feature/model
Feature/model
2 parents c6a739a + a92a828 commit 87e9afc

6 files changed

Lines changed: 95 additions & 3 deletions

File tree

src/SolidifyProject.Engine.Infrastructure/Models/PageModel.cs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ public sealed class PageModel : TextContentModel
2020

2121
private static readonly string[] CUSTOM_ATTRIBUTE_PREFIX_SEPARATOR = {"."};
2222
private static readonly string[] CUSTOM_ATTRIBUTE_PREFIX = {"Custom"};
23+
24+
private static readonly string[] MODEL_ATTRIBUTE_PREFIX = {"Model"};
2325

2426
public string Title { get; set; }
2527
public string Url { get; set; }
@@ -39,10 +41,13 @@ public sealed class PageModel : TextContentModel
3941
/// Raw content after separator
4042
/// </summary>
4143
public string Content { get; set; }
44+
45+
public dynamic Model { get; set; }
4246

4347
public override void Parse()
4448
{
4549
Custom = new ExpandoObject();
50+
Model = new ExpandoObject();
4651

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

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

6166
}
6267

68+
public void MapDataToModel(ExpandoObject data)
69+
{
70+
mapDataToPageModel(Model, data);
71+
}
72+
6373
private void ParseAttributeLine(string line)
6474
{
6575
var attribute = line.Split(ATTRIBUTE_SEPARATORS, StringSplitOptions.None);
@@ -108,6 +118,30 @@ private void ParseAttributeLine(string line)
108118
ParseCustomAttribute(Custom, customAttributeNames.Skip(1), attributeValue);
109119
}
110120
else
121+
{
122+
if (customAttributeNames.Length >= 2 && MODEL_ATTRIBUTE_PREFIX.Any(x =>
123+
x.Equals(customAttributeNames[0], StringComparison.InvariantCultureIgnoreCase)))
124+
{
125+
ParseCustomAttribute(Model, customAttributeNames.Skip(1), attributeValue);
126+
}
127+
else
128+
{
129+
throw new ArgumentException(
130+
$"Unknown name format of custom attribute \"{attributeName}\" at line \"{line}\"");
131+
}
132+
}
133+
134+
return;
135+
}
136+
137+
if (CUSTOM_ATTRIBUTE_PREFIX_SEPARATOR.Any(x => attributeName.Contains(x)))
138+
{
139+
var modelAttributeNames = attributeName.Split(CUSTOM_ATTRIBUTE_PREFIX_SEPARATOR, StringSplitOptions.RemoveEmptyEntries);
140+
if (modelAttributeNames.Length >= 2 && MODEL_ATTRIBUTE_PREFIX.Any(x => x.Equals(modelAttributeNames[0], StringComparison.InvariantCultureIgnoreCase)))
141+
{
142+
ParseCustomAttribute(Model, modelAttributeNames.Skip(1), attributeValue);
143+
}
144+
else
111145
{
112146
throw new ArgumentException($"Unknown name format of custom attribute \"{attributeName}\" at line \"{line}\"");
113147
}
@@ -142,5 +176,55 @@ private void ParseContent(IEnumerable<string> lines)
142176
{
143177
Content = string.Join("\r\n", lines);
144178
}
179+
180+
private void mapDataToPageModel(ExpandoObject model, ExpandoObject data)
181+
{
182+
IDictionary<string, object> modelDict = model;
183+
foreach (var keyValuePair in model)
184+
{
185+
if (keyValuePair.Value is ExpandoObject expObject)
186+
{
187+
mapDataToPageModel(expObject, data);
188+
}
189+
else
190+
{
191+
modelDict[keyValuePair.Key] = getValueFromDataObject(keyValuePair.Value as string, data);
192+
}
193+
}
194+
}
195+
196+
private object getValueFromDataObject(string path, ExpandoObject data)
197+
{
198+
var attributeNames = path.Split('.');
199+
if (attributeNames.Length == 0)
200+
{
201+
return null;
202+
}
203+
204+
if (attributeNames.First() == "Data")
205+
{
206+
if (attributeNames.Length == 1)
207+
{
208+
return null;
209+
}
210+
211+
attributeNames = attributeNames.Skip(1).ToArray();
212+
}
213+
214+
object value = data;
215+
foreach (var attribute in attributeNames)
216+
{
217+
if (value is IDictionary<string,object> dict)
218+
{
219+
value = dict[attribute];
220+
}
221+
else
222+
{
223+
return null;
224+
}
225+
}
226+
227+
return value;
228+
}
145229
}
146230
}

src/SolidifyProject.Engine.Infrastructure/Services/OrchestrationService.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ private async Task ProcessPageByIdAsync(string pageId, ExpandoObject dataModel)
5656
await LoggerService.WriteLogMessage($"{DateTime.Now.ToLongTimeString()}: [Page:Started] \"{pageId}\"");
5757

5858
var page = await PageModelReaderService.LoadContentByIdAsync(pageId).ConfigureAwait(false);
59-
59+
60+
page.MapDataToModel(dataModel);
61+
6062
var html = await TemplateService.RenderTemplateAsync(page.Content, page, dataModel);
6163
html = await MarkupService.RenderMarkupAsync(html).ConfigureAwait(false);
6264
page.Content = html;

src/SolidifyProject.Engine.Services/TemplateService/MustacheTemplateService.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Dynamic;
34
using System.IO;
5+
using System.Linq;
46
using System.Threading.Tasks;
57
using Nustache.Core;
68
using SolidifyProject.Engine.Infrastructure.Interfaces;
@@ -30,7 +32,8 @@ public Task<string> RenderTemplateAsync(string template, PageModel pageModel, Ex
3032
throw new ArgumentNullException(nameof(pageModel));
3133
}
3234

33-
var model = new { Page = pageModel, Data = dataModel };
35+
36+
var model = new { Page = pageModel, Data = dataModel, Model = pageModel.Model };
3437

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

6366
return task.Result;
6467
}
68+
6569
}
6670
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<div>
2-
<i>{{ Data.disclaimer }}</i>
2+
<i>{{ Model.disclaimer }}</i>
33
</div>

src/Test/SolidifyProject.Engine.Test.Integration/_TestData/Pages/about.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
url: pages/about-me.html
22
template: default.hjs
3+
Model.disclaimer: Data.disclaimer
34

45
title: Solidify Project - About ME
56

src/Test/SolidifyProject.Engine.Test.Integration/_TestData/Pages/home.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
url: index.html
22
template: default.hjs
3+
Model.disclaimer: Data.disclaimer
34

45
title: Static Site Generator - Home
56

0 commit comments

Comments
 (0)