private static void ParseParagraphText(MDSlide mdSlide, MDShapeText mdShape, string paragraphText) { if (paragraphText.Length > 0) { var line = paragraphText.ToString() .Replace("``", "") .Replace("*", "x") .Replace("", "→"); if (mdShape.IsTitle) { mdSlide.Shapes.AddLast(new MDShapeText(string.Format("# {0}", line))); } else if (mdShape.IsSecTitle) { if (line == "Live Demo") { mdSlide.IsDemoSlide = true; line = "[Demo]()"; var demoTitle = mdSlide.Shapes.Last.Value.ToString(); mdSlide.Shapes.RemoveLast(); mdSlide.Shapes.AddLast(new MDShapeText(string.Format("<!-- {0} -->", demoTitle))); } mdSlide.Shapes.AddLast(new MDShapeText(string.Format("## {0}", line))); } else if (mdShape.IsMultiCode) { if (!mdShape.AddedCodeOpen) { mdSlide.Shapes.AddLast(new MDShapeText("")); mdSlide.Shapes.AddLast(new MDShapeText(string.Format("```{0}", Language))); mdSlide.Shapes.AddLast(new MDShapeText(line)); mdShape.AddedCodeOpen = true; } else { mdSlide.Shapes.AddLast(new MDShapeText(line)); } } else if (mdShape.IsBalloon) { //mdSlide.Shapes.AddLast(new MDShapeText(string.Format(@"<div class=""fragment balloon"" style=""width:250px; top:60%; left:10%"">{0}</div>", line))); //mdSlide.HasTags = true; } //else if (mdSlide.IsTitleSlide) //{ // mdSlide.Signature.Add(line); //} else { mdSlide.Shapes.AddLast(new MDShapeText(string.Format("{0}- {1}", new string(' ', mdShape.IndentCount * 2), line))); } } }
private static MDSlide AssignSlide(SlideType type) { MDSlide slide; switch (type) { case SlideType.PresentationTitle: slide = new MDSlidePresentationTitle(); break; case SlideType.TableOfContents: slide = new MDSlide(); slide.IsTitleSlide = true; break; case SlideType.SectionStart: slide = new MDSlideSection(); break; case SlideType.Demo: slide = new MDSlideDemo(); break; default: slide = new MDSlide(); break; } return(slide); }
public static void ExtractPPTXtoMD(string rootDir, string githubName, string language, string destinationDir = null) { gitHub = githubName; lang = language; IList <string> pptxFiles = GetAllPPTXFilesFrom(rootDir); DirectoryInfo mdsDir = Directory.CreateDirectory(destinationDir ?? (rootDir + @"\.mds")); for (int presentationIndex = 0; presentationIndex < pptxFiles.Count; presentationIndex++) { string filePath = pptxFiles[presentationIndex]; #region create sub-directorues string lectureDirStr = GetLecturePath(filePath, rootDir); DirectoryInfo lectureDir = mdsDir.CreateSubdirectory(lectureDirStr); 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"); } imagesRootFolder = slidesDir.ToString(); #endregion #region get and convert slide info MDPresentation mdPresentation = new MDPresentation(); using (PresentationDocument presentationDocument = PresentationDocument.Open(filePath, false)) { presentationImageIndex = 0; IList <SlidePart> slideParts = GetSlideParts(presentationDocument); for (int slideIndex = 0; slideIndex < slideParts.Count; slideIndex++) { MDSlide mdSlide = ParseSlidePart(slideParts, slideIndex); mdPresentation.StartNewSection(mdSlide.IsTitleSlide); mdPresentation.AddSlide(mdSlide); } } #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", filePath.Substring(rootDir.Length)); #endregion } }
private static void ParseParagraphText(MDSlide mdSlide, MDShapeText mdShape, string paragraphText) { if (paragraphText.Length > 0) { var line = paragraphText.ToString() .Replace("``", "") .Replace("*", "x") .Replace("", "→"); if (mdShape.IsTitle) { mdSlide.Shapes.AddLast(new MDShapeText(string.Format("# {0}", line))); } else if (mdShape.IsSecTitle) { if (line == "Live Demo") { mdSlide.IsDemoSlide = true; line = "[Demo]()"; var demoTitle = mdSlide.Shapes.Last.Value.ToString(); mdSlide.Shapes.RemoveLast(); mdSlide.Shapes.AddLast(new MDShapeText(string.Format("<!-- {0} -->", demoTitle))); } mdSlide.Shapes.AddLast(new MDShapeText(string.Format("## {0}", line))); } else if (mdShape.IsMultiCode) { if (!mdShape.AddedCodeOpen) { mdSlide.Shapes.AddLast(new MDShapeText("")); mdSlide.Shapes.AddLast(new MDShapeText(string.Format("```{0}", Language))); mdSlide.Shapes.AddLast(new MDShapeText(line)); mdShape.AddedCodeOpen = true; } else { mdSlide.Shapes.AddLast(new MDShapeText(line)); } } else if (mdShape.IsBalloon) { //mdSlide.Shapes.AddLast(new MDShapeText(string.Format(@"<div class=""fragment balloon"" style=""width:250px; top:60%; left:10%"">{0}</div>", line))); //mdSlide.HasTags = true; } //else if (mdSlide.IsTitleSlide) //{ // mdSlide.Signature.Add(line); //} else { mdSlide.Shapes.AddLast(new MDShapeText(string.Format("{0}- {1}", new string(' ', mdShape.IndentCount * 2), line))); } } }
private static MDSlide ParseSlidePart(IList <SlidePart> slideParts, int slideIndex) { var slidePart = slideParts[slideIndex]; if (slidePart == null) { throw new ArgumentNullException("slidePart"); } if (slidePart.Slide == null) { throw new ArgumentNullException("slidePart.Slide"); } SlideType slideType = GetSlideType(slidePart, slideIndex); MDSlide slide = AssignSlide(slideType); // Extract texts var shapes = slidePart.Slide.Descendants <Shape>().Where(s => s.Descendants <Drawing.Paragraph>().Any()); foreach (Shape shape in shapes) { if (string.IsNullOrWhiteSpace(shape.InnerText)) { continue; } slide.AddShapes(GetShapes(shape)); } // Extract images var pictures = slidePart.Slide.Descendants <Picture>().ToList(); foreach (var picture in pictures) { string rId = picture.BlipFill.Blip.Embed.Value; ImagePart imagePart = (ImagePart)slidePart.Slide.SlidePart.GetPartById(rId); Image image = Image.FromStream(imagePart.GetStream()); SaveImageToFile(image, slideIndex); MDShapeImage mdImage = GetImage(slideIndex, image, picture); presentationImageIndex++; slide.AddShape(mdImage); } // Extract graphics var graphics = slidePart.Slide.Descendants <GraphicFrame>().ToList(); foreach (var graphic in graphics) { // TODO: Find a way to export graphics as images } return(slide); }
private static void CheckSlideType(Shape shape, MDSlide mdSlide, MDShapeText mdShape) { var placeholder = shape.Descendants <PlaceholderShape>().FirstOrDefault(); if (placeholder != null && placeholder.Type != null) { mdShape.IsTitle = placeholder.Type.Value == PlaceholderValues.Title || placeholder.Type.Value == PlaceholderValues.CenteredTitle; mdShape.IsSecTitle = placeholder.Type.Value == PlaceholderValues.SubTitle; mdSlide.IsNewSection |= placeholder.Type.Value == PlaceholderValues.CenteredTitle; } }
public static string[] GetAllTextInSlide(SlidePart slidePart, int slideNum) { if (slidePart == null) { throw new ArgumentNullException("slidePart"); } if (slidePart.Slide == null) { throw new ArgumentNullException("slidePart.Slide"); } var mdSlide = new MDSlide(); mdSlide.IsTitleSlide = slideNum == 0; // mdSlide.HasImage = slidePart.Slide.Descendants<Picture>().Any(); foreach (var shape in slidePart.Slide.Descendants <Shape>()) { var mdShape = new MDShapeText(); CheckSlideType(shape, mdSlide, mdShape); CheckWrappingShape(shape, mdShape); foreach (var paragraph in shape.Descendants <Drawing.Paragraph>()) { string paragraphText = ExtractTextFromParagraph(mdShape, paragraph); ParseParagraphText(mdSlide, mdShape, paragraphText); } if (mdShape.IsMultiCode && mdShape.AddedCodeOpen) { mdSlide.Shapes.AddLast(new MDShapeText("```")); mdShape.IsMultiCode = false; mdShape.AddedCodeOpen = false; } } return(mdSlide.ToStringArray()); }
// 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 } }
public static string[] GetAllTextInSlide(SlidePart slidePart, int slideNum) { if (slidePart == null) { throw new ArgumentNullException("slidePart"); } if (slidePart.Slide == null) { throw new ArgumentNullException("slidePart.Slide"); } var mdSlide = new MDSlide(); mdSlide.IsTitleSlide = slideNum == 0; // mdSlide.HasImage = slidePart.Slide.Descendants<Picture>().Any(); foreach (var shape in slidePart.Slide.Descendants<Shape>()) { var mdShape = new MDShapeText(); CheckSlideType(shape, mdSlide, mdShape); CheckWrappingShape(shape, mdShape); foreach (var paragraph in shape.Descendants<Drawing.Paragraph>()) { string paragraphText = ExtractTextFromParagraph(mdShape, paragraph); ParseParagraphText(mdSlide, mdShape, paragraphText); } if (mdShape.IsMultiCode && mdShape.AddedCodeOpen) { mdSlide.Shapes.AddLast(new MDShapeText("```")); mdShape.IsMultiCode = false; mdShape.AddedCodeOpen = false; } } return mdSlide.ToStringArray(); }
private static void CheckSlideType(Shape shape, MDSlide mdSlide, MDShapeText mdShape) { var placeholder = shape.Descendants<PlaceholderShape>().FirstOrDefault(); if (placeholder != null && placeholder.Type != null) { mdShape.IsTitle = placeholder.Type.Value == PlaceholderValues.Title || placeholder.Type.Value == PlaceholderValues.CenteredTitle; mdShape.IsSecTitle = placeholder.Type.Value == PlaceholderValues.SubTitle; mdSlide.IsNewSection |= placeholder.Type.Value == PlaceholderValues.CenteredTitle; } }