Handle book records

This commit is contained in:
Nicolò P 2024-01-11 14:56:52 +01:00
parent 50bbc3f3d1
commit 5e0c737423
7 changed files with 62 additions and 21 deletions

View File

@ -1,11 +1,11 @@
<Window x:Class="Txt2Bib.MainWindow"
<Window x:Name="Txt2BibWindow" x:Class="Txt2Bib.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Txt2Bib"
mc:Ignorable="d"
Title="MainWindow" Height="744" Width="800">
Title="Txt2Bib" Height="744" Width="800" Icon="/aec_icon.png">
<Grid Background="#FF38383E" Margin="0,0,0,-16">
<TextBox x:Name="DropArea" Text="Trascinare qui i file txt da convertire" ToolTip="Trascinare qui i file txt da convertire" HorizontalAlignment="Left" Margin="48,105,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="710" Height="245" PreviewDragEnter="DropArea_PreviewDragEnter" PreviewDragOver="DropArea_PreviewDragOver" Drop="DropArea_Drop"/>
<TextBox x:Name="Debug" VerticalScrollBarVisibility="Auto" IsEnabled="True" HorizontalAlignment="Left" Margin="48,458,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="710" Height="224" RenderTransformOrigin="0.449,1.525"/>
@ -26,16 +26,16 @@
<ImageBrush Stretch="Uniform" ImageSource="/copy-icon.png"/>
</Button.OpacityMask>
</Button>
<Menu x:Name="FileMenu" Margin="22,56,607,476" Background="#FF4B4E5D" LostMouseCapture="FileMenu_LostMouseFocus" Visibility="Collapsed">
<Menu x:Name="FileMenu" Margin="22,53,561,527" Background="#FF4B4E5D" MouseLeave="FileMenu_LostMouseFocus" Visibility="Collapsed">
<Menu.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</Menu.ItemsPanel>
<Label x:Name="MenuExec" Content="Esegui..." Foreground="White" FontSize="14" MouseDown="MenuExec_MouseDown" Cursor="Hand" Width="158"/>
<Label x:Name="OpenOutputDir" Content="Apri cartella output" Foreground="White" FontSize="14" MouseDown="OpenOutputDir_MouseDown" Cursor="Hand" Width="160"/>
<Label x:Name="Help" Content="Help" Foreground="White" FontSize="14" MouseDown="Help_MouseDown" Cursor="Hand" Width="161"/>
<Label x:Name="Exit" Content="Esci..." Foreground="White" FontSize="14" MouseDown="Exit_MouseDown" Cursor="Hand" Width="163"/>
<Label x:Name="OpenOutputDir" Content="Apri cartella output..." Foreground="White" FontSize="14" MouseDown="OpenOutputDir_MouseDown" Cursor="Hand" Width="160" HorizontalAlignment="Center" Margin="0,6,0,0" >
</Label>
<Label x:Name="Help" Content="Help" Foreground="White" FontSize="14" MouseDown="Help_MouseDown" Cursor="Hand" Width="161" Margin="0,6,0,0"/>
<Label x:Name="Exit" Content="Esci..." Foreground="White" FontSize="14" MouseDown="Exit_MouseDown" Cursor="Hand" Width="163" Margin="0,6,0,10"/>
</Menu>
<Button x:Name="ToggleMenu" Content="" HorizontalAlignment="Left" Margin="12,14,0,0" VerticalAlignment="Top" RenderTransformOrigin="0,0.202" Height="42" Width="47" BorderBrush="{x:Null}" Foreground="{x:Null}" Cursor="Hand" Click="ToggleMenu_Click">
<Button.OpacityMask>

View File

@ -8,7 +8,10 @@ using System.Windows.Navigation;
namespace Txt2Bib.Records
{
public interface IBib { public string ToString(); }
public interface IBib {
public string Convert(string[] entryLines);
public string ToString();
}
public record class ArticleBib : IBib
{
@ -26,7 +29,6 @@ namespace Txt2Bib.Records
public string Convert(string[] entryLines)
{
var temp = entryLines[1].Split(',');
var rearrange = (string a) =>
{
a = a.Trim();
@ -34,15 +36,15 @@ namespace Txt2Bib.Records
return $"{s[1]} {s[0]}";
};
var checkVol = (string v) =>
v.Split(',').Length == 2 ? v.Split(',')[0] : v;
v.Split(',').Length == 2 ? v.Split(',')[0].Trim() : v.Trim();
Author = temp.Select(a => rearrange(a)).ToArray();
Author = entryLines[1].Split(',').Select(a => rearrange(a)).ToArray();
Year = ushort.Parse(entryLines[2]);
Title = entryLines[3];
Journal = entryLines[4];
Volume = entryLines[5] != string.Empty ? checkVol(entryLines[5]) : Volume;
Issue = entryLines[5].Split(',').Length == 2 ?
entryLines[5].Split(',')[1] : Issue;
entryLines[5].Split(',')[1].Trim() : Issue;
try
{
FirstPage = ushort.Parse(entryLines[6].Split('-')[0]);
@ -52,21 +54,29 @@ namespace Txt2Bib.Records
{
throw;
}
Doi = entryLines.Length > 7 ? entryLines[7] : "";
if (entryLines.Length > 7 )
{
if (IsDoi(entryLines[7])) Doi = entryLines[7];
else Url = entryLines[7];
}
return ToString();
}
private bool IsDoi(string url)
{
return url.Contains("doi");
}
public override string ToString()
{
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 = \"{volNum}\",\n" +
$"\tvolume = \"{Volume}\",\n" +
$"\tnumber = \"{Issue}\",\n" +
$"\tpages = \"{FirstPage}--{LastPage}\",\n" +
$"\tdoi = \"{Doi}\",\n" +
@ -80,15 +90,44 @@ namespace Txt2Bib.Records
public string Type { get; init; } = "book";
public string[] Author { get; set; } = { "Gianni e Pinotto" };
public string Title { get; set; } = "";
public ushort Year { get; set; } = 1950;
public string Publisher { get; set; } = "";
public string Place { get; set; } = "";
public string Url { get; set; } = "";
public ushort Year { get; set; } = 1950;
public string Doi { get; set; } = "";
public string Convert(string[] entryLines)
{
var rearrange = (string a) =>
{
a = a.Trim();
var s = a.Split(' ');
return $"{s[1]} {s[0]}";
};
Author = entryLines[1].Split(',').Select(a => rearrange(a)).ToArray();
Year = ushort.Parse(entryLines[2]);
Title = entryLines[3];
Place = entryLines[4];
Publisher = entryLines[5];
if (entryLines.Length > 6 )
{
if (IsDoi(entryLines[6])) Doi = entryLines[6];
else Url = entryLines[6];
}
return ToString();
}
private bool IsDoi(string url)
{
return url.Contains("doi");
}
public override string ToString()
{
return $"@{Type}{{{Author[0][..5]}_{Year},\n" +
$"\tauthor = \"{string.Join(" and", Author)}\",\n" +
$"\tauthor = \"{string.Join(" and ", Author)}\",\n" +
$"\ttitle = {{{Title}}},\n" +
$"\tpublisher = \"{Publisher}\",\n" +
$"\taddress = \"{Place}\",\n" +

View File

@ -84,15 +84,15 @@ namespace Txt2Bib
citation = type switch
{
"J" => (new ArticleBib()).Convert(lines),
//"B" => new BookBib(),
"B" => new BookBib().Convert(lines),
//"C" => new ChapterBib(),
//"P" => new ConferenceBib(),
_ => (new ArticleBib()).Convert(lines)
_ => ""
};
}
catch (Exception)
{
throw new Exception("Faulty string in " + lines);
throw new Exception("Faulty string in " + entryFromTxt);
}
return citation;

View File

@ -8,12 +8,14 @@
</PropertyGroup>
<ItemGroup>
<None Remove="aec_icon.png" />
<None Remove="copy-icon.png" />
<None Remove="menu-1768000-1502336.png" />
<None Remove="openfolder_4896.png" />
</ItemGroup>
<ItemGroup>
<Resource Include="aec_icon.png" />
<Resource Include="copy-icon.png" />
<Resource Include="menu-1768000-1502336.png" />
<Resource Include="openfolder_4896.png" />

BIN
aec_icon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 32 KiB

BIN
exit-icon-4597.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

BIN
info-icon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.8 KiB