mirror of
https://github.com/Theodor-Springmann-Stiftung/hamann-ausgabe-core.git
synced 2025-10-30 09:45:32 +00:00
Initial replacement of old repository.
This commit is contained in:
138
HaLive/Classes/HTMLHelpers.cs
Normal file
138
HaLive/Classes/HTMLHelpers.cs
Normal file
@@ -0,0 +1,138 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace HaLive
|
||||
{
|
||||
public struct Attribute
|
||||
{
|
||||
public string Name;
|
||||
public string Value;
|
||||
}
|
||||
|
||||
public static class HTMLHelpers
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
public static string CreateElement(string elementname, string classes = "", string ids = "")
|
||||
{
|
||||
string res = "<" + elementname;
|
||||
if (!String.IsNullOrWhiteSpace(classes))
|
||||
if (elementname == "button")
|
||||
res += CreateAttribute(new HaLive.Attribute() { Name = "type", Value = classes });
|
||||
else
|
||||
res += CreateAttribute(new HaLive.Attribute() { Name = "class", Value = classes });
|
||||
if (!String.IsNullOrWhiteSpace(ids))
|
||||
if (elementname == "a")
|
||||
res += CreateAttribute(new HaLive.Attribute() { Name = "href", Value = ids });
|
||||
else
|
||||
res += CreateAttribute(new HaLive.Attribute() { Name = "id", Value = ids });
|
||||
return res + ">";
|
||||
}
|
||||
|
||||
public static string CreateCustomElement(string elementname, params HaLive.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(HaLive.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 HaLive.Attribute() { Name = "class", Value = classes });
|
||||
if (!String.IsNullOrWhiteSpace(ids))
|
||||
res += CreateAttribute(new HaLive.Attribute() { Name = "id", Value = ids });
|
||||
return res + "></" + elementname + ">";
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
123
HaLive/Classes/LinkBuilder.cs
Normal file
123
HaLive/Classes/LinkBuilder.cs
Normal file
@@ -0,0 +1,123 @@
|
||||
using HaDocument.Interfaces;
|
||||
using HaDocument.Models;
|
||||
using System;
|
||||
using System.Text;
|
||||
using HaXMLReader.Interfaces;
|
||||
using HaXMLReader.EvArgs;
|
||||
using HaXMLReader;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace HaLive {
|
||||
public class LinkBuilder {
|
||||
private ILibrary _lib;
|
||||
private IReader _reader;
|
||||
private StringBuilder _sb;
|
||||
|
||||
private bool _followlinksinchildren;
|
||||
|
||||
private bool _followlinksinthis;
|
||||
|
||||
public LinkBuilder(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.CreateEndElement("a"));
|
||||
}
|
||||
else {
|
||||
if (tag.Name == "wwwlink" && tag.Values.ContainsKey("address") && _followlinksinthis)
|
||||
_sb.Append(HTMLHelpers.CreateCustomElement("a",
|
||||
new HaLive.Attribute() { Name = "class", Value = "hlink wwwlink invlink" },
|
||||
new HaLive.Attribute() { Name = "href", Value = tag["address"]},
|
||||
new HaLive.Attribute() { Name = "target", Value = "_blank"},
|
||||
new HaLive.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.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.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.CreateElement("a", "hlink link invlink", "/Supplementa/Register/" + linkloc[0] + "#" + comment.Index));
|
||||
else if (comment.Type == "bibel")
|
||||
_sb.Append(HTMLHelpers.CreateElement("a", "hlink link invlink", "/Supplementa/Bibelstellen/" + linkloc[0] + linkloc[1] + "#" + comment.Index));
|
||||
else if (comment.Type == "forschung")
|
||||
_sb.Append(HTMLHelpers.CreateElement("a", "hlink link invlink", "/Supplementa/Forschung/" + linkloc[0] + "#" + comment.Index));
|
||||
_sb.Append(GetLemmaString(tag, comment));
|
||||
}
|
||||
}
|
||||
if (tag.IsEmpty && _followlinksinthis) _sb.Append(HTMLHelpers.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 LinkBuilder(_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.CreateElement("div", "reference")) ),
|
||||
( x => x.Name == "titel", (strbd, x) => strbd.Append(HTMLHelpers.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.CreateEndElement("div")) ),
|
||||
( x => x.Name == "titel", (strbd, x) => strbd.Append(HTMLHelpers.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 StandardSubscriber(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;
|
||||
}
|
||||
|
||||
~LinkBuilder() {
|
||||
Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
77
HaLive/Classes/Search.cs
Normal file
77
HaLive/Classes/Search.cs
Normal file
@@ -0,0 +1,77 @@
|
||||
using HaDocument.Models;
|
||||
using HaDocument.Interfaces;
|
||||
using HaDocument.Comparers;
|
||||
using HaXMLReader.Interfaces;
|
||||
using HaLive.Models;
|
||||
using HaXMLReader.EvArgs;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Threading.Tasks;
|
||||
using System.Linq;
|
||||
using System;
|
||||
|
||||
namespace HaLive {
|
||||
internal class DocumentSearch {
|
||||
internal DocumentSearchResult _searchResult { get; }
|
||||
private IReader _reader { get; }
|
||||
private string _search { get; }
|
||||
|
||||
internal DocumentSearch(DocumentSearchResult res, IReader reader, string search) {
|
||||
_searchResult = res;
|
||||
_reader = reader;
|
||||
_search = search;
|
||||
_pg = "";
|
||||
}
|
||||
|
||||
internal DocumentSearchResult Act() {
|
||||
_reader.Text += OnText;
|
||||
_reader.SingleTag += OnSTag;
|
||||
_reader.Read();
|
||||
return _searchResult;
|
||||
}
|
||||
|
||||
internal void OnText(object _, Text text) {
|
||||
if (text.Value.ToLower().Contains(_search.ToLower())) {
|
||||
_searchResult.Results.Add(new DocumentResult(text.Value, _pg, _ln));
|
||||
}
|
||||
}
|
||||
|
||||
private string _pg;
|
||||
private string _ln;
|
||||
|
||||
internal void OnSTag(object _, Tag tag) {
|
||||
if (tag.Name == "page")
|
||||
_pg = tag["index"];
|
||||
else if (tag.Name == "line")
|
||||
_ln = tag["index"];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
internal class RegisterSearch {
|
||||
internal Comment _searchResult { get; }
|
||||
private IReader _reader { get; }
|
||||
private string _search { get; }
|
||||
private bool found;
|
||||
|
||||
internal RegisterSearch(Comment res, IReader reader, string search) {
|
||||
_searchResult = res;
|
||||
_reader = reader;
|
||||
_search = search;
|
||||
found = false;
|
||||
}
|
||||
|
||||
internal bool Act() {
|
||||
_reader.Text += OnText;
|
||||
_reader.Read();
|
||||
return found;
|
||||
}
|
||||
|
||||
internal void OnText(object _, Text text) {
|
||||
if (text.Value.ToLower().Contains(_search.ToLower())) {
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
102
HaLive/Classes/StandardSubscriber.cs
Normal file
102
HaLive/Classes/StandardSubscriber.cs
Normal file
@@ -0,0 +1,102 @@
|
||||
using HaXMLReader.Interfaces;
|
||||
using HaXMLReader.EvArgs;
|
||||
using System.Text;
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
|
||||
namespace HaLive {
|
||||
public class StandardSubscriber {
|
||||
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 StandardSubscriber(
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
~StandardSubscriber() {
|
||||
Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user