117 lines
		
	
	
		
			3.6 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			117 lines
		
	
	
		
			3.6 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);
 | 
						|
            });
 | 
						|
 | 
						|
            foreach (var entry in entries)
 | 
						|
            {
 | 
						|
                output += entry;
 | 
						|
            }
 | 
						|
 | 
						|
            return output;
 | 
						|
        }
 | 
						|
 | 
						|
        private string Process(string entryFromTxt)
 | 
						|
        {
 | 
						|
            var lines = entryFromTxt.Trim().Replace("\r", string.Empty).Split("\n");
 | 
						|
            var type = lines.First();
 | 
						|
 | 
						|
            System.Diagnostics.Debug.WriteLine($"Entry: {entryFromTxt}; Prima riga: {type}");
 | 
						|
 | 
						|
            string citation = type switch
 | 
						|
            {
 | 
						|
                "J" => ConvertArticle(lines),
 | 
						|
                //"B" => new BookBib(),
 | 
						|
                //"C" => new ChapterBib(),
 | 
						|
                //"P" => new ConferenceBib(),
 | 
						|
                _ => ConvertArticle(lines),
 | 
						|
            };
 | 
						|
 | 
						|
            return citation;
 | 
						|
        }
 | 
						|
 | 
						|
        private static string ConvertArticle(string[] entryLines)
 | 
						|
        {
 | 
						|
            var temp = entryLines[1].Split(',');
 | 
						|
            var rearrange = (string a) =>
 | 
						|
            {
 | 
						|
                a = a.Trim();
 | 
						|
                var s = a.Split(' ');
 | 
						|
                return $"{s[1]} {s[0]}";
 | 
						|
            }; 
 | 
						|
            var authors = temp.Select(a => rearrange(a)).ToArray();
 | 
						|
            var article = new ArticleBib
 | 
						|
            {
 | 
						|
                Author = authors,
 | 
						|
                Year = ushort.Parse(entryLines[2]),
 | 
						|
                Title = entryLines[3],
 | 
						|
                Journal = entryLines[4],
 | 
						|
                // TODO split with ',' for issue
 | 
						|
                Volume = entryLines[5].Split(',').Length == 2 ?
 | 
						|
                   byte.Parse(entryLines[5].Split(',')[0]) :
 | 
						|
                   byte.Parse(entryLines[5]), 
 | 
						|
                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]),
 | 
						|
                Doi = entryLines.Length > 7 ? entryLines[7] : "",
 | 
						|
            };
 | 
						|
 | 
						|
            return article.ToString();
 | 
						|
        }
 | 
						|
 | 
						|
        /// <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);
 | 
						|
 | 
						|
        }
 | 
						|
    }
 | 
						|
}
 |