-
Notifications
You must be signed in to change notification settings - Fork 19
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
Changes from 4 commits
dfa5e04
1055b93
5853c58
78a0b5e
0513046
490f222
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,3 +45,7 @@ div { | |
img { | ||
width: 100%; | ||
} | ||
|
||
pre { | ||
word-wrap: break-word; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,3 +45,7 @@ div { | |
img { | ||
width: 100%; | ||
} | ||
|
||
pre { | ||
word-wrap: break-word; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,3 +45,7 @@ div { | |
img { | ||
width: 100%; | ||
} | ||
|
||
pre { | ||
word-wrap: break-word; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
namespace Journaley.Utilities | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Text.RegularExpressions; | ||
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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You need to be more explicit here and say what it fixes.
|
||
/// - automatically adds break tags on single line breaks. | ||
/// - adds strikethrough style support. | ||
/// - makes the first sentence into a header. | ||
/// </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(); | ||
|
||
// 0 - stumbles upon the beginning of a <p> tag/usual parsing. | ||
// 1 - stumbles upon the end of </p> tag. | ||
// 2 - skips check and just dumps the line to builder. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you meant to say -1 here, not 2. |
||
var parseState = -1; | ||
|
||
bool firstTitleParsed = false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm sorry, but could you make the firstTitle feature as a separate PR, once this one is merged? This seems to require more thoughts on it. |
||
|
||
var lines = formattedString.Split( | ||
new string[] { "\r\n", "\n" }, | ||
StringSplitOptions.None); | ||
|
||
foreach (string line in lines) | ||
{ | ||
// We first incrementally check if the line begins with <p> tag. When it does, | ||
// We turn on the paragraphBuilder to append the remaining lines. | ||
|
||
// If the checks hit a line with the </p> closing, we disable check and proceed | ||
// to parse the text, add <br /> tags and then adding it to builder | ||
// where it is assembled with the rest of the file. | ||
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>")) | ||
{ | ||
string singleLine = line; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be fine to just use |
||
if (!firstTitleParsed) | ||
{ | ||
singleLine = Regex.Replace(singleLine, @"(<p>)(.*?)(</p>)", "<h2>$2</h2>\n<br/>"); | ||
firstTitleParsed = true; | ||
} | ||
|
||
builder.AppendLine(ReplaceMDStrikethrough(singleLine)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think applying strikethrough here is correct. See my comments below. |
||
parseState = -1; | ||
} | ||
else | ||
{ | ||
paragraphBuilder.AppendLine(ReplaceMDStrikethrough(line) + "<br />"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one seems fine. |
||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add an else
{
paragraphBuilder.Add(line + "<br />");
} I thought about this for a while, and I think this is the perfect place to add There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 (parseState == 0) | ||
{ | ||
paragraphBuilder.AppendLine(ReplaceMDStrikethrough(line)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, this seems incorrect to me. I suggest applying strikethrough to the entire paragraph at this point, instead of applying it line by line. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you need to evaluate the private function. |
||
|
||
builder.AppendLine(paragraphBuilder.ToString()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
paragraphBuilder.Clear(); | ||
parseState = -1; | ||
} | ||
else | ||
{ | ||
// For <pre> tagged text, just skip check and append to builder | ||
builder.AppendLine(line); | ||
} | ||
} | ||
|
||
return builder.ToString(); | ||
} | ||
|
||
/// <summary> | ||
/// Replaces "~~" strikethrough Markdown tag into del HTML tags. | ||
/// </summary> | ||
/// <param name="parsedString">Any string that has strikethrough tags</param> | ||
/// <returns>A string with del HTML tags in place of Markdown strikethrough tags.</returns> | ||
private static string ReplaceMDStrikethrough(string parsedString) | ||
{ | ||
parsedString = Regex.Replace(parsedString, @"(~~)(.*?)(~~)", "<del>$2</del>", RegexOptions.Singleline); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think having just this one line with |
||
parsedString = Regex.Replace(parsedString, @"(?<=<p>)(~~)", "<del>", RegexOptions.Singleline); | ||
parsedString = Regex.Replace(parsedString, @"(~~)(?=</p>)", "</del>", RegexOptions.Singleline); | ||
|
||
return parsedString; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From the website:
Extramode is needed, becuase most of the implementations in Day One uses Markdown Extra stuff like tables and footnotes
There was a problem hiding this comment.
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.