Process articles

This commit is contained in:
Nicolò P 2023-12-25 14:17:14 +01:00
parent ba6c6ac589
commit 7735d55313
6 changed files with 80 additions and 26 deletions

View File

@ -21,6 +21,11 @@
</Button.RenderTransform> </Button.RenderTransform>
</Button> </Button>
<Label Content="Debug" HorizontalAlignment="Left" Margin="44,544,0,0" VerticalAlignment="Top" Foreground="White"/> <Label Content="Debug" HorizontalAlignment="Left" Margin="44,544,0,0" VerticalAlignment="Top" Foreground="White"/>
<Button x:Name="CopyDebug" Content="Button" HorizontalAlignment="Left" Margin="111,547,0,0" VerticalAlignment="Top" Foreground="{x:Null}" BorderBrush="{x:Null}" Width="23" Background="White" Click="CopyDebug_Click">
<Button.OpacityMask>
<ImageBrush ImageSource="/copy-icon.png"/>
</Button.OpacityMask>
</Button>
</Grid> </Grid>
</Window> </Window>

View File

@ -77,5 +77,11 @@ namespace Txt2Bib
var txt2bib = new Text2Bib(); var txt2bib = new Text2Bib();
Debug.Text = txt2bib.Generate(DropArea.Text); Debug.Text = txt2bib.Generate(DropArea.Text);
} }
private void CopyDebug_Click(object sender, RoutedEventArgs e)
{
Debug.SelectAll();
Debug.Copy();
}
} }
} }

View File

@ -1,26 +1,30 @@
using System; using System;
using System.Configuration;
using System.Linq;
using System.Text.RegularExpressions;
namespace Txt2Bib.Records namespace Txt2Bib.Records
{ {
public interface IBib { } public interface IBib { public string ToString(); }
public record class ArticleBib : IBib public record class ArticleBib : IBib
{ {
public string Type { get; init; } = "article"; public string Type { get; init; } = "article";
public string[] Author { get; init; } = { "Gianni e Pinotto" }; public string[] Author { get; set; } = { "Gianni e Pinotto" };
public string Title { get; init; } = ""; public string Title { get; set; } = "";
public string Journal { get; init; } = ""; public string Journal { get; set; } = "";
public ushort Year { get; init; } = 1950; public ushort Year { get; set; } = 1950;
public ushort Volume { get; init; } = 1; public ushort Volume { get; set; } = 1;
public byte Issue { get; init; } = 1; public byte? Issue { get; set; } = null;
public ushort FirstPage { get; init; } = 1; public ushort FirstPage { get; set; } = 1;
public ushort LastPage { get; init; } = 1; public ushort LastPage { get; set; } = 1;
public string Doi { get; init; } = ""; public string Doi { get; set; } = "";
public string Url { get; init; } = ""; public string Url { get; set; } = "";
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" +
$"\ttitle = \"{Title}\",\n" + $"\ttitle = \"{Title}\",\n" +
$"\tjournal = \"{Journal}\",\n" + $"\tjournal = \"{Journal}\",\n" +
$"\tyear = \"{Year}\",\n" + $"\tyear = \"{Year}\",\n" +
@ -36,16 +40,17 @@ namespace Txt2Bib.Records
public record class BookBib : IBib public record class BookBib : IBib
{ {
public string Type { get; init; } = "book"; public string Type { get; init; } = "book";
public string[] Author { get; init; } = { "Gianni e Pinotto" }; public string[] Author { get; set; } = { "Gianni e Pinotto" };
public string Title { get; init; } = ""; public string Title { get; set; } = "";
public string Publisher { get; init; } = ""; public string Publisher { get; set; } = "";
public string Place { get; init; } = ""; public string Place { get; set; } = "";
public string Url { get; init; } = ""; public string Url { get; set; } = "";
public ushort Year { get; init; } = 1950; public ushort Year { get; set; } = 1950;
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" +
$"\ttitle = \"{Title}\",\n" + $"\ttitle = \"{Title}\",\n" +
$"\tpublisher = \"{Publisher}\",\n" + $"\tpublisher = \"{Publisher}\",\n" +
$"\taddress = \"{Place}\",\n" + $"\taddress = \"{Place}\",\n" +

View File

@ -15,7 +15,7 @@ namespace Txt2Bib
{ {
private readonly Dictionary<string, string> _citTypes = new() private readonly Dictionary<string, string> _citTypes = new()
{ {
{ "J" , "article"}, { "J" , "article" },
{ "B", "book" }, { "B", "book" },
{ "P", "conference" }, { "P", "conference" },
{ "C" , "inbook" } { "C" , "inbook" }
@ -41,7 +41,7 @@ namespace Txt2Bib
var entries = result.Split('%').ToList<string>().Select(entry => var entries = result.Split('%').ToList<string>().Select(entry =>
{ {
return Process(entry).ToString(); return Process(entry);
}); });
foreach (var entry in entries) foreach (var entry in entries)
@ -52,23 +52,53 @@ namespace Txt2Bib
return output; return output;
} }
private IBib Process(string entryFromTxt) private string Process(string entryFromTxt)
{ {
var lines = entryFromTxt.Trim().Split("\n"); var lines = entryFromTxt.Trim().Replace("\r", string.Empty).Split("\n");
var type = lines.First(); var type = lines.First();
System.Diagnostics.Debug.WriteLine($"Entry: {entryFromTxt}; Prima riga: {type}"); System.Diagnostics.Debug.WriteLine($"Entry: {entryFromTxt}; Prima riga: {type}");
IBib citType = type switch string citation = type switch
{ {
"J" => new ArticleBib(), "J" => ConvertArticle(lines),
"B" => new BookBib(), //"B" => new BookBib(),
//"C" => new ChapterBib(), //"C" => new ChapterBib(),
//"P" => new ConferenceBib(), //"P" => new ConferenceBib(),
_ => new ArticleBib(), _ => ConvertArticle(lines),
}; };
return citType; 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> /// <summary>

View File

@ -7,4 +7,12 @@
<UseWPF>true</UseWPF> <UseWPF>true</UseWPF>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<None Remove="copy-icon.png" />
</ItemGroup>
<ItemGroup>
<Resource Include="copy-icon.png" />
</ItemGroup>
</Project> </Project>

BIN
copy-icon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.0 KiB