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

Code block support #206

Merged
merged 8 commits into from
Feb 15, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 8 additions & 2 deletions src/Pretzel.Logic/Extensions/YamlExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using YamlDotNet.RepresentationModel;
using System.IO;
using System.Linq;
using YamlDotNet.Serialization;

namespace Pretzel.Logic.Extensions
Expand Down Expand Up @@ -107,7 +109,11 @@ public static string ExcludeHeader(this string text)
if (m.Count == 0)
return text;

return text.Replace(m[0].Groups[0].Value, "").Trim();
text = text.Replace(m[0].Groups[0].Value, "");
var lines = text.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None).ToList();
while (lines.Any() && string.IsNullOrWhiteSpace(lines.First()))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be done in a more LINQ-friendly way?

var nonEmptyLine = lines.Where(x => !String.IsNullOrWhiteSpace(x));
return String.Join(Environment.NewLine, nonEmptyLines).TrimEnd();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't remove empty line in the middle of the text. My while removes only first empty lines. So, SkipWhile is needed. I will fix it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, 👍 to SkipWhile

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done (commit 0e5338f)

lines.RemoveAt(0);
return string.Join(Environment.NewLine, lines).TrimEnd();
}
}
}
58 changes: 58 additions & 0 deletions src/Pretzel.Logic/Helpers/MarkdownHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System.Text;
using System.Text.RegularExpressions;
using MarkdownDeep;

namespace Pretzel.Logic.Helpers
{
public static class MarkdownHelper
{
public static Markdown CreateMarkdown()
{
return new Markdown { FormatCodeBlock = FormatCodePrettyPrint };
}

// Original code: http://www.toptensoftware.com/Articles/83/MarkdownDeep-Syntax-Highlighting
public static Regex rxExtractLanguage = new Regex("^({{(.+)}}[\r\n])", RegexOptions.Compiled);

public static string FormatCodePrettyPrint(Markdown m, string code)
{
// Try to extract the language from the first line
var match = rxExtractLanguage.Match(code);
string language = null;

if (match.Success)
{
// Save the language
var g = (Group)match.Groups[2];
language = g.ToString();

// Remove the first line
code = code.Substring(match.Groups[1].Length);
}

// If not specified, look for a link definition called "default_syntax" and
// grab the language from its title
if (language == null)
{
var d = m.GetLinkDefinition("default_syntax");
if (d != null)
language = d.title;
}

// Common replacements
if (language == "C#")
language = "cs";
if (language == "C++")
language = "cpp";

// Wrap code in pre/code tags and add PrettyPrint attributes if necessary
if (string.IsNullOrEmpty(language))
return string.Format("<pre><code>{0}</code></pre>\n", code);
else
return string.Format("<pre class=\"prettyprint lang-{0}\"><code>{1}</code></pre>\n",
language.ToLowerInvariant(), code);
}


}
}
3 changes: 3 additions & 0 deletions src/Pretzel.Logic/Pretzel.Logic.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
<Compile Include="Extensions\StringExtensions.cs" />
<Compile Include="Extensions\Tracing.cs" />
<Compile Include="Extensions\YamlExtensions.cs" />
<Compile Include="Helpers\MarkdownHelper.cs" />
<Compile Include="Import\BloggerImport.cs" />
<Compile Include="Import\HtmlToMarkdownConverter.cs" />
<Compile Include="Import\WordpressImport.cs" />
Expand Down Expand Up @@ -211,6 +212,7 @@
<EmbeddedResource Include="Properties\RazorWiki.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>RazorWiki.Designer.cs</LastGenOutput>
<SubType>Designer</SubType>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
Expand Down Expand Up @@ -270,6 +272,7 @@
<Content Include="Resources\RazorWiki\default.css" />
<Content Include="Resources\RazorWiki\style.css" />
<Content Include="Resources\Style.css" />
<Content Include="Resources\run_prettify.js" />
</ItemGroup>
<ItemGroup>
<Folder Include="Templating\Plugins\" />
Expand Down
3 changes: 3 additions & 0 deletions src/Pretzel.Logic/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,7 @@
<data name="Style" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\resources\style.css;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="RunPrettify" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\run_prettify.js;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
</root>
6 changes: 6 additions & 0 deletions src/Pretzel.Logic/Properties/Resources1.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/Pretzel.Logic/Recipe/Recipe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public void Create()
fileSystem.File.WriteAllText(Path.Combine(directory, @"about.md"), Properties.Razor.About);
fileSystem.File.WriteAllText(Path.Combine(directory, string.Format(@"_posts\{0}-myfirstpost.md", DateTime.Today.ToString("yyyy-MM-dd"))), Properties.Razor.FirstPost);
fileSystem.File.WriteAllText(Path.Combine(directory, @"css\style.css"), Properties.Resources.Style);
fileSystem.File.WriteAllText(Path.Combine(directory, @"js\run_prettify.js"), Properties.Resources.RunPrettify);
fileSystem.File.WriteAllText(Path.Combine(directory, @"_config.yml"), Properties.Razor.Config);
CreateImages();
}
Expand All @@ -83,6 +84,7 @@ public void Create()
fileSystem.File.WriteAllText(Path.Combine(directory, @"about.md"), Properties.Liquid.About);
fileSystem.File.WriteAllText(Path.Combine(directory, string.Format(@"_posts\{0}-myfirstpost.md", DateTime.Today.ToString("yyyy-MM-dd"))), Properties.Liquid.FirstPost);
fileSystem.File.WriteAllText(Path.Combine(directory, @"css\style.css"), Properties.Resources.Style);
fileSystem.File.WriteAllText(Path.Combine(directory, @"js\run_prettify.js"), Properties.Resources.RunPrettify);
fileSystem.File.WriteAllText(Path.Combine(directory, @"_config.yml"), Properties.Liquid.Config);

CreateImages();
Expand Down Expand Up @@ -161,6 +163,7 @@ private void CreateDirectories()
fileSystem.Directory.CreateDirectory(Path.Combine(directory, @"_layouts"));
fileSystem.Directory.CreateDirectory(Path.Combine(directory, @"css"));
fileSystem.Directory.CreateDirectory(Path.Combine(directory, @"img"));
fileSystem.Directory.CreateDirectory(Path.Combine(directory, @"js"));
}
}
}
7 changes: 7 additions & 0 deletions src/Pretzel.Logic/Resources/Liquid/FirstPost.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,11 @@ comments: true

## Hello world...

{{C#}}
static void Main()
{
Console.WriteLine("Hello World!");
}


This is my first post on the site!
1 change: 1 addition & 0 deletions src/Pretzel.Logic/Resources/Liquid/Layout.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@
</div>

</div>
<script src="js/run_prettify.js"></script>
</body>
</html>
8 changes: 8 additions & 0 deletions src/Pretzel.Logic/Resources/Razor/FirstPost.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,12 @@ comments: true

## Hello world...

Code:

{{C#}}
static void Main()
{
Console.WriteLine("Hello World!");
}

This is my first post on the site!
1 change: 1 addition & 0 deletions src/Pretzel.Logic/Resources/Razor/Layout.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@
</div>

</div>
<script src="js/run_prettify.js"></script>
</body>
</html>
Loading