-
Notifications
You must be signed in to change notification settings - Fork 365
/
CiteElement.cs
115 lines (97 loc) · 4.45 KB
/
CiteElement.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
//===============================================================================================================
// System : Sandcastle Tools - Sandcastle Tools Core Class Library
// File : CiteElement.cs
// Author : Eric Woodruff (Eric@EWoodruff.us)
// Updated : 07/23/2022
// Note : Copyright 2022, Eric Woodruff, All rights reserved
//
// This file contains the class used to handle cite elements
//
// 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
// ==============================================================================================================
// 01/23/2022 EFW Created the code
//===============================================================================================================
using System;
using System.Xml.Linq;
using Sandcastle.Core.PresentationStyle.Transformation.Elements.OpenXml;
namespace Sandcastle.Core.PresentationStyle.Transformation.Elements
{
/// <summary>
/// This is used to handle general <c>cite</c> elements in a topic
/// </summary>
public class CiteElement : Element
{
#region Properties
//=====================================================================
/// <summary>
/// This is used to get or set the citation style for HTML output formats
/// </summary>
/// <value>The default if not set explicitly is "citation"</value>
public string CitationStyle { get; set; } = "citation";
#endregion
#region Constructor
//=====================================================================
/// <inheritdoc />
public CiteElement() : base("cite")
{
}
#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 cite = element.Value?.NormalizeWhiteSpace();
if(!String.IsNullOrWhiteSpace(cite) &&
transformation.ElementHandlerFor("bibliography") is BibliographyElement handler)
{
// Determine the citation index
var citations = handler.DetermineCitations(transformation);
int idx = 1, foundIdx = -1;
foreach(string c in citations)
{
if(c.Equals(cite, StringComparison.OrdinalIgnoreCase))
{
foundIdx = idx;
break;
}
idx++;
}
if(foundIdx != -1)
{
XElement c;
if(transformation.SupportedFormats != HelpFileFormats.OpenXml)
{
c = new XElement("sup",
new XElement("a",
new XAttribute("href", $"#cite{foundIdx}"),
$"[{foundIdx}]"));
if(transformation.SupportedFormats != HelpFileFormats.Markdown)
c.Add(new XAttribute("class", this.CitationStyle));
}
else
{
c = new XElement("a",
new XAttribute("href", $"#cite{foundIdx}"),
new XElement(OpenXmlElement.WordProcessingML + "r",
new XElement(OpenXmlElement.WordProcessingML + "rPr",
new XElement(OpenXmlElement.WordProcessingML + "vertAlign",
new XAttribute(OpenXmlElement.WordProcessingML + "val", "superscript"))),
new XElement(OpenXmlElement.WordProcessingML + "t", $"[{foundIdx}]")));
}
transformation.CurrentElement.Add(c);
}
}
}
#endregion
}
}