1
0
Fork 0
mirror of https://github.com/Nioux/AideDeJeu.git synced 2025-10-30 15:06:06 +00:00
This commit is contained in:
Yan Maniez 2018-08-29 23:50:23 +02:00
parent 9be2dac7d6
commit 4e68d6e962
9 changed files with 398 additions and 13 deletions

View file

@ -16,6 +16,7 @@ namespace AideDeJeu
DependencyService.Register<MainViewModel>();
DependencyService.Register<BookmarksViewModel>();
DependencyService.Register<StoreViewModel>();
var vm = DependencyService.Get<MainViewModel>();
var tabbeddPage = new AideDeJeu.Views.MainTabbedPage();
//var mainPage = new ItemDetailPage(new ItemDetailViewModel(new HomeItem()) { Title = "Haches & Dés" });

View file

@ -16,6 +16,7 @@ namespace AideDeJeu.Tools
{
public static class MarkdownExtensions
{
/*
public static Item ToItem(string md)
{
var pipeline = new MarkdownPipelineBuilder().UsePipeTables().Build();
@ -216,7 +217,7 @@ namespace AideDeJeu.Tools
}
return null;
}
*/
/*
public static Item ToItem(string md)

View file

@ -23,6 +23,13 @@ namespace AideDeJeu.ViewModels
return DependencyService.Get<BookmarksViewModel>();
}
}
public StoreViewModel Store
{
get
{
return DependencyService.Get<StoreViewModel>();
}
}
bool isBusy = false;
public bool IsBusy

View file

@ -23,13 +23,13 @@ namespace AideDeJeu.ViewModels
public async Task ExecuteSearchCommandAsync(string searchText)
{
Main.IsLoading = true;
await Task.Run(async () => await Main.PreloadAllItemsAsync());
Items = await Task.Run(async () => await Main.DeepSearchAllItemsAsync(searchText));
await Task.Run(async () => await Store.PreloadAllItemsAsync());
Items = await Task.Run(async () => await DeepSearchAllItemsAsync(searchText));
Main.IsLoading = false;
}
public IEnumerable<MainViewModel.SearchedItem> _Items = null;
public IEnumerable<MainViewModel.SearchedItem> Items
public IEnumerable<SearchedItem> _Items = null;
public IEnumerable<SearchedItem> Items
{
get
{
@ -40,5 +40,47 @@ namespace AideDeJeu.ViewModels
SetProperty(ref _Items, value);
}
}
public class SearchedItem
{
public string Preview { get; set; }
public Item Item { get; set; }
}
public async Task<IEnumerable<SearchedItem>> DeepSearchAllItemsAsync(string searchText)
{
List<SearchedItem> primaryItems = new List<SearchedItem>();
List<SearchedItem> secondaryItems = new List<SearchedItem>();
var cleanSearchText = Tools.Helpers.RemoveDiacritics(searchText).ToLower();
foreach (var allItem in Store._AllItems)
{
foreach (var item in allItem.Value.Anchors)
{
var name = item.Value.Name;
var cleanName = Tools.Helpers.RemoveDiacritics(name).ToLower();
if (cleanName.Contains(cleanSearchText))
{
primaryItems.Add(new SearchedItem() { Item = item.Value, Preview = name });
}
else
{
var markdown = item.Value.Markdown;
var cleanMarkdown = Tools.Helpers.RemoveDiacritics(markdown).ToLower();
if (cleanMarkdown.Contains(cleanSearchText))
{
int position = cleanMarkdown.IndexOf(cleanSearchText);
int startPosition = Math.Max(0, position - 30);
int endPosition = Math.Min(markdown.Length, position + searchText.Length + 30);
var preview = markdown.Substring(startPosition, endPosition - startPosition - 1);
secondaryItems.Add(new SearchedItem() { Item = item.Value, Preview = preview });
}
}
}
}
primaryItems.AddRange(secondaryItems);
return primaryItems;
}
}
}

View file

@ -18,7 +18,7 @@ namespace AideDeJeu.ViewModels
get => _isLoading;
set => SetProperty(ref _isLoading, value);
}
/*
void AddAnchor(string source, Dictionary<string, Item> anchors, Item item)
{
if (item != null && item.Name != null)
@ -164,7 +164,7 @@ namespace AideDeJeu.ViewModels
}
return itemWithAnchors.Item;
}
*/
public Command LoadItemsCommand { get; private set; }
private Navigator _Navigator = null;

View file

@ -147,7 +147,7 @@ namespace AideDeJeu.ViewModels
var with = match.Groups["with"].Value;
Main.IsBusy = true;
Main.IsLoading = true;
var item = await Task.Run(async () => await Main.GetItemFromDataAsync(file, anchor));
var item = await Task.Run(async () => await Store.GetItemFromDataAsync(file, anchor));
Main.IsBusy = false;
Main.IsLoading = false;
if (item != null)

View file

@ -0,0 +1,333 @@
using AideDeJeu.Tools;
using AideDeJeuLib;
using Markdig;
using Markdig.Parsers;
using Markdig.Syntax;
using Markdig.Syntax.Inlines;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace AideDeJeu.ViewModels
{
public class StoreViewModel : BaseViewModel
{
public static Item ToItem(string md)
{
var pipeline = new MarkdownPipelineBuilder().UsePipeTables().Build();
var document = MarkdownParser.Parse(md, pipeline);
var enumerator = document.GetEnumerator();
try
{
enumerator.MoveNext();
while (enumerator.Current != null)
{
var block = enumerator.Current;
if (block is HtmlBlock)
{
if (IsNewItem(block))
{
var item = ParseItem(ref enumerator);
return item;
}
}
enumerator.MoveNext();
}
}
finally
{
enumerator.Dispose();
}
return null;
}
public static Item ParseItem(ref ContainerBlock.Enumerator enumerator)
{
var currentItem = GetNewItem(enumerator.Current);
if (currentItem != null)
{
enumerator.MoveNext();
while (enumerator.Current != null)
{
var block = enumerator.Current;
if (block is HtmlBlock)
{
if (IsClosingItem(block))
{
return currentItem;
}
else if (IsNewItem(block))
{
var subItem = ParseItem(ref enumerator);
var propertyName = subItem.GetType().Name;
if (currentItem.GetType().GetProperty(propertyName) != null)
{
PropertyInfo prop = currentItem.GetType().GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance);
if (null != prop && prop.CanWrite)
{
prop.SetValue(currentItem, subItem, null);
}
}
else if (currentItem is Items)
{
var items = currentItem as Items;
items.Add(subItem);
}
}
}
else // if (block is ContainerBlock)
{
ParseItemProperties(currentItem, block);
}
currentItem.Markdown += enumerator.Current.ToMarkdownString();
enumerator.MoveNext();
}
}
return currentItem;
}
public static void ParseItemProperties(Item item, Block block)
{
switch (block)
{
case Markdig.Extensions.Tables.Table table:
ParseItemProperties(item, table);
break;
case ContainerBlock blocks:
ParseItemProperties(item, blocks);
break;
case LeafBlock leaf:
ParseItemProperties(item, leaf.Inline);
break;
}
}
public static void ParseItemProperties(Item item, ContainerBlock blocks)
{
foreach (var block in blocks)
{
ParseItemProperties(item, block);
}
}
public static void ParseItemProperties(Item item, ContainerInline inlines)
{
if (inlines == null)
{
return;
}
PropertyInfo prop = null;
foreach (var inline in inlines)
{
if (inline is HtmlInline)
{
var tag = (inline as HtmlInline).Tag;
if (tag == "<!--br-->" || tag == "<br>")
{
}
else if (tag.StartsWith("<!--/"))
{
prop = null;
}
else if (tag.StartsWith("<!--") && !tag.StartsWith("<!--/"))
{
var propertyName = tag.Substring(4, tag.Length - 7);
prop = item.GetType().GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance);
}
}
else
{
if (null != prop && prop.CanWrite)
{
prop.SetValue(item, inline.ToMarkdownString(), null);
}
}
}
}
public static bool IsNewItem(Block block)
{
var htmlBlock = block as HtmlBlock;
if (htmlBlock.Type == HtmlBlockType.Comment)
{
var tag = htmlBlock.Lines.Lines.FirstOrDefault().Slice.ToString();
if (!string.IsNullOrEmpty(tag) && tag != "<!--br-->" && tag != "<br>")
{
if (tag.StartsWith("<!--") && !tag.StartsWith("<!--/"))
{
return true;
}
}
}
return false;
}
public static bool IsClosingItem(Block block)
{
var htmlBlock = block as HtmlBlock;
if (htmlBlock.Type == HtmlBlockType.Comment)
{
var tag = htmlBlock.Lines.Lines.FirstOrDefault().Slice.ToString();
if (!string.IsNullOrEmpty(tag) && tag != "<!--br-->" && tag != "<br>")
{
if (tag.StartsWith("<!--/"))
{
return true;
}
}
}
return false;
}
public static Item GetNewItem(Block block)
{
var htmlBlock = block as HtmlBlock;
if (htmlBlock.Type == HtmlBlockType.Comment)
{
var tag = htmlBlock.Lines.Lines.FirstOrDefault().Slice.ToString();
if (!string.IsNullOrEmpty(tag) && tag != "<!--br-->" && tag != "<br>")
{
if (tag.StartsWith("<!--") && !tag.StartsWith("<!--/"))
{
var name = $"AideDeJeuLib.{tag.Substring(4, tag.Length - 7)}, AideDeJeu";
var type = Type.GetType(name);
var instance = Activator.CreateInstance(type) as Item;
return instance;
}
}
}
return null;
}
void AddAnchor(string source, Dictionary<string, Item> anchors, Item item)
{
if (item != null && item.Name != null)
{
var basename = Helpers.IdFromName(item.Name);
//var name = $"{source}.md#{basename}";
var name = $"{basename}";
int index = 0;
while (true)
{
if (!anchors.ContainsKey(name))
{
item.Id = name;
anchors.Add(name, item);
return;
}
index++;
//name = $"{source}.md#{basename}{index}";
name = $"{basename}{index}";
}
}
}
void MakeAnchors(string source, Dictionary<string, Item> anchors, Item baseItem)
{
AddAnchor(source, anchors, baseItem);
if (baseItem is Items)
{
foreach (var item in (baseItem as Items))
{
MakeAnchors(source, anchors, item);
}
}
}
public class ItemWithAnchors
{
public Item Item { get; set; }
public Dictionary<string, Item> Anchors { get; set; } = new Dictionary<string, Item>();
}
public Dictionary<string, ItemWithAnchors> _AllItems = new Dictionary<string, ItemWithAnchors>();
public async Task PreloadAllItemsAsync()
{
foreach (var resourceName in Tools.Helpers.GetResourceNames())
{
var regex = new Regex(@"AideDeJeu\.Data\.(?<name>.*?)\.md");
var match = regex.Match(resourceName);
var source = match.Groups["name"].Value;
if (!string.IsNullOrEmpty(source))
{
if (!_AllItems.ContainsKey(source))
{
var md = await Tools.Helpers.GetResourceStringAsync(resourceName);
if (md != null)
{
var item = ToItem(md);
if (item != null)
{
var anchors = new Dictionary<string, Item>();
MakeAnchors(source, anchors, item);
_AllItems[source] = new ItemWithAnchors() { Item = item, Anchors = anchors };
}
}
}
}
}
}
public async Task<Item> GetItemFromDataAsync(string source, string anchor)
{
//await Task.Delay(3000);
if (!_AllItems.ContainsKey(source))
{
//var md = await Tools.Helpers.GetStringFromUrl($"https://raw.githubusercontent.com/Nioux/AideDeJeu/master/Data/{source}.md");
var md = await Tools.Helpers.GetResourceStringAsync($"AideDeJeu.Data.{source}.md");
if (md != null)
{
var item = ToItem(md);
if (item != null)
{
var anchors = new Dictionary<string, Item>();
MakeAnchors(source, anchors, item);
_AllItems[source] = new ItemWithAnchors() { Item = item, Anchors = anchors };
}
else
{
return null;
}
}
else
{
return null;
}
}
var itemWithAnchors = _AllItems[source];
if (!string.IsNullOrEmpty(anchor))
{
if (itemWithAnchors.Anchors.ContainsKey(anchor))
{
return itemWithAnchors.Anchors[anchor];
}
}
return itemWithAnchors.Item;
}
}
}

View file

@ -7,6 +7,7 @@ using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using static AideDeJeu.ViewModels.DeepSearchViewModel;
namespace AideDeJeu.Views
{
@ -30,7 +31,7 @@ namespace AideDeJeu.Views
private async void ItemsListView_ItemTapped(object sender, ItemTappedEventArgs e)
{
var searchedItem = e.Item as MainViewModel.SearchedItem;
var searchedItem = e.Item as SearchedItem;
await Main.Navigator.GotoItemDetailPageAsync(searchedItem.Item);
}
}

View file

@ -1,4 +1,5 @@
using AideDeJeu.Tools;
using AideDeJeu.ViewModels;
using AideDeJeuLib;
using Markdig;
using System;
@ -45,8 +46,7 @@ namespace AideDeJeuCmd
.Build();
//var document = Markdig.Parsers.MarkdownParser.Parse(md, pipeline);
//DumpMarkdownDocument(document);
var monsters = AideDeJeu.Tools.MarkdownExtensions.ToItem(md) as IEnumerable<Monster>; // document.ToMonsters<MonsterHD>();
var monsters = AideDeJeu.ViewModels.StoreViewModel.ToItem(md) as IEnumerable<Monster>; // document.ToMonsters<MonsterHD>();
//document.Dump();
Console.WriteLine("ok");
//var md2 = monsters.ToMarkdownString();
@ -64,7 +64,7 @@ namespace AideDeJeuCmd
var result = string.Empty;
var md = await LoadStringAsync(dataDir + "spells_hd.md");
var items = AideDeJeu.Tools.MarkdownExtensions.ToItem(md) as IEnumerable<Spell>;
var items = StoreViewModel.ToItem(md) as IEnumerable<Spell>;
var classes = new string[]
{
@ -130,7 +130,7 @@ namespace AideDeJeuCmd
//if (name.Contains("_hd."))
//{
var md = await Helpers.GetResourceStringAsync(name);
var item = AideDeJeu.Tools.MarkdownExtensions.ToItem(md);
var item = StoreViewModel.ToItem(md);
allitems.Add(name, item);
//}
}