10 Commits

Author SHA1 Message Date
82c2bc6ce2 Append additional dropped files (don't clear text area) 2024-01-17 11:31:33 +01:00
4e7fd92a13 Handle book chapter 2024-01-16 12:47:27 +01:00
d2dd282307 Refactor records 2024-01-16 12:13:41 +01:00
96dff46c14 Handle proceedings 2024-01-16 11:51:32 +01:00
a1b8218ed4 Some refactoring... 2024-01-12 12:23:44 +01:00
82cde40bee Amend versioning,,, 2024-01-12 12:09:12 +01:00
7938149c90 Add about dialog and versioning 2024-01-12 12:07:36 +01:00
356dc796c8 Process book records correctly 2024-01-12 12:07:03 +01:00
5e0c737423 Handle book records 2024-01-11 14:56:52 +01:00
50bbc3f3d1 More UI changes 2024-01-10 18:09:34 +01:00
9 changed files with 244 additions and 51 deletions

View File

@@ -1,3 +1,4 @@
using System.Reflection;
using System.Windows;
[assembly: ThemeInfo(

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,17 @@
<ImageBrush Stretch="Uniform" ImageSource="/copy-icon.png"/>
</Button.OpacityMask>
</Button>
<Menu x:Name="FileMenu" Margin="22,56,607,476" Background="#FF4B4E5D" Visibility="Collapsed">
<Menu x:Name="FileMenu" Margin="22,53,561,497" 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"/>
<Label x:Name="OpenOutputDir" Content="Apri cartella output" Foreground="White" FontSize="14" MouseDown="OpenOutputDir_MouseDown" Cursor="Hand"/>
<Label x:Name="Help" Content="Help" Foreground="White" FontSize="14" MouseDown="Help_MouseDown" Cursor="Hand"/>
<Label x:Name="Exit" Content="Esci..." Foreground="White" FontSize="14" MouseDown="Exit_MouseDown" Cursor="Hand"/>
<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="About" Content="About" Foreground="White" FontSize="14" MouseDown="About_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,0"/>
</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

@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
@@ -19,12 +20,19 @@ namespace Txt2Bib
OutputDir = $@"{Directory.GetCurrentDirectory()}\bibtex";
Directory.CreateDirectory(OutputDir);
DropArea.AllowDrop = true;
InitialDropText = DropArea.Text;
}
private string OutputDir { get; }
private string InitialDropText { get; }
private void Execute()
{
if (DropArea.Text == InitialDropText)
{
MessageBox.Show("Nessun percorso disponibile...");
return;
}
var txt2bib = new Text2Bib();
var result = txt2bib.Generate(DropArea.Text);
Debug.Text = result;
@@ -47,7 +55,7 @@ namespace Txt2Bib
if (dataObject.ContainsFileDropList())
{
// Clear values
DropArea.Text = string.Empty;
if (DropArea.Text == InitialDropText) DropArea.Text = string.Empty;
// Process file names
var fileNames = dataObject.GetFileDropList();
@@ -57,7 +65,7 @@ namespace Txt2Bib
bd.Append(fileName + "\n");
}
// Set text
DropArea.Text = bd.ToString();
DropArea.Text += bd.ToString();
}
}
@@ -111,11 +119,6 @@ namespace Txt2Bib
System.Diagnostics.Process.Start("explorer.exe", OutputDir);
}
private void MenuExec_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
Execute();
}
private void Exit_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
Environment.Exit(0);
@@ -127,5 +130,16 @@ namespace Txt2Bib
"Il file BibTeX viene generato in una cartella dedicata, che si può aprire selezionando " +
"'Apri cartella output' dal menu.", "Help", MessageBoxButton.OK, MessageBoxImage.Information);
}
private void FileMenu_LostMouseFocus(object sender, RoutedEventArgs e)
{
FileMenu.Visibility = Visibility.Collapsed;
}
private void About_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
MessageBox.Show($"Txt2Bib version {Assembly.GetExecutingAssembly().GetName().Version}\n" +
"Powered by Nicopa", "About", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
}

View File

@@ -1,16 +1,29 @@
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
{
public interface IBib { public string ToString(); }
public interface IBib {
public string Convert(string[] entryLines);
public string ToString();
};
public record class ArticleBib : IBib
public abstract record ItemType
{
protected static bool IsDoi(string url)
{
return url.Contains("doi");
}
protected static string Rearrange(string a)
{
a = a.Trim();
var s = a.Split(' ');
return $"{s[1]} {s[0]}";
}
}
public record class Article : ItemType, IBib
{
public string Type { get; init; } = "article";
public string[] Author { get; set; } = { "Gianni e Pinotto" };
@@ -26,47 +39,43 @@ namespace Txt2Bib.Records
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]}";
};
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]);
LastPage = ushort.Parse(entryLines[6].Split('-')[1]);
LastPage = ushort.Parse(entryLines[6].Split('-')[1].TrimEnd('.'));
}
catch (Exception)
{
throw;
}
Doi = entryLines.Length > 7 ? entryLines[7] : "";
if (entryLines.Length > 7 )
{
if (IsDoi(entryLines[7])) Doi = entryLines[7];
else Url = entryLines[7];
}
return ToString();
}
public override string ToString()
{
var volNum = Volume.Equals(byte.MinValue) ? "" : $"\"{Volume}\"";
return $"@{Type}{{{Author[0][..5]}_{Year},\n" +
return $"@{Type}{{{Author[0][..5].Replace(". ", "_")}_{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" +
@@ -75,25 +84,191 @@ namespace Txt2Bib.Records
}
}
public record class BookBib : IBib
public record class Book : ItemType, IBib
{
public string Type { get; init; } = "book";
public string[] Author { get; set; } = { "Gianni e Pinotto" };
public string[] Author { get; set; } = Array.Empty<string>();
public string[] Editor { get; set; } = Array.Empty<string>();
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 auths = entryLines[1];
if (auths.Contains("eds"))
{
Editor = auths.Replace("(eds.)", "").Split(',').Select(a => Rearrange(a)).ToArray();
}
else
{
Author = auths.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();
}
public override string ToString()
{
return $"@{Type}{{{Author[0][..5]}_{Year},\n" +
$"\tauthor = \"{string.Join(" and", Author)}\",\n" +
string label;
string authString;
if (Author.Length != 0)
{
label = Author[0][..5].Replace(". ", "");
authString = $"\tauthor = \"{string.Join(" and ", Author)}\",\n";
}
else
{
label = Editor[0][..5].Replace(". ", "");
authString = $"\teditor = \"{string.Join(" and ", Editor)}\",\n";
}
return $"@{Type}{{{label}_{Year},\n" +
authString +
$"\ttitle = {{{Title}}},\n" +
$"\tpublisher = \"{Publisher}\",\n" +
$"\taddress = \"{Place}\",\n" +
$"\tyear = \"{Year}\",\n" +
$"\turl = \"{Url}\",\n" +
$"\tdoi = \"{Doi}\",\n" +
"}\n";
}
}
public record class Proceedings : ItemType, IBib
{
public string Type { get; init; } = "inproceedings";
public string[] Author { get; set; } = Array.Empty<string>();
public string Title { get; set; } = "";
public string BookTitle { get; set; } = "";
public ushort Year { get; set; } = 1950;
public string Publisher { get; set; } = "";
public string Address { get; set; } = "";
public ushort FirstPage { get; set; } = 1;
public ushort LastPage { get; set; } = 1;
public string Url { get; set; } = "";
public string Doi { get; set; } = "";
public string Convert(string[] entryLines)
{
Author = entryLines[1].Split(',').Select(a => Rearrange(a)).ToArray();
Year = ushort.Parse(entryLines[2]);
Title = entryLines[3];
BookTitle = entryLines[4];
Address = entryLines[5] != String.Empty ? entryLines[5] : Address;
Publisher = entryLines[6];
try
{
FirstPage = ushort.Parse(entryLines[7].Split('-')[0]);
LastPage = ushort.Parse(entryLines[7].Split('-')[1].TrimEnd('.'));
}
catch (Exception)
{
throw;
}
if (entryLines.Length > 8 )
{
if (IsDoi(entryLines[8])) Doi = entryLines[8];
else Url = entryLines[8];
}
return ToString();
}
public override string ToString()
{
string label = Author[0][..5].Replace(". ", "");
string authString = $"\tauthor = \"{string.Join(" and ", Author)}\",\n";
return $"@{Type}{{{label}_{Year},\n" +
authString +
$"\ttitle = {{{Title}}},\n" +
$"\tbooktitle = {{{BookTitle}}},\n" +
$"\tpublisher = \"{Publisher}\",\n" +
$"\taddress = \"{Address}\",\n" +
$"\tyear = \"{Year}\",\n" +
$"\tpages = \"{FirstPage}--{LastPage}\",\n" +
$"\turl = \"{Url}\",\n" +
$"\tdoi = \"{Doi}\",\n" +
"}\n";
}
}
public record class Chapter : ItemType, IBib
{
public string Type { get; init; } = "incollection";
public string[] Author { get; set; } = Array.Empty<string>();
public string[] Editor { get; set; } = Array.Empty<string>();
public string Title { get; set; } = "";
public string BookTitle { get; set; } = "";
public ushort Year { get; set; } = 1950;
public string Publisher { get; set; } = "";
public string Address { get; set; } = "";
public ushort FirstPage { get; set; } = 1;
public ushort LastPage { get; set; } = 1;
public string Url { get; set; } = "";
public string Doi { get; set; } = "";
public string Convert(string[] entryLines)
{
Author = entryLines[1].Split(',').Select(a => Rearrange(a)).ToArray();
Year = ushort.Parse(entryLines[2]);
Title = entryLines[3];
Editor = entryLines[4].Replace("(eds.)", "").Split(',').Select(a => Rearrange(a)).ToArray();
BookTitle = entryLines[5];
Address = entryLines[6] != String.Empty ? entryLines[6] : Address;
Publisher = entryLines[7];
try
{
FirstPage = ushort.Parse(entryLines[8].Split('-')[0]);
LastPage = ushort.Parse(entryLines[8].Split('-')[1].TrimEnd('.'));
}
catch (Exception)
{
throw;
}
if (entryLines.Length > 9)
{
if (IsDoi(entryLines[9])) Doi = entryLines[9];
else Url = entryLines[9];
}
return ToString();
}
public override string ToString()
{
string label = Author[0][..5].Replace(". ", "");
string authString = $"\tauthor = \"{string.Join(" and ", Author)}\",\n";
string edsString = $"\teditor = \"{string.Join(" and ", Editor)}\",\n";
return $"@{Type}{{{label}_{Year},\n" +
authString +
edsString +
$"\ttitle = {{{Title}}},\n" +
$"\tbooktitle = {{{BookTitle}}},\n" +
$"\tpublisher = \"{Publisher}\",\n" +
$"\taddress = \"{Address}\",\n" +
$"\tyear = \"{Year}\",\n" +
$"\tpages = \"{FirstPage}--{LastPage}\",\n" +
$"\turl = \"{Url}\",\n" +
$"\tdoi = \"{Doi}\",\n" +
"}\n";
}
}

View File

@@ -15,7 +15,7 @@ namespace Txt2Bib
/// </summary>
public class Text2Bib
{
private string[] _citTypes = { "J", "B", "C", "P", "?" };
private readonly string[] _citTypes = { "J", "B", "C", "P" };
/// <summary>
/// Generate single .bib file from input text files
@@ -41,7 +41,7 @@ namespace Txt2Bib
/// <returns></returns>
public string CreateBibTeX(string path)
{
var reader = File.OpenText(path);
using var reader = File.OpenText(path);
byte[] contentBytes = File.ReadAllBytes(path);
var content = Encoding.Latin1.GetString(contentBytes);
@@ -77,22 +77,21 @@ namespace Txt2Bib
if (!IsValidEntry(type)) throw new Exception($"Invalid entry type '{type}'");
string citation = "";
string citation;
try
{
citation = type switch
{
"J" => (new ArticleBib()).Convert(lines),
//"B" => new BookBib(),
//"C" => new ChapterBib(),
//"P" => new ConferenceBib(),
_ => (new ArticleBib()).Convert(lines)
"J" => new Article().Convert(lines),
"B" => new Book().Convert(lines),
"C" => new Chapter().Convert(lines),
"P" => new Proceedings().Convert(lines),
_ => ""
};
}
catch (Exception)
{
throw new Exception("Faulty string in " + lines);
throw new Exception("Faulty string in " + entryFromTxt);
}
return citation;

View File

@@ -5,15 +5,18 @@
<TargetFramework>net6.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
<Version>0.1.4</Version>
</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