mirror of
				https://github.com/Theodor-Springmann-Stiftung/hamann-ausgabe-core.git
				synced 2025-10-30 01:35:32 +00:00 
			
		
		
		
	Ported libs fo net V6
This commit is contained in:
		
							
								
								
									
										64
									
								
								HaWeb/HTMLHelpers/ConversionHelpers.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										64
									
								
								HaWeb/HTMLHelpers/ConversionHelpers.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,64 @@ | ||||
| namespace HaWeb.HTMLHelpers; | ||||
|  | ||||
| public static class ConversionHelpers { | ||||
|     public static string[] MonthNames = { "", "Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember" }; | ||||
|  | ||||
|     private static Dictionary<char, int> RomanMap = new Dictionary<char, int>() | ||||
|     { | ||||
|             {'I', 1}, | ||||
|             {'V', 5}, | ||||
|             {'X', 10}, | ||||
|             {'L', 50}, | ||||
|             {'C', 100}, | ||||
|             {'D', 500}, | ||||
|             {'M', 1000} | ||||
|     }; | ||||
|  | ||||
|     public static int RomanToInteger(string roman) | ||||
|     { | ||||
|         var ro = roman.ToUpper(); | ||||
|         int number = 0; | ||||
|         for (int i = 0; i < roman.Length; i++) | ||||
|         { | ||||
|             if (RomanMap.ContainsKey(ro[i]) && (i + 1 >= ro.Length || RomanMap.ContainsKey(ro[i + 1]))) | ||||
|             { | ||||
|                 if (i + 1 < ro.Length && RomanMap[ro[i]] < RomanMap[ro[i + 1]]) | ||||
|                 { | ||||
|                     number -= RomanMap[ro[i]]; | ||||
|                 } | ||||
|                 else | ||||
|                 { | ||||
|                     number += RomanMap[ro[i]]; | ||||
|                 } | ||||
|             } | ||||
|             else return 0; | ||||
|         } | ||||
|         return number; | ||||
|     } | ||||
|  | ||||
|     public static int RomanOrNumberToInt(string number) | ||||
|     { | ||||
|         var a = 0; | ||||
|         if (Int32.TryParse(number, out a)) return a; | ||||
|         else return RomanToInteger(number); | ||||
|     } | ||||
|     public static string ToRoman(int number) | ||||
|     { | ||||
|         if ((number < 0) || (number > 3999)) return string.Empty; | ||||
|         if (number < 1) return string.Empty; | ||||
|         if (number >= 1000) return "M" + ToRoman(number - 1000); | ||||
|         if (number >= 900) return "CM" + ToRoman(number - 900); | ||||
|         if (number >= 500) return "D" + ToRoman(number - 500); | ||||
|         if (number >= 400) return "CD" + ToRoman(number - 400); | ||||
|         if (number >= 100) return "C" + ToRoman(number - 100); | ||||
|         if (number >= 90) return "XC" + ToRoman(number - 90); | ||||
|         if (number >= 50) return "L" + ToRoman(number - 50); | ||||
|         if (number >= 40) return "XL" + ToRoman(number - 40); | ||||
|         if (number >= 10) return "X" + ToRoman(number - 10); | ||||
|         if (number >= 9) return "IX" + ToRoman(number - 9); | ||||
|         if (number >= 5) return "V" + ToRoman(number - 5); | ||||
|         if (number >= 4) return "IV" + ToRoman(number - 4); | ||||
|         if (number >= 1) return "I" + ToRoman(number - 1); | ||||
|         return string.Empty; | ||||
|     } | ||||
| } | ||||
							
								
								
									
										122
									
								
								HaWeb/HTMLHelpers/LinkHelper.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										122
									
								
								HaWeb/HTMLHelpers/LinkHelper.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,122 @@ | ||||
| namespace HaWeb.HTMLHelpers; | ||||
| using HaDocument.Interfaces; | ||||
| using HaDocument.Models; | ||||
| using System; | ||||
| using System.Text; | ||||
| using HaXMLReader.Interfaces; | ||||
| using HaXMLReader.EvArgs; | ||||
| using HaXMLReader; | ||||
| using System.Collections.Generic; | ||||
|  | ||||
| public class LinkHelper { | ||||
|     private ILibrary _lib; | ||||
|     private IReader _reader; | ||||
|     private StringBuilder _sb; | ||||
|  | ||||
|     private bool _followlinksinchildren; | ||||
|  | ||||
|     private bool _followlinksinthis; | ||||
|  | ||||
|     public LinkHelper(ILibrary lib, IReader reader, StringBuilder stringBuilder, bool followlinksinchildren = true, bool followlinksinthis = true) { | ||||
|         if (lib == null || reader == null || stringBuilder == null) throw new ArgumentNullException(); | ||||
|         _lib = lib; | ||||
|         _reader = reader; | ||||
|         _sb = stringBuilder; | ||||
|         _followlinksinchildren = followlinksinchildren; | ||||
|         _followlinksinthis = followlinksinthis; | ||||
|         reader.Tag += OnTag; | ||||
|     } | ||||
|  | ||||
|     private void OnTag(object _, Tag tag) { | ||||
|         if (tag.Name == "wwwlink" || tag.Name == "intlink" || tag.Name == "link") { | ||||
|             if (tag.EndTag && _followlinksinthis) { | ||||
|                 _sb.Append(HTMLHelpers.TagHelpers.CreateEndElement("a")); | ||||
|             } | ||||
|             else { | ||||
|                 if (tag.Name == "wwwlink" && tag.Values.ContainsKey("address") && _followlinksinthis) | ||||
|                     _sb.Append(HTMLHelpers.TagHelpers.CreateCustomElement("a",  | ||||
|                         new HaWeb.HTMLHelpers.TagHelpers.Attribute() { Name = "class", Value = "hlink wwwlink invlink" },  | ||||
|                         new HaWeb.HTMLHelpers.TagHelpers.Attribute() { Name = "href", Value = tag["address"]}, | ||||
|                         new HaWeb.HTMLHelpers.TagHelpers.Attribute() { Name = "target", Value = "_blank"}, | ||||
|                         new HaWeb.HTMLHelpers.TagHelpers.Attribute() { Name = "rel", Value = "noopener noreferrer"})); | ||||
|                 if (tag.Name == "intlink" && tag.Values.ContainsKey("letter") && _lib.Metas.ContainsKey(tag["letter"])) { | ||||
|                     var letter = _lib.Metas[tag["letter"]]; | ||||
|                     _sb.Append(HTMLHelpers.TagHelpers.CreateElement("a", "hlink intlink invlink", "/Briefe/" + letter.Autopsic + "#" + tag["page"] + "-" + tag["line"])); | ||||
|                     if (!tag.Values.ContainsKey("linktext") || tag.Values["linktext"] == "true") { | ||||
|                         var linkstring = ""; | ||||
|                         var ZHstring = ""; | ||||
|                         var pglnstring= ""; | ||||
|                         linkstring += "HKB " + letter.Autopsic; | ||||
|                         if (tag.Values.ContainsKey("page")) { | ||||
|                             pglnstring += tag["page"]; | ||||
|                             if (tag.Values.ContainsKey("line")) { | ||||
|                                 pglnstring += "/" + tag["line"]; | ||||
|                             } | ||||
|                             if (letter.ZH != null) | ||||
|                                 ZHstring += HTMLHelpers.ConversionHelpers.ToRoman(Int32.Parse(letter.ZH.Volume)) + " "; | ||||
|                             linkstring += " ( "; | ||||
|                             linkstring += ZHstring; | ||||
|                             linkstring += pglnstring; | ||||
|                             linkstring += " )"; | ||||
|                         } | ||||
|                         _sb.Append(linkstring); | ||||
|                     } | ||||
|                 } | ||||
|                 if (tag.Name == "link" && tag.Values != null) { | ||||
|                     Comment comment = null; | ||||
|                     if (tag.Values.ContainsKey("subref") && _lib.SubCommentsByID.ContainsKey(tag["subref"]))  | ||||
|                         comment = _lib.SubCommentsByID[tag["subref"]]; | ||||
|                     else if (tag.Values.ContainsKey("ref")) | ||||
|                         if (_lib.Comments.ContainsKey(tag["ref"])) | ||||
|                             comment = _lib.Comments[tag["ref"]]; | ||||
|                         else if (_lib.SubCommentsByID.ContainsKey(tag["ref"])) | ||||
|                             comment = _lib.SubCommentsByID[tag["ref"]]; | ||||
|                     if (comment != null) { | ||||
|                         var linkloc = String.IsNullOrWhiteSpace(comment.Parent) ? comment.Index : comment.Parent; | ||||
|                         if (_followlinksinthis) | ||||
|                             if (comment.Type == "neuzeit")  | ||||
|                                 _sb.Append(HTMLHelpers.TagHelpers.CreateElement("a", "hlink link invlink", "/Supplementa/Register/" + linkloc[0] + "#" + comment.Index)); | ||||
|                             else if (comment.Type == "bibel") | ||||
|                                 _sb.Append(HTMLHelpers.TagHelpers.CreateElement("a", "hlink link invlink", "/Supplementa/Bibelstellen/" + linkloc[0] + linkloc[1] + "#" + comment.Index)); | ||||
|                             else if (comment.Type == "forschung") | ||||
|                                 _sb.Append(HTMLHelpers.TagHelpers.CreateElement("a", "hlink link invlink", "/Supplementa/Forschung/" + linkloc[0] + "#" + comment.Index)); | ||||
|                         _sb.Append(GetLemmaString(tag, comment)); | ||||
|                     } | ||||
|                 } | ||||
|                 if (tag.IsEmpty && _followlinksinthis) _sb.Append(HTMLHelpers.TagHelpers.CreateEndElement("a")); | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     private string GetLemmaString(Tag tag, Comment comment) { | ||||
|         if (!tag.Values.ContainsKey("linktext") || tag.Values["linktext"] == "true") { | ||||
|             var sb = new StringBuilder(); | ||||
|             var subreader = new UTF8StringReader(comment.Lemma); | ||||
|             new LinkHelper(_lib, subreader, sb, _followlinksinchildren, _followlinksinchildren); | ||||
|             List<(Func<Tag, bool>, Action<StringBuilder, Tag>)> OTag_Funcs = new List<(Func<Tag, bool>, Action<StringBuilder, Tag>)>() { | ||||
|                 ( x => x.Name == "lemma", (strbd, x) => strbd.Append(HTMLHelpers.TagHelpers.CreateElement("div", "reference")) ), | ||||
|                 ( x => x.Name == "titel", (strbd, x) => strbd.Append(HTMLHelpers.TagHelpers.CreateElement("span", "title")) ) | ||||
|             }; | ||||
|             List<(Func<Tag, bool>, Action<StringBuilder, Tag>)> CTag_Funcs = new List<(Func<Tag, bool>, Action<StringBuilder, Tag>)>() { | ||||
|                 ( x => x.Name == "lemma", (strbd, x) => strbd.Append(HTMLHelpers.TagHelpers.CreateEndElement("div")) ), | ||||
|                 ( x => x.Name == "titel", (strbd, x) => strbd.Append(HTMLHelpers.TagHelpers.CreateEndElement("span")) ) | ||||
|             }; | ||||
|             List<(Func<Text, bool>, Action<StringBuilder, Text>)> Text_Funcs = new List<(Func<Text, bool>, Action<StringBuilder, Text>)>() { | ||||
|                 ( x => true, (strbd, txt) => strbd.Append(txt.Value)) | ||||
|             }; | ||||
|             new XMLHelper(subreader, sb, OTag_Funcs, null, CTag_Funcs, Text_Funcs, null); | ||||
|             subreader.Read(); | ||||
|             return sb.ToString(); | ||||
|         } | ||||
|         return ""; | ||||
|     } | ||||
|  | ||||
|     public void Dispose() { | ||||
|         if (_reader != null) | ||||
|             _reader.Tag -= OnTag; | ||||
|     } | ||||
|  | ||||
|     ~LinkHelper() { | ||||
|         Dispose(); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										18
									
								
								HaWeb/HTMLHelpers/StringHelpers.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								HaWeb/HTMLHelpers/StringHelpers.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,18 @@ | ||||
| namespace HaWeb.HTMLHelpers; | ||||
| public static class StringHelpers { | ||||
|     public static string GetEnumerationString(List<string> strlist) | ||||
|     { | ||||
|         var res = ""; | ||||
|         foreach (var str in strlist) | ||||
|         {    | ||||
|             if (str != strlist.First()) | ||||
|                 if (str == strlist.Last())  | ||||
|                     res += " und " + str; | ||||
|                 else | ||||
|                     res += ", " + str;  | ||||
|             else  | ||||
|                 res += str; | ||||
|         } | ||||
|         return res; | ||||
|     } | ||||
| } | ||||
							
								
								
									
										59
									
								
								HaWeb/HTMLHelpers/TagHelpers.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										59
									
								
								HaWeb/HTMLHelpers/TagHelpers.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,59 @@ | ||||
| namespace HaWeb.HTMLHelpers; | ||||
| using System; | ||||
| using System.Collections.Generic; | ||||
| using System.Linq; | ||||
|  | ||||
| public static class TagHelpers | ||||
| { | ||||
|     public struct Attribute | ||||
|     { | ||||
|         public string Name; | ||||
|         public string Value; | ||||
|     } | ||||
|  | ||||
|     public static string CreateElement(string elementname, string classes = "", string ids = "") | ||||
|     { | ||||
|         string res = "<" + elementname; | ||||
|         if (!String.IsNullOrWhiteSpace(classes)) | ||||
|             if (elementname == "button") | ||||
|                 res += CreateAttribute(new Attribute() { Name = "type", Value = classes }); | ||||
|             else                     | ||||
|                 res += CreateAttribute(new Attribute() { Name = "class", Value = classes }); | ||||
|         if (!String.IsNullOrWhiteSpace(ids)) | ||||
|             if (elementname == "a") | ||||
|                 res += CreateAttribute(new Attribute() { Name = "href", Value = ids }); | ||||
|             else | ||||
|                 res += CreateAttribute(new Attribute() { Name = "id", Value = ids }); | ||||
|         return res + ">"; | ||||
|     } | ||||
|  | ||||
|     public static string CreateCustomElement(string elementname, params Attribute[] attributes) | ||||
|     { | ||||
|         string res = "<" + elementname; | ||||
|         if (!(attributes.Length == 0)) | ||||
|         { | ||||
|             foreach (var attrib in attributes) | ||||
|             { | ||||
|                 res += CreateAttribute(attrib); | ||||
|             } | ||||
|         } | ||||
|         return res + ">"; | ||||
|     } | ||||
|  | ||||
|  | ||||
|     public static string CreateEndElement(string elementname) | ||||
|         => "</" + elementname + ">"; | ||||
|  | ||||
|     public static string CreateAttribute(Attribute attr) | ||||
|         => " " + attr.Name + "=\"" + attr.Value + "\" "; | ||||
|  | ||||
|     public static string CreateEmptyElement(string elementname, string classes = "", string ids = "") | ||||
|     { | ||||
|         string res = "<" + elementname; | ||||
|         if (!String.IsNullOrWhiteSpace(classes)) | ||||
|             res += CreateAttribute(new Attribute() { Name = "class", Value = classes }); | ||||
|         if (!String.IsNullOrWhiteSpace(ids)) | ||||
|             res += CreateAttribute(new Attribute() { Name = "id", Value = ids }); | ||||
|         return res + "></" + elementname + ">"; | ||||
|     } | ||||
| } | ||||
							
								
								
									
										101
									
								
								HaWeb/HTMLHelpers/XMLHelper.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										101
									
								
								HaWeb/HTMLHelpers/XMLHelper.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,101 @@ | ||||
| namespace HaWeb.HTMLHelpers; | ||||
| using HaXMLReader.Interfaces; | ||||
| using HaXMLReader.EvArgs; | ||||
| using System.Text; | ||||
| using System.Collections.Generic; | ||||
| using System; | ||||
|  | ||||
| public class XMLHelper { | ||||
|     private IReader _in; | ||||
|     private StringBuilder _target; | ||||
|     private List<(Func<Tag, bool>, Action<StringBuilder, Tag>)> _OTag_Funcs; | ||||
|     private List<(Func<Tag, bool>, Action<StringBuilder, Tag>)> _STag_Funcs; | ||||
|     private List<(Func<Tag, bool>, Action<StringBuilder, Tag>)> _CTag_Funcs; | ||||
|     private List<(Func<Text, bool>, Action<StringBuilder, Text>)> _Text_Funcs; | ||||
|     private List<(Func<Whitespace, bool>, Action<StringBuilder, Whitespace>)> _WS_Funcs; | ||||
|     private bool _deleteLeadingWS; | ||||
|     private bool _deleteTrailingWS; | ||||
|  | ||||
|     public XMLHelper( | ||||
|         IReader input, | ||||
|         StringBuilder target,  | ||||
|         List<(Func<Tag, bool>, Action<StringBuilder, Tag>)> OTag_Funcs = null, | ||||
|         List<(Func<Tag, bool>, Action<StringBuilder, Tag>)> STag_Funcs = null, | ||||
|         List<(Func<Tag, bool>, Action<StringBuilder, Tag>)> CTag_Funcs = null, | ||||
|         List<(Func<Text, bool>, Action<StringBuilder, Text>)> Text_Funcs = null, | ||||
|         List<(Func<Whitespace, bool>, Action<StringBuilder, Whitespace>)> WS_Funcs = null, | ||||
|         bool deleteLeadingWS = false, | ||||
|         bool deleteTrailingWS = false | ||||
|     ) { | ||||
|         if (input == null || target == null) throw new ArgumentNullException(); | ||||
|  | ||||
|         _in = input; | ||||
|         _target = target; | ||||
|         _deleteLeadingWS = deleteLeadingWS; | ||||
|         _deleteTrailingWS = deleteTrailingWS; | ||||
|  | ||||
|         _OTag_Funcs = OTag_Funcs; | ||||
|         _STag_Funcs = STag_Funcs; | ||||
|         _CTag_Funcs = CTag_Funcs; | ||||
|         _Text_Funcs = Text_Funcs; | ||||
|         _WS_Funcs = WS_Funcs; | ||||
|  | ||||
|         if (_OTag_Funcs != null) | ||||
|             _in.OpenTag += OnOTag; | ||||
|         if (_STag_Funcs != null) | ||||
|             _in.SingleTag += OnSTag; | ||||
|         if (_CTag_Funcs != null) | ||||
|             _in.CloseTag += OnCTag; | ||||
|         if (_Text_Funcs != null) | ||||
|             _in.Text += OnText; | ||||
|         if (_WS_Funcs != null) | ||||
|             _in.Whitespace += OnWS; | ||||
|     } | ||||
|  | ||||
|     void OnOTag(object _, Tag tag) { | ||||
|     foreach(var entry in _OTag_Funcs) | ||||
|         if (entry.Item1(tag)) entry.Item2(_target, tag); | ||||
|     } | ||||
|  | ||||
|     void OnText(object _, Text text) { | ||||
|         if (_deleteLeadingWS) text.Value = text.Value.TrimStart(); | ||||
|         if (_deleteTrailingWS) text.Value = text.Value.TrimEnd(); | ||||
|         foreach(var entry in _Text_Funcs) | ||||
|             if (entry.Item1(text)) entry.Item2(_target, text); | ||||
|     } | ||||
|  | ||||
|     void OnSTag(object _, Tag tag) { | ||||
|         foreach(var entry in _STag_Funcs) | ||||
|             if (entry.Item1(tag)) entry.Item2(_target, tag); | ||||
|     } | ||||
|  | ||||
|     void OnCTag (object _, Tag tag) { | ||||
|         foreach (var entry in _CTag_Funcs) | ||||
|             if (entry.Item1(tag)) entry.Item2(_target, tag); | ||||
|     } | ||||
|  | ||||
|     void OnWS (object _, Whitespace ws) { | ||||
|         foreach (var entry in _WS_Funcs) { | ||||
|             if (entry.Item1(ws)) entry.Item2(_target, ws); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     internal void Dispose() { | ||||
|         if (_in != null) { | ||||
|             if (_OTag_Funcs != null) | ||||
|                 _in.OpenTag -= OnOTag; | ||||
|             if (_STag_Funcs != null) | ||||
|                 _in.SingleTag -= OnSTag; | ||||
|             if (_CTag_Funcs != null) | ||||
|                 _in.CloseTag -= OnCTag; | ||||
|             if (_Text_Funcs != null) | ||||
|                 _in.Text -= OnText; | ||||
|             if (_WS_Funcs != null) | ||||
|                 _in.Whitespace -= OnWS; | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     ~XMLHelper() { | ||||
|         Dispose(); | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user
	 schnulller
					schnulller