Adjust "series" and other fields

This commit is contained in:
Nicolò P 2024-01-30 16:54:37 +01:00
parent a43e6fb57f
commit 308f680a87
4 changed files with 35 additions and 30 deletions

View File

@ -34,15 +34,7 @@ namespace Txt2Bib
return; return;
} }
var txt2bib = new Text2Bib(); var txt2bib = new Text2Bib();
var result = ""; var result = txt2bib.Generate(DropArea.Text);
try
{
result = txt2bib.Generate(DropArea.Text);
}
catch (Exception ex)
{
MessageBox.Show($"Errore di esecuzione: {ex.Message}");
}
Debug.Text = result; Debug.Text = result;
using var outputFile = new StreamWriter($@"{OutputDir}\output_bibtex.bib"); using var outputFile = new StreamWriter($@"{OutputDir}\output_bibtex.bib");
outputFile.Write(result); outputFile.Write(result);
@ -96,7 +88,15 @@ namespace Txt2Bib
private void GoBtn_Click(object sender, RoutedEventArgs e) private void GoBtn_Click(object sender, RoutedEventArgs e)
{ {
Execute(); try
{
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

@ -17,9 +17,10 @@ namespace Txt2Bib.Records
} }
protected static string Rearrange(string a) protected static string Rearrange(string a)
{ {
a = a.Trim(); a = a.Trim();
var s = a.Split(' '); var s = a.Split(' ');
return $"{s[1]} {s[0]}"; return s.Length > 1 ?
$"{s[1]} {s[0]}" : a;
} }
} }
@ -56,7 +57,7 @@ namespace Txt2Bib.Records
} }
catch (Exception) catch (Exception)
{ {
throw; throw new Exception("Formato pagine non corretto");
} }
if (entryLines.Length > 7 ) if (entryLines.Length > 7 )
@ -70,7 +71,7 @@ namespace Txt2Bib.Records
public override string ToString() 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" + $"\tauthor = \"{string.Join(" and ", Author)}\",\n" +
$"\ttitle = {{{Title}}},\n" + $"\ttitle = {{{Title}}},\n" +
$"\tjournal = \"{Journal}\",\n" + $"\tjournal = \"{Journal}\",\n" +
@ -92,6 +93,7 @@ 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; } = "";
@ -99,9 +101,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("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 else
{ {
@ -109,13 +111,14 @@ namespace Txt2Bib.Records
} }
Year = ushort.Parse(entryLines[2]); Year = ushort.Parse(entryLines[2]);
Title = entryLines[3]; Title = entryLines[3];
Place = entryLines[4]; Series = entryLines[4] != string.Empty ? entryLines[4] : Series;
Publisher = entryLines[5]; Place = entryLines[5];
Publisher = entryLines[6];
if (entryLines.Length > 6 ) if (entryLines.Length > 7 )
{ {
if (IsDoi(entryLines[6])) Doi = entryLines[6]; if (IsDoi(entryLines[7])) Doi = entryLines[7];
else Url = entryLines[6]; else Url = entryLines[7];
} }
return ToString(); return ToString();
@ -128,12 +131,12 @@ namespace Txt2Bib.Records
if (Author.Length != 0) if (Author.Length != 0)
{ {
label = Author[0][..5].Replace(". ", ""); label = Author[0][..2].Replace(". ", "");
authString = $"\tauthor = \"{string.Join(" and ", Author)}\",\n"; authString = $"\tauthor = \"{string.Join(" and ", Author)}\",\n";
} }
else else
{ {
label = Editor[0][..5].Replace(". ", ""); label = Editor[0][..2].Replace(". ", "");
authString = $"\teditor = \"{string.Join(" and ", Editor)}\",\n"; authString = $"\teditor = \"{string.Join(" and ", Editor)}\",\n";
} }
@ -141,6 +144,7 @@ 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" +
@ -198,7 +202,7 @@ namespace Txt2Bib.Records
public override string ToString() 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 authString = $"\tauthor = \"{string.Join(" and ", Author)}\",\n";
string edsString = Editor.Length != 0 ? string edsString = Editor.Length != 0 ?
$"\teditor = \"{string.Join(" and ", Editor)}\",\n" : ""; $"\teditor = \"{string.Join(" and ", Editor)}\",\n" : "";
@ -266,7 +270,7 @@ namespace Txt2Bib.Records
public override string ToString() 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 authString = $"\tauthor = \"{string.Join(" and ", Author)}\",\n";
string edsString = $"\teditor = \"{string.Join(" and ", Editor)}\",\n"; string edsString = $"\teditor = \"{string.Join(" and ", Editor)}\",\n";

View File

@ -43,6 +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);
// Use Windows 1252??
var content = Encoding.Latin1.GetString(contentBytes); var content = Encoding.Latin1.GetString(contentBytes);
IEnumerable<string> entries = new List<string>(); IEnumerable<string> entries = new List<string>();
@ -75,7 +76,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
@ -89,9 +90,9 @@ namespace Txt2Bib
_ => "" _ => ""
}; };
} }
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; 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.1.7</Version> <Version>0.1.8</Version>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>