Skip to content

Commit 13002a1

Browse files
committed
Code improvement and refactoring.
1 parent 88980b6 commit 13002a1

File tree

8 files changed

+70
-55
lines changed

8 files changed

+70
-55
lines changed

Nodes/BBCodeSyntaxNode.cs

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,14 @@ public class BBCodeSyntaxNode : SyntaxNode
2121
/// </summary>
2222
/// <param name="tag">The tag that the node represents. If the tag is null - this is the main node.</param>
2323
/// <param name="tagRepresentation">The way that the tag is represented ([size] can be represented [size=100])</param>
24-
public BBCodeSyntaxNode(BBTag tag, string tagRepresentation)
25-
: base(tag == null ? null : tag.GetNewlineRepresentationByTagValue(tagRepresentation))
26-
{
27-
Initialize(tag, tagRepresentation);
28-
}
24+
public BBCodeSyntaxNode(BBTag tag, string tagRepresentation) : base(tag, tagRepresentation) { }
2925
/// <summary>
3026
/// Creates a bbcode syntax node with children
3127
/// </summary>
3228
/// <param name="tag">The tag that the node represents.</param>
3329
/// <param name="tagRepresentation">The way that the tag is represented ([size] can be represented [size=100])</param>
3430
/// <param name="children">The children</param>
35-
public BBCodeSyntaxNode(BBTag tag, string tagRepresentation, List<SyntaxNode> children)
36-
: base(tag == null ? null : tag.GetNewlineRepresentationByTagValue(tagRepresentation), children)
37-
{
38-
Initialize(tag, tagRepresentation);
39-
}
31+
public BBCodeSyntaxNode(BBTag tag, string tagRepresentation, List<SyntaxNode> children) : base(tag, tagRepresentation, children) { }
4032
/// <summary>
4133
/// Converts the syntax node to Html
4234
/// </summary>
@@ -75,28 +67,27 @@ public override void AddChild(SyntaxNode child)
7567
/// </summary>
7668
/// <param name="tag">The defined bbtag</param>
7769
/// <param name="representation">The way it was represented in the text (with [])</param>
78-
private void GetTagOptionFromRepresentation(BBTag tag, string representation)
70+
protected override void GetTagOptionFromRepresentation(Tag tag, string representation)
7971
{
80-
Option = tag.GetOptionValue(representation);
72+
Option = (tag as BBTag).GetOptionValue(representation);
8173
}
8274

8375
/// <summary>
8476
/// Initializes the bbcode syntax node
8577
/// </summary>
8678
/// <param name="tag">The bbtag</param>
8779
/// <param name="tagRepresentation">The representation in the text</param>
88-
private void Initialize(BBTag tag, string tagRepresentation)
80+
protected override void Initialize(string tagRepresentation)
8981
{
90-
if (tag != null && string.IsNullOrEmpty(tagRepresentation)) //if the tag is not null, we need a representation. This is to make sure I can create only root nodes like this
82+
if (Tag != null && string.IsNullOrEmpty(tagRepresentation)) //if the tag is not null, we need a representation. This is to make sure I can create only root nodes like this
9183
{
9284
throw new ArgumentNullException("tagRepresentation");
9385
}
94-
Tag = tag;
9586
IsParseContent = true; //set the default value. If the tag is not null, it will take it again.
96-
if (tag != null)
87+
if (Tag != null)
9788
{
98-
GetTagOptionFromRepresentation(tag, tagRepresentation);
99-
IsParseContent = tag.IsParseContent(tagRepresentation);
89+
GetTagOptionFromRepresentation(Tag, tagRepresentation);
90+
IsParseContent = (Tag as BBTag).IsParseContent(tagRepresentation);
10091
}
10192
}
10293

Nodes/HtmlSyntaxNode.cs

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,15 @@ public class HtmlSyntaxNode : SyntaxNode
2222
/// </summary>
2323
/// <param name="tag">The tag that the node represents. If the tag is null - this is the main node.</param>
2424
/// <param name="tagRepresentation">The way that the tag is represented (&lt;a&gt; can be represented &lt;a href="..."&gt;)</param>
25-
public HtmlSyntaxNode(HtmlTag tag, string tagRepresentation)
26-
: base(tag != null ? tag.NewlineReplacer : null)
27-
{
28-
Initialize(tag, tagRepresentation);
29-
}
25+
public HtmlSyntaxNode(HtmlTag tag, string tagRepresentation) : base(tag, tagRepresentation) { }
3026

3127
/// <summary>
3228
/// Creates an html syntax node with children
3329
/// </summary>
3430
/// <param name="tag">The tag that the node represents.</param>
3531
/// <param name="tagRepresentation">The way that the tag is represented (&lt;a&gt; can be represented &lt;a href="..."&gt;)</param>
3632
/// <param name="children">The children</param>
37-
public HtmlSyntaxNode(HtmlTag tag, string tagRepresentation, List<SyntaxNode> children)
38-
: base(tag != null ? tag.NewlineReplacer : null, children)
39-
{
40-
Initialize(tag, tagRepresentation);
41-
}
33+
public HtmlSyntaxNode(HtmlTag tag, string tagRepresentation, List<SyntaxNode> children) : base(tag, tagRepresentation, children) { }
4234

4335
/// <summary>
4436
/// Can't convert to Html, because it is in html...
@@ -88,18 +80,16 @@ public override void AddChild(SyntaxNode child)
8880
/// <summary>
8981
/// Initializes the html syntax node
9082
/// </summary>
91-
/// <param name="tag">The html tag</param>
9283
/// <param name="tagRepresentation">The representation in the text</param>
93-
private void Initialize(HtmlTag tag, string tagRepresentation)
84+
protected override void Initialize(string tagRepresentation)
9485
{
95-
if (tag != null && string.IsNullOrEmpty(tagRepresentation)) //if the tag is not null, we need a representation. This is to make sure I can create only root nodes like this
86+
if (Tag != null && string.IsNullOrEmpty(tagRepresentation)) //if the tag is not null, we need a representation. This is to make sure I can create only root nodes like this
9687
{
9788
throw new ArgumentNullException("tagRepresentation");
98-
}
99-
Tag = tag;
100-
if (tag != null)
89+
}
90+
if (Tag != null)
10191
{
102-
GetTagOptionFromRepresentation(tag, tagRepresentation);
92+
GetTagOptionFromRepresentation(Tag, tagRepresentation);
10393
}
10494
}
10595

@@ -109,9 +99,9 @@ private void Initialize(HtmlTag tag, string tagRepresentation)
10999
/// </summary>
110100
/// <param name="tag">The tag.</param>
111101
/// <param name="tagRepresentation">The representation of the tag.</param>
112-
private void GetTagOptionFromRepresentation(HtmlTag tag, string tagRepresentation)
102+
protected override void GetTagOptionFromRepresentation(Tag tag, string tagRepresentation)
113103
{
114-
TagOption = tag.GetOptionFromRepresentation(tagRepresentation); //get the option that best describes the tag
104+
TagOption = (tag as HtmlTag).GetOptionFromRepresentation(tagRepresentation); //get the option that best describes the tag
115105
if (TagOption == null) return; //this is when it's a closing tag...
116106
if (TagOption.OptionHtmlAttribute != null)
117107
{

Nodes/SyntaxNode.cs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,22 @@ public abstract class SyntaxNode
3030
/// <summary>
3131
/// Creates an empty syntax node
3232
/// </summary>
33-
/// <param name="newlineRepresentation">The string to replace the newline</param>
34-
protected SyntaxNode(string newlineRepresentation)
35-
{
36-
Children = new List<SyntaxNode>();
37-
NewlineRepresentation = newlineRepresentation;
38-
}
33+
/// <param name="tag">The tag used...</param>
34+
/// <param name="tagRepresentation">The representation of the tag.</param>
35+
protected SyntaxNode(Tag tag, string tagRepresentation) : this(tag, tagRepresentation, new List<SyntaxNode>()) { }
3936
/// <summary>
4037
/// Creates a syntax node with children
41-
/// </summary>
42-
/// <param name="newlineRepresentation">The string to replace the newline</param>
38+
/// </summary>
39+
/// <param name="tag">The tag used...</param>
40+
/// <param name="tagRepresentation">The representation of the tag.</param>
4341
/// <param name="children">The children</param>
44-
protected SyntaxNode(string newlineRepresentation, List<SyntaxNode> children)
42+
protected SyntaxNode(Tag tag, string tagRepresentation, List<SyntaxNode> children)
4543
{
4644
if (children == null) throw new ArgumentNullException("children");
45+
this.Tag = tag;
4746
Children = children;
48-
NewlineRepresentation = newlineRepresentation;
47+
NewlineRepresentation = tag == null ? null : tag.GetNewlineRepresentation(tagRepresentation);
48+
Initialize(tagRepresentation);
4949
}
5050
/// <summary>
5151
/// Converts the node to Html
@@ -63,6 +63,15 @@ protected SyntaxNode(string newlineRepresentation, List<SyntaxNode> children)
6363
/// </summary>
6464
/// <param name="child">The child</param>
6565
public abstract void AddChild(SyntaxNode child);
66+
/// <summary>
67+
/// Returns the tag option from its string representation
68+
/// </summary>
69+
protected abstract void GetTagOptionFromRepresentation(Tag tag, string representation);
70+
/// <summary>
71+
/// Initializes the syntax node
72+
/// </summary>
73+
/// <param name="tagRepresentation">The representation in the text</param>
74+
protected abstract void Initialize(string tagRepresentation);
6675

6776
public bool hasSameTags(SyntaxNode other)
6877
{

Nodes/TextSyntaxNode.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using System.Text;
55
using System.Web;
6+
using CodeTranslator.Tags;
67

78
namespace CodeTranslator.Nodes
89
{
@@ -20,7 +21,7 @@ public class TextSyntaxNode : SyntaxNode
2021
/// </summary>
2122
/// <param name="text">The text of the text syntax node.</param>
2223
public TextSyntaxNode(string text)
23-
: base(null)
24+
: base(null, text)
2425
{
2526
if (string.IsNullOrEmpty(text)) throw new ArgumentNullException("text");
2627
Text = text;
@@ -54,5 +55,15 @@ public override void AddChild(SyntaxNode child)
5455
{
5556
throw new NotSupportedException("You can't add children to a text syntax node!"); //the text syntax node doesn't have children
5657
}
58+
59+
protected override void GetTagOptionFromRepresentation(Tag tag, string representation)
60+
{
61+
throw new NotSupportedException("You can't call this method from a text syntax node.");
62+
}
63+
64+
protected override void Initialize(string tagRepresentation)
65+
{
66+
//nothing to do here
67+
}
5768
}
5869
}

Parsers/BBCodeParser.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,9 @@ protected override SyntaxNode GetNextNode(string code, ref int startIndex, out T
101101
string currentSubstring = code.Substring(startIndex);
102102
string regexPattern = previousTag == null || isParseContent ? @"\[/?[a-z*][a-z0-9]*(=[^\[\]\n\r\v\f]+)?\]" : @"\[" + previousTag.CloseTag + @"\]"; //the close tag has /
103103
Match tag = Regex.Match(currentSubstring, regexPattern);
104-
BBTag registeredTag = null;
104+
Tag registeredTag = null;
105105
tagType = TagType.Open; //we just set it here and when needed will do it later
106-
while (tag.Success && (registeredTag = (GetTagByValue(tag.Value) as BBTag)) == null)//getting tags, but they are not registered.
106+
while (tag.Success && (registeredTag = GetTagByValue(tag.Value)) == null)//getting tags, but they are not registered.
107107
{
108108
tag = tag.NextMatch();
109109
}
@@ -116,7 +116,7 @@ protected override SyntaxNode GetNextNode(string code, ref int startIndex, out T
116116
{
117117
if (tag.Value.StartsWith("[/")) tagType = TagType.Close;
118118
startIndex += tag.Value.Length;
119-
return new BBCodeSyntaxNode(registeredTag, tag.Value);
119+
return new BBCodeSyntaxNode(registeredTag as BBTag, tag.Value);
120120
}
121121
//there has been some text before the tag that I found...
122122
startIndex += tag.Index; //move the start index for the other iterations

Tags/BBTag.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,10 @@ private TagOption GetOptionValuesForOption(string option)
9797
}
9898

9999
/// <summary>
100-
/// Returns the correct newline replacer by the given tag value. If it's a closing tag,
101-
/// we can't say anything.
100+
/// Returns the correct newline representation for the tag.
102101
/// </summary>
103-
/// <param name="tagValue">The value of the tag</param>
104-
public string GetNewlineRepresentationByTagValue(string tagValue)
102+
/// <param name="tagValue">The tag value used to determine the newline representation.</param>
103+
public override string GetNewlineRepresentation(string tagValue)
105104
{
106105
string value = GetAndValidateTagOptionValue(tagValue);
107106
if (value == null && tagValue.StartsWith("[/")) return null; //if it's a closing tag, we can't say anything.

Tags/HtmlTag.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,16 @@ public override bool IsValid(string tagValue)
7878
return false;
7979
}
8080

81+
/// <summary>
82+
/// Returns the correct newline representation for the tag. In HTML tag it is
83+
/// the one that is set and the tagValue parameter is not used.
84+
/// </summary>
85+
/// <param name="tagValue">The tag value used to determine the newline representation.</param>
86+
public override string GetNewlineRepresentation(string tagValue)
87+
{
88+
return NewlineReplacer;
89+
}
90+
8191
/// <summary>
8292
/// Returns the most suitable HtmlOption, based on the tag representation
8393
/// </summary>

Tags/Tag.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ public Tag(string openTag, string closeTag, params TagOption[] tagOptions)
5050
/// </summary>
5151
/// <param name="tagValue">The tag value</param>
5252
public abstract bool IsValid(string tagValue);
53+
/// <summary>
54+
/// Returns the correct newline representation for the tag.
55+
/// </summary>
56+
/// <param name="tagValue">The tag value used to determine the newline representation.</param>
57+
public abstract string GetNewlineRepresentation(string tagValue);
5358

5459
public override bool Equals(object obj)
5560
{

0 commit comments

Comments
 (0)