txt2bib/Text2Bib.cs
2024-01-08 16:02:29 +01:00

102 lines
3.0 KiB
C#

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Txt2Bib.Records;
using System.Windows;
using System.Runtime.CompilerServices;
using System.CodeDom;
namespace Txt2Bib
{
/// <summary>
/// Converting entries from text file to BibTeX format
/// </summary>
public class Text2Bib
{
private string[] _citTypes = { "J", "B", "C", "P", "?" };
/// <summary>
/// Generate single .bib file from input text files
/// </summary>
/// <param name="filenames"></param>
/// <returns></returns>
public string Generate(string filenames)
{
var paths = filenames.Trim().Split('\n');
string result = "";
string output = "";
foreach (var path in paths)
{
var reader = File.OpenText(path);
byte[] contentBytes = File.ReadAllBytes(path);
result += Encoding.Latin1.GetString(contentBytes);
}
IEnumerable<string> entries = new List<string>();
try
{
entries = result.Split('%').ToList().Select(entry => Process(entry));
}
catch (Exception)
{
throw;
}
foreach (var entry in entries)
{
output += entry;
}
return output;
}
/// <summary>
/// Creates a bib entry from source text
/// </summary>
/// <remarks>Handle exception</remarks>
/// <param name="entryFromTxt"></param>
/// <returns></returns>
/// <exception cref="Exception">Invalid citation type for this entry</exception>
public string Process(string entryFromTxt)
{
var lines = entryFromTxt.Trim().Replace("\r", string.Empty).Split("\n");
var type = lines.First().Trim();
if (!IsValidEntry(type)) throw new Exception($"Invalid entry type '{type}'");
string citation = type switch
{
"J" => (new ArticleBib()).Convert(lines),
//"B" => new BookBib(),
//"C" => new ChapterBib(),
//"P" => new ConferenceBib(),
_ => (new ArticleBib()).Convert(lines)
};
return citation;
}
/// <summary>
/// Validate txt entry based
/// on citation types
/// </summary>
/// <param name="entry"></param>
protected bool IsValidEntry(string entryType)
{
return _citTypes.Contains(entryType);
}
/// <summary>
/// TODO: How to determine correct encoding of text file??
/// </summary>
/// <param name="reader">The stream reader for the file</param>
/// <returns></returns>
private Encoding GuessEncoding(StreamReader reader)
{
var cp = "UTF-8";
return Encoding.GetEncoding(cp);
}
}
}