Compare commits

..

No commits in common. "master" and "0.1.5" have entirely different histories.

4 changed files with 73 additions and 81 deletions

View File

@ -87,16 +87,8 @@ namespace Txt2Bib
} }
private void GoBtn_Click(object sender, RoutedEventArgs e) private void GoBtn_Click(object sender, RoutedEventArgs e)
{
try
{ {
Execute(); Execute();
}
catch (Exception ex)
{
MessageBox.Show($"Errore di esecuzione: {ex.Message}");
return;
}
MessageBox.Show("File BibTeX generato correttamente.", "Success", MessageBoxButton.OK, MessageBoxImage.Information); MessageBox.Show("File BibTeX generato correttamente.", "Success", MessageBoxButton.OK, MessageBoxImage.Information);
} }

View File

@ -11,8 +11,6 @@ namespace Txt2Bib.Records
public abstract record ItemType public abstract record ItemType
{ {
public string Pages { get; set; } = "";
protected static bool IsDoi(string url) protected static bool IsDoi(string url)
{ {
return url.Contains("doi"); return url.Contains("doi");
@ -21,17 +19,7 @@ namespace Txt2Bib.Records
{ {
a = a.Trim(); a = a.Trim();
var s = a.Split(' '); var s = a.Split(' ');
return s.Length > 1 ? return $"{s[1]} {s[0]}";
$"{s[1]} {s[0][0]}{s[0][1..].ToLower()}" : a;
}
protected string CreatePages(string pages)
{
if (pages.Length == 0) { return ""; }
return pages.Split('-').Length == 2 ?
$"{pages.Split('-')[0]}--{pages.Split('-')[1].TrimEnd('.')}" :
pages;
} }
} }
@ -44,6 +32,8 @@ namespace Txt2Bib.Records
public ushort Year { get; set; } = 1950; public ushort Year { get; set; } = 1950;
public string Volume { get; set; } = ""; public string Volume { get; set; } = "";
public string? Issue { get; set; } = null; public string? Issue { get; set; } = null;
public ushort FirstPage { get; set; } = 1;
public ushort LastPage { get; set; } = 1;
public string Doi { get; set; } = ""; public string Doi { get; set; } = "";
public string Url { get; set; } = ""; public string Url { get; set; } = "";
@ -59,7 +49,15 @@ namespace Txt2Bib.Records
Volume = entryLines[5] != string.Empty ? checkVol(entryLines[5]) : Volume; Volume = entryLines[5] != string.Empty ? checkVol(entryLines[5]) : Volume;
Issue = entryLines[5].Split(',').Length == 2 ? Issue = entryLines[5].Split(',').Length == 2 ?
entryLines[5].Split(',')[1].Trim() : Issue; entryLines[5].Split(',')[1].Trim() : Issue;
Pages = CreatePages(entryLines[6]); try
{
FirstPage = ushort.Parse(entryLines[6].Split('-')[0]);
LastPage = ushort.Parse(entryLines[6].Split('-')[1].TrimEnd('.'));
}
catch (Exception)
{
throw;
}
if (entryLines.Length > 7 ) if (entryLines.Length > 7 )
{ {
@ -72,14 +70,14 @@ namespace Txt2Bib.Records
public override string ToString() public override string ToString()
{ {
return $"@{Type}{{{Author[0][..2].Replace(". ", "_")}_{Year},\n" + return $"@{Type}{{{Author[0][..5].Replace(". ", "_")}_{Year},\n" +
$"\tauthor = \"{string.Join(" and ", Author)}\",\n" + $"\tauthor = \"{string.Join(" and ", Author)}\",\n" +
$"\ttitle = {{{Title}}},\n" + $"\ttitle = {{{Title}}},\n" +
$"\tjournal = \"{Journal}\",\n" + $"\tjournal = \"{Journal}\",\n" +
$"\tyear = \"{Year}\",\n" + $"\tyear = \"{Year}\",\n" +
$"\tvolume = \"{Volume}\",\n" + $"\tvolume = \"{Volume}\",\n" +
$"\tnumber = \"{Issue}\",\n" + $"\tnumber = \"{Issue}\",\n" +
$"\tpages = \"{Pages}\",\n" + $"\tpages = \"{FirstPage}--{LastPage}\",\n" +
$"\tdoi = \"{Doi}\",\n" + $"\tdoi = \"{Doi}\",\n" +
$"\turl = \"{Url}\",\n" + $"\turl = \"{Url}\",\n" +
"}\n"; "}\n";
@ -94,7 +92,6 @@ namespace Txt2Bib.Records
public string Title { get; set; } = ""; public string Title { get; set; } = "";
public ushort Year { get; set; } = 1950; public ushort Year { get; set; } = 1950;
public string Publisher { get; set; } = ""; public string Publisher { get; set; } = "";
public string Series { get; set; } = "";
public string Place { get; set; } = ""; public string Place { get; set; } = "";
public string Url { get; set; } = ""; public string Url { get; set; } = "";
public string Doi { get; set; } = ""; public string Doi { get; set; } = "";
@ -102,9 +99,9 @@ namespace Txt2Bib.Records
public string Convert(string[] entryLines) public string Convert(string[] entryLines)
{ {
var auths = entryLines[1]; var auths = entryLines[1];
if (auths.Contains("ed")) if (auths.Contains("eds"))
{ {
Editor = Regex.Replace(auths,@"\(eds?\.\)", "").Split(',').Select(a => Rearrange(a)).ToArray(); Editor = auths.Replace("(eds.)", "").Split(',').Select(a => Rearrange(a)).ToArray();
} }
else else
{ {
@ -112,14 +109,13 @@ namespace Txt2Bib.Records
} }
Year = ushort.Parse(entryLines[2]); Year = ushort.Parse(entryLines[2]);
Title = entryLines[3]; Title = entryLines[3];
Series = entryLines[4] != string.Empty ? entryLines[4] : Series; Place = entryLines[4];
Place = entryLines[5]; Publisher = entryLines[5];
Publisher = entryLines[6];
if (entryLines.Length > 7 ) if (entryLines.Length > 6 )
{ {
if (IsDoi(entryLines[7])) Doi = entryLines[7]; if (IsDoi(entryLines[6])) Doi = entryLines[6];
else Url = entryLines[7]; else Url = entryLines[6];
} }
return ToString(); return ToString();
@ -132,12 +128,12 @@ namespace Txt2Bib.Records
if (Author.Length != 0) if (Author.Length != 0)
{ {
label = Author[0][..2].Replace(". ", ""); label = Author[0][..5].Replace(". ", "");
authString = $"\tauthor = \"{string.Join(" and ", Author)}\",\n"; authString = $"\tauthor = \"{string.Join(" and ", Author)}\",\n";
} }
else else
{ {
label = Editor[0][..2].Replace(". ", ""); label = Editor[0][..5].Replace(". ", "");
authString = $"\teditor = \"{string.Join(" and ", Editor)}\",\n"; authString = $"\teditor = \"{string.Join(" and ", Editor)}\",\n";
} }
@ -145,7 +141,6 @@ namespace Txt2Bib.Records
authString + authString +
$"\ttitle = {{{Title}}},\n" + $"\ttitle = {{{Title}}},\n" +
$"\tpublisher = \"{Publisher}\",\n" + $"\tpublisher = \"{Publisher}\",\n" +
$"\tseries = \"{Series}\",\n" +
$"\taddress = \"{Place}\",\n" + $"\taddress = \"{Place}\",\n" +
$"\tyear = \"{Year}\",\n" + $"\tyear = \"{Year}\",\n" +
$"\turl = \"{Url}\",\n" + $"\turl = \"{Url}\",\n" +
@ -158,13 +153,13 @@ namespace Txt2Bib.Records
{ {
public string Type { get; init; } = "inproceedings"; public string Type { get; init; } = "inproceedings";
public string[] Author { get; set; } = Array.Empty<string>(); public string[] Author { get; set; } = Array.Empty<string>();
public string[] Editor { get; set; } = Array.Empty<string>();
public string Title { get; set; } = ""; public string Title { get; set; } = "";
public string BookTitle { get; set; } = ""; public string BookTitle { get; set; } = "";
public ushort Year { get; set; } = 1950; public ushort Year { get; set; } = 1950;
public string Publisher { get; set; } = ""; public string Publisher { get; set; } = "";
public string Series { get; set; } = "";
public string Address { 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 Url { get; set; } = "";
public string Doi { get; set; } = ""; public string Doi { get; set; } = "";
@ -173,19 +168,23 @@ namespace Txt2Bib.Records
Author = entryLines[1].Split(',').Select(a => Rearrange(a)).ToArray(); Author = entryLines[1].Split(',').Select(a => Rearrange(a)).ToArray();
Year = ushort.Parse(entryLines[2]); Year = ushort.Parse(entryLines[2]);
Title = entryLines[3]; Title = entryLines[3];
Editor = entryLines[4] != String.Empty ? BookTitle = entryLines[4];
Regex.Replace(entryLines[4], @"\(eds?\.\)", "").Split(',').Select(a => Rearrange(a)).ToArray() : Address = entryLines[5] != String.Empty ? entryLines[5] : Address;
Editor; Publisher = entryLines[6];
BookTitle = entryLines[5]; try
Series = entryLines[6] != String.Empty ? entryLines[6] : Series;
Address = entryLines[7] != String.Empty ? entryLines[7] : Address;
Publisher = entryLines[8];
Pages = CreatePages(entryLines[9]);
if (entryLines.Length > 10)
{ {
if (IsDoi(entryLines[10])) Doi = entryLines[10]; FirstPage = ushort.Parse(entryLines[7].Split('-')[0]);
else Url = entryLines[10]; 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(); return ToString();
@ -193,21 +192,17 @@ namespace Txt2Bib.Records
public override string ToString() public override string ToString()
{ {
string label = Author[0][..2].Replace(". ", ""); string label = Author[0][..5].Replace(". ", "");
string authString = $"\tauthor = \"{string.Join(" and ", Author)}\",\n"; string authString = $"\tauthor = \"{string.Join(" and ", Author)}\",\n";
string edsString = Editor.Length != 0 ?
$"\teditor = \"{string.Join(" and ", Editor)}\",\n" : "";
return $"@{Type}{{{label}_{Year},\n" + return $"@{Type}{{{label}_{Year},\n" +
authString + authString +
edsString +
$"\ttitle = {{{Title}}},\n" + $"\ttitle = {{{Title}}},\n" +
$"\tbooktitle = {{{BookTitle}}},\n" + $"\tbooktitle = {{{BookTitle}}},\n" +
$"\tpublisher = \"{Publisher}\",\n" + $"\tpublisher = \"{Publisher}\",\n" +
$"\tseries = {{{Series}}},\n" +
$"\taddress = \"{Address}\",\n" + $"\taddress = \"{Address}\",\n" +
$"\tyear = \"{Year}\",\n" + $"\tyear = \"{Year}\",\n" +
$"\tpages = \"{Pages}\",\n" + $"\tpages = \"{FirstPage}--{LastPage}\",\n" +
$"\turl = \"{Url}\",\n" + $"\turl = \"{Url}\",\n" +
$"\tdoi = \"{Doi}\",\n" + $"\tdoi = \"{Doi}\",\n" +
"}\n"; "}\n";
@ -223,8 +218,9 @@ namespace Txt2Bib.Records
public string BookTitle { get; set; } = ""; public string BookTitle { get; set; } = "";
public ushort Year { get; set; } = 1950; public ushort Year { get; set; } = 1950;
public string Publisher { get; set; } = ""; public string Publisher { get; set; } = "";
public string Series { get; set; } = "";
public string Address { 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 Url { get; set; } = "";
public string Doi { get; set; } = ""; public string Doi { get; set; } = "";
@ -233,17 +229,24 @@ namespace Txt2Bib.Records
Author = entryLines[1].Split(',').Select(a => Rearrange(a)).ToArray(); Author = entryLines[1].Split(',').Select(a => Rearrange(a)).ToArray();
Year = ushort.Parse(entryLines[2]); Year = ushort.Parse(entryLines[2]);
Title = entryLines[3]; Title = entryLines[3];
Editor = Regex.Replace(entryLines[4], @"\(eds?\.\)", "").Split(',').Select(a => Rearrange(a)).ToArray(); Editor = entryLines[4].Replace("(eds.)", "").Split(',').Select(a => Rearrange(a)).ToArray();
BookTitle = entryLines[5]; BookTitle = entryLines[5];
Series = entryLines[6] != String.Empty ? entryLines[6] : Series; Address = entryLines[6] != String.Empty ? entryLines[6] : Address;
Address = entryLines[7] != String.Empty ? entryLines[7] : Address; Publisher = entryLines[7];
Publisher = entryLines[8]; try
Pages = CreatePages(entryLines[9]);
if (entryLines.Length > 10)
{ {
if (IsDoi(entryLines[10])) Doi = entryLines[10]; FirstPage = ushort.Parse(entryLines[8].Split('-')[0]);
else Url = entryLines[10]; 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(); return ToString();
@ -251,7 +254,7 @@ namespace Txt2Bib.Records
public override string ToString() public override string ToString()
{ {
string label = Author[0][..2].Replace(". ", ""); string label = Author[0][..5].Replace(". ", "");
string authString = $"\tauthor = \"{string.Join(" and ", Author)}\",\n"; string authString = $"\tauthor = \"{string.Join(" and ", Author)}\",\n";
string edsString = $"\teditor = \"{string.Join(" and ", Editor)}\",\n"; string edsString = $"\teditor = \"{string.Join(" and ", Editor)}\",\n";
@ -261,10 +264,9 @@ namespace Txt2Bib.Records
$"\ttitle = {{{Title}}},\n" + $"\ttitle = {{{Title}}},\n" +
$"\tbooktitle = {{{BookTitle}}},\n" + $"\tbooktitle = {{{BookTitle}}},\n" +
$"\tpublisher = \"{Publisher}\",\n" + $"\tpublisher = \"{Publisher}\",\n" +
$"\tseries = {{{Series}}},\n" +
$"\taddress = \"{Address}\",\n" + $"\taddress = \"{Address}\",\n" +
$"\tyear = \"{Year}\",\n" + $"\tyear = \"{Year}\",\n" +
$"\tpages = \"{Pages}\",\n" + $"\tpages = \"{FirstPage}--{LastPage}\",\n" +
$"\turl = \"{Url}\",\n" + $"\turl = \"{Url}\",\n" +
$"\tdoi = \"{Doi}\",\n" + $"\tdoi = \"{Doi}\",\n" +
"}\n"; "}\n";

View File

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

View File

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