package org.jbake.util;

import org.jbake.app.Crawler.Attributes;
import org.jbake.app.configuration.JBakeConfiguration;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.Map;

/**
 * @author Manik Magar
 */
public class HtmlUtil {

    /**
     * Image paths are specified as w.r.t. assets folder. This function prefix site host to all img src except
     * the ones that starts with http://, https://.
     * <p>
     * If image path starts with "./", i.e. relative to the source file, then it first replace that with output file directory and the add site host.
     *
     * @param fileContents  Map representing file contents
     * @param configuration Configuration object
     */
    public static void fixImageSourceUrls(Map<String, Object> fileContents, JBakeConfiguration configuration) {

        String htmlContent = fileContents.get(Attributes.BODY).toString();

        boolean prependSiteHost = configuration.getImgPathPrependHost();
        String siteHost = configuration.getSiteHost();
        String uri = fileContents.get(Attributes.URI).toString();

        if (fileContents.get(Attributes.NO_EXTENSION_URI) != null) {
            uri = fileContents.get(Attributes.NO_EXTENSION_URI).toString();
            uri = removeTrailingSlash(uri);
        }

        if (uri.contains("/")) {
            uri = removeFilename(uri);
        }

        Document document = Jsoup.parseBodyFragment(htmlContent);
        Elements allImgs = document.getElementsByTag("img");

        for (Element img : allImgs) {
            String source = img.attr("src");

            // Now add the root path
            if (!source.startsWith("http://") && !source.startsWith("https://")) {

                if (isRelative(source)) {
                    source = uri + source.replaceFirst("\\./", "");
                }

                if (prependSiteHost) {
                    if (!siteHost.endsWith("/") && isRelative(source)) {
                        siteHost = siteHost.concat("/");
                    }
                    source = siteHost + source;
                }

                img.attr("src", source);
            }
        }

        //Use body().html() to prevent adding <body></body> from parsed fragment.
        fileContents.put(Attributes.BODY, document.body().html());
    }

    private static String removeFilename(String uri) {
        uri = uri.substring(0, uri.lastIndexOf("/") + 1);
        return uri;
    }

    private static String removeTrailingSlash(String uri) {
        if (uri.endsWith("/")) {
            uri = uri.substring(0, uri.length() - 1);
        }
        return uri;
    }

    private static boolean isRelative(String source) {
        return !source.startsWith("/");
    }
}

@@ -313,24 +313,56 @@
 HtmlUtil %7B%0A%0A
+    private HtmlUtil() %7B%0A    %7D%0A%0A
     /**%0A    
@@ -912,25 +912,24 @@
 guration) %7B%0A
-%0A
         Stri
@@ -983,33 +983,32 @@
 DY).toString();%0A
-%0A
         boolean 
@@ -1111,32 +1111,565 @@
 .getSiteHost();%0A
+        String uri = getDocumentUri(fileContents);%0A%0A        Document document = Jsoup.parseBodyFragment(htmlContent);%0A        Elements allImgs = document.getElementsByTag(%22img%22);%0A%0A        for (Element img : allImgs) %7B%0A            transformImageSource(img, uri, siteHost, prependSiteHost);%0A        %7D%0A%0A        //Use body().html() to prevent adding %3Cbody%3E%3C/body%3E from parsed fragment.%0A        fileContents.put(Attributes.BODY, document.body().html());%0A    %7D%0A%0A    private static String getDocumentUri(Map%3CString, Object%3E fileContents) %7B%0A
         String u
@@ -2001,25 +2001,24 @@
       %7D%0A
-%0A
         
 Document
@@ -2013,170 +2013,141 @@
     
-Document document = Jsoup.parseBodyFragment(htmlContent)
+return uri
 ;%0A    
+%7D%0A%0A
     
-Elements allImgs = document.getElementsByTag(%22img%22);%0A%0A        for (Element img : allImgs) %7B%0A    
+private static void transformImageSource(Element img, String uri, String siteHost, boolean prependSiteHost) %7B%0A
     
@@ -2192,20 +2192,16 @@
         
-  
-  
 // Now a
@@ -2217,20 +2217,16 @@
 ot path%0A
-    
         
@@ -2305,28 +2305,24 @@
             
- 
-   
 if (isRelati
@@ -2327,36 +2327,32 @@
 tive(source)) %7B%0A
-    
                 
@@ -2407,35 +2407,27 @@
             
-    %7D%0A%0A    
+%7D%0A%0A
             
@@ -2461,28 +2461,24 @@
             
-    
 if (!siteHos
@@ -2538,20 +2538,16 @@
         
-    
 siteHost
@@ -2583,32 +2583,26 @@
             
-    %7D%0A  
+%7D%0A
             
@@ -2597,34 +2597,32 @@
                 
-  
 source = siteHos
@@ -2637,36 +2637,32 @@
 ce;%0A            
-    
 %7D%0A%0A             
@@ -2660,20 +2660,16 @@
         
-  
-  
 img.attr
@@ -2697,173 +2697,9 @@
     
-    %7D%0A        %7D%0A%0A        //Use body().html() to prevent adding %3Cbody%3E%3C/body%3E from parsed fragment.%0A        fileContents.put(Attributes.BODY, document.body().html());
+%7D
 %0A   
@@ -2804,19 +2804,19 @@
 IndexOf(
-%22/%22
+'/'
 ) + 1);%0A

refactor HtmlUtil