2 Commits

Author SHA1 Message Date
96dff46c14 Handle proceedings 2024-01-16 11:51:32 +01:00
a1b8218ed4 Some refactoring... 2024-01-12 12:23:44 +01:00
3 changed files with 86 additions and 21 deletions

View File

@@ -13,7 +13,15 @@ namespace Txt2Bib.Records
public string ToString(); public string ToString();
} }
public record class ArticleBib : IBib public abstract record ItemType
{
protected static bool IsDoi(string url)
{
return url.Contains("doi");
}
}
public record class Article : ItemType, IBib
{ {
public string Type { get; init; } = "article"; public string Type { get; init; } = "article";
public string[] Author { get; set; } = { "Gianni e Pinotto" }; public string[] Author { get; set; } = { "Gianni e Pinotto" };
@@ -48,7 +56,7 @@ namespace Txt2Bib.Records
try try
{ {
FirstPage = ushort.Parse(entryLines[6].Split('-')[0]); FirstPage = ushort.Parse(entryLines[6].Split('-')[0]);
LastPage = ushort.Parse(entryLines[6].Split('-')[1]); LastPage = ushort.Parse(entryLines[6].Split('-')[1].TrimEnd('.'));
} }
catch (Exception) catch (Exception)
{ {
@@ -64,11 +72,6 @@ namespace Txt2Bib.Records
return ToString(); return ToString();
} }
private bool IsDoi(string url)
{
return url.Contains("doi");
}
public override string ToString() public override string ToString()
{ {
return $"@{Type}{{{Author[0][..5].Replace(". ", "_")}_{Year},\n" + return $"@{Type}{{{Author[0][..5].Replace(". ", "_")}_{Year},\n" +
@@ -85,7 +88,7 @@ namespace Txt2Bib.Records
} }
} }
public record class BookBib : IBib public record class Book : ItemType, IBib
{ {
public string Type { get; init; } = "book"; public string Type { get; init; } = "book";
public string[] Author { get; set; } = Array.Empty<string>(); public string[] Author { get; set; } = Array.Empty<string>();
@@ -128,15 +131,10 @@ namespace Txt2Bib.Records
return ToString(); return ToString();
} }
private bool IsDoi(string url)
{
return url.Contains("doi");
}
public override string ToString() public override string ToString()
{ {
var label = ""; string label;
var authString = ""; string authString;
if (Author.Length != 0) if (Author.Length != 0)
{ {
@@ -160,4 +158,71 @@ namespace Txt2Bib.Records
"}\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 Address { get; set; } = "";
public ushort FirstPage { get; set; } = 1;
public ushort LastPage { get; set; } = 1;
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];
BookTitle = entryLines[4];
Address = entryLines[5] != String.Empty ? entryLines[5] : Address;
Publisher = entryLines[6];
try
{
FirstPage = ushort.Parse(entryLines[7].Split('-')[0]);
LastPage = ushort.Parse(entryLines[7].Split('-')[1].TrimEnd('.'));
}
catch (Exception)
{
throw;
}
if (entryLines.Length > 8 )
{
if (IsDoi(entryLines[8])) Doi = entryLines[8];
else Url = entryLines[8];
}
return ToString();
}
public override string ToString()
{
string label = Author[0][..5].Replace(". ", "");
string authString = $"\tauthor = \"{string.Join(" and ", Author)}\",\n";
return $"@{Type}{{{label}_{Year},\n" +
authString +
$"\ttitle = {{{Title}}},\n" +
$"\tbooktitle = {{{BookTitle}}},\n" +
$"\tpublisher = \"{Publisher}\",\n" +
$"\taddress = \"{Address}\",\n" +
$"\tyear = \"{Year}\",\n" +
$"\tpages = \"{FirstPage}--{LastPage}\",\n" +
$"\turl = \"{Url}\",\n" +
$"\tdoi = \"{Doi}\",\n" +
"}\n";
}
}
} }

View File

@@ -15,7 +15,7 @@ namespace Txt2Bib
/// </summary> /// </summary>
public class Text2Bib public class Text2Bib
{ {
private string[] _citTypes = { "J", "B", "C", "P", "?" }; private string[] _citTypes = { "J", "B", "C", "P", "I" };
/// <summary> /// <summary>
/// Generate single .bib file from input text files /// Generate single .bib file from input text files
@@ -41,7 +41,7 @@ namespace Txt2Bib
/// <returns></returns> /// <returns></returns>
public string CreateBibTeX(string path) public string CreateBibTeX(string path)
{ {
var reader = File.OpenText(path); using var reader = File.OpenText(path);
byte[] contentBytes = File.ReadAllBytes(path); byte[] contentBytes = File.ReadAllBytes(path);
var content = Encoding.Latin1.GetString(contentBytes); var content = Encoding.Latin1.GetString(contentBytes);
@@ -82,10 +82,10 @@ namespace Txt2Bib
{ {
citation = type switch citation = type switch
{ {
"J" => (new ArticleBib()).Convert(lines), "J" => new Article().Convert(lines),
"B" => new BookBib().Convert(lines), "B" => new Book().Convert(lines),
//"C" => new ChapterBib(), //"C" => new ChapterBib(),
//"P" => new ConferenceBib(), "P" => new Proceedings().Convert(lines),
_ => "" _ => ""
}; };
} }

View File

@@ -5,7 +5,7 @@
<TargetFramework>net6.0-windows</TargetFramework> <TargetFramework>net6.0-windows</TargetFramework>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<UseWPF>true</UseWPF> <UseWPF>true</UseWPF>
<Version>0.1.0</Version> <Version>0.1.2</Version>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>