Esempio n. 1
0
        private static ICollection <IMDShape> GetShapes(Shape shape)
        {
            ICollection <IMDShape> mdShapes = new List <IMDShape>();
            var type = GetShapeType(shape);

            var paragraphs = shape.Descendants <Drawing.Paragraph>().ToList();

            for (int paragraphIndex = 0; paragraphIndex < paragraphs.Count(); paragraphIndex++)
            {
                var    paragraph = paragraphs[paragraphIndex];
                string text      = ParseText(paragraph, false);
                switch (type)
                {
                case ShapeType.CenteredTitle:
                    mdShapes.Add(new MDShapeTitle(text));
                    break;

                case ShapeType.SlideSection:
                    mdShapes.Add(new MDShapeTitle(text));
                    break;

                case ShapeType.Title:
                    mdShapes.Add(new MDShapeTitle(text));
                    break;

                case ShapeType.SubTitle:
                    mdShapes.Add(new MDShapeTitle(text, true));
                    break;

                case ShapeType.SlideDemo:
                    mdShapes.Add(new MDShapeTitle(text, true));
                    break;

                case ShapeType.MultilineCode:
                    IMDShape mdShape = new MDShapeMultiCode(lang);
                    while (paragraphIndex < paragraphs.Count())
                    {
                        paragraph = paragraphs[paragraphIndex];
                        mdShape.AddLine(ParseText(paragraph, true), GetIndent(paragraph));
                        paragraphIndex++;
                    }
                    mdShapes.Add(mdShape);
                    break;

                case ShapeType.Balloon:
                    var  offset = shape.ShapeProperties.Transform2D.Offset;
                    long top    = (offset.Y.HasValue ? offset.Y.Value : 0);
                    long left   = (offset.X.HasValue ? offset.X.Value : 0);
                    long width  = shape.ShapeProperties.Transform2D.Extents.Cx;

                    mdShapes.Add(new MDShapeBalloon(text, top, left, width));
                    break;

                case ShapeType.None:
                    if (!string.IsNullOrEmpty(text))
                    {
                        mdShapes.Add(new MDShapeText(text, GetIndent(paragraph)));
                    }
                    break;

                case ShapeType.Box:
                    // TODO: render with dark blue box
                    break;

                case ShapeType.SlideNumber:
                    // Don't add this to the slides
                    break;

                case ShapeType.UnknownBox:
                    // Don't add this to the slides
                    break;
                }
            }

            return(mdShapes);
        }
Esempio n. 2
0
        // open at your own risk
        public static void ExtractWebPageToMD(string baseUrl, string[] pages, string githubName, string language, string destinationDir)
        {
            gitHub = githubName;
            lang   = language;

            for (int i = 0; i < pages.Length; i++)
            {
                #region get and convert slide info

                string       page    = pages[i];
                string       url     = baseUrl + page + ".html";
                string       html    = GetHTML(url).Result;
                HtmlDocument htmlDoc = new HtmlDocument();
                htmlDoc.LoadHtml(html);

                var            doc            = htmlDoc.DocumentNode;
                MDPresentation mdPresentation = new MDPresentation();

                var      headings    = doc.SelectNodes("//h1");
                IMDShape shape       = null;
                int      slideLength = 0;

                for (int j = 0; j < headings.Count; j++)
                {
                    var heading = headings[j];
                    mdPresentation.StartNewSection(true);

                    IMDSlide slide;
                    if (mdPresentation.Sections.Count == 1 && mdPresentation.Sections.First().Slides.Count == 0)
                    {
                        slide = new MDSlidePresentationTitle();
                        slide.AddShape(new MDShapeTitle(heading.InnerText));
                        slide.AddShape(new MDShapeText("TypeScript OOP", 0));
                        slide.AddShape(new MDShapeText("Telerik Software Academy", 0));
                        slide.AddShape(new MDShapeText("http://academy.telerik.com", 0));
                        mdPresentation.AddSlide(slide);

                        slide = new MDSlide();
                        slide.AddShape(new MDShapeTitle("Table of Contents"));
                        for (int k = 1; k < headings.Count; k++)
                        {
                            if (k != 0 && k % 10 == 0)
                            {
                                mdPresentation.AddSlide(slide);
                                slide = new MDSlide();
                                slide.AddShape(new MDShapeTitle("Table of Contents"));
                            }

                            slide.AddShape(new MDShapeText(string.Format("[{0}](#{1})", headings[k].InnerText, headings[k].InnerText.ToLower().Replace(' ', '-')), 0));
                        }

                        mdPresentation.AddSlide(slide);
                    }
                    else if (mdPresentation.Sections.Count > 1 && mdPresentation.Sections.Last().Slides.Count == 0)
                    {
                        slide = new MDSlideSection();
                        slide.AddShape(new MDShapeTitle(heading.InnerText));
                        slide.CssId = headings[j].InnerText.ToLower().Replace(' ', '-');
                        mdPresentation.AddSlide(slide);

                        slide = new MDSlide();
                    }
                    else
                    {
                        slide = new MDSlide();
                        slide.AddShape(new MDShapeTitle(heading.InnerText));
                    }

                    slide = new MDSlide();
                    slide.AddShape(new MDShapeTitle(heading.InnerText));
                    slideLength = 0;
                    var tag = heading;
                    while (tag.NextSibling != null && tag.NextSibling.Name != "h1")
                    {
                        // start new slide if current is too long
                        if (slideLength > 350)
                        {
                            mdPresentation.AddSlide(slide);
                            slide = new MDSlide();
                            slide.AddShape(new MDShapeTitle(heading.InnerText, false, true));
                            slideLength = 0;
                        }

                        tag = tag.NextSibling;
                        if (tag.Name == "p")
                        {
                            shape = new MDShapeText(tag.InnerText, 0);
                        }
                        else if (tag.Name == "pre")
                        {
                            shape = new MDShapeMultiCode(lang);
                            shape.AddLine(tag.InnerText, 0);
                        }

                        slideLength += tag.InnerText.Length;
                        slide.AddShape(shape);
                    }

                    mdPresentation.AddSlide(slide);
                }

                mdPresentation.StartNewSection(true);
                mdPresentation.AddSlide(new MDSlideQuestions(headings[0].InnerText));

                #endregion

                #region create sub-directorues
                DirectoryInfo mdsDir = Directory.CreateDirectory(destinationDir);

                string        lectureSubDir = page.ToLower().Replace(' ', '-');
                DirectoryInfo lectureDir    = mdsDir.CreateSubdirectory(string.Format("{0:d2}. {1}", i + 1, lectureSubDir));
                DirectoryInfo slidesDir     = lectureDir;

                var demosDir    = lectureDir.GetDirectories().FirstOrDefault(x => x.Name.Equals("demos", StringComparison.CurrentCultureIgnoreCase));
                var homeworkDir = lectureDir.GetDirectories().FirstOrDefault(x => x.Name.Equals("homework", StringComparison.CurrentCultureIgnoreCase));

                if (demosDir == null)
                {
                    demosDir = lectureDir.CreateSubdirectory("demos");
                }

                if (homeworkDir == null)
                {
                    homeworkDir = lectureDir.CreateSubdirectory("homework");
                }
                #endregion

                #region add all files
                //AddTableOfContentsREADME(lectureDir);
                AddLocalServerFiles(slidesDir);
                File.WriteAllLines(slidesDir + @"\README.md", mdPresentation.ToStringArray());

                Console.WriteLine("Added files for presentation:\n{0}\n", page);
                #endregion
            }
        }