Handle empty volume line; output directory

This commit is contained in:
Nicolò P 2024-01-09 10:33:30 +01:00
parent 768278c234
commit f56028b6a2
3 changed files with 22 additions and 10 deletions

View File

@ -21,7 +21,7 @@
</Button.RenderTransform>
</Button>
<Label Content="Debug" HorizontalAlignment="Left" Margin="44,544,0,0" VerticalAlignment="Top" Foreground="White"/>
<Button x:Name="CopyDebug" Content="Button" HorizontalAlignment="Left" Margin="111,547,0,0" VerticalAlignment="Top" Foreground="{x:Null}" BorderBrush="{x:Null}" Width="23" Background="White" Click="CopyDebug_Click">
<Button x:Name="CopyDebug" ToolTip="Copia contenuto debug negli appunti" Cursor="Hand" Content="Button" HorizontalAlignment="Left" Margin="111,547,0,0" VerticalAlignment="Top" Foreground="{x:Null}" BorderBrush="{x:Null}" Width="23" Background="White" Click="CopyDebug_Click">
<Button.OpacityMask>
<ImageBrush ImageSource="/copy-icon.png"/>
</Button.OpacityMask>

View File

@ -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);
}
}
}

View File

@ -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" +