Exemple #1
0
        private Dictionary <HeadSection, string> CreateTableOfContents(Section[] sections)
        {
            Dictionary <HeadSection, string> contents = new Dictionary <HeadSection, string>();

            HashSet <string> set = new HashSet <string>();

            foreach (Section section in sections)
            {
                if (section is HeadSection)
                {
                    HeadSection s = (HeadSection)section;

                    string text = GetString(s.Text);
                    if (set.Contains(text))
                    {
                        int sequence = 1;
                        for (;;)
                        {
                            string name = text + "_" + sequence;
                            if (!set.Contains(name))
                            {
                                text = name;
                                break;
                            }
                            sequence++;
                        }
                    }
                    set.Add(text);

                    contents.Add(s, text);
                }
            }

            return(contents);
        }
Exemple #2
0
        private void ReadHeadChunk(EndianBinaryReader reader)
        {
            reader.BaseStream.Position = Header.OffsetToHeadChunk;

            HEAD        = new HeadSection();
            HEAD.ID     = new string(reader.ReadChars(4));
            HEAD.Length = reader.ReadInt32();
            HEAD.Data   = reader.ReadBytes(HEAD.Length - 8); // -8 to take reading the ID and length into account.
        }
        public static List <ContentItem> Generate(Section[] sections, int index, int levelLower, int levelUpper)
        {
            int headLevel = 0;

            for (int i = index; i > 0; i--)
            {
                if (sections[i - 1] is HeadSection)
                {
                    HeadSection s = (HeadSection)sections[i - 1];
                    headLevel = s.Level;
                    break;
                }
            }
            return(Generate(sections, index, headLevel, levelLower, levelUpper));
        }
        public static List <ContentItem> Generate(Section[] sections, int index, int headLevel, int levelLower, int levelUpper)
        {
            List <ContentItem> items = new List <ContentItem>();

            for (int i = index; i < sections.Length; i++)
            {
                if (sections[i] is HeadSection)
                {
                    HeadSection s = (HeadSection)sections[i];
                    if (s.Level <= headLevel)
                    {
                        break;
                    }
                    else
                    {
                        if (s.Level < levelLower || levelUpper < s.Level)
                        {
                            continue;
                        }

                        int depth = s.Level - headLevel;

                        List <ContentItem> current = items;
                        for (int j = 0; j < depth; j++)
                        {
                            if (depth - j <= 1)
                            {
                                current.Add(new ContentItem(s));
                                break;
                            }
                            if (current.Count <= 0)
                            {
                                current.Add(new ContentItem(null));
                            }
                            current = current[current.Count - 1].Items;
                        }
                    }
                }
            }
            return(items);
        }
 public ContentItem(HeadSection head)
 {
     this.Head = head;
 }
Exemple #6
0
        private void EncodeInternal(TextWriter writer, Section[] sections)
        {
            Dictionary <HeadSection, string> headRefs = CreateTableOfContents(sections);

            for (int i = 0; i < sections.Length; i++)
            {
                Section section = sections[i];

                if (section is ParagraphSection)
                {
                    ParagraphSection s = (ParagraphSection)section;

                    writer.Write("<p>");
                    WriteText(writer, s.Text);
                    writer.WriteLine("</p>");
                }
                else if (section is HeadSection)
                {
                    HeadSection s = (HeadSection)section;

                    writer.Write("<a name=\"{0}\">", headRefs[s]);
                    writer.Write("<h{0}>", s.Level);
                    WriteText(writer, s.Text);
                    writer.Write("</h{0}>", s.Level);
                    writer.WriteLine("</a>");
                }
                else if (section is HorizonSection)
                {
                    HorizonSection s = (HorizonSection)section;

                    writer.Write("<hr/>");
                }
                else if (section is CodeSection)
                {
                    CodeSection s = (CodeSection)section;

                    writer.Write("<pre class=\"code\"><code>");
                    writer.Write(Escape(s.Text));
                    writer.WriteLine("</code></pre>");
                }
                else if (section is QuoteSection)
                {
                    QuoteSection s = (QuoteSection)section;

                    writer.Write("<blockquote>");
                    EncodeInternal(writer, s.Texts);
                    writer.WriteLine("</blockquote>");
                }
                else if (section is OrderListSection)
                {
                    OrderListSection s = (OrderListSection)section;

                    writer.WriteLine("<ol>");
                    foreach (ListItemSection j in s.Items)
                    {
                        writer.Write("<li>");
                        WriteText(writer, j.Text);
                        EncodeInternal(writer, j.ChildList.ToArray());
                        writer.WriteLine("</li>");
                    }
                    writer.WriteLine("</ol>");
                }
                else if (section is ListSection)
                {
                    ListSection s = (ListSection)section;

                    writer.WriteLine("<ul>");
                    foreach (ListItemSection j in s.Items)
                    {
                        writer.Write(String.Format("<li {0}>", GetListClass(j.Mark)));
                        WriteText(writer, j.Text);
                        EncodeInternal(writer, j.ChildList.ToArray());
                        writer.WriteLine("</li>");
                    }
                    writer.WriteLine("</ul>");
                }
                else if (section is DefinitionListSection)
                {
                    DefinitionListSection s = (DefinitionListSection)section;

                    writer.Write("<dl>");
                    foreach (DefinitionItemSection j in s.Items)
                    {
                        writer.Write("<dt>");
                        WriteText(writer, j.Caption);
                        writer.WriteLine("</dt>");
                        writer.Write("<dd>");
                        WriteText(writer, j.Data);
                        writer.WriteLine("</dd>");
                    }
                    writer.WriteLine("</dl>");
                }
                else if (section is ContentsSection)
                {
                    ContentsSection s = (ContentsSection)section;

                    List <ContentItem> contents
                        = ContentsTableGenerator.Generate(sections, i, s.LevelLower, s.LevelUpper);

                    WriteContents(writer, contents, headRefs);
                }
                else if (section is ContentsAllSection)
                {
                    ContentsAllSection s = (ContentsAllSection)section;

                    List <ContentItem> contents
                        = ContentsTableGenerator.GenerateAll(sections, s.LevelLower, s.LevelUpper);

                    WriteContents(writer, contents, headRefs);
                }
            }
        }