From a1b8218ed4054032533e30408f4af9ce92b9fc43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20P?= Date: Fri, 12 Jan 2024 12:23:44 +0100 Subject: [PATCH] Some refactoring... --- Records.cs | 26 ++++++++++++-------------- Text2Bib.cs | 8 ++++---- Txt2Bib.csproj | 2 +- 3 files changed, 17 insertions(+), 19 deletions(-) diff --git a/Records.cs b/Records.cs index 6ffb53d..70485b5 100644 --- a/Records.cs +++ b/Records.cs @@ -13,7 +13,15 @@ namespace Txt2Bib.Records 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[] Author { get; set; } = { "Gianni e Pinotto" }; @@ -64,11 +72,6 @@ namespace Txt2Bib.Records return ToString(); } - private bool IsDoi(string url) - { - return url.Contains("doi"); - } - public override string ToString() { 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[] Author { get; set; } = Array.Empty(); @@ -128,15 +131,10 @@ namespace Txt2Bib.Records return ToString(); } - private bool IsDoi(string url) - { - return url.Contains("doi"); - } - public override string ToString() { - var label = ""; - var authString = ""; + string label; + string authString; if (Author.Length != 0) { diff --git a/Text2Bib.cs b/Text2Bib.cs index 3c3d19a..a089b6c 100644 --- a/Text2Bib.cs +++ b/Text2Bib.cs @@ -15,7 +15,7 @@ namespace Txt2Bib /// public class Text2Bib { - private string[] _citTypes = { "J", "B", "C", "P", "?" }; + private string[] _citTypes = { "J", "B", "C", "P", "I" }; /// /// Generate single .bib file from input text files @@ -41,7 +41,7 @@ namespace Txt2Bib /// public string CreateBibTeX(string path) { - var reader = File.OpenText(path); + using var reader = File.OpenText(path); byte[] contentBytes = File.ReadAllBytes(path); var content = Encoding.Latin1.GetString(contentBytes); @@ -82,8 +82,8 @@ namespace Txt2Bib { citation = type switch { - "J" => (new ArticleBib()).Convert(lines), - "B" => new BookBib().Convert(lines), + "J" => (new Article()).Convert(lines), + "B" => new Book().Convert(lines), //"C" => new ChapterBib(), //"P" => new ConferenceBib(), _ => "" diff --git a/Txt2Bib.csproj b/Txt2Bib.csproj index f396f7f..c7ae09c 100644 --- a/Txt2Bib.csproj +++ b/Txt2Bib.csproj @@ -5,7 +5,7 @@ net6.0-windows enable true - 0.1.0 + 0.1.1