87 lines
2.3 KiB
C#
87 lines
2.3 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Controls.Primitives;
|
|
using System.Windows;
|
|
using Txt2Bib.Records;
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
namespace Txt2Bib
|
|
{
|
|
internal class Text2Bib
|
|
{
|
|
private readonly Dictionary<string, string> _citTypes = new()
|
|
{
|
|
{ "J" , "article"},
|
|
{ "B", "book" },
|
|
{ "P", "conference" },
|
|
{ "C" , "inbook" }
|
|
};
|
|
/// <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);
|
|
}
|
|
|
|
var entries = result.Split('%').ToList<string>().Select(entry =>
|
|
{
|
|
return Process(entry).ToString();
|
|
});
|
|
|
|
foreach (var entry in entries)
|
|
{
|
|
output += entry;
|
|
}
|
|
|
|
return output;
|
|
}
|
|
|
|
private IBib Process(string entryFromTxt)
|
|
{
|
|
var lines = entryFromTxt.Trim().Split("\n");
|
|
var type = lines.First();
|
|
|
|
System.Diagnostics.Debug.WriteLine($"Entry: {entryFromTxt}; Prima riga: {type}");
|
|
|
|
IBib citType = type switch
|
|
{
|
|
"J" => new ArticleBib(),
|
|
"B" => new BookBib(),
|
|
//"C" => new ChapterBib(),
|
|
//"P" => new ConferenceBib(),
|
|
_ => new ArticleBib(),
|
|
};
|
|
|
|
return citType;
|
|
}
|
|
|
|
/// <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);
|
|
|
|
}
|
|
}
|
|
}
|