Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Collections;
- using System.Collections.Generic;
- using System.Text;
- using System.Linq;
- using System.Text.RegularExpressions;
- namespace PandoraBox
- {
- public class APARTICLEPlugin : IPluginMacro
- {
- //пример / sample: [APARTICLE]
- Regex _block = new Regex(@"\[AP(FREE){0,1}ARTICLE\]", RegexOptions.Compiled);
- //кодировка / encoding
- Encoding _enc = Encoding.GetEncoding("windows-1251");
- //путь к данным / where data is stored
- String _root = @"c:\ap-data\text";
- //сколько минимум предложений должно быть в статье / how many sentances must be in article minimum
- int _minSentances = 10;
- //сколько максимум предложений должно быть в статье / how many sentances must be in article maximum
- int _maxSentances = 999;
- public string Execute(string template, PluginMacroArgs args)
- {
- return _block.Replace(template, delegate(Match match)
- {
- String key = CleanKey(args.Key);
- String dir = Path.Combine(_root, key);
- bool isFreeText = match.Groups[1].Value.Length > 0;
- if (!Directory.Exists(dir)) //текста по ключу нет, выводим стандартным макросом текста
- return isFreeText ? "[FREETEXT-3-5/p]" : "[TEXT-3-5/p]";
- String article = GetArticle(dir);
- if (article == null) //статей по требованиям нет, выводим стандартным макросом текста
- return isFreeText ? "[FREETEXT-3-5/p]" : "[TEXT-3-5/p]";
- if (!isFreeText) article = "{INSERTKEYS}" + article + "{/INSERTKEYS}";
- return article;
- });
- }
- private string GetArticle(String dir)
- {
- var files = Shuffle(Directory.GetFiles(dir).ToList());
- for (int i = 0; i < files.Count; i++)
- {
- var sentances = File.ReadAllText(files[i], _enc).Split('|');
- if (sentances.Length >= _minSentances)
- return String.Join("<br />", sentances.Take(_maxSentances));
- }
- return null;
- }
- private string CleanKey(String key)
- {
- String key1 = key;
- key1 = String.Join("", key1.Split(Path.GetInvalidPathChars()));
- key1 = String.Join("", key1.Split(Path.GetInvalidFileNameChars()));
- return key1;
- }
- private static List<T> Shuffle<T>(List<T> items)
- {
- if (items.Count < 2) return items;
- for (int i = 0; i < items.Count; i++)
- {
- int j = Rnd.InnerObject.Next(items.Count);
- T temp = items[i];
- items[i] = items[j];
- items[j] = temp;
- }
- return items;
- }
- public ushort Level { get { return 5; } }
- public String Name { get { return "APARTICLEPlugin"; } }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment