9 Commits

4 changed files with 131 additions and 57 deletions

View File

@@ -55,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();
@@ -65,7 +65,7 @@ namespace Txt2Bib
bd.Append(fileName + "\n");
}
// Set text
DropArea.Text = bd.ToString();
DropArea.Text += bd.ToString();
}
}
@@ -87,9 +87,18 @@ namespace Txt2Bib
}
private void GoBtn_Click(object sender, RoutedEventArgs e)
{
try
{
Execute();
}
catch (Exception ex)
{
MessageBox.Show($"Errore di esecuzione: {ex.Message}");
return;
}
MessageBox.Show("File BibTeX generato correttamente.", "Success", MessageBoxButton.OK, MessageBoxImage.Information);
}
private void CopyDebug_Click(object sender, RoutedEventArgs e)
{

View File

@@ -1,17 +1,13 @@
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 Convert(string[] entryLines);
public string ToString();
}
};
public abstract record ItemType
{
@@ -19,6 +15,13 @@ namespace Txt2Bib.Records
{
return url.Contains("doi");
}
protected static string Rearrange(string a)
{
a = a.Trim();
var s = a.Split(' ');
return s.Length > 1 ?
$"{s[1]} {s[0][0]}{s[0][1..].ToLowerInvariant()}" : a;
}
}
public record class Article : ItemType, IBib
@@ -37,16 +40,10 @@ namespace Txt2Bib.Records
public string Convert(string[] entryLines)
{
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].Trim() : v.Trim();
Author = entryLines[1].Split(',').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];
@@ -60,7 +57,7 @@ namespace Txt2Bib.Records
}
catch (Exception)
{
throw;
throw new Exception("Formato pagine non corretto");
}
if (entryLines.Length > 7 )
@@ -74,7 +71,7 @@ namespace Txt2Bib.Records
public override string ToString()
{
return $"@{Type}{{{Author[0][..5].Replace(". ", "_")}_{Year},\n" +
return $"@{Type}{{{Author[0][..2].Replace(". ", "_")}_{Year},\n" +
$"\tauthor = \"{string.Join(" and ", Author)}\",\n" +
$"\ttitle = {{{Title}}},\n" +
$"\tjournal = \"{Journal}\",\n" +
@@ -96,36 +93,32 @@ namespace Txt2Bib.Records
public string Title { get; set; } = "";
public ushort Year { get; set; } = 1950;
public string Publisher { get; set; } = "";
public string Series { get; set; } = "";
public string Place { get; set; } = "";
public string Url { get; set; } = "";
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]}";
};
var auths = entryLines[1];
if (auths.Contains("eds"))
if (auths.Contains("ed"))
{
Editor = auths.Replace("(eds.)", "").Split(',').Select(a => rearrange(a)).ToArray();
Editor = Regex.Replace(auths,@"\(eds?\.\)", "").Split(',').Select(a => Rearrange(a)).ToArray();
}
else
{
Author = auths.Split(',').Select(a => rearrange(a)).ToArray();
Author = auths.Split(',').Select(a => Rearrange(a)).ToArray();
}
Year = ushort.Parse(entryLines[2]);
Title = entryLines[3];
Place = entryLines[4];
Publisher = entryLines[5];
Series = entryLines[4] != string.Empty ? entryLines[4] : Series;
Place = entryLines[5];
Publisher = entryLines[6];
if (entryLines.Length > 6 )
if (entryLines.Length > 7 )
{
if (IsDoi(entryLines[6])) Doi = entryLines[6];
else Url = entryLines[6];
if (IsDoi(entryLines[7])) Doi = entryLines[7];
else Url = entryLines[7];
}
return ToString();
@@ -138,12 +131,12 @@ namespace Txt2Bib.Records
if (Author.Length != 0)
{
label = Author[0][..5].Replace(". ", "");
label = Author[0][..2].Replace(". ", "");
authString = $"\tauthor = \"{string.Join(" and ", Author)}\",\n";
}
else
{
label = Editor[0][..5].Replace(". ", "");
label = Editor[0][..2].Replace(". ", "");
authString = $"\teditor = \"{string.Join(" and ", Editor)}\",\n";
}
@@ -151,6 +144,7 @@ namespace Txt2Bib.Records
authString +
$"\ttitle = {{{Title}}},\n" +
$"\tpublisher = \"{Publisher}\",\n" +
$"\tseries = \"{Series}\",\n" +
$"\taddress = \"{Place}\",\n" +
$"\tyear = \"{Year}\",\n" +
$"\turl = \"{Url}\",\n" +
@@ -168,6 +162,7 @@ namespace Txt2Bib.Records
public string BookTitle { get; set; } = "";
public ushort Year { get; set; } = 1950;
public string Publisher { get; set; } = "";
public string Series { get; set; } = "";
public string Address { get; set; } = "";
public ushort FirstPage { get; set; } = 1;
public ushort LastPage { get; set; } = 1;
@@ -176,32 +171,30 @@ namespace Txt2Bib.Records
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();
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];
Editor = entryLines[4] != String.Empty ?
Regex.Replace(entryLines[4], @"\(eds?\.\)", "").Split(',').Select(a => Rearrange(a)).ToArray() :
Editor;
BookTitle = entryLines[5];
Series = entryLines[6] != String.Empty ? entryLines[6] : Series;
Address = entryLines[7] != String.Empty ? entryLines[7] : Address;
Publisher = entryLines[8];
try
{
FirstPage = ushort.Parse(entryLines[7].Split('-')[0]);
LastPage = ushort.Parse(entryLines[7].Split('-')[1].TrimEnd('.'));
FirstPage = ushort.Parse(entryLines[9].Split('-')[0]);
LastPage = ushort.Parse(entryLines[9].Split('-')[1].TrimEnd('.'));
}
catch (Exception)
{
throw;
throw new Exception("Formato numeri di pagina errato...");
}
if (entryLines.Length > 8 )
if (entryLines.Length > 10)
{
if (IsDoi(entryLines[8])) Doi = entryLines[8];
else Url = entryLines[8];
if (IsDoi(entryLines[10])) Doi = entryLines[10];
else Url = entryLines[10];
}
return ToString();
@@ -209,14 +202,85 @@ namespace Txt2Bib.Records
public override string ToString()
{
string label = Author[0][..5].Replace(". ", "");
string label = Author[0][..2].Replace(". ", "");
string authString = $"\tauthor = \"{string.Join(" and ", Author)}\",\n";
string edsString = Editor.Length != 0 ?
$"\teditor = \"{string.Join(" and ", Editor)}\",\n" : "";
return $"@{Type}{{{label}_{Year},\n" +
authString +
edsString +
$"\ttitle = {{{Title}}},\n" +
$"\tbooktitle = {{{BookTitle}}},\n" +
$"\tpublisher = \"{Publisher}\",\n" +
$"\tseries = {{{Series}}},\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 Series { 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 = Regex.Replace(entryLines[4], @"\(eds?\.\)", "").Split(',').Select(a => Rearrange(a)).ToArray();
BookTitle = entryLines[5];
Series = entryLines[6] != String.Empty ? entryLines[6] : Series;
Address = entryLines[7] != String.Empty ? entryLines[7] : Address;
Publisher = entryLines[8];
try
{
FirstPage = ushort.Parse(entryLines[9].Split('-')[0]);
LastPage = ushort.Parse(entryLines[9].Split('-')[1].TrimEnd('.'));
}
catch (Exception)
{
throw new Exception("Formato numeri di pagina errato...");
}
if (entryLines.Length > 10)
{
if (IsDoi(entryLines[10])) Doi = entryLines[10];
else Url = entryLines[10];
}
return ToString();
}
public override string ToString()
{
string label = Author[0][..2].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" +
$"\tseries = {{{Series}}},\n" +
$"\taddress = \"{Address}\",\n" +
$"\tyear = \"{Year}\",\n" +
$"\tpages = \"{FirstPage}--{LastPage}\",\n" +

View File

@@ -15,7 +15,7 @@ namespace Txt2Bib
/// </summary>
public class Text2Bib
{
private string[] _citTypes = { "J", "B", "C", "P", "I" };
private readonly string[] _citTypes = { "J", "B", "C", "P" };
/// <summary>
/// Generate single .bib file from input text files
@@ -43,6 +43,7 @@ namespace Txt2Bib
{
using var reader = File.OpenText(path);
byte[] contentBytes = File.ReadAllBytes(path);
// Use Windows 1252??
var content = Encoding.Latin1.GetString(contentBytes);
IEnumerable<string> entries = new List<string>();
@@ -52,7 +53,7 @@ namespace Txt2Bib
}
catch (Exception)
{
throw;
throw new Exception($"Errore di elaborazione in {entries.Last()}");
}
var bibtex = "";
@@ -75,7 +76,7 @@ namespace Txt2Bib
var lines = entryFromTxt.Trim().Replace("\r", string.Empty).Split("\n");
var type = lines.First().Trim();
if (!IsValidEntry(type)) throw new Exception($"Invalid entry type '{type}'");
//if (!IsValidEntry(type)) throw new Exception($"Invalid entry type '{type}'");
string citation;
try
@@ -84,14 +85,14 @@ namespace Txt2Bib
{
"J" => new Article().Convert(lines),
"B" => new Book().Convert(lines),
//"C" => new ChapterBib(),
"C" => new Chapter().Convert(lines),
"P" => new Proceedings().Convert(lines),
_ => ""
};
}
catch (Exception)
catch (Exception ex)
{
throw new Exception("Faulty string in " + entryFromTxt);
throw new Exception($"Errore in {entryFromTxt}\nMessaggio di errore: \"{ex.Message}\"");
}
return citation;

View File

@@ -5,7 +5,7 @@
<TargetFramework>net6.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
<Version>0.1.2</Version>
<Version>0.1.9</Version>
</PropertyGroup>
<ItemGroup>