Пример #1
0
        private void SaveExamplesAsXml(XmlElement element)
        {
            int nrOfExamples = Examples.Count();

            SetXmlAttribute(element, "examples", nrOfExamples.ToString());
            var examples = Examples.ToArray();

            for (int i = 0; i < Examples.Count(); i++)
            {
                SetXmlAttribute(element, "example_" + i, examples[i].ToString());
            }
        }
Пример #2
0
        public List <DiscordEmbedBuilder> GetEmbedsFromSearchResult(DiscordClient discordClient)
        {
            var outEmbed = new DiscordEmbedBuilder();

            outEmbed.SetupWithDefaultValues(discordClient);
            outEmbed.AddSearchTitle(discordClient, "Exempelmeningar");

            int maxExamples = Resources.ConstantData.ExempelMeningar.MaxSentences;

            var filteredExamples = new List <string>();

            if (Examples.Count() != 0)
            {
                float sample = Math.Max(1, Examples.Count() / (float)maxExamples);

                for (int i = 0; i < Math.Min(maxExamples, Examples.Count() - 1); i++)
                {
                    int sampleIndex = (int)Math.Ceiling(i * sample);
                    sampleIndex = (int)MathF.Min(sampleIndex, Examples.Count() - 1);

                    filteredExamples.Add(Examples[sampleIndex]);
                }
            }

            if (filteredExamples.Count() != 0)
            {
                var outExamples = new List <string>();

                // Embed values can have a max of 1024 characters.
                const int lengthLimit = 1024;

                var totalLength = 0;

                for (int i = 0; i < filteredExamples.Count(); i++)
                {
                    var example = filteredExamples[i];

                    // Make search term bold.
                    var regex = new Regex(SearchTerm, RegexOptions.IgnoreCase);
                    var match = regex.Match(example);
                    example = example.Replace(match.Value, match.Value.ToBoldAndItalics());

                    //  Make ordinal.
                    example = $"{ i + 1 }. { example }.";

                    totalLength += example.Length;

                    if (totalLength < lengthLimit)
                    {
                        outExamples.Add(example);
                    }
                }

                outEmbed.AddField("Exempel", string.Join("\n", outExamples));

                outEmbed.AddField(Strings.Source, SourceUrl);
            }
            else
            {
                outEmbed.WithDescription(Strings.NoResultFound);
            }

            var outEmbeds = new List <DiscordEmbedBuilder>();

            outEmbeds.Add(outEmbed);

            return(new List <DiscordEmbedBuilder>(outEmbeds));
        }