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

For evaluation: Implementation of MarkdownDeep.net over Journaley. #129

Merged
merged 6 commits into from
Nov 26, 2015
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
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;
Copy link
Owner

Choose a reason for hiding this comment

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

What are these two options?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

From the website:

bool SafeMode
Set to true to only allow whitelisted safe html tags

bool ExtraMode
Set to true to enable ExtraMode, which enables the same set of features as implemented by PHP Markdown Extra.

Extramode is needed, becuase most of the implementations in Day One uses Markdown Extra stuff like tables and footnotes

Copy link
Owner

Choose a reason for hiding this comment

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

Thanks. We might want to turn on the SafeMode option later for resolving #118. Let's keep this false for now.

}

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.
Copy link
Owner

Choose a reason for hiding this comment

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

You need to be more explicit here and say what it fixes.

  1. automatically add <br /> tags where the user hit enter.
  2. add strikethrough style support

/// - 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;
}
Copy link
Owner

Choose a reason for hiding this comment

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

Add an else clause like this:

else
{
    paragraphBuilder.Add(line + "<br />");
}

I thought about this for a while, and I think this is the perfect place to add <br /> tags.
This is the place where we're within the <p> tag, and seeing the line break before the </p> tag.
I don't think we need that complex regex below.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think you thought this after I clarified to you about how the regex worked right?

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