Compare commits

..

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

3 changed files with 41 additions and 23 deletions

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");
@ -22,16 +20,7 @@ namespace Txt2Bib.Records
a = a.Trim(); a = a.Trim();
var s = a.Split(' '); var s = a.Split(' ');
return s.Length > 1 ? return s.Length > 1 ?
$"{s[1]} {s[0][0]}{s[0][1..].ToLower()}" : a; $"{s[1]} {s[0]}" : 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 +33,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 +50,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 new Exception("Formato pagine non corretto");
}
if (entryLines.Length > 7 ) if (entryLines.Length > 7 )
{ {
@ -79,7 +78,7 @@ namespace Txt2Bib.Records
$"\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";
@ -165,6 +164,8 @@ namespace Txt2Bib.Records
public string Publisher { get; set; } = ""; public string Publisher { get; set; } = "";
public string Series { 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; } = "";
@ -180,7 +181,15 @@ namespace Txt2Bib.Records
Series = entryLines[6] != String.Empty ? entryLines[6] : Series; Series = entryLines[6] != String.Empty ? entryLines[6] : Series;
Address = entryLines[7] != String.Empty ? entryLines[7] : Address; Address = entryLines[7] != String.Empty ? entryLines[7] : Address;
Publisher = entryLines[8]; Publisher = entryLines[8];
Pages = CreatePages(entryLines[9]); 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 (entryLines.Length > 10)
{ {
@ -207,7 +216,7 @@ namespace Txt2Bib.Records
$"\tseries = {{{Series}}},\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";
@ -225,6 +234,8 @@ namespace Txt2Bib.Records
public string Publisher { get; set; } = ""; public string Publisher { get; set; } = "";
public string Series { 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; } = "";
@ -238,7 +249,15 @@ namespace Txt2Bib.Records
Series = entryLines[6] != String.Empty ? entryLines[6] : Series; Series = entryLines[6] != String.Empty ? entryLines[6] : Series;
Address = entryLines[7] != String.Empty ? entryLines[7] : Address; Address = entryLines[7] != String.Empty ? entryLines[7] : Address;
Publisher = entryLines[8]; Publisher = entryLines[8];
Pages = CreatePages(entryLines[9]); 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 (entryLines.Length > 10)
{ {
@ -264,7 +283,7 @@ namespace Txt2Bib.Records
$"\tseries = {{{Series}}},\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,8 @@ 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! // Use Windows 1252??
var content = Encoding.UTF8.GetString(contentBytes); var content = Encoding.Latin1.GetString(contentBytes);
IEnumerable<string> entries = new List<string>(); IEnumerable<string> entries = new List<string>();
try try
@ -85,7 +85,6 @@ 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),
_ => "" _ => ""

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.8</Version>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>