Skip to content

Commit e6c6850

Browse files
committed
Create the BBCode translator repository. There are some bugs, so start fixing them.
0 parents  commit e6c6850

24 files changed

+1918
-0
lines changed

.gitattributes

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto
3+
4+
# Custom for Visual Studio
5+
*.cs diff=csharp
6+
*.sln merge=union
7+
*.csproj merge=union
8+
*.vbproj merge=union
9+
*.fsproj merge=union
10+
*.dbproj merge=union
11+
12+
# Standard to msysgit
13+
*.doc diff=astextplain
14+
*.DOC diff=astextplain
15+
*.docx diff=astextplain
16+
*.DOCX diff=astextplain
17+
*.dot diff=astextplain
18+
*.DOT diff=astextplain
19+
*.pdf diff=astextplain
20+
*.PDF diff=astextplain
21+
*.rtf diff=astextplain
22+
*.RTF diff=astextplain

.gitignore

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
#################
2+
## Eclipse
3+
#################
4+
5+
*.pydevproject
6+
.project
7+
.metadata
8+
bin/
9+
tmp/
10+
*.tmp
11+
*.bak
12+
*.swp
13+
*~.nib
14+
local.properties
15+
.classpath
16+
.settings/
17+
.loadpath
18+
19+
# External tool builders
20+
.externalToolBuilders/
21+
22+
# Locally stored "Eclipse launch configurations"
23+
*.launch
24+
25+
# CDT-specific
26+
.cproject
27+
28+
# PDT-specific
29+
.buildpath
30+
31+
32+
#################
33+
## Visual Studio
34+
#################
35+
36+
## Ignore Visual Studio temporary files, build results, and
37+
## files generated by popular Visual Studio add-ons.
38+
39+
# User-specific files
40+
*.suo
41+
*.user
42+
*.sln.docstates
43+
44+
# Build results
45+
[Dd]ebug/
46+
[Rr]elease/
47+
*_i.c
48+
*_p.c
49+
*.ilk
50+
*.meta
51+
*.obj
52+
*.pch
53+
*.pdb
54+
*.pgc
55+
*.pgd
56+
*.rsp
57+
*.sbr
58+
*.tlb
59+
*.tli
60+
*.tlh
61+
*.tmp
62+
*.vspscc
63+
.builds
64+
*.dotCover
65+
66+
## TODO: If you have NuGet Package Restore enabled, uncomment this
67+
#packages/
68+
69+
# Visual C++ cache files
70+
ipch/
71+
*.aps
72+
*.ncb
73+
*.opensdf
74+
*.sdf
75+
76+
# Visual Studio profiler
77+
*.psess
78+
*.vsp
79+
80+
# ReSharper is a .NET coding add-in
81+
_ReSharper*
82+
83+
# Installshield output folder
84+
[Ee]xpress
85+
86+
# DocProject is a documentation generator add-in
87+
DocProject/buildhelp/
88+
DocProject/Help/*.HxT
89+
DocProject/Help/*.HxC
90+
DocProject/Help/*.hhc
91+
DocProject/Help/*.hhk
92+
DocProject/Help/*.hhp
93+
DocProject/Help/Html2
94+
DocProject/Help/html
95+
96+
# Click-Once directory
97+
publish
98+
99+
# Others
100+
[Bb]in
101+
[Oo]bj
102+
sql
103+
TestResults
104+
*.Cache
105+
ClientBin
106+
stylecop.*
107+
~$*
108+
*.dbmdl
109+
Generated_Code #added for RIA/Silverlight projects
110+
111+
# Backup & report files from converting an old project file to a newer
112+
# Visual Studio version. Backup files are not needed, because we have git ;-)
113+
_UpgradeReport_Files/
114+
Backup*/
115+
UpgradeLog*.XML
116+
117+
118+
119+
############
120+
## Windows
121+
############
122+
123+
# Windows image file caches
124+
Thumbs.db
125+
126+
# Folder config file
127+
Desktop.ini
128+
129+
130+
#############
131+
## Python
132+
#############
133+
134+
*.py[co]
135+
136+
# Packages
137+
*.egg
138+
*.egg-info
139+
dist
140+
build
141+
eggs
142+
parts
143+
bin
144+
var
145+
sdist
146+
develop-eggs
147+
.installed.cfg
148+
149+
# Installer logs
150+
pip-log.txt
151+
152+
# Unit test / coverage reports
153+
.coverage
154+
.tox
155+
156+
#Translations
157+
*.mo
158+
159+
#Mr Developer
160+
.mr.developer.cfg
161+
162+
# Mac crap
163+
.DS_Store

Attributes/AttributeOption.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace CodeTranslator.Attributes
7+
{
8+
/// <summary>
9+
/// A class that represents an attribute option:
10+
/// if we have style="font-size: 100%;", this class represents
11+
/// font-size: 100%;
12+
/// </summary>
13+
public class AttributeOption
14+
{
15+
/// <summary>
16+
/// Separates the name from the value
17+
/// </summary>
18+
public const string ValueSeparator = ":";
19+
/// <summary>
20+
/// Delimits different attributevalues
21+
/// </summary>
22+
public string OptionDelimiter { get; private set; }
23+
/// <summary>
24+
/// The name of the attribute value, e. g. 'font-style'. (No :)
25+
/// </summary>
26+
public string Name { get; private set; }
27+
28+
/// <summary>
29+
/// Creates an attribute option.
30+
/// </summary>
31+
/// <param name="name">The name, e. g. 'font-style'. (No :)</param>
32+
/// <param name="optionDelimiter">The delimiter, until which the value is taken. We can use space.</param>
33+
public AttributeOption(string name, string optionDelimiter)
34+
{
35+
if (string.IsNullOrEmpty(name.Trim())) throw new ArgumentNullException("name");
36+
if (string.IsNullOrEmpty(optionDelimiter)) throw new ArgumentNullException("optionDelimiter");
37+
Name = name.Trim();
38+
OptionDelimiter = optionDelimiter;
39+
}
40+
41+
public override bool Equals(object obj)
42+
{
43+
AttributeOption av = obj as AttributeOption;
44+
if (av == null) return false;
45+
return this.GetHashCode() == av.GetHashCode();
46+
}
47+
48+
public override int GetHashCode()
49+
{
50+
return Name.GetHashCode(); //we don't care about the option delimiter!
51+
}
52+
}
53+
}

Attributes/HtmlAttribute.cs

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Text.RegularExpressions;
6+
7+
namespace CodeTranslator.Attributes
8+
{
9+
/// <summary>
10+
/// Represents a base class for the different attribute types.
11+
/// </summary>
12+
public class HtmlAttribute
13+
{
14+
/// <summary>
15+
/// The attribute name, e. g. style
16+
/// </summary>
17+
public string Name { get; private set; }
18+
/// <summary>
19+
/// The value for the current attribute, e. g.
20+
/// for the font-size, width, etc. value of the style attribute.
21+
/// </summary>
22+
public AttributeOption Value { get; private set; }
23+
/// <summary>
24+
/// Tells if the current attribute has a value (part which we use.)
25+
/// </summary>
26+
public bool HasValue { get { return Value != null; } }
27+
28+
/// <summary>
29+
/// Creates a new html attribute without a specific option (takes the whole value).
30+
/// </summary>
31+
/// <param name="name">The name of the attribute</param>
32+
public HtmlAttribute(string name)
33+
{
34+
if (string.IsNullOrEmpty(name.Trim())) throw new ArgumentNullException("name");
35+
Name = name.Trim();
36+
}
37+
38+
/// <summary>
39+
/// Creates a new html attribute with a specific option (takes a part of the value).
40+
/// </summary>
41+
/// <param name="name">The name of the attribute.</param>
42+
/// <param name="value">The value we're interested in.</param>
43+
public HtmlAttribute(string name, AttributeOption value)
44+
: this(name)
45+
{
46+
if (value == null) throw new ArgumentNullException("value");
47+
Value = value;
48+
}
49+
50+
public override bool Equals(object obj)
51+
{
52+
HtmlAttribute attr = obj as HtmlAttribute;
53+
if (attr == null) return false;
54+
return this.GetHashCode() == attr.GetHashCode();
55+
}
56+
57+
public override int GetHashCode()
58+
{
59+
//here I use the Name only, because:
60+
//if we have two pairs of options:
61+
//A(x)-B and A-B, where A(x) means only the option 'x' from attribute A and just A means the whole attribute value,
62+
//we won't know which of them to use. We will have to add additional options, which I don't need right now.
63+
//I need to use the value here!
64+
return Name.GetHashCode() ^ (Value != null ? Value.GetHashCode() : -1);
65+
}
66+
67+
/// <summary>
68+
/// Gets the tag representation and returns all attributes that are currently used.
69+
/// </summary>
70+
/// <param name="tagRepresentation">The tag representation - usually without &lt;&gt;, but will work with them, too...</param>
71+
public static HtmlAttribute[] GetAttributes(string tagRepresentation)
72+
{
73+
if (string.IsNullOrEmpty(tagRepresentation)) throw new ArgumentNullException("tagRepresentation");
74+
List<HtmlAttribute> result = new List<HtmlAttribute>();
75+
Match attribute = Regex.Match(tagRepresentation, "\\w+=\"[^\"]*\"");
76+
//make all possible combinations - the whole value and all possible attribute values.
77+
while (attribute.Success)
78+
{
79+
string attributeName = attribute.Value.Substring(0, attribute.Value.IndexOf('='));
80+
result.Add(new HtmlAttribute(attributeName));
81+
foreach (Match attributeValue in Regex.Matches(attribute.Value.Substring(attribute.Value.IndexOf('=')),"[\\w\\-]+\\s*" + AttributeOption.ValueSeparator)) //the attributes are usually words or words with '-' ending on ':' - if the value separator is different than ":" there may be a need to change something!!!
82+
{
83+
result.Add(new HtmlAttribute(attributeName, new AttributeOption(attributeValue.Value.Substring(0, attributeValue.Value.IndexOf(AttributeOption.ValueSeparator)).Trim(), ";")));//the option delimiter here is not important
84+
}
85+
attribute = attribute.NextMatch();
86+
}
87+
return result.ToArray();
88+
}
89+
90+
/// <summary>
91+
/// Returns the value of the given attribute in the given tag representation
92+
/// </summary>
93+
/// <param name="tagRepresentation">The tag representation.</param>
94+
/// <param name="attribute">The attribute which value we need.</param>
95+
public static string GetAttributeValue(string tagRepresentation, HtmlAttribute attribute)
96+
{
97+
if (string.IsNullOrEmpty(tagRepresentation)) throw new ArgumentNullException("tagRepresentation");
98+
if (attribute == null) throw new ArgumentNullException("attribute");
99+
//there can be a problem if the attribute name contains some regex symbols.
100+
Match attr = Regex.Match(tagRepresentation, attribute.Name + "=\"[^\"]*\"");
101+
if (!attr.Success) return null; //don't have such attribute.
102+
if (!attribute.HasValue) return attr.Value.Substring(attr.Value.IndexOf('=') + 1).Trim('"');//if we want to get a whole value attribute - return only the value without the quotation marks...
103+
string attributeNameWithSeparator = attribute.Value.Name + AttributeOption.ValueSeparator;
104+
if (!attr.Value.Contains(attributeNameWithSeparator)) return null;
105+
string attrVal = attr.Value.Substring(attr.Value.IndexOf(attributeNameWithSeparator) + attributeNameWithSeparator.Length).Trim(); //get everything after the name
106+
if (!attrVal.Contains(attribute.Value.OptionDelimiter)) return attrVal; //if there is no delimiter, return everything else.
107+
return attrVal.Substring(0, attrVal.IndexOf(attribute.Value.OptionDelimiter)).Trim();
108+
}
109+
}
110+
}

0 commit comments

Comments
 (0)