/*
 * Copyright 2014-2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package org.dbflute.intro.app.logic.document;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

import org.lastaflute.core.exception.LaSystemException;

/**
 * @author deco
 * @author jflute
 */
public class DocumentDisplayLogic {

    public String modifyHtmlForIntroOpening(String clientProject, File file) {
        try (BufferedReader br = Files.newBufferedReader(file.toPath())) {
            boolean addIntroExecuteTag = false;
            final StringBuilder sb = new StringBuilder();
            while (true) {
                String line = br.readLine();
                if (line == null) {
                    break;
                }
                if (!addIntroExecuteTag && line.contains("<script>")) {
                    line = "<input id=\"intro_opening\" type=\"hidden\" />" + line;
                    addIntroExecuteTag = true;
                }
                if (line.contains("<a href=\"./history-" + clientProject + ".html\">to HistoryHTML</a>")) {
                    line = "<a href=\"/api/document/" + clientProject + "/historyhtml\">to HistoryHTML</a>";
                }
                if (line.contains("<a href=\"./schema-" + clientProject + ".html\">to SchemaHTML</a>")) {
                    line = "<a href=\"/api/document/" + clientProject + "/schemahtml\">to SchemaHTML</a>";
                }
                if (line.contains("<a href=\"./properties-" + clientProject + ".html\">to PropertiesHTML</a>")) {
                    line = "<a href=\"/api/document/" + clientProject + "/propertieshtml\">to PropertiesHTML</a>";
                }
                sb.append(line).append("\n");
            }
            return sb.toString();
        } catch (IOException e) {
            throw new LaSystemException("Cannot mark intro opening at document HTML: " + file, e);
        }
    }
}

@@ -769,16 +769,110 @@
 Files;%0A%0A
+import javax.annotation.Resource;%0A%0Aimport org.dbflute.intro.app.logic.intro.IntroSystemLogic;%0A
 import o
@@ -919,16 +919,16 @@
 eption;%0A
-
 %0A/**%0A * 
@@ -999,16 +999,78 @@
 ogic %7B%0A%0A
+    @Resource%0A    private IntroSystemLogic introSystemLogic;%0A%0A
     publ
@@ -1263,16 +1263,141 @@
  false;%0A
+            boolean addIntroServerTag = false;%0A            boolean isDecommentServer = introSystemLogic.isDecommentServer();%0A
         
@@ -1755,16 +1755,16 @@
 + line;%0A
-
         
@@ -1812,32 +1812,275 @@
               %7D%0A
+                if (!addIntroServerTag && isDecommentServer && line.contains(%22%3Cscript%3E%22)) %7B%0A                    line = %22%3Cinput id=%5C%22decomment_server%5C%22 type=%5C%22hidden%5C%22 /%3E%22 + line;%0A                    addIntroServerTag = true;%0A                %7D%0A
                 

Add tag if run as decomment server