diff --git a/MainWindow.xaml b/MainWindow.xaml index c1905a3..ce677e2 100644 --- a/MainWindow.xaml +++ b/MainWindow.xaml @@ -21,7 +21,7 @@ - + diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index 44139ff..e15b278 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -16,9 +16,13 @@ namespace Txt2Bib public MainWindow() { InitializeComponent(); + OutputDir = $@"{Directory.GetCurrentDirectory()}\bibtex"; + Directory.CreateDirectory(OutputDir); DropArea.AllowDrop = true; } + private string OutputDir { get; } + private void DropArea_Drop(object sender, DragEventArgs e) { // Get data object @@ -38,7 +42,7 @@ namespace Txt2Bib // Process file names var fileNames = dataObject.GetFileDropList(); - StringBuilder bd = new StringBuilder(); + var bd = new StringBuilder(); foreach (var fileName in fileNames) { bd.Append(fileName + "\n"); @@ -70,7 +74,7 @@ namespace Txt2Bib var txt2bib = new Text2Bib(); var result = txt2bib.Generate(DropArea.Text); Debug.Text = result; - using var outputFile = new StreamWriter("output_bibtex.bib"); + using var outputFile = new StreamWriter($@"{OutputDir}\output_bibtex.bib"); outputFile.Write(result); } @@ -82,7 +86,7 @@ namespace Txt2Bib private void OpenDestFolderBtn_Click(object sender, RoutedEventArgs e) { - System.Diagnostics.Process.Start("explorer.exe", Directory.GetCurrentDirectory()); + System.Diagnostics.Process.Start("explorer.exe", OutputDir); } } } diff --git a/Records.cs b/Records.cs index a036e40..72f3ffa 100644 --- a/Records.cs +++ b/Records.cs @@ -2,6 +2,7 @@ using System.Configuration; using System.Linq; using System.Text.RegularExpressions; +using System.Windows.Navigation; namespace Txt2Bib.Records { @@ -29,14 +30,19 @@ namespace Txt2Bib.Records a = a.Trim(); var s = a.Split(' '); 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); + }; 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]); + Volume = checkVol(entryLines[5]); Issue = entryLines[5].Split(',').Length == 2 ? byte.Parse(entryLines[5].Split(',')[1]) : null; FirstPage = ushort.Parse(entryLines[6].Split('-')[0]); @@ -48,12 +54,14 @@ namespace Txt2Bib.Records public override string ToString() { - return $"@{Type}" + "{" +$"{Author[0][..5]}_{Year},\n" + + var volNum = Volume.Equals(byte.MinValue) ? "" : $"\"{Volume}\""; + + return $"@{Type}{{{Author[0][..5]}_{Year},\n" + $"\tauthor = \"{string.Join(" and ", Author)}\",\n" + $"\ttitle = {{{Title}}},\n" + $"\tjournal = \"{Journal}\",\n" + $"\tyear = \"{Year}\",\n" + - $"\tvolume = \"{Volume}\",\n" + + $"\tvolume = \"{volNum}\",\n" + $"\tnumber = \"{Issue}\",\n" + $"\tpages = \"{FirstPage}--{LastPage}\",\n" + $"\tdoi = \"{Doi}\",\n" +