diff --git a/MainWindow.xaml b/MainWindow.xaml index 5fd0ec1..c1905a3 100644 --- a/MainWindow.xaml +++ b/MainWindow.xaml @@ -5,12 +5,12 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:Txt2Bib" mc:Ignorable="d" - Title="MainWindow" Height="718" Width="800"> - + Title="MainWindow" Height="744" Width="800"> + diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index 9e0443b..44139ff 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -1,17 +1,10 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; -using System.Windows.Controls; -using System.Windows.Data; -using System.Windows.Documents; -using System.Windows.Input; -using System.Windows.Media; -using System.Windows.Media.Imaging; -using System.Windows.Navigation; -using System.Windows.Shapes; namespace Txt2Bib { @@ -75,7 +68,10 @@ namespace Txt2Bib private void GoBtn_Click(object sender, RoutedEventArgs e) { var txt2bib = new Text2Bib(); - Debug.Text = txt2bib.Generate(DropArea.Text); + var result = txt2bib.Generate(DropArea.Text); + Debug.Text = result; + using var outputFile = new StreamWriter("output_bibtex.bib"); + outputFile.Write(result); } private void CopyDebug_Click(object sender, RoutedEventArgs e) @@ -83,5 +79,10 @@ namespace Txt2Bib Debug.SelectAll(); Debug.Copy(); } + + private void OpenDestFolderBtn_Click(object sender, RoutedEventArgs e) + { + System.Diagnostics.Process.Start("explorer.exe", Directory.GetCurrentDirectory()); + } } } diff --git a/Records.cs b/Records.cs index 103b93f..a036e40 100644 --- a/Records.cs +++ b/Records.cs @@ -21,6 +21,31 @@ namespace Txt2Bib.Records public string Doi { get; set; } = ""; public string Url { get; set; } = ""; + public string Convert(string[] entryLines) + { + var temp = entryLines[1].Split(','); + var rearrange = (string a) => + { + a = a.Trim(); + var s = a.Split(' '); + return $"{s[1]} {s[0]}"; + }; + Author = temp.Select(a => rearrange(a)).ToArray(); + Year = ushort.Parse(entryLines[2]); + Title = entryLines[3]; + Journal = entryLines[4]; + Volume = entryLines[5].Split(',').Length == 2 ? + byte.Parse(entryLines[5].Split(',')[0]) : + byte.Parse(entryLines[5]); + 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]); + Doi = entryLines.Length > 7 ? entryLines[7] : ""; + + return ToString(); + } + public override string ToString() { return $"@{Type}" + "{" +$"{Author[0][..5]}_{Year},\n" + @@ -51,7 +76,7 @@ namespace Txt2Bib.Records { return $"@{Type}" + "{" +$"{Author[0][..5]}_{Year},\n" + $"\tauthor = \"{string.Join(" and", Author)}\",\n" + - $"\ttitle = \"{Title}\",\n" + + $"\ttitle = {{{Title}}},\n" + $"\tpublisher = \"{Publisher}\",\n" + $"\taddress = \"{Place}\",\n" + $"\tyear = \"{Year}\",\n" + diff --git a/Text2Bib.cs b/Text2Bib.cs index a0422b6..93c94bf 100644 --- a/Text2Bib.cs +++ b/Text2Bib.cs @@ -3,23 +3,19 @@ using System.IO; using System.Collections.Generic; using System.Linq; using System.Text; -using System.Threading.Tasks; -using System.Windows.Controls.Primitives; -using System.Windows; using Txt2Bib.Records; -using System.ComponentModel.DataAnnotations; +using System.Windows; +using System.Runtime.CompilerServices; +using System.CodeDom; namespace Txt2Bib { - internal class Text2Bib + /// + /// Converting entries from text file to BibTeX format + /// + public class Text2Bib { - private readonly Dictionary _citTypes = new() - { - { "J" , "article" }, - { "B", "book" }, - { "P", "conference" }, - { "C" , "inbook" } - }; + private string[] _citTypes = { "J", "B", "C", "P", "?" }; /// /// Generate single .bib file from input text files /// @@ -39,10 +35,15 @@ namespace Txt2Bib result += Encoding.Latin1.GetString(contentBytes); } - var entries = result.Split('%').ToList().Select(entry => + IEnumerable entries = new List(); + try { - return Process(entry); - }); + entries = result.Split('%').ToList().Select(entry => Process(entry)); + } + catch (Exception) + { + throw; + } foreach (var entry in entries) { @@ -51,56 +52,40 @@ namespace Txt2Bib return output; } - - private string Process(string entryFromTxt) + /// + /// Creates a bib entry from source text + /// + /// Handle exception + /// + /// + /// Invalid citation type for this entry + public string Process(string entryFromTxt) { var lines = entryFromTxt.Trim().Replace("\r", string.Empty).Split("\n"); - var type = lines.First(); + var type = lines.First().Trim(); - System.Diagnostics.Debug.WriteLine($"Entry: {entryFromTxt}; Prima riga: {type}"); + if (!IsValidEntry(type)) throw new Exception($"Invalid entry type '{type}'"); string citation = type switch { - "J" => ConvertArticle(lines), + "J" => (new ArticleBib()).Convert(lines), //"B" => new BookBib(), //"C" => new ChapterBib(), //"P" => new ConferenceBib(), - _ => ConvertArticle(lines), + _ => (new ArticleBib()).Convert(lines) }; return citation; } - - private static string ConvertArticle(string[] entryLines) + /// + /// Validate txt entry based + /// on citation types + /// + /// + protected bool IsValidEntry(string entryType) { - var temp = entryLines[1].Split(','); - var rearrange = (string a) => - { - a = a.Trim(); - var s = a.Split(' '); - return $"{s[1]} {s[0]}"; - }; - var authors = temp.Select(a => rearrange(a)).ToArray(); - var article = new ArticleBib - { - Author = authors, - Year = ushort.Parse(entryLines[2]), - Title = entryLines[3], - Journal = entryLines[4], - // TODO split with ',' for issue - Volume = entryLines[5].Split(',').Length == 2 ? - byte.Parse(entryLines[5].Split(',')[0]) : - byte.Parse(entryLines[5]), - 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]), - Doi = entryLines.Length > 7 ? entryLines[7] : "", - }; - - return article.ToString(); + return _citTypes.Contains(entryType); } - /// /// TODO: How to determine correct encoding of text file?? /// diff --git a/Txt2Bib.csproj b/Txt2Bib.csproj index 261334f..a163a13 100644 --- a/Txt2Bib.csproj +++ b/Txt2Bib.csproj @@ -9,10 +9,12 @@ + + diff --git a/openfolder_4896.png b/openfolder_4896.png new file mode 100644 index 0000000..e5d6281 Binary files /dev/null and b/openfolder_4896.png differ