txt2bib/Records.cs
2024-02-26 17:20:47 +01:00

273 lines
9.9 KiB
C#

using System;
using System.Linq;
using System.Text.RegularExpressions;
namespace Txt2Bib.Records
{
public interface IBib {
public string Convert(string[] entryLines);
public string ToString();
};
public abstract record ItemType
{
public string Pages { get; set; } = "";
protected static bool IsDoi(string url)
{
return url.Contains("doi");
}
protected static string Rearrange(string a)
{
a = a.Trim();
var s = a.Split(' ');
return s.Length > 1 ?
$"{s[1]} {s[0][0]}{s[0][1..].ToLower()}" : a;
}
protected string CreatePages(string pages)
{
if (pages.Length == 0) { return ""; }
return pages.Split('-').Length == 2 ?
$"{pages.Split('-')[0]}--{pages.Split('-')[1].TrimEnd('.')}" :
pages;
}
}
public record class Article : ItemType, 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 string Doi { get; set; } = "";
public string Url { get; set; } = "";
public string Convert(string[] entryLines)
{
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;
Pages = CreatePages(entryLines[6]);
if (entryLines.Length > 7 )
{
if (IsDoi(entryLines[7])) Doi = entryLines[7];
else Url = entryLines[7];
}
return ToString();
}
public override string ToString()
{
return $"@{Type}{{{Author[0][..2].Replace(". ", "_")}_{Year},\n" +
$"\tauthor = \"{string.Join(" and ", Author)}\",\n" +
$"\ttitle = {{{Title}}},\n" +
$"\tjournal = \"{Journal}\",\n" +
$"\tyear = \"{Year}\",\n" +
$"\tvolume = \"{Volume}\",\n" +
$"\tnumber = \"{Issue}\",\n" +
$"\tpages = \"{Pages}\",\n" +
$"\tdoi = \"{Doi}\",\n" +
$"\turl = \"{Url}\",\n" +
"}\n";
}
}
public record class Book : ItemType, IBib
{
public string Type { get; init; } = "book";
public string[] Author { get; set; } = Array.Empty<string>();
public string[] Editor { get; set; } = Array.Empty<string>();
public string Title { get; set; } = "";
public ushort Year { get; set; } = 1950;
public string Publisher { get; set; } = "";
public string Series { get; set; } = "";
public string Place { get; set; } = "";
public string Url { get; set; } = "";
public string Doi { get; set; } = "";
public string Convert(string[] entryLines)
{
var auths = entryLines[1];
if (auths.Contains("ed"))
{
Editor = Regex.Replace(auths,@"\(eds?\.\)", "").Split(',').Select(a => Rearrange(a)).ToArray();
}
else
{
Author = auths.Split(',').Select(a => Rearrange(a)).ToArray();
}
Year = ushort.Parse(entryLines[2]);
Title = entryLines[3];
Series = entryLines[4] != string.Empty ? entryLines[4] : Series;
Place = entryLines[5];
Publisher = entryLines[6];
if (entryLines.Length > 7 )
{
if (IsDoi(entryLines[7])) Doi = entryLines[7];
else Url = entryLines[7];
}
return ToString();
}
public override string ToString()
{
string label;
string authString;
if (Author.Length != 0)
{
label = Author[0][..2].Replace(". ", "");
authString = $"\tauthor = \"{string.Join(" and ", Author)}\",\n";
}
else
{
label = Editor[0][..2].Replace(". ", "");
authString = $"\teditor = \"{string.Join(" and ", Editor)}\",\n";
}
return $"@{Type}{{{label}_{Year},\n" +
authString +
$"\ttitle = {{{Title}}},\n" +
$"\tpublisher = \"{Publisher}\",\n" +
$"\tseries = \"{Series}\",\n" +
$"\taddress = \"{Place}\",\n" +
$"\tyear = \"{Year}\",\n" +
$"\turl = \"{Url}\",\n" +
$"\tdoi = \"{Doi}\",\n" +
"}\n";
}
}
public record class Proceedings : ItemType, IBib
{
public string Type { get; init; } = "inproceedings";
public string[] Author { get; set; } = Array.Empty<string>();
public string[] Editor { get; set; } = Array.Empty<string>();
public string Title { get; set; } = "";
public string BookTitle { get; set; } = "";
public ushort Year { get; set; } = 1950;
public string Publisher { get; set; } = "";
public string Series { get; set; } = "";
public string Address { get; set; } = "";
public string Url { get; set; } = "";
public string Doi { get; set; } = "";
public string Convert(string[] entryLines)
{
Author = entryLines[1].Split(',').Select(a => Rearrange(a)).ToArray();
Year = ushort.Parse(entryLines[2]);
Title = entryLines[3];
Editor = entryLines[4] != String.Empty ?
Regex.Replace(entryLines[4], @"\(eds?\.\)", "").Split(',').Select(a => Rearrange(a)).ToArray() :
Editor;
BookTitle = entryLines[5];
Series = entryLines[6] != String.Empty ? entryLines[6] : Series;
Address = entryLines[7] != String.Empty ? entryLines[7] : Address;
Publisher = entryLines[8];
Pages = CreatePages(entryLines[9]);
if (entryLines.Length > 10)
{
if (IsDoi(entryLines[10])) Doi = entryLines[10];
else Url = entryLines[10];
}
return ToString();
}
public override string ToString()
{
string label = Author[0][..2].Replace(". ", "");
string authString = $"\tauthor = \"{string.Join(" and ", Author)}\",\n";
string edsString = Editor.Length != 0 ?
$"\teditor = \"{string.Join(" and ", Editor)}\",\n" : "";
return $"@{Type}{{{label}_{Year},\n" +
authString +
edsString +
$"\ttitle = {{{Title}}},\n" +
$"\tbooktitle = {{{BookTitle}}},\n" +
$"\tpublisher = \"{Publisher}\",\n" +
$"\tseries = {{{Series}}},\n" +
$"\taddress = \"{Address}\",\n" +
$"\tyear = \"{Year}\",\n" +
$"\tpages = \"{Pages}\",\n" +
$"\turl = \"{Url}\",\n" +
$"\tdoi = \"{Doi}\",\n" +
"}\n";
}
}
public record class Chapter : ItemType, IBib
{
public string Type { get; init; } = "incollection";
public string[] Author { get; set; } = Array.Empty<string>();
public string[] Editor { get; set; } = Array.Empty<string>();
public string Title { get; set; } = "";
public string BookTitle { get; set; } = "";
public ushort Year { get; set; } = 1950;
public string Publisher { get; set; } = "";
public string Series { get; set; } = "";
public string Address { get; set; } = "";
public string Url { get; set; } = "";
public string Doi { get; set; } = "";
public string Convert(string[] entryLines)
{
Author = entryLines[1].Split(',').Select(a => Rearrange(a)).ToArray();
Year = ushort.Parse(entryLines[2]);
Title = entryLines[3];
Editor = Regex.Replace(entryLines[4], @"\(eds?\.\)", "").Split(',').Select(a => Rearrange(a)).ToArray();
BookTitle = entryLines[5];
Series = entryLines[6] != String.Empty ? entryLines[6] : Series;
Address = entryLines[7] != String.Empty ? entryLines[7] : Address;
Publisher = entryLines[8];
Pages = CreatePages(entryLines[9]);
if (entryLines.Length > 10)
{
if (IsDoi(entryLines[10])) Doi = entryLines[10];
else Url = entryLines[10];
}
return ToString();
}
public override string ToString()
{
string label = Author[0][..2].Replace(". ", "");
string authString = $"\tauthor = \"{string.Join(" and ", Author)}\",\n";
string edsString = $"\teditor = \"{string.Join(" and ", Editor)}\",\n";
return $"@{Type}{{{label}_{Year},\n" +
authString +
edsString +
$"\ttitle = {{{Title}}},\n" +
$"\tbooktitle = {{{BookTitle}}},\n" +
$"\tpublisher = \"{Publisher}\",\n" +
$"\tseries = {{{Series}}},\n" +
$"\taddress = \"{Address}\",\n" +
$"\tyear = \"{Year}\",\n" +
$"\tpages = \"{Pages}\",\n" +
$"\turl = \"{Url}\",\n" +
$"\tdoi = \"{Doi}\",\n" +
"}\n";
}
}
}