Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds Sass template processing #439

Merged
merged 2 commits into from
May 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Wyam.sln
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Wyam.Hosting", "src\core\Wy
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Wyam.Hosting.Tests", "tests\core\Wyam.Hosting.Tests\Wyam.Hosting.Tests.csproj", "{9C59DA3D-6A41-4DC4-A4BB-0927C6978367}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Wyam.Sass", "src\extensions\Wyam.Sass\Wyam.Sass.csproj", "{90A0AB19-0146-4931-BEFA-D895B192E050}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Wyam.Sass.Tests", "tests\extensions\Wyam.Sass.Tests\Wyam.Sass.Tests.csproj", "{A6B7B1A6-7BE5-4E98-9286-D03A0F936E98}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "misc", "misc", "{9B01F618-6AB3-4DCD-A044-4CA58EEF6419}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
Expand Down Expand Up @@ -332,6 +336,14 @@ Global
{9C59DA3D-6A41-4DC4-A4BB-0927C6978367}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9C59DA3D-6A41-4DC4-A4BB-0927C6978367}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9C59DA3D-6A41-4DC4-A4BB-0927C6978367}.Release|Any CPU.Build.0 = Release|Any CPU
{90A0AB19-0146-4931-BEFA-D895B192E050}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{90A0AB19-0146-4931-BEFA-D895B192E050}.Debug|Any CPU.Build.0 = Debug|Any CPU
{90A0AB19-0146-4931-BEFA-D895B192E050}.Release|Any CPU.ActiveCfg = Release|Any CPU
{90A0AB19-0146-4931-BEFA-D895B192E050}.Release|Any CPU.Build.0 = Release|Any CPU
{A6B7B1A6-7BE5-4E98-9286-D03A0F936E98}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A6B7B1A6-7BE5-4E98-9286-D03A0F936E98}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A6B7B1A6-7BE5-4E98-9286-D03A0F936E98}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A6B7B1A6-7BE5-4E98-9286-D03A0F936E98}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -393,5 +405,7 @@ Global
{95080CB0-DC4E-4617-8F73-6E06A6CA2F5F} = {4813B17D-EFCA-401D-B0E6-7B24B3946A70}
{A3041B22-8A32-4ADB-89F3-A808AC60899B} = {4813B17D-EFCA-401D-B0E6-7B24B3946A70}
{9C59DA3D-6A41-4DC4-A4BB-0927C6978367} = {12931CA5-E734-4CD9-883D-27D897512183}
{90A0AB19-0146-4931-BEFA-D895B192E050} = {5A431149-2B88-40C3-9717-CA6CCF214317}
{A6B7B1A6-7BE5-4E98-9286-D03A0F936E98} = {2E5842F3-4797-4BB5-9A55-F3A005B434F8}
EndGlobalSection
EndGlobal
1 change: 1 addition & 0 deletions src/core/Wyam.Configuration/KnownExtensionGenerated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public partial class KnownExtension
public static readonly KnownExtension Markdown = new KnownExtension("Wyam.Markdown");
public static readonly KnownExtension Minification = new KnownExtension("Wyam.Minification");
public static readonly KnownExtension Razor = new KnownExtension("Wyam.Razor");
public static readonly KnownExtension Sass = new KnownExtension("Wyam.Sass");
public static readonly KnownExtension SearchIndex = new KnownExtension("Wyam.SearchIndex");
public static readonly KnownExtension Tables = new KnownExtension("Wyam.Tables");
public static readonly KnownExtension TextGeneration = new KnownExtension("Wyam.TextGeneration");
Expand Down
8 changes: 8 additions & 0 deletions src/extensions/Wyam.Sass/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

[assembly: AssemblyTitle("Wyam.Sass")]
[assembly: AssemblyDescription("")]
[assembly: ComVisible(false)]
[assembly: Guid("90a0ab19-0146-4931-befa-d895b192e050")]
153 changes: 153 additions & 0 deletions src/extensions/Wyam.Sass/Sass.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
using System;
using System.Collections.Generic;
using System.Linq;
using LibSass.Compiler;
using LibSass.Compiler.Options;
using Wyam.Common.Documents;
using Wyam.Common.Execution;
using Wyam.Common.IO;
using Wyam.Common.Meta;
using Wyam.Common.Modules;
using Wyam.Common.Tracing;
using Wyam.Common.Util;

namespace Wyam.Sass
{
/// <summary>
/// Compiles Sass CSS files to CSS stylesheets.
/// </summary>
/// <remarks>
/// The content of the input document is compiled to CSS and the content of the output document contains the compiled CSS stylesheet.
/// </remarks>
/// <example>
/// This is a pipeline that compiles two Sass CSS files, one for Bootstrap (which contains a lot of includes) and a second for custom CSS.
/// <code>
/// Pipelines.Add("Sass",
/// ReadFiles("master.scss"),
/// Concat(ReadFiles("foundation.scss")),
/// Sass().WithCompactOutputStyle(),
/// WriteFiles(".css")
/// );
/// </code>
/// </example>
/// <category>Templates</category>
public class Sass : IModule
{
private readonly List<string> _includePaths = new List<string>();
private bool _includeSourceComments = true;
private SassOutputStyle _outputStyle = SassOutputStyle.Compact;

/// <summary>
/// Adds a list of paths to search while processing includes.
/// </summary>
/// <param name="paths">The paths to include.</param>
/// <returns>The current instance.</returns>
public Sass WithIncludePaths(IEnumerable<string> paths)
{
_includePaths.AddRange(paths);
return this;
}

/// <summary>
/// Adds a path to search while processing includes.
/// </summary>
/// <param name="path">The path to include.</param>
/// <returns>The current instance.</returns>
public Sass WithIncludePath(string path)
{
_includePaths.Add(path);
return this;
}

/// <summary>
/// Sets whether the source comments are included
/// </summary>
/// <param name="includeSourceComments">The default value is <c>true</c></param>
/// <returns>The current instance.</returns>
public Sass IncludeSourceComments(bool includeSourceComments = true)
{
_includeSourceComments = includeSourceComments;
return this;
}

/// <summary>
/// Sets the output style to compact.
/// </summary>
/// <returns>The current instance.</returns>
public Sass WithCompactOutputStyle()
{
_outputStyle = SassOutputStyle.Compact;
return this;
}


/// <summary>
/// Sets the output style to expanded.
/// </summary>
/// <returns>The current instance.</returns>
public Sass WithExpandedOutputStyle()
{
_outputStyle = SassOutputStyle.Expanded;
return this;
}

/// <summary>
/// Sets the output style to compressed.
/// </summary>
/// <returns>The current instance.</returns>
public Sass WithCompressedOutputStyle()
{
_outputStyle = SassOutputStyle.Compressed;
return this;
}

/// <summary>
/// Sets the output style to nested.
/// </summary>
/// <returns>The current instance.</returns>
public Sass WithNestedOutputStyle()
{
_outputStyle = SassOutputStyle.Nested;
return this;
}

public IEnumerable<IDocument> Execute(IReadOnlyList<IDocument> inputs, IExecutionContext context)
{
return inputs.AsParallel().Select(context, input =>
{
Trace.Verbose("Processing Sass for {0}", input.SourceString());

FilePath path = input.FilePath(Keys.SourceFilePath);
SassOptions sassOptions = new SassOptions
{
Data = input.Content,
IncludeSourceComments = _includeSourceComments,
OutputStyle = _outputStyle,
IncludePaths = path == null
? _includePaths.ToArray()
: new List<string>(_includePaths) { path.Directory.FullPath }.ToArray()
};

SassCompiler sassCompiler = new SassCompiler(sassOptions);
SassResult sassResult ;
try
{
sassResult = sassCompiler.Compile();
}
catch (Exception ex)
{
Trace.Warning("Exception while compiling sass file {0}: {}", input.SourceString(), ex.ToString());
return context.GetDocument(input, input.SourceString());
}

if (sassResult.ErrorStatus != 0)
{
Trace.Warning("Exception while compiling sass file {0}: {1}", input.SourceString(), sassResult.ErrorMessage);
return context.GetDocument(input, input.SourceString());
}

return context.GetDocument(input, sassResult.Output);
});
}
}
}
79 changes: 79 additions & 0 deletions src/extensions/Wyam.Sass/Wyam.Sass.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{90A0AB19-0146-4931-BEFA-D895B192E050}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Wyam.Sass</RootNamespace>
<AssemblyName>Wyam.Sass</AssemblyName>
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="LibSass.NET, Version=4.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\..\packages\libsassnet.3.3.7\lib\net40\LibSass.NET.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Sass.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="..\..\..\SolutionInfo.cs">
<Link>Properties\SolutionInfo.cs</Link>
</Compile>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\core\Wyam.Common\Wyam.Common.csproj">
<Project>{F40B73E9-C0CC-465C-925E-B51E686C1D5C}</Project>
<Name>Wyam.Common</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\..\..\packages\libsassnet.3.3.7\build\libsassnet.targets" Condition="Exists('..\..\..\packages\libsassnet.3.3.7\build\libsassnet.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\..\..\packages\libsassnet.3.3.7\build\libsassnet.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\..\packages\libsassnet.3.3.7\build\libsassnet.targets'))" />
</Target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
18 changes: 18 additions & 0 deletions src/extensions/Wyam.Sass/Wyam.Sass.nuspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0"?>
<package >
<metadata>
<id>Wyam.Sass</id>
<version>$version$</version>
<title>Wyam.Sass</title>
<authors>Dave Glick</authors>
<owners>Dave Glick</owners>
<licenseUrl>https://github.com/Wyamio/Wyam/blob/master/LICENSE</licenseUrl>
<iconUrl>https://wyam.io/Content/images/logo-square-64.png</iconUrl>
<projectUrl>https://wyam.io</projectUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Wyam is a simple to use, highly modular, and extremely configurable static content generator. This library provides support for parsing Sass CSS content.</description>
<copyright>Copyright 2016</copyright>
<tags>Wyam Static StaticContent StaticSite Blog BlogEngine CSS Sass SassCSS</tags>
</metadata>
<files></files>
</package>
4 changes: 4 additions & 0 deletions src/extensions/Wyam.Sass/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="libsassnet" version="3.3.7" targetFramework="net462" />
</packages>
8 changes: 8 additions & 0 deletions tests/extensions/Wyam.Sass.Tests/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

[assembly: AssemblyTitle("Wyam.Sass.Tests")]
[assembly: AssemblyDescription("")]
[assembly: ComVisible(false)]
[assembly: Guid("a6b7b1a6-7be5-4e98-9286-d03a0f936e98")]
68 changes: 68 additions & 0 deletions tests/extensions/Wyam.Sass.Tests/SassFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System;
using System.Linq;
using NSubstitute;
using NUnit.Framework;
using Wyam.Common.Documents;
using Wyam.Common.Execution;
using Wyam.Testing;

namespace Wyam.Sass.Tests
{
[TestFixture]
[Parallelizable(ParallelScope.Self | ParallelScope.Children)]
public class SassFixture : BaseFixture
{
[Test]
public void Convert()
{
string input = @"
$font-stack: Helvetica, sans-serif;
$primary-color: #333;

body {
font: 100% $font-stack;
color: $primary-color;
}";

string output = "body { font: 100% Helvetica, sans-serif; color: #333; }\n";

IExecutionContext context = Substitute.For<IExecutionContext>();
IDocument document = Substitute.For<IDocument>();
document.Content.Returns(input);

Sass sass = new Sass().IncludeSourceComments(false).WithCompactOutputStyle();

// When
sass.Execute(new[] { document }, context).ToList(); // Make sure to materialize the result list

// Then
context.Received(1).GetDocument(Arg.Any<IDocument>(), Arg.Any<string>());
context.Received().GetDocument(document, output);
}

[Test]
public void ConvertingBadSassFails()
{
string input = @"
$font-stack: Helvetica, sans-serif
$primary-color: #333

body {
font: 100% $font-stack;
color: $primary-color;
}";

IExecutionContext context = Substitute.For<IExecutionContext>();
IDocument document = Substitute.For<IDocument>();
document.Content.Returns(input);

Sass sass = new Sass();

// That
Assert.Catch<AggregateException>(() =>
{
sass.Execute(new[] { document }, context).ToList(); // Make sure to materialize the result list
});
}
}
}
Loading