Exemple #1
0
        protected string ExpandUnitsContent(Unit unit, string number = "", int unitNum = 0)
        {
            if (unit == null)
            {
                return("");
            }

            try
            {
                string unitText = "";

                if (unit.IsTitleVisible())
                {
                    string unitTitle = unit.GetTitle();
                    if (m_TextWork.IsShowUnitNumber() && unitNum > 0 && unit.IsTitleNumerable())
                    {
                        if (number.Length > 0)
                        {
                            number += string.Format(".{0}", unitNum);
                        }
                        else
                        {
                            number += string.Format("{0}", unitNum);
                        }
                        unitTitle = (number + " " + unitTitle);
                    }

                    string summaryAnchor = "";
                    if (unit.IsVisibleInSummary())
                    {
                        summaryAnchor = "<a name=\"" + string.Format("{0}", unit.GetId()) + "\"></a>";
                    }

                    switch (unit.GetLevel())
                    {
                    case 1:
                    {
                        unitText += ("<h2>" + summaryAnchor + unitTitle + "</h2>\n");
                        break;
                    }

                    case 2:
                    {
                        unitText += ("<h3>" + summaryAnchor + unitTitle + "</h3>\n");
                        break;
                    }

                    case 3:
                    {
                        unitText += ("<h4>" + summaryAnchor + unitTitle + "</h4>\n");
                        break;
                    }

                    case 4:
                    {
                        unitText += ("<h5>" + summaryAnchor + unitTitle + "</h5>\n");
                        break;
                    }

                    case 5:
                    {
                        unitText += ("<h6>" + summaryAnchor + unitTitle + "</h6>\n");
                        break;
                    }

                    default:
                    {
                        unitText += ("<h6>" + summaryAnchor + unitTitle + "</h6>\n");
                        break;
                    }
                    }
                    if (unit.IsVisibleInSummary())
                    {
                        unitText += ("<p class=\"returnIndex\"><a href=\"#index\">[indice]</a></p>\n");
                    }
                }

                for (int idContent = 0; idContent < unit.GetContentCount(); idContent++)
                {
                    Content content = unit.GetContentByPos(idContent);
                    if (content == null)
                    {
                        continue;
                    }
                    List <string> allowedTags = new List <string>();
                    if (content.GetType() == typeof(Paragraph))
                    {
                        string paragraph = "";
                        if (((Paragraph)content).GetContent().Length < Globals.PARAGRAPH_LINE_CHARS)
                        {
                            paragraph += "<p>\n";
                        }
                        else
                        {
                            paragraph += "<p class=\"indent\">\n";
                        }


                        allowedTags.Add("<b>");
                        allowedTags.Add("</b>");
                        allowedTags.Add("<i>");
                        allowedTags.Add("</i>");

                        string contentText = Utils.EscapeHTMLTags(((Paragraph)content).GetContent(), allowedTags);
                        contentText = contentText.Replace("\n", "<br/>");
                        contentText = contentText.Replace("||", "<br/>");

                        // biblio reference and attachments - start
                        int parId          = ((Paragraph)content).GetId();
                        int nParagraphRefs = 0;
                        for (int idSubContent = 0; idSubContent < unit.GetContentCount(); idSubContent++)
                        {
                            Content subContent = unit.GetContentByPos(idSubContent);
                            if (subContent == null)
                            {
                                continue;
                            }
                            if (subContent.GetType() == typeof(BiblioRef))
                            {
                                BiblioRef curLink = (BiblioRef)subContent;
                                if (parId != curLink.GetContentId())
                                {
                                    continue;
                                }
                                nParagraphRefs++;
                            }
                            else if (subContent.GetType() == typeof(Attachment))
                            {
                                Attachment curLink = (Attachment)subContent;
                                if (parId != curLink.GetContentId())
                                {
                                    continue;
                                }
                                nParagraphRefs++;
                            }
                        }

                        // expand link into html content storing them in the links array
                        int parRefCount = 0;
                        int nRefAddedBeforeParagraph = m_BiblioRefs.Count;

                        List <string> reversedRefs = new List <string>();
                        for (int idSubContent = unit.GetContentCount() - 1; idSubContent >= 0; idSubContent--) // reverse order
                        {
                            Content subContent = unit.GetContentByPos(idSubContent);
                            if (subContent == null)
                            {
                                continue;
                            }
                            string reference = "";
                            if (subContent.GetType() == typeof(BiblioRef))
                            {
                                BiblioRef curRef = (BiblioRef)subContent;
                                if (parId != curRef.GetContentId())
                                {
                                    continue;
                                }
                                parRefCount++;

                                reference = curRef.GetContent();
                                int parPos      = curRef.GetPos();
                                int globalIndex = nRefAddedBeforeParagraph + (nParagraphRefs - parRefCount + 1);

                                string preLink  = contentText.Substring(0, parPos);
                                string postLink = contentText.Substring(parPos);
                                contentText = string.Format("{0}<a name=\"backlink{1}\"></a><a href=\"#link{2}\">[{3}]</a>{4}", preLink, globalIndex, globalIndex, globalIndex, postLink);
                                reversedRefs.Add(reference);
                            }
                            else if (subContent.GetType() == typeof(Attachment))
                            {
                                Attachment curAtt = (Attachment)subContent;
                                if (parId != curAtt.GetContentId())
                                {
                                    continue;
                                }
                                parRefCount++;

                                reference = curAtt.GetContent();
                                int parPos = curAtt.GetPos();

                                string preLink  = contentText.Substring(0, parPos);
                                string postLink = contentText.Substring(parPos);
                                string attUrl   = Globals.GetMainUrl().TrimEnd('/') + curAtt.GetRelativePath();

                                contentText = string.Format("{0}<a href=\"{1}\" target=\"_blank\"\" class=\"attach\">{2}</a>{3}", preLink, attUrl, curAtt.GetAttachmentName(), postLink);
                            }
                        }
                        for (int iUrl = reversedRefs.Count - 1; iUrl >= 0; iUrl--)
                        {
                            m_BiblioRefs.Add(reversedRefs[iUrl]);
                        }
                        // biblio reference - end

                        paragraph += contentText;
                        paragraph += "</p>\n";
                        unitText  += paragraph;
                    }
                    else if (content.GetType() == typeof(Quote))
                    {
                        unitText += "<blockquote>\n";
                        string contentText = Utils.EscapeHTMLTags(((Quote)content).GetContent());
                        unitText += contentText.Replace("\n", "<br/>");
                        unitText += "</blockquote>\n";
                    }
                    else if (content.GetType() == typeof(MetaImage))
                    {
                        string contentText = (((MetaImage)content).GetBase64Content());
                        if (contentText.Length > 0)
                        {
                            unitText += string.Format("<img class=\"center\" src=\"data:image/jpeg;base64,{0}\"/>\n", contentText);
                        }
                    }
                    else if (content.GetType() == typeof(ItemList))
                    {
                        // open list item
                        allowedTags.Add("<li>");
                        allowedTags.Add("</li>");
                        allowedTags.Add("<b>");
                        allowedTags.Add("</b>");
                        allowedTags.Add("<i>");
                        allowedTags.Add("</i>");
                        bool bOrdered = ((ItemList)content).IsOrdered();
                        if (bOrdered)
                        {
                            unitText += "<ol class=\"orderedList\">\n";
                        }
                        else
                        {
                            unitText += "<ul class=\"unorderedList\">\n";
                        }

                        for (int idSubContent = idContent; idSubContent < unit.GetContentCount(); idSubContent++)
                        {
                            content = unit.GetContentByPos(idSubContent);
                            if (content == null)
                            {
                                continue;
                            }
                            if (content.GetType() == typeof(ItemList))
                            {
                                string contentText = Utils.EscapeHTMLTags(((ItemList)content).GetContent(), allowedTags);

                                unitText += contentText;
                                idContent++;
                                if (idContent == unit.GetContentCount())
                                {
                                    // close the list if itemlist is the last content in the unit (#34)
                                    if (bOrdered)
                                    {
                                        unitText += "</ol>\n";
                                    }
                                    else
                                    {
                                        unitText += "</ul>\n";
                                    }
                                    idContent--;
                                }
                            }
                            else
                            {
                                if (bOrdered)
                                {
                                    unitText += "</ol>\n";
                                }
                                else
                                {
                                    unitText += "</ul>\n";
                                }
                                idContent--;
                                break;
                            }
                        }
                        // close list item
                    }
                }

                int iNextUnit    = 1;
                int subUnitCount = unit.GetUnitCount();
                for (int iUnit = 0; iUnit < subUnitCount; iUnit++)
                {
                    unitText += ExpandUnitsContent(unit.GetUnitByPos(iUnit), number, iNextUnit);
                    Unit subUnit = unit.GetUnitByPos(iUnit);
                    if (subUnit.IsTitleNumerable())
                    {
                        iNextUnit++;
                    }
                }
                return(unitText);
            }
            catch (Exception ex)
            {
                Globals.m_Logger.Error(ex.ToString());
                return("");
            }
        }