using System; using System.CodeDom; using System.Configuration; using System.Linq; using System.Text.RegularExpressions; using System.Windows.Media.Converters; using System.Windows.Navigation; namespace Txt2Bib.Records { public interface IBib { public string Convert(string[] entryLines); public string ToString(); } public record class ArticleBib : IBib { public string Type { get; init; } = "article"; public string[] Author { get; set; } = { "Gianni e Pinotto" }; public string Title { get; set; } = ""; public string Journal { get; set; } = ""; public ushort Year { get; set; } = 1950; public string Volume { get; set; } = ""; public string? Issue { get; set; } = null; public ushort FirstPage { get; set; } = 1; public ushort LastPage { get; set; } = 1; public string Doi { get; set; } = ""; public string Url { get; set; } = ""; public string Convert(string[] entryLines) { var rearrange = (string a) => { a = a.Trim(); var s = a.Split(' '); return $"{s[1]} {s[0]}"; }; var checkVol = (string v) => v.Split(',').Length == 2 ? v.Split(',')[0].Trim() : v.Trim(); Author = entryLines[1].Split(',').Select(a => rearrange(a)).ToArray(); Year = ushort.Parse(entryLines[2]); Title = entryLines[3]; Journal = entryLines[4]; Volume = entryLines[5] != string.Empty ? checkVol(entryLines[5]) : Volume; Issue = entryLines[5].Split(',').Length == 2 ? entryLines[5].Split(',')[1].Trim() : Issue; try { FirstPage = ushort.Parse(entryLines[6].Split('-')[0]); LastPage = ushort.Parse(entryLines[6].Split('-')[1]); } catch (Exception) { throw; } if (entryLines.Length > 7 ) { if (IsDoi(entryLines[7])) Doi = entryLines[7]; else Url = entryLines[7]; } return ToString(); } private bool IsDoi(string url) { return url.Contains("doi"); } public override string ToString() { return $"@{Type}{{{Author[0][..5]}_{Year},\n" + $"\tauthor = \"{string.Join(" and ", Author)}\",\n" + $"\ttitle = {{{Title}}},\n" + $"\tjournal = \"{Journal}\",\n" + $"\tyear = \"{Year}\",\n" + $"\tvolume = \"{Volume}\",\n" + $"\tnumber = \"{Issue}\",\n" + $"\tpages = \"{FirstPage}--{LastPage}\",\n" + $"\tdoi = \"{Doi}\",\n" + $"\turl = \"{Url}\",\n" + "}\n"; } } public record class BookBib : IBib { public string Type { get; init; } = "book"; public string[] Author { get; set; } = { "Gianni e Pinotto" }; public string Title { get; set; } = ""; public ushort Year { get; set; } = 1950; public string Publisher { get; set; } = ""; public string Place { get; set; } = ""; public string Url { get; set; } = ""; public string Doi { get; set; } = ""; public string Convert(string[] entryLines) { var rearrange = (string a) => { a = a.Trim(); var s = a.Split(' '); return $"{s[1]} {s[0]}"; }; Author = entryLines[1].Split(',').Select(a => rearrange(a)).ToArray(); Year = ushort.Parse(entryLines[2]); Title = entryLines[3]; Place = entryLines[4]; Publisher = entryLines[5]; if (entryLines.Length > 6 ) { if (IsDoi(entryLines[6])) Doi = entryLines[6]; else Url = entryLines[6]; } return ToString(); } private bool IsDoi(string url) { return url.Contains("doi"); } public override string ToString() { return $"@{Type}{{{Author[0][..5]}_{Year},\n" + $"\tauthor = \"{string.Join(" and ", Author)}\",\n" + $"\ttitle = {{{Title}}},\n" + $"\tpublisher = \"{Publisher}\",\n" + $"\taddress = \"{Place}\",\n" + $"\tyear = \"{Year}\",\n" + $"\turl = \"{Url}\",\n" + "}\n"; } } }