Skip to content
This repository has been archived by the owner on Jun 25, 2020. It is now read-only.

Commit

Permalink
Merge pull request #18 from shiftkey/templating-start
Browse files Browse the repository at this point in the history
Some more templating work
  • Loading branch information
Paul Jenkins committed Feb 1, 2012
2 parents cd92432 + 867e0a6 commit 092902a
Show file tree
Hide file tree
Showing 11 changed files with 135 additions and 30 deletions.
3 changes: 3 additions & 0 deletions src/Pretzel.Logic/Pretzel.Logic.csproj
Expand Up @@ -46,6 +46,7 @@
<HintPath>..\packages\MarkdownDeep.NET.1.4\lib\.NetFramework 3.5\MarkdownDeep.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.Composition" />
<Reference Include="System.Core" />
<Reference Include="System.Drawing" />
<Reference Include="System.IO.Abstractions">
Expand All @@ -70,6 +71,8 @@
<Compile Include="Minification\FileSystemExtensions.cs" />
<Compile Include="Minification\JsMinifier.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Templating\SiteEngineInfoAttribute.cs" />
<Compile Include="Templating\ISiteEngineInfo.cs" />
<Compile Include="Templating\ISiteEngine.cs" />
<Compile Include="Templating\Jekyll\JekyllEngine.cs" />
<Compile Include="Templating\Jekyll\JekyllExtensions.cs" />
Expand Down
3 changes: 3 additions & 0 deletions src/Pretzel.Logic/Templating/ISiteEngine.cs
@@ -1,10 +1,13 @@
using System.ComponentModel.Composition;
using System.IO.Abstractions;
using Pretzel.Logic.Templating.Jekyll;

namespace Pretzel.Logic.Templating
{
[InheritedExport]
public interface ISiteEngine
{
bool CanProcess(IFileSystem fileSystem, string directory);
void Initialize(IFileSystem fileSystem, SiteContext context);
void Process();
}
Expand Down
7 changes: 7 additions & 0 deletions src/Pretzel.Logic/Templating/ISiteEngineInfo.cs
@@ -0,0 +1,7 @@
namespace Pretzel.Logic.Templating
{
public interface ISiteEngineInfo
{
string Engine { get; }
}
}
18 changes: 15 additions & 3 deletions src/Pretzel.Logic/Templating/Jekyll/JekyllEngine.cs
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.IO;
using System.IO.Abstractions;
using DotLiquid;
Expand All @@ -7,6 +8,8 @@

namespace Pretzel.Logic.Templating.Jekyll
{
[PartCreationPolicy(CreationPolicy.Shared)]
[SiteEngineInfo(Engine = "jekyll")]
public class JekyllEngine : ISiteEngine
{
private static readonly Markdown Markdown = new Markdown();
Expand All @@ -26,10 +29,14 @@ public void Process()

var outputFile = Path.Combine(outputDirectory, relativePath);

var directory = Path.GetDirectoryName(outputFile);
if (!fileSystem.Directory.Exists(directory))
fileSystem.Directory.CreateDirectory(directory);

var extension = Path.GetExtension(file);
if (extension.IsImageFormat())
{
fileSystem.File.Copy(file, outputFile);
fileSystem.File.Copy(file, outputFile, true);
continue;
}

Expand All @@ -44,7 +51,6 @@ public void Process()
// markdown file should not be treated differently
// output from markdown file should be sent to template pipeline


if (extension.IsMarkdownFile())
{
outputFile = outputFile.Replace(extension, ".html");
Expand All @@ -55,7 +61,7 @@ public void Process()
}
else
{
RenderTemplate(inputFile, outputFile);
RenderTemplate(inputFile.ExcludeHeader(), outputFile);
}
}
}
Expand Down Expand Up @@ -115,6 +121,12 @@ private static string RenderTemplate(string templateContents, Hash data)
return template.Render(data);
}

public bool CanProcess(IFileSystem fileSystem, string directory)
{
var configPath = Path.Combine(directory, "_config.yml");
return fileSystem.File.Exists(configPath);
}

public void Initialize(IFileSystem fileSystem, SiteContext context)
{
this.fileSystem = fileSystem;
Expand Down
16 changes: 16 additions & 0 deletions src/Pretzel.Logic/Templating/SiteEngineInfoAttribute.cs
@@ -0,0 +1,16 @@
using System;
using System.ComponentModel.Composition;

namespace Pretzel.Logic.Templating
{
[MetadataAttribute]
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public sealed class SiteEngineInfoAttribute : ExportAttribute
{
public string Engine { get; set; }

public SiteEngineInfoAttribute() : base(typeof(ISiteEngine))
{
}
}
}
3 changes: 2 additions & 1 deletion src/Pretzel.Tests/Pretzel.Tests.csproj
Expand Up @@ -58,8 +58,9 @@
<Compile Include="Recipe\RecipeTests.cs" />
<Compile Include="SampleTest.cs" />
<Compile Include="Templating\Jekyll\BakingEnvironment.cs" />
<Compile Include="Templating\Jekyll\LiquidEngineTests.cs" />
<Compile Include="Templating\Jekyll\JekyllEngineTests.cs" />
<Compile Include="Templating\Jekyll\LiquidExtensionsTests.cs" />
<Compile Include="Templating\Jekyll\StringTestExtensions.cs" />
<Compile Include="YamlExtensionsTests.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
Expand Up @@ -4,15 +4,7 @@

namespace Pretzel.Tests.Templating.Jekyll
{
public static class TestExtensions
{
public static string RemoveWhiteSpace(this string s)
{
return s.Replace("\r\n", "").Replace("\n", "");
}
}

public class LiquidEngineTests
public class JekyllEngineTests
{
public class When_Recieving_A_Folder_Containing_One_File : BakingEnvironment<JekyllEngine>
{
Expand Down Expand Up @@ -50,7 +42,6 @@ public void The_File_Is_Identical()
}
}


public class When_Recieving_A_Folder_Without_A_Trailing_Slash : BakingEnvironment<JekyllEngine>
{
const string FileContents = "<html><head></head><body></body></html>";
Expand Down Expand Up @@ -279,5 +270,29 @@ public void The_Folder_Should_Be_Ignored()
Assert.False(FileSystem.File.Exists(@"C:\website\_site\.gems\file.txt"));
}
}

public class When_Aeoth_Tests_The_Edge_Cases_Of_Handling_YAML_Front_Matter : BakingEnvironment<JekyllEngine>
{
const string PageContents = "---\n---";

public override JekyllEngine Given()
{
return new JekyllEngine();
}

public override void When()
{
FileSystem.AddFile(@"C:\website\file.txt", new MockFileData(PageContents));
var context = new SiteContext { Folder = @"C:\website\", Title = "My Web Site" };
Subject.Initialize(FileSystem, context);
Subject.Process();
}

[Fact]
public void The_Yaml_Matter_Should_Be_Cleared()
{
Assert.Equal("", FileSystem.File.ReadAllText(@"C:\website\_site\file.txt"));
}
}
}
}
10 changes: 10 additions & 0 deletions src/Pretzel.Tests/Templating/Jekyll/StringTestExtensions.cs
@@ -0,0 +1,10 @@
namespace Pretzel.Tests.Templating.Jekyll
{
public static class StringTestExtensions
{
public static string RemoveWhiteSpace(this string s)
{
return s.Replace("\r\n", "").Replace("\n", "");
}
}
}
52 changes: 40 additions & 12 deletions src/Pretzel/Commands/BakeCommand.cs
@@ -1,16 +1,23 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.IO;
using System.IO.Abstractions;
using NDesk.Options;
using Pretzel.Logic.Templating;
using Pretzel.Logic.Templating.Jekyll;

namespace Pretzel.Commands
{
[PartCreationPolicy(CreationPolicy.Shared)]
[CommandInfo(CommandName = "bake")]
public sealed class BakeCommand : ICommand
public sealed class BakeCommand : ICommand, IPartImportsSatisfiedNotification
{
private Dictionary<string, ISiteEngine> engineMap;

[ImportMany]
private Lazy<ISiteEngine, ISiteEngineInfo>[] Engines { get; set; }

public string Path { get; private set; }
public string Engine { get; private set; }
public bool Debug { get; private set; }
Expand All @@ -31,38 +38,59 @@ private OptionSet Settings
public void Execute(string[] arguments)
{
Settings.Parse(arguments);
Console.WriteLine("Path: " + Path);
Console.WriteLine("Engine: " + Engine);
Console.WriteLine("Debug: " + Debug);

var fileSystem = new FileSystem();

if (string.IsNullOrWhiteSpace(Path))
{
Path = Directory.GetCurrentDirectory();
}

if (string.IsNullOrWhiteSpace(Engine))
{
Engine = InferEngineFromDirectory(Path);
Engine = InferEngineFromDirectory(Path, fileSystem);
}

if (Engine.ToLower() == "jekyll")
ISiteEngine engine;

if (engineMap.TryGetValue(Engine, out engine))
{
var engine = new JekyllEngine();
var fileSystem = new FileSystem();
var context = new SiteContext { Folder = Path }; // TODO: process _config.yml file
var context = new SiteContext { Folder = Path };
engine.Initialize(fileSystem, context);
engine.Process();
}

else
{
System.Diagnostics.Debug.WriteLine("Cannot find engine for input: '{0}'", Engine);
}
}

private string InferEngineFromDirectory(string path)
private string InferEngineFromDirectory(string path, IFileSystem fileSystem)
{
return "jekyll"; // TODO: logic
foreach(var engine in engineMap)
{
if (!engine.Value.CanProcess(fileSystem, path)) continue;
System.Diagnostics.Debug.WriteLine("Recommended engine for directory: '{0}'", engine.Key);
return engine.Key;
}

return string.Empty;
}

public void WriteHelp(TextWriter writer)
{
Settings.WriteOptionDescriptions(writer);
}

public void OnImportsSatisfied()
{
engineMap = new Dictionary<string, ISiteEngine>(Engines.Length);

foreach (var command in Engines)
{
if (!engineMap.ContainsKey(command.Metadata.Engine))
engineMap.Add(command.Metadata.Engine, command.Value);
}
}
}
}
1 change: 1 addition & 0 deletions src/Pretzel/Commands/CommandCollection.cs
Expand Up @@ -5,6 +5,7 @@
namespace Pretzel.Commands
{
[Export]
[PartCreationPolicy(CreationPolicy.Shared)]
public sealed class CommandCollection : IPartImportsSatisfiedNotification
{
[ImportMany]
Expand Down
17 changes: 13 additions & 4 deletions src/Pretzel/Program.cs
@@ -1,9 +1,11 @@
using System.ComponentModel.Composition;
using System;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using Pretzel.Commands;

using Pretzel.Logic.Templating;

namespace Pretzel
{
Expand All @@ -30,12 +32,19 @@ public void Run(string[] args)
var commandName = args[0];
var commandArgs = args.Skip(1).ToArray();
Commands[commandName].Execute(commandArgs);
WaitForClose();
}

[Conditional("DEBUG")]
public void WaitForClose()
{
Console.ReadLine();
}

public void Compose()
{
var catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());
var container = new CompositionContainer(catalog);
var first = new AssemblyCatalog(Assembly.GetExecutingAssembly());
var container = new CompositionContainer(first);
container.ComposeParts(this);
}
}
Expand Down

0 comments on commit 092902a

Please sign in to comment.