/// <summary> /// Walks through the mobile doc, calling the provided listeners for processing /// each individual element in the document. /// </summary> /// <param name="mobileDoc">Mobile document to walk through</param> /// <param name="listener">Listener for the overall structure of the document.</param> /// <param name="sectionListeners">Set of listeners for specific section types.</param> /// <param name="skipUnsupportedSectionTypes"> /// Flag indicating that unsupported section types should be skipped. /// </param> /// <exception cref="Exception">Gets thrown when a section could not be processed.</exception> public void Walk(MobileDoc mobileDoc, IMobileDocListener listener, IEnumerable <ISectionListener> sectionListeners, bool skipUnsupportedSectionTypes = false) { listener.EnterMobileDoc(mobileDoc); listener.EnterMarkups(mobileDoc.Markups); foreach (var markup in mobileDoc.Markups) { listener.EnterMarkup(markup); listener.ExitMarkup(markup); } listener.ExitMarkups(mobileDoc.Markups); listener.EnterAtoms(mobileDoc.Atoms); foreach (var atom in mobileDoc.Atoms) { listener.EnterAtom(atom); listener.ExitAtom(atom); } listener.ExitAtoms(mobileDoc.Atoms); listener.EnterCards(mobileDoc.Cards); foreach (var card in mobileDoc.Cards) { listener.EnterCard(card); listener.ExitCard(card); } listener.ExitCards(mobileDoc.Cards); listener.EnterSections(mobileDoc.Sections); if (sectionListeners.Any()) { foreach (var section in mobileDoc.Sections) { var sectionListener = sectionListeners.FirstOrDefault( x => x.SectionType == section.SectionType); if (sectionListener != null) { sectionListener.EnterSection(section); sectionListener.ExitSection(section); } else if (!skipUnsupportedSectionTypes) { throw new Exception("Unsupported section encountered."); } } } listener.ExitSections(mobileDoc.Sections); listener.ExitMobileDoc(mobileDoc); }
/// <summary> /// Renders the mobiledoc using the rendering settings specified for this renderer. /// </summary> /// <param name="mobileDoc">Mobiledoc instance to render.</param> /// <returns>Returns the rendered mobiledoc instance.</returns> public string Render(MobileDoc mobileDoc) { var outputBuffer = new StringBuilder(); var outputWriter = new StringWriter(outputBuffer); var walker = new MobileDocWalker(); var listener = new MobileDocHtmlTranslator(_cardRenderers, _atomRenderers, outputWriter); walker.Walk(mobileDoc, listener, Enumerable.Empty <ISectionListener>()); outputWriter.Flush(); return(outputBuffer.ToString()); }
/// <summary> /// Serializes a mobile doc to JSON /// </summary> /// <param name="document">Document to serialize</param> /// <returns>Returns the serialized document.</returns> public static string Serialize(MobileDoc document) { var outputBuilder = new StringBuilder(); using var jsonWriter = new JsonTextWriter(new StringWriter(outputBuilder)); var documentListener = new MobileDocJsonTranslator(jsonWriter); var sectionListeners = new ISectionListener[] { new MarkupSectionJsonTranslator(jsonWriter), new CardSectionJsonTranslator(jsonWriter), new ListSectionJsonTranslator(jsonWriter), new ImageSectionJsonTranslator(jsonWriter), }; var walker = new MobileDocWalker(); walker.Walk(document, documentListener, sectionListeners); jsonWriter.Flush(); return(outputBuilder.ToString()); }
/// <summary> /// Writes the opening tag and the header for the document. /// </summary> /// <param name="mobileDoc"></param> public void EnterMobileDoc(MobileDoc mobileDoc) { _jsonWriter.WriteStartObject(); _jsonWriter.WritePropertyName("version"); _jsonWriter.WriteValue(mobileDoc.Version); }
/// <summary> /// Writes the final closing tag for the document. /// </summary> /// <param name="mobileDoc"></param> public void ExitMobileDoc(MobileDoc mobileDoc) { _jsonWriter.WriteEndObject(); }
public virtual void ExitMobileDoc(MobileDoc mobileDoc) { }
public virtual void EnterMobileDoc(MobileDoc mobileDoc) { }
/// <summary> /// Writes the final document content to the output. /// </summary> /// <param name="mobileDoc"></param> public override void ExitMobileDoc(MobileDoc mobileDoc) { _rootElement.WriteTo(_outputWriter); }