Example #1
0
        private static void InheritAuthors(SIDocument doc2, Round round, Theme theme)
        {
            var authors = theme.Info.Authors;

            if (authors.Count == 0)
            {
                authors = round.Info.Authors;
                if (authors.Count == 0)
                {
                    authors = doc2.Package.Info.Authors;
                }

                if (authors.Count > 0)
                {
                    var realAuthors = doc2.GetRealAuthors(authors);
                    theme.Info.Authors.Clear();

                    foreach (var item in realAuthors)
                    {
                        theme.Info.Authors.Add(item);
                    }
                }
            }
            else
            {
                for (int i = 0; i < authors.Count; i++)
                {
                    var link = doc2.GetLink(theme.Info.Authors, i, out string tail);
                    if (link != null)
                    {
                        theme.Info.Authors[i] = link + tail;
                    }
                }
            }
        }
        private void AppendInfo(SIDocument doc, Paragraph paragraph, InfoOwner owner)
        {
            var count = owner.Info.Authors.Count;

            if (count > 0)
            {
                paragraph.AppendLine().Append(string.Format("{0}{1}: ", Resources.BaseAuthors, count > 1 ? "ы" : ""));
                paragraph.Append(string.Join(", ", doc.GetRealAuthors(owner.Info.Authors)).EndWithPoint());
            }

            count = owner.Info.Sources.Count;
            if (count > 0)
            {
                paragraph.AppendLine().Append(string.Format("{0}{1}: ", Resources.BaseSources, count > 1 ? "и" : ""));
                paragraph.Append(string.Join(", ", doc.GetRealSources(owner.Info.Sources)).EndWithPoint());
            }

            if (owner.Info.Comments.Text.Length > 0)
            {
                paragraph.AppendLine().Append(string.Format("{0}: ", Resources.Comments));
                paragraph.Append(owner.Info.Comments.Text);
            }
        }
        /// <summary>
        /// Сформировать документ требуемого формата
        /// </summary>
        public override IFlowDocumentWrapper BuildDocument(SIDocument doc, ExportFormats format)
        {
            var document = new FlowDocument {
                ColumnWidth = double.PositiveInfinity
            };

            switch (format)
            {
            case ExportFormats.Dinabank:
            {
                var paragraph = new Paragraph();

                paragraph.AppendText(doc.Package.Name);
                AppendInfo(doc, paragraph, doc.Package);
                paragraph.AppendLine();

                doc.Package.Rounds.ForEach(round =>
                    {
                        paragraph.AppendLine();
                        paragraph.AppendLine(round.Name);

                        AppendInfo(doc, paragraph, round);

                        for (int i = 0; i < round.Themes.Count; i++)
                        {
                            var theme = round.Themes[i];
                            paragraph.AppendLine();
                            paragraph.AppendFormat("Тема {0}. {1}", i + 1, theme.Name?.ToUpper().EndWithPoint());
                            AppendInfo(doc, paragraph, theme);
                            paragraph.AppendLine();
                            paragraph.AppendLine();

                            theme.Questions.ForEach(quest =>
                            {
                                paragraph.AppendFormat("{0}. ", quest.Price);
                                paragraph.AppendLine(quest.Scenario.ToString().EndWithPoint());
                                paragraph.AppendFormat(STR_Definition, Resources.Answer, string.Join(", ", quest.Right.ToArray()).GrowFirstLetter().EndWithPoint());
                                AppendInfo(doc, paragraph, quest);
                                paragraph.AppendLine();
                            });
                        }
                    });

                document.Blocks.Add(paragraph);
            }
            break;

            case ExportFormats.TvSI:
            {
                var paragraph = new Paragraph();

                doc.Package.Rounds.ForEach(round => round.Themes.ForEach(theme => theme.Questions.ForEach(quest =>
                    {
                        paragraph.AppendFormat("\\{0}\\", theme.Name).AppendLine();
                        paragraph.AppendFormat("\\{0}", quest.Scenario);
                        if (quest.Info.Comments.Text.Length > 0)
                        {
                            paragraph.AppendFormat(". {0}: {1}", Resources.Comments, quest.Info.Comments.Text);
                        }

                        paragraph.Append('\\').AppendLine().Append('\\');
                        paragraph.Append(string.Join(", ", quest.Right.ToArray()));
                        if (quest.Info.Sources.Count > 0)
                        {
                            paragraph.Append('\\').AppendLine().Append('\\');
                            paragraph.Append(string.Join(", ", doc.GetRealSources(quest.Info.Sources)));
                        }

                        paragraph.Append('\\').AppendLine().AppendLine();
                    })));

                document.Blocks.Add(paragraph);
            }
            break;

            case ExportFormats.Sns:
            {
                var paragraph = new Paragraph();

                paragraph.AppendLine(doc.Package.Name);
                int i = 0;
                doc.Package.Rounds.ForEach(round =>
                    {
                        paragraph.AppendLine();
                        paragraph.AppendLine(round.Name);
                        paragraph.AppendLine();
                        paragraph.AppendLine(Resources.YourThemes);
                        round.Themes.ForEach(theme =>
                        {
                            paragraph.AppendFormat(STR_ExtendedDefinition, Resources.Theme, theme.Name.ToUpper(), ++i);
                            paragraph.AppendLine();
                        });
                        round.Themes.ForEach(theme =>
                        {
                            paragraph.AppendLine();
                            paragraph.AppendFormat(STR_Definition, Resources.Theme, theme.Name.ToUpper());
                            paragraph.AppendLine();
                            if (theme.Info.Comments.Text.Length > 0)
                            {
                                paragraph.AppendFormat(STR_ExtendedDefinition, Resources.Author, string.Join(", ", doc.GetRealAuthors(theme.Info.Authors)), theme.Info.Comments.Text);
                            }
                            else
                            {
                                paragraph.AppendFormat(STR_Definition, Resources.Author, string.Join(", ", doc.GetRealAuthors(theme.Info.Authors)));
                            }
                            paragraph.AppendLine();
                            theme.Questions.ForEach(quest =>
                            {
                                paragraph.AppendLine();
                                paragraph.Append(quest.Price.ToString());
                                paragraph.AppendLine(".");
                                paragraph.AppendLine(quest.Scenario.ToString().Replace(Environment.NewLine, "//").EndWithPoint());
                                paragraph.AppendLine();
                                paragraph.AppendFormat(STR_Definition, Resources.Answer, string.Join(", ", quest.Right.ToArray()).GrowFirstLetter().EndWithPoint());
                                paragraph.AppendLine();
                                paragraph.AppendFormat(STR_Definition, Resources.Comment, (quest.Info.Comments.Text.Length > 0 ? quest.Info.Comments.Text.GrowFirstLetter() : Resources.No.ToLower()).EndWithPoint());
                                paragraph.AppendLine();
                                paragraph.AppendFormat(STR_Definition, Resources.Source, (quest.Info.Sources.Count > 0 ? string.Join(", ", quest.Info.Sources.ToArray()).GrowFirstLetter() : Resources.No.ToLower()).EndWithPoint());
                                paragraph.AppendLine();
                            });
                        });
                    });

                document.Blocks.Add(paragraph);
            }
            break;

            case ExportFormats.Db:
            {
                var text = new StringBuilder();
                text.AppendLine(string.Format("{0}:", Resources.Championship));
                text.AppendLine(doc.Package.Name.EndWithPoint().GrowFirstLetter().Trim());
                text.AppendLine();
                var info         = new StringBuilder();
                int authorsCount = doc.Package.Info.Authors.Count;
                if (authorsCount > 0 && !(authorsCount == 1 && doc.Package.Info.Authors[0] == Resources.Empty))
                {
                    info.AppendLine(string.Join(Environment.NewLine, doc.GetRealAuthors(doc.Package.Info.Authors)).Trim());
                    info.AppendLine();
                }

                if (doc.Package.Info.Sources.Count > 0)
                {
                    info.AppendLine(string.Join(Environment.NewLine, doc.GetRealSources(doc.Package.Info.Sources)).Trim());
                    info.AppendLine();
                }

                if (doc.Package.Info.Comments.Text.Length > 0)
                {
                    info.AppendLine(doc.Package.Info.Comments.Text.GrowFirstLetter().EndWithPoint().Trim());
                    info.AppendLine();
                }

                if (info.Length > 0)
                {
                    text.AppendLine("Инфо:");
                    text.Append(info);
                }

                int r = 1;
                foreach (var round in doc.Package.Rounds)
                {
                    text.AppendLine(string.Format("{0}:", Resources.Tour));
                    text.Append(round.Name.GrowFirstLetter().Trim());
                    if (round.Type == RoundTypes.Final)
                    {
                        text.Append(string.Format(" ({0})", Resources.Final));
                    }

                    text.AppendLine();
                    text.AppendLine();

                    if (round.Info.Authors.Count > 0)
                    {
                        text.AppendLine(string.Format("{0}:", Resources.BaseAuthors));
                        text.AppendLine(string.Join(Environment.NewLine, doc.GetRealAuthors(round.Info.Authors)).Trim());
                        text.AppendLine();
                    }

                    if (round.Info.Sources.Count > 0)
                    {
                        text.AppendLine(string.Format("{0}:", Resources.BaseSources));
                        text.AppendLine(string.Join(Environment.NewLine, doc.GetRealSources(round.Info.Sources)).Trim());
                        text.AppendLine();
                    }

                    if (round.Info.Comments.Text.Length > 0)
                    {
                        text.AppendLine(string.Format("{0}:", Resources.Comments));
                        text.AppendLine(round.Info.Comments.Text.GrowFirstLetter().EndWithPoint().Trim());
                        text.AppendLine();
                    }

                    int i = 1;
                    foreach (var theme in round.Themes)
                    {
                        text.AppendLine(string.Format("{0} {1}:", Resources.Question, i));
                        text.AppendLine(theme.Name.EndWithPoint().GrowFirstLetter().Trim());

                        if (theme.Info.Comments.Text.Length > 0)
                        {
                            text.Append("   (");
                            text.Append(theme.Info.Comments.Text.GrowFirstLetter().Trim());
                            text.AppendLine(")");
                        }

                        int L = theme.Questions.Count;

                        for (int j = 0; j < L; j++)
                        {
                            text.Append("   ");
                            if (j < 5)
                            {
                                text.Append((j + 1).ToString());
                            }
                            else
                            {
                                text.Append("Резерв");
                            }
                            text.AppendLine(string.Format(". {0}", theme.Questions[j].Scenario.ToString().EndWithPoint().GrowFirstLetter().Trim()));
                        }

                        text.AppendLine();
                        text.AppendLine(string.Format("{0}:", Resources.Answer));

                        for (int j = 0; j < L; j++)
                        {
                            var qLine = new StringBuilder("   ");
                            if (j < 5)
                            {
                                qLine.Append((j + 1).ToString());
                            }
                            else
                            {
                                qLine.Append("Резерв");
                            }

                            qLine.Append(string.Format(". {0}", theme.Questions[j].Right[0].ClearPoints().GrowFirstLetter().Trim()));
                            int A = theme.Questions[j].Right.Count;
                            if (A > 1)
                            {
                                qLine.Append(string.Format(" {0}: ", Resources.Accept));
                                for (int k = 1; k < A; k++)
                                {
                                    qLine.Append(theme.Questions[j].Right[k].ClearPoints().GrowFirstLetter());
                                    if (k < A - 1)
                                    {
                                        qLine.Append(", ");
                                    }
                                }
                            }

                            if (theme.Questions[j].Info.Comments.Text.Length > 0)
                            {
                                qLine.Append(string.Format(" ({0})", theme.Questions[j].Info.Comments.Text.ClearPoints().GrowFirstLetter().Trim()));
                            }
                            text.AppendLine(qLine.ToString().EndWithPoint());
                        }

                        bool qHasSource(Question quest) => quest.Info.Sources.Count > 0 && quest.Info.Sources[0].Length > 3;

                        if (theme.Questions.Any(qHasSource))
                        {
                            text.AppendLine();
                            text.AppendLine(string.Format("{0}:", Resources.BaseSources));

                            for (int j = 0; j < L; j++)
                            {
                                if (qHasSource(theme.Questions[j]))
                                {
                                    if (j < 5)
                                    {
                                        text.Append(string.Format("   {0}. ", j + 1));
                                    }
                                    else
                                    {
                                        text.Append("   Резерв.");
                                    }
                                    text.AppendLine(string.Join(", ", doc.GetRealSources(theme.Questions[j].Info.Sources)).EndWithPoint().Trim());
                                }
                            }
                        }

                        text.AppendLine();

                        var authors = new List <string>(doc.GetRealAuthors(theme.Info.Authors));

                        foreach (var quest in theme.Questions)
                        {
                            authors.AddRange(doc.GetRealAuthors(quest.Info.Authors));
                        }

                        if (authors.Count == 0)
                        {
                            authors.AddRange(doc.GetRealAuthors(round.Info.Authors));
                        }

                        if (authors.Count == 0)
                        {
                            authors.AddRange(doc.GetRealAuthors(doc.Package.Info.Authors));
                        }

                        authorsCount = authors.Count;
                        if (authorsCount > 0 && !(authorsCount == 1 && authors[0] == Resources.Empty))
                        {
                            text.AppendLine(string.Format("{0}:", Resources.BaseAuthors));
                            text.AppendLine(string.Join(", ", authors.ToArray()).Trim());
                            text.AppendLine();
                        }

                        if (theme.Info.Sources.Count > 0)
                        {
                            text.AppendLine(string.Format("{0}:", Resources.BaseSources));
                            text.AppendLine(string.Join(Environment.NewLine, doc.GetRealSources(theme.Info.Sources)).Trim());
                            text.AppendLine();
                        }

                        i++;
                    }

                    r++;
                }

                int    counter = 0, iold = 0;
                string fileStr = text.Replace('«', '\"').Replace('»', '\"').Replace('–', '-').Replace('—', '-').Replace("…", "...").ToString();
                int    fl      = fileStr.Length;
                var    fileRes = new StringBuilder();
                for (int i = 0; i < fl; i++)
                {
                    if (fileStr[i] == '\r')
                    {
                        counter = 0;
                        fileRes.Append(fileStr.Substring(iold, i - iold + 2));
                        i++;
                        iold = i + 1;
                    }
                    else
                    {
                        counter++;
                        if (counter == 73)
                        {
                            while (!char.IsWhiteSpace(fileStr, i) && i > 0 && i > iold)
                            {
                                i--;
                            }

                            if (i == iold || i == iold + 5 && char.IsDigit(fileStr[iold + 3]) && fileStr[iold + 4] == '.')
                            {
                                i = iold + 72;
                                while (!char.IsWhiteSpace(fileStr, i) && i < fl)
                                {
                                    i++;
                                }
                            }

                            fileRes.AppendLine(fileStr.Substring(iold, i - iold));
                            if (fileStr[i] == '\r')
                            {
                                i++;
                                iold = i + 1;
                            }
                            else
                            {
                                iold = i + (char.IsWhiteSpace(fileStr, i) ? 1 : 0);
                            }

                            counter = 0;
                        }
                    }
                }

                var paragraph = new Paragraph();
                foreach (var line in fileRes.ToString().Split(new string[] { "\r\n", "\r", "\n" }, StringSplitOptions.None))
                {
                    if (paragraph.Inlines.Count > 0)
                    {
                        paragraph.AppendLine();
                    }

                    if (line.Length > 0)
                    {
                        paragraph.AppendText(line);
                    }
                }

                document.Blocks.Add(paragraph);
            }

            break;
            }

            document.PageWidth  = 21.0 / 2.54 * 96;
            document.PageHeight = 29.7 / 2.54 * 96;

            return(new FlowDocumentWrapper(document));
        }