public static void ParseElements(TransformationData data, EMDocument doc, EMElement parent, List <TextFragment> fragments, IParser parser, Action <EMElement> elementEmitter) { var newFragments = new List <TextFragment>(); foreach (var fragment in fragments) { var origins = new List <ITextFragment>(); try { foreach (var match in parser.TextMatches(fragment.Text)) { var elementOrigin = new EMElementOrigin(fragment.Start + match.Index, match.Text); try { elementEmitter(parser.Create(match, doc, elementOrigin, parent, data)); origins.Add(elementOrigin); } catch (EMSkipParsingException) { } } newFragments.AddRange(SplitFragment(fragment, origins)); } catch (EMParsingErrorException e) { data.ErrorList.Add(new ErrorDetail(e.Message, MessageClass.Error, "", "", e.Line, e.Column, e.Path != data.Document.LocalPath ? e.Path : null)); } } fragments.Clear(); fragments.AddRange(newFragments); }
private static EMElement CreateDecoration(IMatch match, EMDocument doc, EMElementOrigin origin, EMElement parent, TransformationData data) { var decorationMatch = match as EMDecorationMatch; var element = new EMDecorationElement(doc, origin, parent, decorationMatch.Type); element.Content.Parse(decorationMatch.ContentStart, decorationMatch.Content, data); return element; }
private static EMElement CreateDecoration(IMatch match, EMDocument doc, EMElementOrigin origin, EMElement parent, TransformationData data) { var decorationMatch = match as EMDecorationMatch; var element = new EMDecorationElement(doc, origin, parent, decorationMatch.Type); element.Content.Parse(decorationMatch.ContentStart, decorationMatch.Content, data); return(element); }
public override EMElement Create(IMatch match, EMDocument doc, EMElementOrigin origin, EMElement parent, TransformationData data) { if (match is EMBracketedImagesAndLinksMatch) { return CreateBracketed(match, doc, origin, parent, data); } if (match is EMDecorationMatch) { return CreateDecoration(match, doc, origin, parent, data); } throw new InvalidOperationException("Should not happend!"); }
public override EMElement Create(IMatch match, EMDocument doc, EMElementOrigin origin, EMElement parent, TransformationData data) { if (match is EMBracketedImagesAndLinksMatch) { return(CreateBracketed(match, doc, origin, parent, data)); } if (match is EMDecorationMatch) { return(CreateDecoration(match, doc, origin, parent, data)); } throw new InvalidOperationException("Should not happend!"); }
public override EMElement Create(IMatch match, EMDocument doc, EMElementOrigin origin, EMElement parent, TransformationData data) { if (match is EMRawHTMLMatch) { var htmlMatch = match as EMRawHTMLMatch; if (htmlMatch.Name.ToLower() == "code") { return(new EMCodeBlock(doc, origin, parent, htmlMatch.Content, true)); } var element = new EMRawHTML(doc, origin, parent, htmlMatch.Name, htmlMatch.AttributesString); element.Elements.Parse(Markdown.Outdent(htmlMatch.Content, element.Elements.TextMap), data); return(element); } if (match is EMMarkdownTaggedElementMatch) { var markdownMatch = match as EMMarkdownTaggedElementMatch; switch (markdownMatch.Name.ToLower()) { case "region": return(EMRegion.CreateRegion(origin, doc, parent, data, markdownMatch, markdownMatch.Parameters)); case "include": return(EMInclude.CreateFromText(markdownMatch.Text, doc, origin, parent, data)); case "include_files": return(EMInclude.CreateIncludeFilesFromText(markdownMatch.Text, doc, origin, parent, data)); case "relative": return(EMRelativeLink.CreateFromText(markdownMatch.Parameters, doc, origin, parent, data)); case "relative_img": return(EMRelativeLink.CreateFromText(markdownMatch.Parameters, doc, origin, parent, data, true)); case "object": return(EMObject.CreateFromText(markdownMatch.Parameters, markdownMatch, doc, origin, parent, data)); default: return(EMErrorElement.Create(doc, origin, parent, data, "UnsupportedTaggedMarkdownElement")); } } return(EMErrorElement.Create(doc, origin, parent, data, "UnknownMatchTypeForTaggedElementsParser")); }
private static EMElement CreateBracketed( IMatch match, EMDocument doc, EMElementOrigin origin, EMElement parent, TransformationData data) { var taggedMatch = match as EMBracketedImagesAndLinksMatch; // Check if image. if (taggedMatch.Text.StartsWith("!") && !string.IsNullOrWhiteSpace(taggedMatch.Parameters)) { EMElement outElement = null; var fragments = new List <TextFragment>() { new TextFragment(origin.Start, match.Text) }; EMParsingHelper.ParseElements( data, doc, parent, fragments, EMImage.GetParser(), element => { outElement = element; }); if (outElement == null) { string imageError = "Could not find image in given text: " + origin.Text; throw new InvalidOperationException(imageError); } return(outElement); } // Check if shortcut ref if (string.IsNullOrWhiteSpace(taggedMatch.Parameters)) { return(EMLink.CreateFromAnchorRefShortcut( EMLink.AnchorRefShortcut.Match(taggedMatch.Text), doc, origin, parent, data)); } // If not an image and not a shortcut link, then must have been other link. // Parse span content for internal elements. var span = new EMSpanElements(doc, origin, parent); span.Parse(taggedMatch.ContentStart, taggedMatch.Content, data); // Check if ref link return(taggedMatch.Parameters.StartsWith("[") ? EMLink.CreateFromAnchorRef(doc, origin, parent, data, span, taggedMatch.Parameters) : EMLink.CreateFromInline(doc, origin, parent, data, span, taggedMatch.Parameters)); }
public override EMElement Create(IMatch match, EMDocument doc, EMElementOrigin origin, EMElement parent, TransformationData data) { if (match is EMRawHTMLMatch) { var htmlMatch = match as EMRawHTMLMatch; if (htmlMatch.Name.ToLower() == "code") { return new EMCodeBlock(doc, origin, parent, htmlMatch.Content, true); } var element = new EMRawHTML(doc, origin, parent, htmlMatch.Name, htmlMatch.AttributesString); element.Elements.Parse(Markdown.Outdent(htmlMatch.Content, element.Elements.TextMap), data); return element; } if (match is EMMarkdownTaggedElementMatch) { var markdownMatch = match as EMMarkdownTaggedElementMatch; switch (markdownMatch.Name.ToLower()) { case "region": return EMRegion.CreateRegion(origin, doc, parent, data, markdownMatch, markdownMatch.Parameters); case "include": return EMInclude.CreateFromText(markdownMatch.Text, doc, origin, parent, data); case "include_files": return EMInclude.CreateIncludeFilesFromText(markdownMatch.Text, doc, origin, parent, data); case "relative": return EMRelativeLink.CreateFromText(markdownMatch.Parameters, doc, origin, parent, data); case "relative_img": return EMRelativeLink.CreateFromText(markdownMatch.Parameters, doc, origin, parent, data, true); case "object": return EMObject.CreateFromText(markdownMatch.Parameters, markdownMatch, doc, origin, parent, data); default: return EMErrorElement.Create(doc, origin, parent, data, "UnsupportedTaggedMarkdownElement"); } } return EMErrorElement.Create(doc, origin, parent, data, "UnknownMatchTypeForTaggedElementsParser"); }
private static EMElement CreateBracketed( IMatch match, EMDocument doc, EMElementOrigin origin, EMElement parent, TransformationData data) { var taggedMatch = match as EMBracketedImagesAndLinksMatch; // Check if image. if (taggedMatch.Text.StartsWith("!") && !string.IsNullOrWhiteSpace(taggedMatch.Parameters)) { EMElement outElement = null; var fragments = new List<TextFragment>() { new TextFragment(origin.Start, match.Text) }; EMParsingHelper.ParseElements( data, doc, parent, fragments, EMImage.GetParser(), element => { outElement = element; }); if (outElement == null) { string imageError = "Could not find image in given text: " + origin.Text; throw new InvalidOperationException(imageError); } return outElement; } // Check if shortcut ref if (string.IsNullOrWhiteSpace(taggedMatch.Parameters)) { return EMLink.CreateFromAnchorRefShortcut( EMLink.AnchorRefShortcut.Match(taggedMatch.Text), doc, origin, parent, data); } // If not an image and not a shortcut link, then must have been other link. // Parse span content for internal elements. var span = new EMSpanElements(doc, origin, parent); span.Parse(taggedMatch.ContentStart, taggedMatch.Content, data); // Check if ref link return taggedMatch.Parameters.StartsWith("[") ? EMLink.CreateFromAnchorRef(doc, origin, parent, data, span, taggedMatch.Parameters) : EMLink.CreateFromInline(doc, origin, parent, data, span, taggedMatch.Parameters); }
public abstract EMElement Create(IMatch match, EMDocument doc, EMElementOrigin origin, EMElement parent, TransformationData data);
public EMElement Create(IMatch match, EMDocument doc, EMElementOrigin origin, EMElement parent, TransformationData data) { return(elementCreator((TMatch)match, doc, origin, parent, data)); }
public static void ParseElements(TransformationData data, EMDocument doc, EMElement parent, List<TextFragment> fragments, IParser parser, Action<EMElement> elementEmitter) { var newFragments = new List<TextFragment>(); foreach (var fragment in fragments) { var origins = new List<ITextFragment>(); try { foreach (var match in parser.TextMatches(fragment.Text)) { var elementOrigin = new EMElementOrigin(fragment.Start + match.Index, match.Text); try { elementEmitter(parser.Create(match, doc, elementOrigin, parent, data)); origins.Add(elementOrigin); } catch (EMSkipParsingException) { } } newFragments.AddRange(SplitFragment(fragment, origins)); } catch (EMParsingErrorException e) { data.ErrorList.Add(new ErrorDetail(e.Message, MessageClass.Error, "", "", e.Line, e.Column, e.Path != data.Document.LocalPath ? e.Path : null)); } } fragments.Clear(); fragments.AddRange(newFragments); }