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