-
Notifications
You must be signed in to change notification settings - Fork 365
/
CodeElement.cs
118 lines (101 loc) · 5.12 KB
/
CodeElement.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//===============================================================================================================
// System : Sandcastle Tools - Sandcastle Tools Core Class Library
// File : CodeElement.cs
// Author : Eric Woodruff (Eric@EWoodruff.us)
// Updated : 05/20/2022
// Note : Copyright 2022, Eric Woodruff, All rights reserved
//
// This file contains the class used to handle code elements in Open XML presentation styles
//
// This code is published under the Microsoft Public License (Ms-PL). A copy of the license should be
// distributed with the code and can be found at the project website: https://GitHub.com/EWSoftware/SHFB. This
// notice, the author's name, and all copyright notices must remain intact in all applications, documentation,
// and source files.
//
// Date Who Comments
// ==============================================================================================================
// 05/08/2022 EFW Created the code
//===============================================================================================================
using System;
using System.Xml.Linq;
namespace Sandcastle.Core.PresentationStyle.Transformation.Elements.OpenXml
{
/// <summary>
/// This is used to handle <c>code</c> and <c>snippet</c> elements in a topic for Open XML presentation
/// styles.
/// </summary>
public class CodeElement : OpenXmlElement
{
#region Constructor
//=====================================================================
/// <summary>
/// Constructor
/// </summary>
/// <param name="name">The element name</param>
public CodeElement(string name) : base(name)
{
}
#endregion
#region Methods
//=====================================================================
/// <inheritdoc />
public override void Render(TopicTransformationCore transformation, XElement element)
{
if(transformation == null)
throw new ArgumentNullException(nameof(transformation));
if(element == null)
throw new ArgumentNullException(nameof(element));
string language = element.Attribute("language")?.Value, title = element.Attribute("title")?.Value;
XElement row, table = new XElement(WordProcessingML + "tbl"),
tableProperties = new XElement(WordProcessingML + "tblPr",
new XElement(WordProcessingML + "tblStyle",
new XAttribute(WordProcessingML + "val", "CodeTable")),
new XElement(WordProcessingML + "tblW",
new XAttribute(WordProcessingML + "w", "5000"),
new XAttribute(WordProcessingML + "type", "pct")));
table.Add(tableProperties);
if(!String.IsNullOrWhiteSpace(title) || (title == null && !String.IsNullOrWhiteSpace(language) &&
!language.Equals("other", StringComparison.OrdinalIgnoreCase) &&
!language.Equals("none", StringComparison.OrdinalIgnoreCase)))
{
XNode content;
if(title != null)
content = new XText(title);
else
{
content = new XElement("include",
new XAttribute("item", $"devlang_{language}"),
new XAttribute("undefined", language));
}
row = new XElement(WordProcessingML + "tr",
new XElement(WordProcessingML + "trPr",
new XElement(WordProcessingML + "cnfStyle",
new XAttribute(WordProcessingML + "val", "100000000000"))),
new XElement(WordProcessingML + "tc",
new XElement(WordProcessingML + "p",
new XElement(WordProcessingML + "pPr",
new XElement(WordProcessingML + "keepNext")),
new XElement(WordProcessingML + "r",
new XElement(WordProcessingML + "t", content)))));
table.Add(row);
}
else
{
// Turn off first row formatting
tableProperties.Add(new XElement(WordProcessingML + "tblLook",
new XAttribute(WordProcessingML + "firstRow", "0"),
new XAttribute(WordProcessingML + "noHBand", "1"),
new XAttribute(WordProcessingML + "noVBand", "1")));
}
var para = new XElement(WordProcessingML + "p");
table.Add(new XElement(WordProcessingML + "tr", new XElement(WordProcessingML + "tc", para)));
transformation.RenderChildElements(para, element.Nodes());
transformation.CurrentElement.Add(table);
transformation.CurrentElement.Add(new XElement(WordProcessingML + "p",
new XElement(WordProcessingML + "pPr",
new XElement(WordProcessingML + "spacing",
new XAttribute(WordProcessingML + "after", "0")))));
}
#endregion
}
}