Export HTML snippet

7 posts, 0 answers
  1. Ari
    Ari avatar
    8 posts
    Member since:
    Nov 2008

    Posted 04 Aug 2010 Link to this post

    Is it possible to export the content as an html snippet, the way your asp.net editor does?
    The HtmlFormatProvider exports a full html document complete with DOCTYPE, etc.
    This is a lot of bloat for my scenario, and actually is technically invalid as I display the saved content in a standard webpage.

    Thanks,
    Ari
  2. Ivailo Karamanolev
    Admin
    Ivailo Karamanolev avatar
    276 posts

    Posted 06 Aug 2010 Link to this post

    Hello Ari,

    Currently, the HtmlFormatProvider is designed to export valid XHTML documents, in the sense the specification defines "document", which includes doctype and other declarations. For your case, some processing will be needed. As our documents are valid XML, you should extract the CSS styles and the content of the body tag and place them in your page.
    Let us know if you have any other questions.

    Best wishes,
    Ivailo
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. DevCraft banner
  4. Roland Klug
    Roland Klug avatar
    25 posts
    Member since:
    Dec 2002

    Posted 11 Aug 2010 Link to this post

    Hi,

    we had the same problem and so we implemented our own provider. Here ist our very simple implementation which has to be extended with further features.

    public class SimpleHtmlFormatProvider : IDocumentFormatProvider
    {
        #region Implementation of IDocumentFormatProvider
     
        public RadDocument Import(Stream input)
        {
            return new HtmlFormatProvider().Import(input);
        }
     
        public void Export(RadDocument document, Stream output)
        {
            var html = GetHtmlOutput(document);
            var bytes = Encoding.UTF8.GetBytes(html);
     
            output.Write(bytes, 0, bytes.Length);
        }
     
        public string Name
        {
            get { throw new NotImplementedException(); }
        }
     
        public IEnumerable<string> SupportedExtensions
        {
            get
            {
                yield return ".htm";
                yield return ".html";
            }
        }
     
        public bool CanImport
        {
            get { return true; }
        }
     
        public bool CanExport
        {
            get { return true; }
        }
     
        private static String GetHtmlOutput(RadDocument document)
        {
            var html = String.Empty;
     
            foreach (var section in document.Sections)
            {
                foreach (var paragraph in section.Paragraphs)
                {
                    if (paragraph.IsEmpty)
                    {
                        html += "<br />";
                        continue;
                    }
     
                    html += "<p>";
     
                    foreach (var inline in paragraph.Inlines)
                    {
                        if (!(inline is Span)) continue;
     
                        var span = inline as Span;
                        var openTag = String.Empty;
                        var closeTag = String.Empty;
                        var content = HttpUtility.HtmlEncode(span.Text);
                        var style = String.Empty;
     
                        if (inline is Hyperlink)
                        {
                            var link = inline as Hyperlink;
                             
                            openTag = string.Format("<a href=\"{0}\" target=\"{1}\"{{0}}>", link.URL, GetTarget(link.Target));
                            closeTag = "</a>";
                        }
     
                        if (String.IsNullOrWhiteSpace(content)) continue;
     
                        if (span.FontWeight != FontWeights.Normal)
                        {
                            openTag += "<strong>";
                            closeTag = "</strong>" + closeTag;
                        }
     
                        if (span.Underline && !(inline is Hyperlink))
                        {
                            style = " text-decoration: underline;";
                        }
     
                        if (span.FontStyle == FontStyles.Italic)
                        {
                            openTag += "<i>";
                            closeTag = "</i>" + closeTag;
                        }
     
                        if (!String.IsNullOrEmpty(style))
                        {
                            style = string.Format(" style=\"{0}\"", style.Trim());
     
                            if (inline is Hyperlink)
                            {
                                openTag = String.Format(openTag, style);
                            }
                            else
                            {
                                openTag += string.Format("<span{0}>", style);
                                closeTag = closeTag + "</span>";
                            }
                        }
                        else if (inline is Hyperlink)
                        {
                            openTag = string.Format(openTag, String.Empty);
                        }
     
                        html += openTag + content + closeTag;
                    }
     
                    html += "</p>";
                }
            }
     
            html = html.Replace("\n", "<br />").Replace("\r", string.Empty);
     
            return Regex.IsMatch(html, @"^<p>[\s]*</p>$") ? String.Empty : html;
        }
     
        private static String GetTarget(HyperlinkTargets target)
        {
            switch (target)
            {
                default:
                case HyperlinkTargets.Self:
                    return "_self";
                case HyperlinkTargets.Blank:
                    return "_blank";
            }
        }
     
        #endregion
    }

    Best regards
  5. Ivailo Karamanolev
    Admin
    Ivailo Karamanolev avatar
    276 posts

    Posted 13 Aug 2010 Link to this post

    Hello Roland Klug,

    We have received some requests to support HTML exporting of document excerpts, so we have added this to our to-do list. It will be available in the next release (at the latest - Q3 2010).
    Get back to us if you have other questions.

    Greetings,
    Ivailo
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  6. Cyprian
    Cyprian avatar
    1 posts
    Member since:
    Nov 2010

    Posted 30 Nov 2010 Link to this post

    Did "Export HTML snippet" mis Q3 release? We are interested in this functionality to avoid writing out own custom provider.
  7. Ivailo Karamanolev
    Admin
    Ivailo Karamanolev avatar
    276 posts

    Posted 30 Nov 2010 Link to this post

    Hi Cyprian,

    We have included this functionality in the Q3 release. You can access it like this:
    HtmlExportSettings exportSettings = new HtmlExportSettings();
    exportSettings.DocumentExportLevel = DocumentExportLevel.Fragment;
    HtmlFormatProvider formatProvider = new HtmlFormatProvider();
    formatProvider.ExportSettings = exportSettings;
    .. formatProvider.Export(document); ...
    Let us know if you require further assistance.

    Best wishes,
    Ivailo
    the Telerik team
    Browse the videos here>> to help you get started with RadControls for Silverlight
  8. Chris
    Chris avatar
    3 posts
    Member since:
    Sep 2011

    Posted 01 Sep 2011 Link to this post

    Hi Roland,

    Did you ever expand upon this solution (i.e. to handle other elements, such as <ul>, colours, etc.)?  

    We need to get some really clean HTML from the editor (see here) and this may have to be the way forward.

    Thanks in advance for any help!

    Chris.
Back to Top
DevCraft banner