Handle mutiple files. use strings in article fields
This commit is contained in:
parent
f56028b6a2
commit
8c043dc18e
27
Records.cs
27
Records.cs
@ -1,7 +1,9 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.CodeDom;
|
||||||
using System.Configuration;
|
using System.Configuration;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
|
using System.Windows.Media.Converters;
|
||||||
using System.Windows.Navigation;
|
using System.Windows.Navigation;
|
||||||
|
|
||||||
namespace Txt2Bib.Records
|
namespace Txt2Bib.Records
|
||||||
@ -15,8 +17,8 @@ namespace Txt2Bib.Records
|
|||||||
public string Title { get; set; } = "";
|
public string Title { get; set; } = "";
|
||||||
public string Journal { get; set; } = "";
|
public string Journal { get; set; } = "";
|
||||||
public ushort Year { get; set; } = 1950;
|
public ushort Year { get; set; } = 1950;
|
||||||
public ushort Volume { get; set; } = 1;
|
public string Volume { get; set; } = "";
|
||||||
public byte? Issue { get; set; } = null;
|
public string? Issue { get; set; } = null;
|
||||||
public ushort FirstPage { get; set; } = 1;
|
public ushort FirstPage { get; set; } = 1;
|
||||||
public ushort LastPage { get; set; } = 1;
|
public ushort LastPage { get; set; } = 1;
|
||||||
public string Doi { get; set; } = "";
|
public string Doi { get; set; } = "";
|
||||||
@ -32,21 +34,24 @@ namespace Txt2Bib.Records
|
|||||||
return $"{s[1]} {s[0]}";
|
return $"{s[1]} {s[0]}";
|
||||||
};
|
};
|
||||||
var checkVol = (string v) =>
|
var checkVol = (string v) =>
|
||||||
{
|
v.Split(',').Length == 2 ? v.Split(',')[0] : v;
|
||||||
if (v.Length == 0) return byte.MinValue;
|
|
||||||
return v.Split(',').Length == 2 ?
|
|
||||||
byte.Parse(v.Split(',')[0]) :
|
|
||||||
byte.Parse(v);
|
|
||||||
};
|
|
||||||
Author = temp.Select(a => rearrange(a)).ToArray();
|
Author = temp.Select(a => rearrange(a)).ToArray();
|
||||||
Year = ushort.Parse(entryLines[2]);
|
Year = ushort.Parse(entryLines[2]);
|
||||||
Title = entryLines[3];
|
Title = entryLines[3];
|
||||||
Journal = entryLines[4];
|
Journal = entryLines[4];
|
||||||
Volume = checkVol(entryLines[5]);
|
Volume = entryLines[5] != string.Empty ? checkVol(entryLines[5]) : Volume;
|
||||||
Issue = entryLines[5].Split(',').Length == 2 ?
|
Issue = entryLines[5].Split(',').Length == 2 ?
|
||||||
byte.Parse(entryLines[5].Split(',')[1]) : null;
|
entryLines[5].Split(',')[1] : Issue;
|
||||||
|
try
|
||||||
|
{
|
||||||
FirstPage = ushort.Parse(entryLines[6].Split('-')[0]);
|
FirstPage = ushort.Parse(entryLines[6].Split('-')[0]);
|
||||||
LastPage = ushort.Parse(entryLines[6].Split('-')[1]);
|
LastPage = ushort.Parse(entryLines[6].Split('-')[1]);
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
throw;
|
||||||
|
}
|
||||||
Doi = entryLines.Length > 7 ? entryLines[7] : "";
|
Doi = entryLines.Length > 7 ? entryLines[7] : "";
|
||||||
|
|
||||||
return ToString();
|
return ToString();
|
||||||
@ -82,7 +87,7 @@ namespace Txt2Bib.Records
|
|||||||
|
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
return $"@{Type}" + "{" +$"{Author[0][..5]}_{Year},\n" +
|
return $"@{Type}{{{Author[0][..5]}_{Year},\n" +
|
||||||
$"\tauthor = \"{string.Join(" and", Author)}\",\n" +
|
$"\tauthor = \"{string.Join(" and", Author)}\",\n" +
|
||||||
$"\ttitle = {{{Title}}},\n" +
|
$"\ttitle = {{{Title}}},\n" +
|
||||||
$"\tpublisher = \"{Publisher}\",\n" +
|
$"\tpublisher = \"{Publisher}\",\n" +
|
||||||
|
39
Text2Bib.cs
39
Text2Bib.cs
@ -16,6 +16,7 @@ namespace Txt2Bib
|
|||||||
public class Text2Bib
|
public class Text2Bib
|
||||||
{
|
{
|
||||||
private string[] _citTypes = { "J", "B", "C", "P", "?" };
|
private string[] _citTypes = { "J", "B", "C", "P", "?" };
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Generate single .bib file from input text files
|
/// Generate single .bib file from input text files
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -24,33 +25,43 @@ namespace Txt2Bib
|
|||||||
public string Generate(string filenames)
|
public string Generate(string filenames)
|
||||||
{
|
{
|
||||||
var paths = filenames.Trim().Split('\n');
|
var paths = filenames.Trim().Split('\n');
|
||||||
string result = "";
|
var output = "";
|
||||||
string output = "";
|
|
||||||
|
|
||||||
foreach (var path in paths)
|
foreach (var path in paths)
|
||||||
|
{
|
||||||
|
output += CreateBibTeX(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Create a BibTeX string from an input file path
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="path"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public string CreateBibTeX(string path)
|
||||||
{
|
{
|
||||||
var reader = File.OpenText(path);
|
var reader = File.OpenText(path);
|
||||||
byte[] contentBytes = File.ReadAllBytes(path);
|
byte[] contentBytes = File.ReadAllBytes(path);
|
||||||
|
var content = Encoding.Latin1.GetString(contentBytes);
|
||||||
result += Encoding.Latin1.GetString(contentBytes);
|
|
||||||
}
|
|
||||||
|
|
||||||
IEnumerable<string> entries = new List<string>();
|
IEnumerable<string> entries = new List<string>();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
entries = result.Split('%').ToList().Select(entry => Process(entry));
|
entries = content.Split('%').ToList().Select(entry => Process(entry));
|
||||||
}
|
}
|
||||||
catch (Exception)
|
catch (Exception)
|
||||||
{
|
{
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var bibtex = "";
|
||||||
foreach (var entry in entries)
|
foreach (var entry in entries)
|
||||||
{
|
{
|
||||||
output += entry;
|
bibtex += entry;
|
||||||
}
|
}
|
||||||
|
|
||||||
return output;
|
return bibtex;
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a bib entry from source text
|
/// Creates a bib entry from source text
|
||||||
@ -66,7 +77,11 @@ namespace Txt2Bib
|
|||||||
|
|
||||||
if (!IsValidEntry(type)) throw new Exception($"Invalid entry type '{type}'");
|
if (!IsValidEntry(type)) throw new Exception($"Invalid entry type '{type}'");
|
||||||
|
|
||||||
string citation = type switch
|
string citation = "";
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
citation = type switch
|
||||||
{
|
{
|
||||||
"J" => (new ArticleBib()).Convert(lines),
|
"J" => (new ArticleBib()).Convert(lines),
|
||||||
//"B" => new BookBib(),
|
//"B" => new BookBib(),
|
||||||
@ -74,6 +89,11 @@ namespace Txt2Bib
|
|||||||
//"P" => new ConferenceBib(),
|
//"P" => new ConferenceBib(),
|
||||||
_ => (new ArticleBib()).Convert(lines)
|
_ => (new ArticleBib()).Convert(lines)
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
throw new Exception("Faulty string in " + lines);
|
||||||
|
}
|
||||||
|
|
||||||
return citation;
|
return citation;
|
||||||
}
|
}
|
||||||
@ -95,7 +115,6 @@ namespace Txt2Bib
|
|||||||
{
|
{
|
||||||
var cp = "UTF-8";
|
var cp = "UTF-8";
|
||||||
return Encoding.GetEncoding(cp);
|
return Encoding.GetEncoding(cp);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user