Guest User

Untitled

a guest
Jan 4th, 2016
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.IO;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Text;
  6. using System.Linq;
  7. using System.Text.RegularExpressions;
  8.  
  9. namespace PandoraBox
  10. {
  11.     public class APARTICLEPlugin : IPluginMacro
  12.     {
  13.         //пример / sample: [APARTICLE]
  14.         Regex _block = new Regex(@"\[AP(FREE){0,1}ARTICLE\]", RegexOptions.Compiled);
  15.         //кодировка / encoding
  16.         Encoding _enc = Encoding.GetEncoding("windows-1251");
  17.         //путь к данным / where data is stored
  18.         String _root = @"c:\ap-data\text";
  19.         //сколько минимум предложений должно быть в статье / how many sentances must be in article minimum
  20.         int _minSentances = 10;
  21.         //сколько максимум предложений должно быть в статье / how many sentances must be in article maximum
  22.         int _maxSentances = 999;
  23.  
  24.         public string Execute(string template, PluginMacroArgs args)
  25.         {
  26.             return _block.Replace(template, delegate(Match match)
  27.             {
  28.                 String key = CleanKey(args.Key);
  29.                 String dir = Path.Combine(_root, key);
  30.                 bool isFreeText = match.Groups[1].Value.Length > 0;
  31.                 if (!Directory.Exists(dir)) //текста по ключу нет, выводим стандартным макросом текста
  32.                     return isFreeText ? "[FREETEXT-3-5/p]" : "[TEXT-3-5/p]";
  33.                 String article = GetArticle(dir);
  34.                 if (article == null) //статей по требованиям нет, выводим стандартным макросом текста
  35.                     return isFreeText ? "[FREETEXT-3-5/p]" : "[TEXT-3-5/p]";
  36.                 if (!isFreeText) article =  "{INSERTKEYS}" + article + "{/INSERTKEYS}";
  37.                 return article;
  38.             });
  39.         }
  40.  
  41.         private string GetArticle(String dir)
  42.         {
  43.             var files = Shuffle(Directory.GetFiles(dir).ToList());
  44.             for (int i = 0; i < files.Count; i++)
  45.             {
  46.                 var sentances = File.ReadAllText(files[i], _enc).Split('|');
  47.                 if (sentances.Length >= _minSentances)
  48.                     return String.Join("<br />", sentances.Take(_maxSentances));
  49.             }
  50.             return null;
  51.         }
  52.  
  53.         private string CleanKey(String key)
  54.         {
  55.             String key1 = key;
  56.             key1 = String.Join("", key1.Split(Path.GetInvalidPathChars()));
  57.             key1 = String.Join("", key1.Split(Path.GetInvalidFileNameChars()));
  58.             return key1;
  59.         }
  60.  
  61.         private static List<T> Shuffle<T>(List<T> items)
  62.         {
  63.             if (items.Count < 2) return items;
  64.  
  65.             for (int i = 0; i < items.Count; i++)
  66.             {
  67.                 int j = Rnd.InnerObject.Next(items.Count);
  68.                 T temp = items[i];
  69.                 items[i] = items[j];
  70.                 items[j] = temp;
  71.             }
  72.             return items;
  73.         }
  74.  
  75.         public ushort Level { get { return 5; } }
  76.         public String Name { get { return "APARTICLEPlugin"; } }
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment