From 8c043dc18e4011b98786297bcaa8a867ffaabb34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20P?= Date: Wed, 10 Jan 2024 09:57:42 +0100 Subject: [PATCH] Handle mutiple files. use strings in article fields --- Records.cs | 31 ++++++++++++++++++------------- Text2Bib.cs | 53 ++++++++++++++++++++++++++++++++++++----------------- 2 files changed, 54 insertions(+), 30 deletions(-) diff --git a/Records.cs b/Records.cs index 72f3ffa..8c6f6cd 100644 --- a/Records.cs +++ b/Records.cs @@ -1,7 +1,9 @@ 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 @@ -15,8 +17,8 @@ namespace Txt2Bib.Records public string Title { get; set; } = ""; public string Journal { get; set; } = ""; public ushort Year { get; set; } = 1950; - public ushort Volume { get; set; } = 1; - public byte? Issue { get; set; } = null; + 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; } = ""; @@ -32,21 +34,24 @@ namespace Txt2Bib.Records return $"{s[1]} {s[0]}"; }; var checkVol = (string v) => - { - if (v.Length == 0) return byte.MinValue; - return v.Split(',').Length == 2 ? - byte.Parse(v.Split(',')[0]) : - byte.Parse(v); - }; + v.Split(',').Length == 2 ? v.Split(',')[0] : v; + Author = temp.Select(a => rearrange(a)).ToArray(); Year = ushort.Parse(entryLines[2]); Title = entryLines[3]; Journal = entryLines[4]; - Volume = checkVol(entryLines[5]); + Volume = entryLines[5] != string.Empty ? checkVol(entryLines[5]) : Volume; Issue = entryLines[5].Split(',').Length == 2 ? - byte.Parse(entryLines[5].Split(',')[1]) : null; - FirstPage = ushort.Parse(entryLines[6].Split('-')[0]); - LastPage = ushort.Parse(entryLines[6].Split('-')[1]); + entryLines[5].Split(',')[1] : Issue; + try + { + FirstPage = ushort.Parse(entryLines[6].Split('-')[0]); + LastPage = ushort.Parse(entryLines[6].Split('-')[1]); + } + catch (Exception) + { + throw; + } Doi = entryLines.Length > 7 ? entryLines[7] : ""; return ToString(); @@ -82,7 +87,7 @@ namespace Txt2Bib.Records public override string ToString() { - return $"@{Type}" + "{" +$"{Author[0][..5]}_{Year},\n" + + return $"@{Type}{{{Author[0][..5]}_{Year},\n" + $"\tauthor = \"{string.Join(" and", Author)}\",\n" + $"\ttitle = {{{Title}}},\n" + $"\tpublisher = \"{Publisher}\",\n" + diff --git a/Text2Bib.cs b/Text2Bib.cs index 93c94bf..485abc8 100644 --- a/Text2Bib.cs +++ b/Text2Bib.cs @@ -16,6 +16,7 @@ namespace Txt2Bib public class Text2Bib { private string[] _citTypes = { "J", "B", "C", "P", "?" }; + /// /// Generate single .bib file from input text files /// @@ -24,33 +25,43 @@ namespace Txt2Bib public string Generate(string filenames) { var paths = filenames.Trim().Split('\n'); - string result = ""; - string output = ""; + var output = ""; foreach (var path in paths) { - var reader = File.OpenText(path); - byte[] contentBytes = File.ReadAllBytes(path); - - result += Encoding.Latin1.GetString(contentBytes); + output += CreateBibTeX(path); } + return output; + } + /// + /// Create a BibTeX string from an input file path + /// + /// + /// + public string CreateBibTeX(string path) + { + var reader = File.OpenText(path); + byte[] contentBytes = File.ReadAllBytes(path); + var content = Encoding.Latin1.GetString(contentBytes); + IEnumerable entries = new List(); try { - entries = result.Split('%').ToList().Select(entry => Process(entry)); + entries = content.Split('%').ToList().Select(entry => Process(entry)); } catch (Exception) { throw; } + var bibtex = ""; foreach (var entry in entries) { - output += entry; + bibtex += entry; } - return output; + return bibtex; } /// /// Creates a bib entry from source text @@ -66,14 +77,23 @@ namespace Txt2Bib if (!IsValidEntry(type)) throw new Exception($"Invalid entry type '{type}'"); - string citation = type switch + string citation = ""; + + try { - "J" => (new ArticleBib()).Convert(lines), - //"B" => new BookBib(), - //"C" => new ChapterBib(), - //"P" => new ConferenceBib(), - _ => (new ArticleBib()).Convert(lines) - }; + citation = type switch + { + "J" => (new ArticleBib()).Convert(lines), + //"B" => new BookBib(), + //"C" => new ChapterBib(), + //"P" => new ConferenceBib(), + _ => (new ArticleBib()).Convert(lines) + }; + } + catch (Exception) + { + throw new Exception("Faulty string in " + lines); + } return citation; } @@ -95,7 +115,6 @@ namespace Txt2Bib { var cp = "UTF-8"; return Encoding.GetEncoding(cp); - } } }