Modify ReverseFormatter
Create ReverseFormatterTest.cs
This commit is contained in:
parent
833ed18440
commit
77266fa8ab
4 changed files with 317 additions and 1 deletions
|
@ -90,6 +90,7 @@
|
|||
<Compile Include="PagesStorageProviderTests.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="ProviderLoaderTests.cs" />
|
||||
<Compile Include="ReverseFormatterTests.cs" />
|
||||
<Compile Include="SettingsStorageProviderTests.cs" />
|
||||
<Compile Include="TestSettingsStorageProvider.cs" />
|
||||
<Compile Include="UsersStorageProviderTests.cs" />
|
||||
|
|
36
Core-Tests/ReverseFormatterTests.cs
Normal file
36
Core-Tests/ReverseFormatterTests.cs
Normal file
|
@ -0,0 +1,36 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace ScrewTurn.Wiki.Tests {
|
||||
|
||||
[TestFixture]
|
||||
public class ReverseFormatterTests {
|
||||
|
||||
[Test]
|
||||
[TestCase("<b>text</b>", "'''text'''")]
|
||||
[TestCase("<strong>text</strong>", "'''text'''")]
|
||||
[TestCase("<i>text</i>", "''text''")]
|
||||
[TestCase("<em>text</em>", "''text''")]
|
||||
[TestCase("<u>text</u>", "__text__")]
|
||||
[TestCase("<s>text</s>", "--text--")]
|
||||
[TestCase("<h1>text</h1>", "==text==")]
|
||||
[TestCase("<h2>text</h2>", "===text===")]
|
||||
[TestCase("<h3>text</h3>", "====text====")]
|
||||
[TestCase("<h4>text</s>", "=====text=====")]
|
||||
[TestCase("<sup>text</sup>", "<sup>text</sup>")]
|
||||
[TestCase("<sub>text</sub>", "<sub>text</sub>")]
|
||||
[TestCase("<pre>text</pre>", "(((text)))")]
|
||||
[TestCase("<code>text</code>", "@@text@@")]
|
||||
[TestCase("<div>text</div>", "text{br}")]
|
||||
[TestCase("<ol><li>1</li><li>2</li></ol>","{br}# 1{br}# 2{br}{br}")]
|
||||
[TestCase("<ul><li>1</li><li>2</li></ul>", "{br}* 1{br}* 2{br}{br}")]
|
||||
[TestCase("<a id=\"Init\" />I'm an anchor", "[anchor|#init]I'm an anchor")]
|
||||
[TestCase("<a class=\"internallink\" href=\"#init\" title=\"This recall an anchor\"></a>", "[#init|This recall an anchor]")]
|
||||
public void PlainTest(string input, string output) {
|
||||
Assert.AreEqual(output, ReverseFormatter.ReverseFormat(input));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -61,6 +61,9 @@
|
|||
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="SgmlReaderDll">
|
||||
<HintPath>..\References\Lib\MindTouch SGML Reader\SgmlReaderDll.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core">
|
||||
<RequiredTargetFramework>3.5</RequiredTargetFramework>
|
||||
|
|
|
@ -5,6 +5,8 @@ using System.Linq;
|
|||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Web;
|
||||
using System.Xml;
|
||||
using System.IO;
|
||||
|
||||
namespace ScrewTurn.Wiki {
|
||||
|
||||
|
@ -73,14 +75,288 @@ namespace ScrewTurn.Wiki {
|
|||
// A=1 - ATitle=2 - AHref=3 - ATarget=4 - ImageAlt=5 - ImageSrc=6 --- Href/Src=http://www.server.com/Blah.ashx/GetFile.aspx...
|
||||
private static readonly Regex ImageInlineRegexIE = new Regex(@"(<a title=\""?(.*?)\"" href=\""(.*?)\""?( target=_blank)?>)?<img alt=\""?(.*?)\""? src=\""(.*?)\"">(</a>)?", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
|
||||
|
||||
|
||||
private static List<string> listText= new List<string>();
|
||||
//private static string result = "";
|
||||
|
||||
private static string processImage(XmlNode node) {
|
||||
string result = "";
|
||||
if(node.Attributes.Count != 0) {
|
||||
foreach(XmlAttribute attName in node.Attributes) {
|
||||
if(attName.Name.ToString() == "src") {
|
||||
string[] path = attName.Value.ToString().Split('=');
|
||||
//result += "|" + processChild(node.ChildNodes);
|
||||
result += "{" + "UP(" + path[1].Split('&')[0] + ")}" + path[2];
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Processes the child Image.
|
||||
/// </summary>
|
||||
/// <param name="nodes">The nodes.</param>
|
||||
/// <returns></returns>
|
||||
private static string processChildImage(XmlNodeList nodes) {
|
||||
string image ="";
|
||||
string p ="";
|
||||
string url = "";
|
||||
string result ="";
|
||||
foreach(XmlNode node in nodes) {
|
||||
if(node.Name.ToLowerInvariant() == "img")
|
||||
image += processImage(node);
|
||||
else if(node.Name.ToLowerInvariant() == "p") {
|
||||
p += "|" + processChild(node.ChildNodes) + "|";
|
||||
}
|
||||
else if(node.Name.ToLowerInvariant() == "a") {
|
||||
string link = "";
|
||||
string target = "";
|
||||
|
||||
if(node.Attributes.Count != 0) {
|
||||
XmlAttributeCollection attribute = node.Attributes;
|
||||
foreach(XmlAttribute attName in attribute) {
|
||||
if(attName.Value.ToString() == "_blank")
|
||||
target += "^";
|
||||
if(attName.Name.ToString() == "href")
|
||||
link += attName.Value.ToString();
|
||||
}
|
||||
}
|
||||
image += processImage(node.LastChild);
|
||||
url = "|" + target + link;
|
||||
}
|
||||
}
|
||||
result = p+image+ url;
|
||||
return result;
|
||||
}
|
||||
/// <summary>
|
||||
/// Processes the child.
|
||||
/// </summary>
|
||||
/// <param name="nodes">The nodes.</param>
|
||||
/// <returns></returns>
|
||||
private static string processChild(XmlNodeList nodes) {
|
||||
string result = "";
|
||||
foreach(XmlNode node in nodes) {
|
||||
bool anchor = false;
|
||||
if(node.NodeType == XmlNodeType.Text) {
|
||||
result += node.Value;
|
||||
//string result = "";
|
||||
}
|
||||
else {
|
||||
switch(node.Name.ToLowerInvariant()) {
|
||||
case "b":
|
||||
case "strong":
|
||||
result += ("'''" + processChild(node.ChildNodes) + "'''");
|
||||
break;
|
||||
case "s":
|
||||
result += ("--" + processChild(node.ChildNodes) + "--");
|
||||
break;
|
||||
case "em":
|
||||
case "i":
|
||||
result += ("''" + processChild(node.ChildNodes) + "''");
|
||||
break;
|
||||
case "u":
|
||||
result += ("__" + processChild(node.ChildNodes) + "__");
|
||||
break;
|
||||
//break;
|
||||
case "h1":
|
||||
result += ("==" + processChild(node.ChildNodes) + "==");
|
||||
break;
|
||||
//break;
|
||||
case "h2":
|
||||
result += ("===" + processChild(node.ChildNodes) + "===");
|
||||
break;
|
||||
//break;
|
||||
case "h3":
|
||||
result += ("====" + processChild(node.ChildNodes) + "====");
|
||||
break;
|
||||
//break;
|
||||
case "h4":
|
||||
result += ("=====" + processChild(node.ChildNodes) + "=====");
|
||||
break;
|
||||
case "pre":
|
||||
result += ("(((" + processChild(node.ChildNodes) + ")))");
|
||||
break;
|
||||
case "code":
|
||||
result += ("@@" + processChild(node.ChildNodes) + "@@");
|
||||
break;
|
||||
case "hr":
|
||||
case "hr /":
|
||||
result += ("----" + processChild(node.ChildNodes));
|
||||
break;
|
||||
case "\t":
|
||||
result += (":" + processChild(node.ChildNodes));
|
||||
break;
|
||||
case "éé":
|
||||
result += ("~~~~" + processChild(node.ChildNodes));
|
||||
break;
|
||||
case "span":
|
||||
if(node.Attributes.Count != 0) {
|
||||
XmlAttributeCollection attribute = node.Attributes;
|
||||
foreach(XmlAttribute attName in attribute) {
|
||||
if(attName.Value.ToString() == "italic")
|
||||
result += "''" + processChild(node.ChildNodes) + "''";
|
||||
}
|
||||
}
|
||||
break;
|
||||
case "\n":
|
||||
case "br":
|
||||
result += ("{br}" + processChild(node.ChildNodes));
|
||||
break;
|
||||
case "ol":
|
||||
result += "{br}" + processChild(node.ChildNodes) + "{br}";
|
||||
break;
|
||||
case "ul":
|
||||
result += "{br}" + processChild(node.ChildNodes) + "{br}";
|
||||
break;
|
||||
case "table":
|
||||
result += processChild(node.ChildNodes);
|
||||
break;
|
||||
case "tbody":
|
||||
result += processChild(node.ChildNodes);
|
||||
break;
|
||||
case "tr":
|
||||
result += processChild(node.ChildNodes);
|
||||
break;
|
||||
case "td":
|
||||
result += processChild(node.ChildNodes);
|
||||
break;
|
||||
case "li":
|
||||
if (node.ParentNode.Name.ToLowerInvariant() == "ol")
|
||||
result += ("# " + processChild(node.ChildNodes) + "{br}");
|
||||
else if (node.ParentNode.Name.ToLowerInvariant() == "ul")
|
||||
result += ("* " + processChild(node.ChildNodes) + "{br}");
|
||||
break;
|
||||
case "sup":
|
||||
result += ("<sup>" + processChild(node.ChildNodes) + "</sup>");
|
||||
break;
|
||||
case "sub":
|
||||
result += ("<sub>" + processChild(node.ChildNodes) + "</sub>");
|
||||
break;
|
||||
case "p":
|
||||
if(node.Attributes.Count != 0) {
|
||||
XmlAttributeCollection attribute = node.Attributes;
|
||||
foreach(XmlAttribute attName in attribute) {
|
||||
if(attName.Value.ToString() == "imagedescription")
|
||||
result += "";
|
||||
}
|
||||
}
|
||||
break;
|
||||
case "div":
|
||||
if(node.Attributes.Count != 0) {
|
||||
XmlAttributeCollection attribute = node.Attributes;
|
||||
foreach(XmlAttribute attName in attribute) {
|
||||
if (attName.Value.ToString() == "box"){
|
||||
result += "(((" + processChild(node.ChildNodes) + "))){br}";
|
||||
}
|
||||
if(attName.Value.ToString() == "imageleft") {
|
||||
result += "[imageleft" + processChildImage(node.ChildNodes) + "]{br}";
|
||||
}
|
||||
if(attName.Value.ToString() == "imageright")
|
||||
result += "[imageleft" + processChildImage(node.ChildNodes) + "]{br}";
|
||||
if(attName.Value.ToString() == "imageauto")
|
||||
result += "[imageleft" + processChildImage(node.ChildNodes) + "]{br}";
|
||||
}
|
||||
}
|
||||
else
|
||||
result += (processChild(node.ChildNodes) + "{br}");
|
||||
break;
|
||||
|
||||
case "img":
|
||||
if(node.Attributes.Count != 0) {
|
||||
XmlAttributeCollection attribute = node.Attributes;
|
||||
foreach(XmlAttribute attName in attribute) {
|
||||
//if(attName.Name.ToString() == "src") {
|
||||
// string[] path = attName.Value.ToString().Split('=');
|
||||
//result += "|" + processChild(node.ChildNodes);
|
||||
result += "";
|
||||
//}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case "a":
|
||||
string link="";
|
||||
string target="";
|
||||
string title="";
|
||||
if(node.Attributes.Count != 0) {
|
||||
XmlAttributeCollection attribute = node.Attributes;
|
||||
foreach(XmlAttribute attName in attribute) {
|
||||
if(attName.Name.ToString() != "id".ToLowerInvariant()) {
|
||||
if(attName.Value.ToString() == "_blank")
|
||||
target += "^";
|
||||
if(attName.Name.ToString() == "href")
|
||||
link += attName.Value.ToString();
|
||||
if(attName.Name.ToString() == "title")
|
||||
title += attName.Value.ToString();
|
||||
}
|
||||
else{
|
||||
anchor = true;
|
||||
result += "[anchor|#" + attName.Value.ToString().ToLowerInvariant() + "]" + processChild(node.ChildNodes);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(!anchor)
|
||||
result += "[" + target + link + "|" + title + "]" + processChild(node.ChildNodes); //"]");
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
result += (node.OuterXml);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Froms the HTML.
|
||||
/// </summary>
|
||||
/// <param name="reader">The reader.</param>
|
||||
/// <returns></returns>
|
||||
private static XmlDocument FromHTML(TextReader reader) {
|
||||
|
||||
// setup SgmlReader
|
||||
Sgml.SgmlReader sgmlReader = new Sgml.SgmlReader();
|
||||
sgmlReader.DocType = "HTML";
|
||||
sgmlReader.WhitespaceHandling = WhitespaceHandling.All;
|
||||
|
||||
sgmlReader.CaseFolding = Sgml.CaseFolding.ToLower;
|
||||
sgmlReader.InputStream = reader;
|
||||
|
||||
// create document
|
||||
XmlDocument doc = new XmlDocument();
|
||||
doc.PreserveWhitespace = true;
|
||||
doc.XmlResolver = null;
|
||||
doc.Load(sgmlReader);
|
||||
return doc;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reverse formats HTML content into WikiMarkup.
|
||||
/// </summary>
|
||||
/// <param name="html">The input HTML.</param>
|
||||
/// <returns>The corresponding WikiMarkup.</returns>
|
||||
public static string ReverseFormat(string html) {
|
||||
Match match = null;
|
||||
|
||||
StringReader strReader = new StringReader(html);
|
||||
XmlDocument x = FromHTML((TextReader)strReader);
|
||||
string text = processChild(x.FirstChild.ChildNodes);
|
||||
//StringBuilder t = new StringBuilder(html);
|
||||
//result = "";
|
||||
listText.Clear();
|
||||
return text;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Reverse formats HTML content into WikiMarkup.
|
||||
/// </summary>
|
||||
/// <param name="html">The input HTML.</param>
|
||||
/// <returns>The corresponding WikiMarkup.</returns>
|
||||
public static string ReverseFormatOld(string html) {
|
||||
|
||||
Match match = null;
|
||||
StringBuilder buffer = new StringBuilder(html);
|
||||
if(!html.EndsWith("\r\n")) buffer.Append("\r\n");
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue