
package flow.netbeans.markdown;

import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import org.pegdown.LinkRenderer;
import org.pegdown.ast.AutoLinkNode;
import org.pegdown.ast.ExpLinkNode;
import org.pegdown.ast.MailLinkNode;
import org.pegdown.ast.RefLinkNode;
import org.pegdown.ast.WikiLinkNode;

/**
 *
 * @author Holger Stenger
 */
public class PreviewLinkRenderer extends LinkRenderer {
    private final URL baseUrl;

    private final boolean resolveLinkUrls;

    public PreviewLinkRenderer(URL baseUrl, boolean resolveLinkUrls) {
        super();
        this.baseUrl = baseUrl;
        this.resolveLinkUrls = resolveLinkUrls;
    }

    @Override
    public LinkRenderer.Rendering render(AutoLinkNode node) {
        return transform(super.render(node));
    }

    @Override
    public LinkRenderer.Rendering render(MailLinkNode node) {
        return transform(super.render(node));
    }

    @Override
    public LinkRenderer.Rendering render(WikiLinkNode node) {
        return transform(super.render(node));
    }

    @Override
    public LinkRenderer.Rendering render(ExpLinkNode node, String text) {
        return transform(super.render(node, text));
    }

    @Override
    public LinkRenderer.Rendering render(RefLinkNode node, String url, String title, String text) {
        return transform(super.render(node, url, title, text));
    }

    private String resolveUrl(final String uriText) {
        try {
            return baseUrl.toURI().resolve(uriText).toURL().toExternalForm();
        }
        catch (URISyntaxException ex) {
        }
        catch (MalformedURLException ex) {
        }
        return uriText;
    }

    /**
     * Transforms the rendered href URL
     *
     * @param rendering
     * @return
     */
    private LinkRenderer.Rendering transform(LinkRenderer.Rendering rendering) {
        if (resolveLinkUrls && (rendering.href != null)) {
            LinkRenderer.Rendering resolvedRendering = new LinkRenderer.Rendering(
                resolveUrl(rendering.href), rendering.text);
            for (LinkRenderer.Attribute attr : rendering.attributes) {
              resolvedRendering.withAttribute(attr);
            }
            return resolvedRendering;
        } else {
          return rendering;
        }
    }

}

@@ -1,9 +1,8 @@
-%0A
 package 
@@ -349,170 +349,804 @@
 *%0A *
-%0A * @author Holger Stenger%0A */%0Apublic class PreviewLinkRenderer extends LinkRenderer %7B%0A    private final URL baseUrl;%0A%0A    private final boolean resolveLinkUrls;%0A
+ Turns pegdown AST link nodes into rendered links, optionally resolving relative URLs to%0A * absolute URLs using a given base URL.%0A *%0A * @author Holger Stenger%0A */%0Apublic class PreviewLinkRenderer extends LinkRenderer %7B%0A%0A    /** The base URL against which absolute URLs should be resolved. */%0A    private final URL baseUrl;%0A%0A    /** Indicates whether relative URLs should be resolved against the base URL. */%0A    private final boolean resolveLinkUrls;%0A%0A    /**%0A     * Create a new link renderer that will optionally resolve relative URLs to absolute URLs.%0A     *%0A     * @param baseUrl the base URL against which relative URLs will be resolved.%0A     * @param resolveLinkUrls if true, relative URLs in rendered links will be resolved to%0A     *          absolute URLs with the given base URL.%0A     */
 %0A   
@@ -2032,24 +2032,443 @@
 t));%0A    %7D%0A%0A
+    /**%0A     * Resolves relative URLs against a base URL to return an absolute URL.%0A     *%0A     * @param uriText the possibly relative URL to resolve%0A     * @return the absolute form of the provided relative URL; if the URL is already absolute, or if it%0A     *          would otherwise result in an invalid URL when resolved against the base URL, then the%0A     *          original string is returned unmodified%0A     */%0A
     private 
@@ -2788,16 +2788,23 @@
 red 
+link's 
 href URL
 %0A   
@@ -2803,54 +2803,395 @@
  URL
-%0A     *%0A     * @param rendering%0A     * @return
+ to its absolute form using a base URL,%0A     * if URL resolution is turned on.%0A     *%0A     * @param rendering the previously rendered link%0A     * @return if URL resolution is turned on and the link is relative, then the rendered%0A     *          link with the href URL in its absolute form using the base URL provided to%0A     *          this renderer; otherwise the original rendered link
 %0A   

add some javadoc