<commit_before>package com.github.cukedoctor.extension;

import static com.github.cukedoctor.extension.CukedoctorExtensionRegistry.*;

import org.asciidoctor.ast.Document;
import org.asciidoctor.extension.Postprocessor;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;

import java.util.Map;

/**
 * Created by pestano on 20/07/15.
 * adds javascript functions to html documentation
 */
public class CukedoctorScriptExtension extends Postprocessor{

    public CukedoctorScriptExtension(Map<String, Object> config) {
        super(config);
    }

    @Override
    public String process(Document document, String output) {
        if(document.basebackend("html") ){
            org.jsoup.nodes.Document doc = Jsoup.parse(output, "UTF-8");

            Element contentElement = doc.getElementById("footer");
                if(System.getProperty(FILTER_DISABLE_EXT_KEY) == null) {
                        addSearchScript(contentElement);
                }
                if(System.getProperty(MINMAX_DISABLE_EXT_KEY) == null){
                        addMinMaxScript(contentElement);
                }
                if(System.getProperty(THEME_DISABLE_EXT_KEY) == null){
                        addThemeScript(contentElement);
                }
            return doc.html();

        }else{
            return output;
        }
    }

        private void addThemeScript(Element contentElement) {
             String themeScript = " <script type=\"text/javascript\">  \n" +
                     "\n" +
                     "document.addEventListener(\"DOMContentLoaded\", function(event) { \n" +
                     "  themefy();\n" +
                     "});\n"+
                     "\tfunction themefy() {\n" +
                     " \t\t\t\t\t\tvar theme = getThemeFromQueryParameters() || 'asciidoctor';\n" +
                     " \t\t\t\t\t\tvar themeLink = document.createElement('link');\n" +
                     "\t\t\t\t\t if('asciidoctor' !== theme) {" +
                     " \t\t\t\t\t\tthemeLink.rel = 'stylesheet';\n" +
                     " \t\t\t\t\t\tthemeLink.id = 'asciidoctor-style';\n" +
                     " \t\t\t\t\t\tthemeLink.href = 'themes/' + theme + '.css';\n" +
                     " \t\t\t\t\t\t document.head.appendChild(themeLink);\n" +
                     "\t\t\t\t }" +
                     "}\n" +
                     "\n" +
                     "\n" +
                     "\tfunction getThemeFromQueryParameters() {\n" +
                     "\t\tvar query = location.search.substr(1);\n" +
                     "\t\tvar result = 'asciidoctor';\n" +
                     "\tquery.split(\"&\").forEach(function (part) {\n" +
                     "\t\t\t\t// part can be empty\n" +
                     "\t\t\t\tif (part) {\n" +
                     "\t\t\t\t\tvar item = part.split(\"=\");\n" +
                     "\t\t\t\t\tvar key = item[0];\n" +
                     "\t\t\t\tvar value = item[1];\n" +
                     "\t\t\t\tif (typeof value !== 'undefined') {\n" +
                     "\t\t\t\t\t\tif(key == 'theme'){\n" +
                     "\t\t\t\t\t\t\t\tresult = value;\n" +
                     "\t\t\t\t\t\t\t\t}\n" +
                     "\t\t\t}\n" +
                     "\t\t}\n" +
                     " \t});\n" +
                     "\tif(!result){\n" +
                     "\t\tresult = 'asciidoctor';\n" +
                     "\t\t\n\t}\n"+
                     "\tdocument.getElementById('themes').value = result;  \n" +
                     "\tvar els = document.querySelector('select[name=\"select\"] option[value=\"' + result + '\"]');  \n" +
                     "\tif(els){\n" +
                     "    els.selected = true;\n" +
                     "\t}"+
                     "\treturn result;\n" +
                     "}\n" +
                     " \n" +
                     "</script>      ";

                contentElement.after(themeScript);
        }

        private void addMinMaxScript(Element contentElement) {
        String minMaxScript = "<script type=\"text/javascript\">\n" +
                "\tfunction showFeatureScenarios(featureId){\n" +
                "\t\tvar element = document.getElementById(featureId).parentNode;\n" +
                "\t\tfor (var i = 0; i < element.childNodes.length; i++) {\n" +
                "    \t\tif (element.childNodes[i].className == \"sect3\" || element.childNodes[i].className == \"fa fa-minus-square fa-fw\") {\n" +
                "      \t\t     element.childNodes[i].style.display = 'inline';\n" +
                "    \t\t\t}\n" +
                "    \t\tif (element.childNodes[i].className == \"sidebarblock\") {\n" +
                "      \t\t     element.childNodes[i].style.display = 'block';\n" +
                "    \t\t\t}\t\t\t        \n" +
                "\t\t}\n" +
                "\t\t \n" +
                " \t\tdocument.getElementById(featureId).childNodes[0].click();\n" +
                "\t}\n" +
                "\n" +
                "\tfunction hideFeatureScenarios(featureId){\n" +
                "\t\tvar element = document.getElementById(featureId).parentNode;\n" +
                "\t\tfor (var i = 0; i < element.childNodes.length; i++) {\n" +
                "    \t\tif (element.childNodes[i].className == \"sect3\" || element.childNodes[i].className == \"sidebarblock\") { \n" +
                "      \t\t     element.childNodes[i].style.display = 'none';\t   \t\t    \n" +
                "                 \t}\n" +
                "\t\t      \n" +
                "\t\t}\n" +
                "\t   document.getElementById(featureId).childNodes[0].click();\n" +
                " \t}\n" +
                "\n" +
                "</script>";
        contentElement.after(minMaxScript);
    }

    private void addSearchScript(Element contentElement) {
        String searchScripts = "<script type = \"text/javascript\" >\n" +
                "var allLevel2ListItens = null;\n" +
                "\n" +
                "function searchFeature(criteria) {\n" +
                "    if (criteria != null && criteria.length >= 3) {\n" +
                "        var sect2List = document.getElementsByClassName(\"sect2\");\n" +
                "        var firstMatch = null;\n" +
                "        if (sect2List != null) {\n" +
                "            for (var i = 0; i < sect2List.length; i++) {\n" +
                "                var h3 = null;\n" +<commit_msg>refs #52<commit_after>package com.github.cukedoctor.extension;

import static com.github.cukedoctor.extension.CukedoctorExtensionRegistry.*;

import org.asciidoctor.ast.Document;
import org.asciidoctor.extension.Postprocessor;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;

import java.util.Map;

/**
 * Created by pestano on 20/07/15.
 * adds javascript functions to html documentation
 */
public class CukedoctorScriptExtension extends Postprocessor{

    public CukedoctorScriptExtension(Map<String, Object> config) {
        super(config);
    }

    @Override
    public String process(Document document, String output) {
        if(document.basebackend("html") ){
            org.jsoup.nodes.Document doc = Jsoup.parse(output, "UTF-8");

            Element contentElement = doc.getElementById("footer");
                if(System.getProperty(FILTER_DISABLE_EXT_KEY) == null) {
                        addSearchScript(contentElement);
                }
                if(System.getProperty(MINMAX_DISABLE_EXT_KEY) == null){
                        addMinMaxScript(contentElement);
                }
                if(System.getProperty(THEME_DISABLE_EXT_KEY) == null){
                        addThemeScript(contentElement);
                }
            return doc.html();

        }else{
            return output;
        }
    }

        private void addThemeScript(Element contentElement) {
             String themeScript = " <script type=\"text/javascript\">  \n" +
                     "\n" +
                     "document.addEventListener(\"DOMContentLoaded\", function(event) { \n" +
                     "  themefy();\n" +
                     "});\n"+
                     "\tfunction themefy() {\n" +
                     " \t\t\t\t\t\tvar theme = getThemeFromQueryParameters() || 'asciidoctor';\n" +
                     " \t\t\t\t\t\tvar themeLink = document.createElement('link');\n" +
                     "\t\t\t\t\t if('asciidoctor' !== theme) {" +
                     " \t\t\t\t\t\tthemeLink.rel = 'stylesheet';\n" +
                     " \t\t\t\t\t\tthemeLink.id = 'asciidoctor-style';\n" +
                     " \t\t\t\t\t\tthemeLink.href = 'themes/' + theme + '.css';\n" +
                     " \t\t\t\t\t\t document.head.appendChild(themeLink);\n" +
                     "\t\t\t\t }" +
                     "}\n" +
                     "\n" +
                     "\n" +
                     "\tfunction getThemeFromQueryParameters() {\n" +
                     "\t\tvar query = location.search.substr(1);\n" +
                     "\t\tvar result = 'asciidoctor';\n" +
                     "\tquery.split(\"&\").forEach(function (part) {\n" +
                     "\t\t\t\t// part can be empty\n" +
                     "\t\t\t\tif (part) {\n" +
                     "\t\t\t\t\tvar item = part.split(\"=\");\n" +
                     "\t\t\t\t\tvar key = item[0];\n" +
                     "\t\t\t\tvar value = item[1];\n" +
                     "\t\t\t\tif (typeof value !== 'undefined') {\n" +
                     "\t\t\t\t\t\tif(key == 'theme'){\n" +
                     "\t\t\t\t\t\t\t\tresult = value;\n" +
                     "\t\t\t\t\t\t\t\t}\n" +
                     "\t\t\t}\n" +
                     "\t\t}\n" +
                     " \t});\n" +
                     "\tif(!result){\n" +
                     "\t\tresult = 'asciidoctor';\n" +
                     "\t\t\n\t}\n"+
                     "\tdocument.getElementById('themes').value = result;  \n" +
                     "\tvar els = document.querySelector('select[name=\"select\"] option[value=\"' + result + '\"]');  \n" +
                     "\tif(els){\n" +
                     "    els.selected = true;\n" +
                     "\t}"+
                     "\treturn result;\n" +
                     "}\n" +
                     " \n" +
                     "</script>      ";

                contentElement.after(themeScript);
        }

        private void addMinMaxScript(Element contentElement) {
        String minMaxScript = "<script type=\"text/javascript\">\n" +
                "\tfunction showFeatureScenarios(featureId){\n" +
                "\t\tvar element = document.getElementById(featureId).parentNode;\n" +
                "\t\tfor (var i = 0; i < element.childNodes.length; i++) {\n" +
                "    \t\tif (element.childNodes[i].className == \"sect3\" || element.childNodes[i].className == \"fa fa-minus-square fa-fw\") {\n" +
                "      \t\t     element.childNodes[i].style.display = 'inline';\n" +
                "    \t\t\t}\n" +
                "    \t\tif (element.childNodes[i].className == \"sidebarblock\") {\n" +
                "      \t\t     element.childNodes[i].style.display = 'block';\n" +
                "    \t\t\t}\t\t\t        \n" +
                "\t\t}\n" +
                "\t\t \n" +
                " \t\tdocument.getElementById(featureId).children[0].click();\n" +
                "\t}\n" +
                "\n" +
                "\tfunction hideFeatureScenarios(featureId){\n" +
                "\t\tvar element = document.getElementById(featureId).parentNode;\n" +
                "\t\tfor (var i = 0; i < element.childNodes.length; i++) {\n" +
                "    \t\tif (element.childNodes[i].className == \"sect3\" || element.childNodes[i].className == \"sidebarblock\") { \n" +
                "      \t\t     element.childNodes[i].style.display = 'none';\t   \t\t    \n" +
                "                 \t}\n" +
                "\t\t      \n" +
                "\t\t}\n" +
                "\t   document.getElementById(featureId).children[0].click();\n" +
                " \t}\n" +
                "\n" +
                "</script>";
        contentElement.after(minMaxScript);
    }

    private void addSearchScript(Element contentElement) {
        String searchScripts = "<script type = \"text/javascript\" >\n" +
                "var allLevel2ListItens = null;\n" +
                "\n" +
                "function searchFeature(criteria) {\n" +
                "    if (criteria != null && criteria.length >= 3) {\n" +
                "        var sect2List = document.getElementsByClassName(\"sect2\");\n" +
                "        var firstMatch = null;\n" +
                "        if (sect2List != null) {\n" +
                "            for (var i = 0; i < sect2List.length; i++) {\n" +
                "                var h3 = null;\n" +
{'fraction_non_alphanumeric': 0.16510855288572973, 'fraction_numerical': 0.003863092018851889, 'mean_word_length': 1.2692847124824684, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1224990', 'n_tokens_mistral': 4021, 'n_tokens_neox': 3806, 'n_words': 597}