forked from egonl/SharpDocx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduced document directives. Added support for referencing assembl…
…ies and importing namespaces in views. Examples: -To reference an assembly, use <%@ Assembly Name="System.Speech" %>. -To use a namespace, use <%@ Import Namespace="System.Speech.Synthesis" %>.
- Loading branch information
Showing
10 changed files
with
208 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using SharpDocx.Extensions; | ||
using SharpDocx.Models; | ||
|
||
namespace SharpDocx | ||
{ | ||
public class Directive : CodeBlock | ||
{ | ||
public string Name { get; } | ||
|
||
public Dictionary<string, string> Attributes { get; } = new Dictionary<string, string>(); | ||
|
||
public Directive(string code) : base(code) | ||
{ | ||
var stringExpressions = new Dictionary<string, string>(); | ||
string stringExpression; | ||
var startIndex = 0; | ||
|
||
do | ||
{ | ||
stringExpression = GetExpressionInApostrophes(code, startIndex); | ||
if (stringExpression != null) | ||
{ | ||
// Replace attribute values enclosed in apostrophes with guids to avoid parsing problems later. | ||
// E.g. attribute="The value of 1 + 1 = 2." becomes attribute=041fb3eedc5349d88437082a71f00ee5 | ||
var stringId = Guid.NewGuid().ToString("N"); | ||
stringExpressions.Add(stringId, stringExpression); | ||
code = code.Replace($"\"{stringExpression}\"", stringId); | ||
startIndex = code.IndexOf(stringId) + stringId.Length; | ||
} | ||
} while (stringExpression != null); | ||
|
||
var tagContents = code | ||
.Replace('\n', ' ') | ||
.RemoveRedundantWhitespace() | ||
.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries); | ||
|
||
Name = tagContents[0].ToLower(); | ||
|
||
for (var i = 1; i < tagContents.Length; ++i) | ||
{ | ||
var attributeContents = tagContents[i].Split('='); | ||
|
||
Attributes.Add( | ||
attributeContents[0].ToLower(), | ||
stringExpressions.ContainsKey(attributeContents[1]) | ||
? stringExpressions[attributeContents[1]] | ||
: attributeContents[1]); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System.Text; | ||
|
||
namespace SharpDocx.Extensions | ||
{ | ||
public static class StringExtensions | ||
{ | ||
public static string RemoveRedundantWhitespace(this string s) | ||
{ | ||
var sb = new StringBuilder(); | ||
var previousCharIsWhitespace = false; | ||
|
||
foreach (var c in s) | ||
{ | ||
if (char.IsWhiteSpace(c) && previousCharIsWhitespace) | ||
{ | ||
continue; | ||
} | ||
|
||
previousCharIsWhitespace = char.IsWhiteSpace(c); | ||
sb.Append(c); | ||
} | ||
|
||
return sb.ToString(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters