Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions src/Highlight/Configuration/XmlConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text.RegularExpressions;
using System.Xml.Linq;
Expand All @@ -23,7 +22,8 @@ public IDictionary<string, Definition> Definitions

public XmlConfiguration(XDocument xmlDocument)
{
if (xmlDocument == null) {
if (xmlDocument == null)
{
throw new ArgumentNullException("xmlDocument");
}

Expand All @@ -36,7 +36,8 @@ protected XmlConfiguration()

private IDictionary<string, Definition> GetDefinitions()
{
if (definitions == null) {
if (definitions == null)
{
definitions = XmlDocument
.Descendants("definition")
.Select(GetDefinition)
Expand Down Expand Up @@ -70,13 +71,16 @@ private Pattern GetPattern(XElement patternElement)
{
const StringComparison stringComparison = StringComparison.OrdinalIgnoreCase;
var patternType = patternElement.GetAttributeValue("type");
if (patternType.Equals("block", stringComparison)) {
if (patternType.Equals("block", stringComparison))
{
return GetBlockPattern(patternElement);
}
if (patternType.Equals("markup", stringComparison)) {
if (patternType.Equals("markup", stringComparison))
{
return GetMarkupPattern(patternElement);
}
if (patternType.Equals("word", stringComparison)) {
if (patternType.Equals("word", stringComparison))
{
return GetWordPattern(patternElement);
}

Expand Down Expand Up @@ -119,7 +123,8 @@ private IEnumerable<string> GetPatternWords(XContainer patternElement)
{
var words = new List<string>();
var wordElements = patternElement.Descendants("word");
if (wordElements != null) {
if (wordElements != null)
{
words.AddRange(from wordElement in wordElements select Regex.Escape(wordElement.Value));
}

Expand All @@ -146,10 +151,11 @@ private ColorPair GetPatternColors(XElement fontElement)
private Font GetPatternFont(XElement fontElement, Font defaultFont = null)
{
var fontFamily = fontElement.GetAttributeValue("name");
if (fontFamily != null) {
if (fontFamily != null)
{
var emSize = fontElement.GetAttributeValue("size").ToSingle(11f);
var style = Enum<FontStyle>.Parse(fontElement.GetAttributeValue("style"), FontStyle.Regular, true);

return SystemFonts.CreateFont(fontFamily, emSize, style);
}

Expand Down Expand Up @@ -178,7 +184,8 @@ private ColorPair GetMarkupPatternColors(XContainer patternElement, XName descen
{
var fontElement = patternElement.Descendants("font").Single();
var element = fontElement.Descendants(descendantName).SingleOrDefault();
if (element != null) {
if (element != null)
{
var colors = GetPatternColors(element);

return colors;
Expand Down Expand Up @@ -209,8 +216,8 @@ private Font GetDefinitionFont(XElement fontElement)
{
var fontName = fontElement.GetAttributeValue("name");
var fontSize = Convert.ToSingle(fontElement.GetAttributeValue("size"));
var fontStyle = (FontStyle) Enum.Parse(typeof(FontStyle), fontElement.GetAttributeValue("style"), true);
var fontStyle = (FontStyle)Enum.Parse(typeof(FontStyle), fontElement.GetAttributeValue("style"), true);

return SystemFonts.CreateFont(fontName, fontSize, fontStyle);
}
}
Expand Down
25 changes: 16 additions & 9 deletions src/Highlight/Engines/HtmlEngineHelper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Drawing;
using System.Text;
using Highlight.Patterns;
using SixLabors.Fonts;
Expand Down Expand Up @@ -27,26 +26,34 @@ public static string CreatePatternStyle(Style style)
public static string CreatePatternStyle(ColorPair colors, Font font)
{
var patternStyle = new StringBuilder();
if (colors != null) {
if (colors.ForeColor != Color.Empty) {
if (colors != null)
{
if (colors.ForeColor != Color.Empty)
{
patternStyle.Append("color: " + colors.ForeColor.Name + ";");
}
if (colors.BackColor != Color.Empty) {
if (colors.BackColor != Color.Empty)
{
patternStyle.Append("background-color: " + colors.BackColor.Name + ";");
}
}

if (font != null) {
if (font.Name != null) {
if (font != null)
{
if (font.Name != null)
{
patternStyle.Append("font-family: " + font.Name + ";");
}
if (font.Size > 0f) {
if (font.Size > 0f)
{
patternStyle.Append("font-size: " + font.Size + "px;");
}
if (font.Instance.Description.Style == FontStyle.Regular) {
if (font.Instance.Description.Style == FontStyle.Regular)
{
patternStyle.Append("font-weight: normal;");
}
if (font.Instance.Description.Style == FontStyle.Bold) {
if (font.Instance.Description.Style == FontStyle.Bold)
{
patternStyle.Append("font-weight: bold;");
}
}
Expand Down
31 changes: 20 additions & 11 deletions src/Highlight/Engines/RtfEngine.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Runtime.InteropServices;
Expand Down Expand Up @@ -49,19 +48,23 @@ protected override string ProcessMarkupPatternMatch(Definition definition, Marku
var bracketStyle = CreateRtfPatternStyle(pattern.BracketColors.ForeColor, pattern.BracketColors.BackColor, pattern.Style.Font);
string attributeNameStyle = null;
string attributeValueStyle = null;
if (pattern.HighlightAttributes) {
if (pattern.HighlightAttributes)
{
attributeNameStyle = CreateRtfPatternStyle(pattern.AttributeNameColors.ForeColor, pattern.AttributeNameColors.BackColor, pattern.Style.Font);
attributeValueStyle = CreateRtfPatternStyle(pattern.AttributeValueColors.ForeColor, pattern.AttributeValueColors.BackColor, pattern.Style.Font);
}
builder.AppendFormat(RtfFormat, bracketStyle, match.Groups["openTag"].Value);
builder.Append(match.Groups["ws1"].Value);
builder.AppendFormat(RtfFormat, style, match.Groups["tagName"].Value);
if (attributeNameStyle != null) {
for (var i = 0; i < match.Groups["attribute"].Captures.Count; i++) {
if (attributeNameStyle != null)
{
for (var i = 0; i < match.Groups["attribute"].Captures.Count; i++)
{
builder.Append(match.Groups["ws2"].Captures[i].Value);
builder.AppendFormat(RtfFormat, attributeNameStyle, match.Groups["attribName"].Captures[i].Value);

if (String.IsNullOrWhiteSpace(match.Groups["attribValue"].Captures[i].Value)) {
if (String.IsNullOrWhiteSpace(match.Groups["attribValue"].Captures[i].Value))
{
continue;
}

Expand Down Expand Up @@ -89,18 +92,21 @@ private string CreateRtfPatternStyle(Color foreColor, Color backColor, Font font
private int GetIndexOfColor(Color color)
{
var color2 = new HexColor();
if (color.Name.IndexOf("#") > -1) {
if (color.Name.IndexOf("#") > -1)
{
color2.Red = Int32.Parse(color.Name.Substring(1, 2), NumberStyles.AllowHexSpecifier);
color2.Green = Int32.Parse(color.Name.Substring(3, 2), NumberStyles.AllowHexSpecifier);
color2.Blue = Int32.Parse(color.Name.Substring(5, 2), NumberStyles.AllowHexSpecifier);
}
else {
else
{
color2.Red = color.R;
color2.Green = color.G;
color2.Blue = color.B;
}
var index = colors.IndexOf(color2);
if (index > -1) {
if (index > -1)
{
return (index + 1);
}
colors.Add(color2);
Expand All @@ -111,7 +117,8 @@ private int GetIndexOfColor(Color color)
private int GetIndexOfFont(string font)
{
var index = fonts.IndexOf(font);
if (index > -1) {
if (index > -1)
{
return (index + 1);
}
fonts.Add(font);
Expand All @@ -122,7 +129,8 @@ private int GetIndexOfFont(string font)
private string BuildColorList()
{
var builder = new StringBuilder();
foreach (var hexColor in colors.Cast<HexColor>()) {
foreach (var hexColor in colors.Cast<HexColor>())
{
builder.AppendFormat(@"\red{0}\green{1}\blue{2};", hexColor.Red, hexColor.Green, hexColor.Blue);
}
return builder.ToString();
Expand All @@ -131,7 +139,8 @@ private string BuildColorList()
private string BuildFontList()
{
var builder = new StringBuilder();
for (var i = 0; i < fonts.Count; i++) {
for (var i = 0; i < fonts.Count; i++)
{
builder.AppendFormat(@"\f{0} {1};", i, fonts[i]);
}
return builder.ToString();
Expand Down
57 changes: 57 additions & 0 deletions src/Highlight/Patterns/Color.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System.Globalization;

// Replacement for System.Drawing.Color which is not available on non-Windows platforms
namespace Highlight
{
public class Color
{
public string Name { get; set; } // Either Hex or Web Color Name
public int R { get; set; }
public int G { get; set; }
public int B { get; set; }

public Color(string name, int r, int g, int b)
{
Name = name;
R = r;
G = g;
B = b;
}

public static Color Empty = new Color("#FFFFFF", 255, 255, 255);

public static Color FromName(string name)
{
// Todo: Add extended colors from https://en.wikipedia.org/wiki/Web_colors#HTML_color_names
if (name.IndexOf("#") == -1)
{
name = name switch
{
"white" => "#FFFFFF",
"silver" => "#C0C0C0",
"gray" => "#808080",
"black" => "#000000",
"red" => "#FF0000",
"maroon" => "#800000",
"yellow" => "#FFFF00",
"olive" => "#808000",
"lime" => "#00FF00",
"green" => "#008000",
"aqua" => "#00FFFF",
"teal" => "#008080",
"blue" => "#0000FF",
"navy" => "#000080",
"fuchsia" => "#FF00FF",
"purple" => "#800080",
_ => throw new System.Exception("Unknown color name: " + name),
};
}

int red = int.Parse(name.Substring(1, 2), NumberStyles.AllowHexSpecifier);
int green = int.Parse(name.Substring(3, 2), NumberStyles.AllowHexSpecifier);
int blue = int.Parse(name.Substring(5, 2), NumberStyles.AllowHexSpecifier);

return new Color(name, red, green, blue);
}
}
}
2 changes: 0 additions & 2 deletions src/Highlight/Patterns/ColorPair.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Drawing;

namespace Highlight.Patterns
{
public class ColorPair
Expand Down
1 change: 0 additions & 1 deletion src/Highlight/Patterns/Style.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Drawing;
using SixLabors.Fonts;

namespace Highlight.Patterns
Expand Down