Skip to content

Commit

Permalink
Merge pull request #129 from crxtrdude/feature/markdown_deep_2
Browse files Browse the repository at this point in the history
Implementation of MarkdownDeep.net over Journaley.
  • Loading branch information
yyoon committed Nov 26, 2015
2 parents 5a3c500 + 490f222 commit 85e765d
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 49 deletions.
60 changes: 15 additions & 45 deletions Journaley/Forms/MainForm.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Journaley.Forms
{
extern alias MDS;
extern alias MDD;

using System;
using System.Collections.Generic;
Expand All @@ -21,7 +21,7 @@
using Journaley.Core.Utilities;
using Journaley.Core.Watcher;
using Journaley.Utilities;
using MDS.MarkdownSharp;
using MDD.MarkdownDeep;
using Pabo.Calendar;
using Squirrel;

Expand Down Expand Up @@ -475,7 +475,8 @@ private Markdown Markdown
if (this.markdown == null)
{
this.markdown = new Markdown();
((MarkdownOptions)this.markdown.Options).AutoNewLines = true;
this.markdown.ExtraMode = true;
this.markdown.SafeMode = false;
}

return this.markdown;
Expand Down Expand Up @@ -638,53 +639,22 @@ private void OnGetMinMaxInfo(ref Message m)
/// </summary>
private void UpdateWebBrowser()
{
string initialString = this.SelectedEntry.EntryText;

// Fix for backtick fenced code blocks is to replace it into tilde fenced.
initialString = initialString.Replace("```", "~~~");

string docType = "<!DOCTYPE html>";

string formattedString = string.Format(
"<style type=\"text/css\">\n<!-- Font CSS -->\n{0}\n<!-- Size CSS -->\n{1}\n<!-- Custom CSS -->\n{2}\n</style><html><body><div>{3}</div></body></html>",
"{0}\n<style type=\"text/css\">\n<!-- Font CSS -->\n{1}\n<!-- Size CSS -->\n{2}\n<!-- Custom CSS -->\n{3}\n</style>\n<html>\n<body>\n<div>\n{4}</div>\n</body>\n</html>",
docType,
this.GetWebBrowserTypefaceCSS(),
this.GetWebBrowserSizeCSS(),
this.CustomCSS ?? string.Empty,
Markdown.Transform(this.SelectedEntry.EntryText));

this.webBrowser.DocumentText = this.RemoveLineBreaksWithinLists(formattedString);
}

/// <summary>
/// Removes the wrong line breaks within nested ordered/unordered lists.
/// Hack to fix the issue #114.
/// </summary>
/// <param name="formattedString">The entry content formatted by MarkdownSharp.</param>
/// <returns>correctly formatted string with the wrong line breaks removed.</returns>
private string RemoveLineBreaksWithinLists(string formattedString)
{
System.Text.StringBuilder builder = new System.Text.StringBuilder();
string pattern = @"^</?(ol|ul|li)>";

var lines = formattedString.Split(
new string[] { "\r\n", "\n" },
StringSplitOptions.None);

for (int i = 0; i < lines.Length - 1; ++i)
{
string line = lines[i];
string nextLine = lines[i + 1];

// Remove the "<br />" tags only if the current line and the next line are starting
// with the opening/closing list tags. By doing this, it can prevent removing
// line breaks that are intentionally added by the user.
if (Regex.IsMatch(line, pattern) && Regex.IsMatch(nextLine, pattern))
{
while (line.EndsWith("<br />"))
{
line = line.Substring(0, line.LastIndexOf("<br />"));
}
}

builder.AppendLine(line);
}

builder.AppendLine(lines.Last());
PostMarkdownParser.PostMarkdown(Markdown.Transform(initialString)));

return builder.ToString();
this.webBrowser.DocumentText = formattedString;
}

/// <summary>
Expand Down
8 changes: 5 additions & 3 deletions Journaley/Journaley.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,10 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\squirrel.windows.0.99.2\lib\Net45\ICSharpCode.SharpZipLib.dll</HintPath>
</Reference>
<Reference Include="MarkdownSharp, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\MarkdownSharp.1.13.0.0\lib\35\MarkdownSharp.dll</HintPath>
<Aliases>MDS</Aliases>
<Reference Include="MarkdownDeep, Version=1.5.4615.26275, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\MarkdownDeep.NET.1.5\lib\.NetFramework 3.5\MarkdownDeep.dll</HintPath>
<Private>True</Private>
<Aliases>MDD</Aliases>
</Reference>
<Reference Include="Microsoft.WindowsAPICodePack">
<HintPath>..\packages\winapicp.1.1\lib\Microsoft.WindowsAPICodePack.dll</HintPath>
Expand Down Expand Up @@ -202,6 +203,7 @@
<DesignTime>True</DesignTime>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="Utilities\PostMarkdownParser.cs" />
<Compile Include="Utilities\CursorReader.cs" />
<Compile Include="Utilities\FontReader.cs" />
<Compile Include="Utilities\HtmlToText.cs">
Expand Down
4 changes: 4 additions & 0 deletions Journaley/Resources/JournaleyLarge.css
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,7 @@ div {
img {
width: 100%;
}

pre {
word-wrap: break-word;
}
4 changes: 4 additions & 0 deletions Journaley/Resources/JournaleyMedium.css
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,7 @@ div {
img {
width: 100%;
}

pre {
word-wrap: break-word;
}
4 changes: 4 additions & 0 deletions Journaley/Resources/JournaleySmall.css
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,7 @@ div {
img {
width: 100%;
}

pre {
word-wrap: break-word;
}
84 changes: 84 additions & 0 deletions Journaley/Utilities/PostMarkdownParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
namespace Journaley.Utilities
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

/// <summary>
/// Helper class for fixing MarkdownDeep parsed HTML.
/// </summary>
public class PostMarkdownParser
{
/// <summary>
/// Perform fixes after MarkdownDeep parsing for publishing
/// to Journaley.
/// - automatically adds break tags on single line breaks.
/// - makes the first sentence into a header.
/// - properly parses code blocks enclosed in p tags into pre.
/// </summary>
/// <param name="formattedString">Formatted HTML string</param>
/// <returns>Properly formatted HTML string for publishing.</returns>
public static string PostMarkdown(string formattedString)
{
StringBuilder builder = new StringBuilder();
StringBuilder paragraphBuilder = new StringBuilder();

// -1 - skips check and just dumps the line to builder.
// 0 - stumbles upon the beginning of a <p> tag/usual parsing.
// 1 - stumbles upon the end of </p> tag.
var parseState = -1;

string line;
using (StringReader reader = new StringReader(formattedString))
{
while ((line = reader.ReadLine()) != null)
{
if (line.Contains("<p>"))
{
parseState = 1;
}
else if (line.Contains("</p>"))
{
parseState = 0;
}
else if (line.Contains("<pre>"))
{
parseState = -1;
}

if (parseState == 1)
{
if (line.Contains("</p>"))
{
builder.AppendLine(line);
parseState = -1;
}
else
{
paragraphBuilder.AppendLine(line + "<br />");
}
}
else if (parseState == 0)
{
paragraphBuilder.AppendLine(line);

string paragraph = paragraphBuilder.ToString();

builder.Append(paragraph);
paragraphBuilder.Clear();
parseState = -1;
}
else
{
builder.AppendLine(line);
}
}
}

return builder.ToString();
}
}
}
2 changes: 1 addition & 1 deletion Journaley/packages.config
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="DeltaCompressionDotNet" version="1.0.0" targetFramework="net45" />
<package id="MarkdownSharp" version="1.13.0.0" targetFramework="net40-Client" />
<package id="MarkdownDeep.NET" version="1.5" targetFramework="net45" />
<package id="Splat" version="1.6.2" targetFramework="net45" />
<package id="squirrel.windows" version="0.99.2" targetFramework="net45" />
<package id="StyleCop.MSBuild" version="4.7.48.2" targetFramework="net40-Client" />
Expand Down

0 comments on commit 85e765d

Please sign in to comment.