Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Links and block handling modified for standard textile.

  • Loading branch information...
commit 8b7cb33a25fbdef1d6b8b4091fc3fda58ea5955e 1 parent 553881e
@benogle authored
Showing with 125 additions and 45 deletions.
  1. +59 −28 HTML2Markup.Test/HTML2TextileFixture.cs
  2. +66 −17 HTML2Markup/TextileParser.cs
View
87 HTML2Markup.Test/HTML2TextileFixture.cs
@@ -54,7 +54,7 @@ public string ParseHTML(string html)
#region paragraphs
[Test]
- public void PNoStyle()
+ public void P_NoStyle()
{
string s = @"<p>my text</p><p>Yeah it is</p><p>woot</p>";
string t = ParseHTML(s);
@@ -63,7 +63,7 @@ public void PNoStyle()
}
[Test]
- public void PStyle()
+ public void P_Style()
{
string s = "<p >my text</p><p style=\"text-align: center;\">Yeah it is</p><p>woot</p>";
string t = ParseHTML(s);
@@ -72,7 +72,7 @@ public void PStyle()
}
[Test]
- public void PMultiStyle()
+ public void P_MultiStyle()
{
string s = "<p >my text</p><p style=\"text-align: center; color: red;\">Yeah it is</p><p>woot</p>";
string t = ParseHTML(s);
@@ -89,6 +89,15 @@ public void P_noPara()
Assert.AreEqual("my text\n\nRandom text\n\nwoot\n\n", t);
}
+ [Test]
+ public void P_class()
+ {
+ string s = "<p class=\"wowza\">my text</p>Random text<p>woot</p>";
+ string t = ParseHTML(s);
+
+ Assert.AreEqual("p(wowza). my text\n\nRandom text\n\nwoot\n\n", t);
+ }
+
#endregion
#region lists
@@ -121,7 +130,7 @@ public void PRE_simple()
string s = "some text <pre>hey this is my pre</pre> more text";
string t = ParseHTML(s);
- Assert.AreEqual("some text\n\npre. \nhey this is my pre\n.pre\n\nmore text", t);
+ Assert.AreEqual("some text\n\npre.. hey this is my pre\n\np. more text", t);
}
[Test]
@@ -130,16 +139,34 @@ public void PRE_newlines()
string s = "some text <pre>hey\n\nthis\nis\nmy\n\n\npre</pre> more text";
string t = ParseHTML(s);
- Assert.AreEqual("some text\n\npre. \nhey\n\nthis\nis\nmy\n\n\npre\n.pre\n\nmore text", t);
+ Assert.AreEqual("some text\n\npre.. hey\n\nthis\nis\nmy\n\n\npre\n\np. more text", t);
+ }
+
+ [Test]
+ public void PRE_class()
+ {
+ string s = "some text <pre class=\"prettyprint\">hey\n\nthis\nis\nmy\n\n\npre</pre> more text";
+ string t = ParseHTML(s);
+
+ Assert.AreEqual("some text\n\npre(prettyprint).. hey\n\nthis\nis\nmy\n\n\npre\n\np. more text", t);
}
[Test]
public void PRECODE_simple()
{
- string s = "some text <pre> <code>hey this is my code</code> </pre> more text";
+ string s = "some text <pre> <code>hey this is my code\nyeah man</code> </pre> more text";
string t = ParseHTML(s);
- Assert.AreEqual("some text\n\nbc. \nhey this is my code\n.bc\n\nmore text", t);
+ Assert.AreEqual("some text\n\nbc.. hey this is my code\nyeah man\n\np. more text", t);
+ }
+
+ [Test]
+ public void PRECODE_header()
+ {
+ string s = "some text <pre> <code>hey this is my code\nyeah man</code> </pre> <h2>header</h2> more text";
+ string t = ParseHTML(s);
+
+ Assert.AreEqual("some text\n\nbc.. hey this is my code\nyeah man\n\nh2. header\n\nmore text", t);
}
[Test]
@@ -209,69 +236,73 @@ public void STYLE_caseBaaaad()
#region links
[Test]
- public void LINK_wikiInternal()
+ public void LINK_basic()
{
string s = "text! and <a href=\"/Project/something/Wiki/Wowza\">a link</a> ok!!";
string t = ParseHTML(s);
- Assert.AreEqual("text! and [a link|something:Wowza] ok!!", t);
+ Assert.AreEqual("text! and \"a link\":/Project/something/Wiki/Wowza ok!!", t);
}
[Test]
- public void LINK_bug()
+ public void LINK_title()
{
- string s = "text! and <a href=\"/bUg/1\">a link</a> ok!!";
+ string s = "text! and <a title=\"title\" href=\"http://something.com/Project/something/Wiki/Wowza\">a link</a> ok!!";
string t = ParseHTML(s);
- Assert.AreEqual("text! and [a link|bug 1] ok!!", t);
+ Assert.AreEqual("text! and \"a link(title)\":http://something.com/Project/something/Wiki/Wowza ok!!", t);
}
[Test]
- public void LINK_task()
+ public void LINK_class()
{
- string s = "text! and <a href=\"/tAsk/1\">a link</a> ok!!";
+ string s = "text! and <a class=\"something\" href=\"http://something.com/Project/something/Wiki/Wowza\">a link</a> ok!!";
string t = ParseHTML(s);
- Assert.AreEqual("text! and [a link|task 1] ok!!", t);
+ Assert.AreEqual("text! and \"(something)a link\":http://something.com/Project/something/Wiki/Wowza ok!!", t);
}
[Test]
- public void LINK_external()
+ public void LINK_style()
{
- string s = "text! and <a href=\"/issue/1\">a link</a> ok!!";
+ string s = "text! and <a style=\"background:#0f0; color:#00c;\" title=\"my title\" href=\"http://something.com/Project/something/Wiki/Wowza\">a link</a> ok!!";
string t = ParseHTML(s);
- Assert.AreEqual("text! and [a link|/issue/1] ok!!", t);
+ Assert.AreEqual("text! and \"{background: #0f0;color: #00c}a link(my title)\":http://something.com/Project/something/Wiki/Wowza ok!!", t);
}
+
+ #endregion
+
+ #region images
+
[Test]
- public void LINK_externalWiki()
+ public void IMAGE_basic()
{
- string s = "text! and <a href=\"http://something.com/Project/something/Wiki/Wowza\">a link</a> ok!!";
+ string s = "text! and <img src=\"/meow/omg.jpg\" /> ok!!";
string t = ParseHTML(s);
- Assert.AreEqual("text! and [a link|http://something.com/Project/something/Wiki/Wowza] ok!!", t);
+ Assert.AreEqual("text! and !/meow/omg.jpg! ok!!", t);
}
[Test]
- public void LINK_externalWikiTitle()
+ public void IMAGE_class()
{
- string s = "text! and <a title=\"title\" href=\"http://something.com/Project/something/Wiki/Wowza\">a link</a> ok!!";
+ string s = "text! and <img class=\"mycl\" style=\"color:#fff;\" src=\"/meow/omg.jpg\" /> ok!!";
string t = ParseHTML(s);
- Assert.AreEqual("text! and [a link (title)|http://something.com/Project/something/Wiki/Wowza] ok!!", t);
+ Assert.AreEqual("text! and !(mycl)/meow/omg.jpg! ok!!", t);
}
[Test]
- public void LINK_wikiInternalTitle()
+ public void IMAGE_style()
{
- string s = "text! and <a title=\"title!\" href=\"/Project/something/Wiki/Wowza\">a link</a> ok!!";
+ string s = "text! and <img style=\"color:#fff;\" src=\"/meow/omg.jpg\" /> ok!!";
string t = ParseHTML(s);
- Assert.AreEqual("text! and [a link (title!)|something:Wowza] ok!!", t);
+ Assert.AreEqual("text! and !{color: #fff}/meow/omg.jpg! ok!!", t);
}
-
#endregion
}
}
View
83 HTML2Markup/TextileParser.cs
@@ -42,10 +42,11 @@ public class TextileParser : Parser
private Stack<ListType> _inList = new Stack<ListType>();
private bool _inTable = false;
+ private bool _inExtendedBlock = false;
+ private bool _exitExtendedBlock = false;
+
private static Regex NUMRE = new Regex("[0-9]+", RegexOptions.Compiled);
private static Regex GLYPH_NUMRE = new Regex("&#([0-9]+);", RegexOptions.Compiled);
- private static Regex WIKI_LINK = new Regex("^/Project/(?<project>[a-zA-Z0-9_\\.,-]+)/Wiki/(?<page>[a-zA-Z0-9_~,\\.-]+)(?<anchor>#[a-zA-Z0-9_]+)?$", RegexOptions.Compiled);
- private static Regex ISSUE_LINK = new Regex("^/(?<issue>bug|task)/(?<num>[0-9]+)$", RegexOptions.Compiled);
private static Dictionary<string, string> GLYPHS = new Dictionary<string,string>(){
{"&nbsp;", " "},
@@ -116,6 +117,25 @@ private string GetStyleStr(ParserNode node)
return ret;
}
+ private string GetClassStr(ParserNode node)
+ {
+ string cl = node.Attributes["class"];
+ string ret = "";
+ if (cl != null)
+ {
+ ret = String.Format("({0})", cl);
+ }
+ return ret;
+ }
+
+ private string GetStyleOrClassStr(ParserNode node)
+ {
+ string ret = GetClassStr(node);
+ if (ret.Length < 1)
+ ret = GetStyleStr(node);
+ return ret;
+ }
+
private string GetTextileHref(string href)
{
href = href.Trim();
@@ -186,8 +206,26 @@ private void HandleQuickTags(ParserNode node, bool isStart)
private void AddBlockTag(ParserNode node, string tag, bool isStart)
{
- if (isStart) AddOutput(tag + GetStyleStr(node) + ". ", 2, true);
- else AddOutput(string.Format(".{0}", tag), 2, false);
+ _exitExtendedBlock = false;
+
+ if (isStart) AddOutput(tag + GetStyleOrClassStr(node) + ". ", 2, true);
+ else AddOutput("", 2, false);
+ }
+
+ private void AddExtendedBlockTag(ParserNode node, string tag, bool isStart)
+ {
+ if (isStart)
+ {
+ _inExtendedBlock = true;
+ _exitExtendedBlock = false;
+ AddOutput(tag + GetStyleOrClassStr(node) + ".. ", 2, true);
+ }
+ else
+ {
+ _inExtendedBlock = false;
+ _exitExtendedBlock = true;
+ AddOutput("", 2, false);
+ }
}
#endregion
@@ -222,10 +260,11 @@ protected override void HandleElementStart(ParserNode node)
switch (node.Name)
{
case "p":
- if (node.Attributes["style"] == null)
- AddOutput("", 2, true);
+ if (node.Attributes["style"] != null || node.Attributes["class"] != null || _exitExtendedBlock)
+ AddBlockTag(node, "p", true);
else
- AddBlockTag(node, "p", true);
+ AddOutput("", 2, true);
+
break;
case "h1":
@@ -243,7 +282,8 @@ protected override void HandleElementStart(ParserNode node)
break;
case "a":
- AddOutput(" [");
+ AddOutput(" \"");
+ AddOutput(GetStyleOrClassStr(CurrentNode));
//text node will be aded next, then on the end tag,
//we will output the href and all that.
CurrentLink = CurrentNode;
@@ -252,7 +292,7 @@ protected override void HandleElementStart(ParserNode node)
case "img":
string src = node.Attributes["src"];
if(src != null)
- AddOutput(string.Format(" !{0}! ", src));
+ AddOutput(string.Format(" !{0}{1}! ", GetStyleOrClassStr(CurrentNode), src));
break;
case "br":
@@ -269,8 +309,7 @@ protected override void HandleElementStart(ParserNode node)
{
break;
}
- AddBlockTag(node, "pre", true);
- AddOutput("", 1, false);
+ AddExtendedBlockTag(node, "pre", true);
break;
case "code":
@@ -278,8 +317,7 @@ protected override void HandleElementStart(ParserNode node)
prev.NodeType == XmlNodeType.Element &&
prev.Name == "pre")
{
- AddBlockTag(node, "bc", true);
- AddOutput("", 1, false);
+ AddExtendedBlockTag(node, "bc", true);
}
break;
@@ -370,9 +408,12 @@ protected override void HandleElementEnd(ParserNode node)
case "a":
if (CurrentLink.Attributes["title"] != null)
- AddOutput(" (" + CurrentLink.Attributes["title"] + ")");
+ AddOutput("(" + CurrentLink.Attributes["title"] + ")");
+
+ AddOutput("\"");
+
if (CurrentLink != null && CurrentLink.Attributes.ContainsKey("href"))
- AddOutput("|" + GetTextileHref(CurrentLink.Attributes["href"]) + "] ");
+ AddOutput(":" + GetTextileHref(CurrentLink.Attributes["href"]) + " ");
CurrentLink = null;
break;
@@ -386,7 +427,7 @@ protected override void HandleElementEnd(ParserNode node)
}
AddOutput("", 1, false);
- AddBlockTag(node, "pre", false);
+ AddExtendedBlockTag(node, "pre", false);
break;
case "code":
@@ -395,7 +436,7 @@ protected override void HandleElementEnd(ParserNode node)
next.Name == "pre")
{
AddOutput("", 1, false);
- AddBlockTag(node, "bc", false);
+ AddExtendedBlockTag(node, "bc", false);
}
break;
@@ -430,6 +471,14 @@ protected override void HandleElementEnd(ParserNode node)
protected override void HandleText(ParserNode node)
{
+ //special case: if extended block ends and just text is the next element,
+ //we will use a paragraph tag.
+ if (_exitExtendedBlock)
+ {
+ AddOutput("p. ", 2, true);
+ _exitExtendedBlock = false;
+ }
+
string output = node.Value;
AddOutput(output.Trim());
Please sign in to comment.
Something went wrong with that request. Please try again.