public static OutlineItem Outline( Document document )
 {
     OutlineItem documentItem = new OutlineItem( document );
      List<OutlineItem> outlineStack = new List<OutlineItem>();
      outlineStack.Add( documentItem );
      int end = 0;
      int paragraphIndex = 0;
      foreach ( Paragraph paragraph in document.Paragraphs )
      {
     int outlineLevel = (int) paragraph.OutlineLevel;
     if ( outlineLevel == (int) WdOutlineLevel.wdOutlineLevelBodyText )
     {
        end = paragraph.Range.End;
     }
     else
     {
        while ( outlineStack[outlineStack.Count - 1].Level >= outlineLevel )
        {
           outlineStack[outlineStack.Count - 1].UpdateEnd( end, paragraphIndex );
           outlineStack.RemoveAt( outlineStack.Count - 1 );
        }
        outlineStack.Add( new OutlineItem( paragraph, outlineStack[outlineStack.Count - 1], paragraphIndex ) );
        end = paragraph.Range.End;
     }
     paragraphIndex++;
      }
      while ( outlineStack.Count > 1 )
      {
     outlineStack[outlineStack.Count - 1].UpdateEnd( end, paragraphIndex );
     outlineStack.RemoveAt( outlineStack.Count - 1 );
      }
      documentItem.UpdateEnd( document.Content.End, document.Paragraphs.Count );
      return documentItem;
 }
 private void DemoteRecursively(OutlineItem item)
 {
     item.Demote();
      foreach (OutlineItem child in item.Children)
      {
     DemoteRecursively(child);
      }
 }
 private OutlineItem( Range _range, OutlineItem _parent, int _startParagraphIndex )
 {
     m_document = null;
      m_title = null;
      m_range = _range;
      m_children = new List<OutlineItem>();
      m_parent = _parent;
      if ( m_parent != null )
      {
     m_parent.m_children.Add( this );
      }
      m_startParagraphIndex = _startParagraphIndex;
      m_endParagraphIndex = _startParagraphIndex;
 }
 public OutlineItem Get(params string[] _path)
 {
     OutlineItem parent = m_root;
      foreach (string pathItem in _path)
      {
     OutlineItem item = parent.GetChild(pathItem);
     if (item == null)
     {
        parent.AppendChild(pathItem);
        m_root = Edward.Refresh( m_root );
        return Get(_path);
     }
     parent = item;
      }
      return parent;
 }
        public void Geneate( OutlineItem _document, string _outputFilename )
        {
            string title = Program.GetTitle( _document );
             string author = Program.GetAuthor( _document );

             using ( TextWriter csWriter = new StreamWriter( _outputFilename ) )
             {
            csWriter.WriteLine( "using Sequanto.Manual;" );
            csWriter.WriteLine( "using SeqZap.Manual;" );
            csWriter.WriteLine();
            csWriter.WriteLine( "namespace {0}", Namespace );
            csWriter.WriteLine( "{" );
            csWriter.WriteLine( "   public class {0} : IManual", ClassName );
            csWriter.WriteLine( "   {" );
            csWriter.WriteLine( "      private static readonly {0} s_instance = new {0} ();", ClassName );
            csWriter.WriteLine( "      private {0} ()", ClassName );
            csWriter.WriteLine( "      {" );
            csWriter.WriteLine( "      }" );
            csWriter.WriteLine( "      public static void Register ()", ClassName );
            csWriter.WriteLine( "      {" );
            csWriter.WriteLine( "         SeqZap.Base.ServiceManager.Get<IManualManager>().Register(s_instance);" );
            csWriter.WriteLine( "      }" );
            csWriter.WriteLine( "      public static {0} Instance {{ get {{ return s_instance; }} }}", ClassName );
            csWriter.WriteLine( "      public string Name {{ get {{ return \"{0}\"; }} }}", ClassName );
            csWriter.WriteLine( "      public string Title {{ get {{ return \"{0}\"; }} }}", title );
            csWriter.WriteLine( "      public string Author {{ get {{ return \"{0}\"; }} }}", author );
            csWriter.WriteLine( "      private static readonly Bookmark s_tableOfContents = new Bookmark( s_instance, \"toc\", \"\", 1 );" );
            csWriter.WriteLine( "      public Bookmark TableOfContents { get { return s_tableOfContents; } }" );

            List<KeyValuePair<string, Uri>> constUrls = new List<KeyValuePair<string, Uri>>();
            foreach ( OutlineItem child in _document.Children )
            {
               GenerateItem( child, csWriter, constUrls );
            }
            csWriter.WriteLine( "      public class Const" );
            csWriter.WriteLine( "      {" );
            foreach ( KeyValuePair<string, Uri> constUrl in constUrls )
            {
               csWriter.WriteLine( "         public const string {0} = \"{1}\";", constUrl.Key, constUrl.Value );
            }
            csWriter.WriteLine( "      }" );
            csWriter.WriteLine( "   }" );
            csWriter.WriteLine( "}" );
             }
        }
 public static string GetAuthor( OutlineItem _item )
 {
     return _item.Document.BuiltInDocumentProperties[WdBuiltInProperty.wdPropertyAuthor].Value;
 }
 internal OutlineItem( Paragraph _title, OutlineItem _parent, int _startParagraphIndex )
     : this(_title.Range, _parent, _startParagraphIndex)
 {
     m_title = _title;
 }
 public static OutlineItem Refresh( OutlineItem item )
 {
     return Outline( item.Range.Document );
 }
 private static LinkedList<KeyValuePair<string, OutlineItem>> GetHeritage( OutlineItem _item )
 {
     LinkedList<KeyValuePair<string, OutlineItem>> stack = new LinkedList<KeyValuePair<string, OutlineItem>>();
      OutlineItem current = _item;
      while ( current.Parent != null )
      {
     stack.AddFirst( new KeyValuePair<string, OutlineItem>( HtmlGenerator.ToHtmlId( current.Title ), current ) );
     for ( int i = current.Parent.Level; i < current.Level - 1; i++ )
     {
        stack.AddFirst( new KeyValuePair<string, OutlineItem>( String.Format( "MISSING-H{0}", i + 1 ), null ) );
     }
     current = current.Parent;
      }
      return stack;
 }
        private void UpdateTree(OutlineItem outline)
        {
            this.Enabled = false;

             m_tree.BeginUpdate();
             m_tree.Nodes.Clear();
             Populate(outline, m_tree.Nodes);
             m_tree.EndUpdate();

             this.Enabled = true;
        }
 private void Populate(OutlineItem outline, TreeNodeCollection nodes)
 {
     TreeNode node = nodes.Add(outline.Path(" / "), outline.Title, outline.Level,outline.Level);
      node.Tag = outline;
      foreach (OutlineItem child in outline.Children)
      {
     Populate(child, node.Nodes);
      }
      if (m_expanded.Contains(node.Name))
      {
     node.Expand();
      }
 }
 public static void GenerateFilenameAndId( OutlineItem _item, int smallestLevel, out string _filename, out string _id, out string _csharpName )
 {
     GenerateFilenameAndId( GetHeritage( _item ), smallestLevel, out _filename, out _id, out _csharpName );
 }
 private void HtmlItemRecursively( OutlineItem _item, TextWriter _writer, int _topLevel )
 {
     HtmlItem( _item, _writer, _topLevel );
      foreach ( OutlineItem child in _item.Children )
      {
     HtmlItemRecursively( child, _writer, _topLevel );
      }
 }
        private void HtmlItem( OutlineItem _item, TextWriter _writer, int _topLevel )
        {
            string filename;
             string id;
             //LinkedList<string> stack = GetHeritage(_item);
             GenerateFilenameAndId( _item, this.SmallestLevel, out filename, out id );

             if ( String.IsNullOrEmpty( id ) )
             {
            _writer.WriteLine( "<h{0}>{1}</h{0}>", _item.Level - _topLevel + 1, HtmlEscape( _item.Title ) );
             }
             else
             {
            _writer.WriteLine( "<h{0} id=\"{2}\">{1}</h{0}>", _item.Level - _topLevel + 1, HtmlEscape( _item.Title ), id );
             }
             _writer.WriteLine( "<div class=\"content\">" );
             List<string> listLevels = new List<string>();
             foreach ( Content content in _item.Content )
             {
            try
            {
               switch ( content.Type )
               {
                  case ContentType.Text:
                     {
                        TextContent text = content.AsText;
                        while ( listLevels.Count != text.ListLevel )
                        {
                           if ( listLevels.Count > text.ListLevel )
                           {
                              _writer.WriteLine( "</{0}>", listLevels[listLevels.Count - 1] );
                              listLevels.RemoveAt( listLevels.Count - 1 );
                           }
                           else
                           {
                              if ( text.NumberedList )
                              {
                                 listLevels.Add( "ol" );
                              }
                              else
                              {
                                 listLevels.Add( "ul" );
                              }
                              _writer.WriteLine( "<{0}>", listLevels[listLevels.Count - 1] );
                           }
                        }
                        if ( listLevels.Count == 0 )
                        {
                           switch ( text.Style )
                           {
                              case "Code":
                                 _writer.Write( "<code>" );
                                 HtmlTextContent( filename, text, _writer );
                                 _writer.WriteLine( "</code>" );
                                 break;

                              case "seqzap-manual-generator-error":
                                 AddError( text.Text, filename );
                                 _writer.Write( "<div class=\"{0}\">", text.Style );
                                 HtmlTextContent( filename, text, _writer );
                                 _writer.WriteLine( "</div>" );
                                 break;

                              default:
                                 _writer.Write( "<p>" );
                                 HtmlTextContent( filename, text, _writer );
                                 _writer.WriteLine( "</p>" );
                                 break;
                           }
                        }
                        else
                        {
                           _writer.WriteLine( "<li>{0}</li>", HtmlEscape( text.Text ) );
                        }
                     }
                     break;

                  case ContentType.ImagePng:
                     {
                        PngImageContent image = content.AsPngImage;
                        string imageFileName = SaveImage( image );
                        _writer.WriteLine( "<div class=\"figure\">" );
                        _writer.WriteLine( "<img src=\"{0}\" alt=\"{1}\" style=\"width: {2}pt; height: {3}pt;\" />", imageFileName, HtmlEscape( image.AltText ), image.Width.ToString( System.Globalization.CultureInfo.InvariantCulture ), image.Height.ToString( System.Globalization.CultureInfo.InvariantCulture ) );
                        if ( !String.IsNullOrEmpty( image.Title ) )
                        {
                           _writer.WriteLine( "<div class=\"figcaption\">{0}</div>", HtmlEscape( image.Title ) );
                        }
                        _writer.WriteLine( "</div>" );
                     }
                     break;

                  case ContentType.Table:
                     {
                        _writer.WriteLine( "<table>" );
                        TableContent table = content.AsTable;
                        foreach ( TableContent.Row row in table.Rows )
                        {
                           string tag = "td";
                           if ( row.IsHeading )
                           {
                              tag = "th";
                           }
                           _writer.Write( "  <tr>" );
                           foreach ( TableContent.Cell cell in row.Cells )
                           {
                              int colSpan = cell.ColumnSpan;
                              string text = HtmlEscape( cell.Text );
                              if ( colSpan > 1 )
                              {
                                 _writer.Write( "<{0} colspan=\"{2}\">{1}</{0}>", tag, text, colSpan );
                              }
                              else
                              {
                                 _writer.Write( "<{0}>{1}</{0}>", tag, text );
                              }
                           }
                           _writer.WriteLine( "</tr>" );
                        }
                        _writer.WriteLine( "</table>" );
                     }
                     break;
               }
            }
            catch ( Exception ex )
            {
               AddError( String.Format( "{0}{1}{1}{2}", ex.Message, Environment.NewLine, ex.StackTrace ), filename );

               _writer.WriteLine( "<div class=\"seqzap-manual-generator-error\">" );
               _writer.WriteLine( "<h1>While trying to generate {0}</h1>", content.Type );
               _writer.WriteLine( "<h2>Message</h2>" );
               _writer.WriteLine( "<p>{0}</p>", HtmlEscape( ex.Message ) );
               _writer.WriteLine( "<h2>Stacktrace</h2>" );
               _writer.WriteLine( "<pre>{0}</pre>", HtmlEscape( ex.StackTrace ) );
               _writer.WriteLine( "</div>" );
            }
             }
             _writer.WriteLine( "</div>" );
        }
        private void GenerateItem( OutlineItem item, TextWriter tocWriter, TextWriter parentWriter, string parentFilename, string author )
        {
            string filename;
             string id;
             string csharpName;
             LinkedList<KeyValuePair<string, OutlineItem>> heritage = GetHeritage( item );
             GenerateFilenameAndId( heritage, this.SmallestLevel, out filename, out id, out csharpName );
             if ( filename.Length == 0 )
             {
            return;
             }

             using ( TextWriter writer = new StreamWriter( Path.Combine( BaseDirectory, filename + ".html" ) ) )
             {
            HtmlStart( writer, item.Title, author, item.PageNumber );
            writer.WriteLine( "<div class=\"nav\">" );
            string fullname = null;
            writer.Write( "<a href=\"toc.html\">TOC</a>" );
            foreach ( KeyValuePair<string, OutlineItem> heritageItem in heritage )
            {
               writer.Write( " &raquo; " );
               if ( fullname == null )
               {
                  fullname = heritageItem.Key;
               }
               else
               {
                  fullname = fullname + "-" + heritageItem.Key;
               }
               if ( heritageItem.Value != null )
               {
                  writer.Write( "<a href=\"{0}.html\">{1}</a>", fullname, HtmlEscape( heritageItem.Value.Title ) );
               }
            }
            writer.WriteLine();
            writer.WriteLine( "<span class=\"relational\">" );
            int indexOfItem = item.Parent.Children.IndexOf( item );
            if ( indexOfItem == 0 )
            {
               writer.WriteLine( "Prev" );
            }
            else
            {
               string prevFilename;
               string prevId;
               OutlineItem prev = item.Parent.Children[indexOfItem - 1];
               GenerateFilenameAndId( prev, this.SmallestLevel, out prevFilename, out prevId );
               writer.WriteLine( "<a href=\"{0}.html\">Prev</a>", prevFilename );
            }
            writer.WriteLine( "<a href=\"{0}.html\"> Up </a>", parentFilename );
            if ( indexOfItem == item.Parent.Children.Count - 1 )
            {
               writer.WriteLine( "Next", parentFilename );
            }
            else
            {
               string nextFilename;
               string nextId;
               OutlineItem next = item.Parent.Children[indexOfItem + 1];
               GenerateFilenameAndId( next, this.SmallestLevel, out nextFilename, out nextId );
               writer.WriteLine( "<a href=\"{0}.html\">Next</a>", nextFilename );
            }
            writer.WriteLine( "</span>" );
            writer.WriteLine( "</div>" );
            if ( parentWriter != null )
            {
               parentWriter.Write( "<li><a href=\"{0}.html\">{1}</a>", filename, HtmlEscape( item.Title ) );
            }
            tocWriter.Write( "<li><a href=\"{0}.html\">{1}</a>", filename, HtmlEscape( item.Title ) );
            HtmlItem( item, writer, item.Level );
            if ( item.Level < this.SmallestLevel )
            {
               writer.WriteLine( "<div class=\"children\">" );
               writer.WriteLine( "<ul>" );
               tocWriter.WriteLine( "<ul>" );
               foreach ( OutlineItem child in item.Children )
               {
                  GenerateItem( child, tocWriter, writer, filename, author );
               }
               tocWriter.WriteLine( "</ul>" );
               writer.WriteLine( "</ul>" );
               writer.WriteLine( "</div>" );
            }
            else
            {
               foreach ( OutlineItem child in item.Children )
               {
                  HtmlItemRecursively( child, writer, item.Level );
               }
            }
            tocWriter.WriteLine( "</li>" );
            HtmlEnd( writer );
             }
        }
 public static string GetTitle( OutlineItem _item )
 {
     return _item.Document.BuiltInDocumentProperties[WdBuiltInProperty.wdPropertyTitle].Value;
 }
 public DocRoot(OutlineItem _root)
 {
     m_root = _root;
 }
        private void GenerateItem( OutlineItem _item, TextWriter _writer, List<KeyValuePair<string, Uri>> _constUrls )
        {
            string filename;
             string id;
             string csharpName;
             HtmlGenerator.GenerateFilenameAndId( _item, this.SmallestLevel, out filename, out id, out csharpName );
             if ( filename.Length == 0 )
             {
            return;
             }

             UriBuilder builder = new UriBuilder( "seqzap-manual", ClassName, _item.PageNumber );
             builder.Path = filename;
             if ( _item.Level > this.SmallestLevel )
             {
            _writer.WriteLine( "      public static readonly Bookmark {0} = new Bookmark( s_instance, \"{1}\", \"{2}\", {3} );", csharpName, filename, id, _item.PageNumber );
            builder.Fragment = id;
            _constUrls.Add( new KeyValuePair<string, Uri>( csharpName, builder.Uri ) );
             }
             else
             {
            if ( _item.Title.Length > 0 )
            {
               _writer.WriteLine( "      public static readonly Bookmark {0} = new Bookmark( s_instance, \"{1}\", \"\", {2} );", csharpName, filename, _item.PageNumber );
               _constUrls.Add( new KeyValuePair<string, Uri>( csharpName, builder.Uri ) );
            }
             }
             foreach ( OutlineItem child in _item.Children )
             {
            GenerateItem( child, _writer, _constUrls );
             }
        }
 public void Refresh()
 {
     m_root = Edward.Refresh( m_root );
 }
        public void Generate( OutlineItem document, string _baseDirectory )
        {
            BaseDirectory = _baseDirectory;

             if ( Directory.Exists( BaseDirectory ) )
             {
            Directory.Delete( BaseDirectory, true );
             }
             Directory.CreateDirectory( BaseDirectory );

             string title = Program.GetTitle( document );
             string author = Program.GetAuthor( document );
             File.WriteAllText( Path.Combine( BaseDirectory, "style.css" ), Properties.Resources.style );

             using ( TextWriter tocWriter = new StreamWriter( Path.Combine( BaseDirectory, "toc.html" ) ) )
             {
            //HtmlStart(tocWriter, title + " - Table of contents", author);
            HtmlStart( tocWriter, "Table of contents", author, -1 );
            tocWriter.WriteLine( "<ul>" );
            foreach ( OutlineItem child in document.Children )
            {
               GenerateItem( child, tocWriter, null, "toc", author );
            }
            tocWriter.WriteLine( "</ul>" );
            HtmlEnd( tocWriter );
             }
        }