Skip to content

Commit

Permalink
Converter reworked
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton92nd committed Nov 12, 2014
1 parent 79f5a45 commit 383c16d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 47 deletions.
80 changes: 39 additions & 41 deletions HtmlConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using Mark.HTMLParser;

namespace Mark
Expand All @@ -15,56 +13,56 @@ public class HtmlConverter
{TokenType.Code, "code"}, {TokenType.Underscore, "em"}, {TokenType.DoubleUnderscore, "strong"}
};

private static string RecursiveConstruct(List<Token> tokens, int start, int finish, bool code = false)
private static readonly Dictionary<TokenType, string> TypeToSource = new Dictionary<TokenType, string>
{
string result = "";
if (code)
{TokenType.Code, "`"}, {TokenType.Underscore, "_"}, {TokenType.DoubleUnderscore, "__"}
};

private static string BuildTree(List<int>[] edges, List<Token> tokens, int node, bool code = false)
{
bool isCode = code || node > 0 && tokens[node - 1].type == TokenType.Code;
var subTree = edges[node].Aggregate("", (current, i) => current + BuildTree(edges, tokens, i, isCode));
if (node == 0)
return subTree;
if (TypeToTag.ContainsKey(tokens[node - 1].type))
{
return tokens.Skip(start).Take(finish - start).Select(token => token.value)
.Aggregate("", (str, token) => str + token);
var tag = TypeToTag[tokens[node - 1].type];
var oldTag = TypeToSource[tokens[node - 1].type];
return code ? oldTag + subTree + oldTag : "<" + tag + ">" + subTree + "</" + tag + ">";
}
int position = start;
while (position < finish)
return tokens[node - 1].value;
}

private static string ConstructHtmlParagraph(List<Token> tokens)
{
var edges = new List<int>[tokens.Count + 1];
for (int i = 0; i < edges.Length; i++)
edges[i] = new List<int>();
var stack = new Stack<Tuple<TokenType, int>>();
stack.Push(new Tuple<TokenType, int>(TokenType.Word, 0));
for (int i = 0; i < tokens.Count; i++)
{
var token = tokens[position];
if (token.type == TokenType.Underscore || token.type == TokenType.DoubleUnderscore)
var token = tokens[i];
if (!TypeToTag.ContainsKey(token.type))
{
int i = -1;
for (int j = position + 1; j < finish; j++)
{
if (tokens[j].type == token.type && (j + 1 == tokens.Count || tokens[j + 1].type != TokenType.Word))
{
i = j;
break;
}
}
if (i > position)
{
var tag = TypeToTag[token.type];
result += "<" + tag + ">" + RecursiveConstruct(tokens, position + 1, i) + "</" + tag + ">";
position = i + 1;
continue;
}
}
if (token.type == TokenType.Code)
edges[stack.Peek().Item2].Add(i + 1);
continue;
}
if (token.type == stack.Peek().Item1)
{
int i = tokens.FindIndex(position + 1, current => current.type == TokenType.Code);
if (i > start && i < finish)
if (token.type == TokenType.Code || i == tokens.Count - 1 || tokens[i + 1].type != TokenType.Word)
{
result += "<code>" + RecursiveConstruct(tokens, position + 1, i, true) + "</code>";
position = i + 1;
stack.Pop();
continue;
}
}
result += token.value;
position++;
edges[stack.Peek().Item2].Add(i + 1);
if (token.type == TokenType.Code || i == 0 || tokens[i - 1].type != TokenType.Word)
{
stack.Push(new Tuple<TokenType, int>(token.type, i + 1));
}
}
return result;
}

private static string ConstructHtmlParagraph(List<Token> tokens)
{
return RecursiveConstruct(tokens, 0, tokens.Count);
return BuildTree(edges, tokens, 0);
}

static private List<string> BuildParagraphs(string[] lines)
Expand Down
8 changes: 2 additions & 6 deletions Tests/HtmlConverter_should.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using Mark;

Expand Down Expand Up @@ -62,9 +58,9 @@ public void find_strong_tag()
[Test]
public void find_code_tag()
{
string text = "`hello world`";
string text = "`hello _p_ world`";
string result = HtmlConverter.ConvertString(text);
Assert.AreEqual("<html><head><meta charset=\"UTF-8\"></head>\n<p>\n<code>hello world</code>\n</p>\n</html>", result);
Assert.AreEqual("<html><head><meta charset=\"UTF-8\"></head>\n<p>\n<code>hello _p_ world</code>\n</p>\n</html>", result);
}

[Test]
Expand Down

0 comments on commit 383c16d

Please sign in to comment.