| | 1 | | namespace Songhay.Xml; |
| | 2 | |
|
| | 3 | | /// <summary> |
| | 4 | | /// Static members for XHTML Documents. |
| | 5 | | /// </summary> |
| | 6 | | public static partial class XhtmlDocumentUtility |
| | 7 | | { |
| | 8 | | /// <summary> |
| | 9 | | /// XHTML Namespace |
| | 10 | | /// </summary> |
| 0 | 11 | | public static XNamespace Xhtml => "http://www.w3.org/1999/xhtml"; |
| | 12 | |
|
| | 13 | | /// <summary> |
| | 14 | | /// Loads the <see cref="XhtmlDocument"/>. |
| | 15 | | /// </summary> |
| | 16 | | /// <param name="document">The XML document.</param> |
| | 17 | | /// <param name="webPath">The public web path.</param> |
| | 18 | | public static XhtmlDocument? GetDocument(XDocument? document, string? webPath) => |
| 0 | 19 | | GetDocument(document, webPath, true); |
| | 20 | |
|
| | 21 | | /// <summary> |
| | 22 | | /// Loads the <see cref="XhtmlDocument"/>. |
| | 23 | | /// </summary> |
| | 24 | | /// <param name="document">The XML document.</param> |
| | 25 | | /// <param name="webPath">The public web path.</param> |
| | 26 | | /// <param name="useXhtmlNamespace">if set to <c>true</c> use XHTML namespace (<c>true</c> by default).</param> |
| | 27 | | public static XhtmlDocument? GetDocument(XDocument? document, string? webPath, bool useXhtmlNamespace) |
| 0 | 28 | | { |
| 0 | 29 | | if (document == null) return null; |
| | 30 | |
|
| 0 | 31 | | var heading = useXhtmlNamespace |
| 0 | 32 | | ? document.Root? |
| 0 | 33 | | .Element(Xhtml + "body")? |
| 0 | 34 | | .Element(Xhtml + "h1") |
| 0 | 35 | | : document.Root? |
| 0 | 36 | | .Element("body")? |
| 0 | 37 | | .Element("h1"); |
| | 38 | |
|
| 0 | 39 | | var title = useXhtmlNamespace |
| 0 | 40 | | ? document.Root? |
| 0 | 41 | | .Element(Xhtml + "head")? |
| 0 | 42 | | .Element(Xhtml + "title")? |
| 0 | 43 | | .Value |
| 0 | 44 | | : document.Root? |
| 0 | 45 | | .Element("head")? |
| 0 | 46 | | .Element("title")? |
| 0 | 47 | | .Value; |
| | 48 | |
|
| 0 | 49 | | var d = new XhtmlDocument |
| 0 | 50 | | { |
| 0 | 51 | | Header = heading?.Value, |
| 0 | 52 | | Location = webPath, |
| 0 | 53 | | Title = title |
| 0 | 54 | | }; |
| | 55 | |
|
| 0 | 56 | | return d; |
| 0 | 57 | | } |
| | 58 | |
|
| | 59 | | /// <summary> |
| | 60 | | /// Loads the <see cref="XhtmlDocument"/>. |
| | 61 | | /// </summary> |
| | 62 | | /// <param name="pathToDocument">The path to document.</param> |
| | 63 | | /// <param name="webPath">The public web path.</param> |
| | 64 | | public static XhtmlDocument? LoadDocument(string? pathToDocument, string? webPath) |
| 0 | 65 | | { |
| 0 | 66 | | var xd = XDocument.Load(pathToDocument!); |
| 0 | 67 | | var hasAttributes = (xd.Root?.HasAttributes).GetValueOrDefault(); |
| 0 | 68 | | var hasXhtmlNamespace = false; |
| 0 | 69 | | if (hasAttributes) hasXhtmlNamespace = (xd.Root?.Attributes("xmlns") ?? Array.Empty<XAttribute>()).Any(); |
| 0 | 70 | | if (hasAttributes && hasXhtmlNamespace) |
| 0 | 71 | | { |
| 0 | 72 | | return GetDocument(xd, webPath); |
| | 73 | | } |
| | 74 | |
|
| 0 | 75 | | return GetDocument(xd, webPath, false); |
| 0 | 76 | | } |
| | 77 | |
|
| | 78 | | /// <summary> |
| | 79 | | /// Writes the index of XHTML documents. |
| | 80 | | /// </summary> |
| | 81 | | /// <param name="indexFileName">Name of the index file.</param> |
| | 82 | | /// <param name="indexTitle">The index title.</param> |
| | 83 | | /// <param name="publicRoot">The public root.</param> |
| | 84 | | /// <param name="pathToDirectory">The path to the specified directory.</param> |
| | 85 | | /// <param name="pathToOutput">The path to output.</param> |
| | 86 | | public static void WriteDocumentIndex(string? indexFileName, |
| | 87 | | string? indexTitle, string? publicRoot, |
| | 88 | | string? pathToDirectory, string? pathToOutput) |
| 0 | 89 | | { |
| 0 | 90 | | var directory = new DirectoryInfo(pathToDirectory!); |
| 0 | 91 | | var list = new List<XhtmlDocument>(); |
| 0 | 92 | | directory.GetFiles() |
| 0 | 93 | | .ForEachInEnumerable(f => |
| 0 | 94 | | { |
| 0 | 95 | | var uri = string.Concat(publicRoot, f.Name); |
| 0 | 96 | | var d = LoadDocument(f.FullName, uri); |
| 0 | 97 | | if (d != null) list.Add(d); |
| 0 | 98 | | }); |
| | 99 | |
|
| 0 | 100 | | var serializer = new XmlSerializer(typeof(XhtmlDocuments)); |
| | 101 | |
|
| 0 | 102 | | using var writer = new XmlTextWriter(string.Concat(pathToOutput, indexFileName), Encoding.UTF8); |
| | 103 | |
|
| 0 | 104 | | var documents = new XhtmlDocuments |
| 0 | 105 | | { |
| 0 | 106 | | Documents = list.OrderBy(d => d.Title).ToArray(), |
| 0 | 107 | | Title = indexTitle |
| 0 | 108 | | }; |
| | 109 | |
|
| 0 | 110 | | serializer.Serialize(writer, documents); |
| 0 | 111 | | } |
| | 112 | | } |