1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package Torello.Java.Build;

import Torello.Java.FileRW;
import Torello.Java.FileNode;
import Torello.Java.RTC;
import Torello.Java.Q;

import static Torello.Java.C.RESET;
import static Torello.Java.C.BRED;
import static Torello.Java.C.BGREEN;
import static Torello.Java.C.BYELLOW;

import java.util.Iterator;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import java.io.File;
import java.io.IOException;

class ExternalHTML
{
    private static final Pattern CODES = Pattern.compile("\\{@code [^\\}]+\\}");
    private static final Pattern LINKS = Pattern.compile("\\{@link ([\\w.]+)?#?([^\\}]+)?\\}");
    private static final Pattern JDSTARS = Pattern.compile("^\\s+\\*( |$)", Pattern.MULTILINE);

    static void cleanIt(String directoryName) throws IOException
    {
        Iterator<String> externalHTMLFiles = FileNode
            .createRoot(directoryName)
            .loadTree(-1, (File dir, String fileName) -> fileName.endsWith(".html"), null)
            .flattenJustFiles(RTC.FULLPATH_ITERATOR());

        while (externalHTMLFiles.hasNext())
        {
            String  fileName    = externalHTMLFiles.next();
            String  fileAsStr   = FileRW.loadFileToString(fileName);
            String  newFileStr  = fileAsStr;
            Matcher codes       = CODES.matcher(fileAsStr);

            System.out.println("Linting File: " + BYELLOW + fileName + RESET);

            if (! Q.YN("Shall We Lint?"))
            {
                if (! Q.YN("Continue to Next File? (say 'no' to Exit)")) System.exit(0);
                else continue;
            }



            // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
            // Replace the {@code ...}
            // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***

            while (codes.find())
            {
                String s = codes.group();

                String newS = 
                    "<CODE>" +
                    s.substring(7, s.length() - 1).replace("<", "&lt;").replace(">", "&gt;") +
                    "</CODE>";

                System.out.println(
                    BYELLOW + s.replace("\n", "\\n") +  RESET +
                    BRED + "\n    ===>\n" + RESET +
                    BGREEN + newS + RESET
                );

                if (Q.YN("Replace this one?"))
                {
                    newFileStr = codes.replaceFirst(newS);
                    codes.reset(newFileStr);
                }
            }


            // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
            // Replace the {@link ...}  
            // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
            //
            // **NOTE: The replacements are JUST HELPERS, THEY ARE NOT COMPLETE.
            //         This does about 95% of the typing for you, but it is not 100%
            //         The relative-path string (dot-dots) has not been added.

            Matcher links = LINKS.matcher(newFileStr);

            while (links.find())
            {
                String s = links.group();

                String file = links.group(1);
                String rel = links.group(2);

                String href = 
                    ((file != null) ? (file + ".html") : "") +
                    ((rel != null) ? ("#" + rel) : "");

                String linkText =
                    ((file != null) ? (file) : "") +
                    ((rel != null)
                        ? (((file != null) ? "." : "") + rel)
                        : "");

                String newS =
                    "<B><CODE><A HREF='" + href + "'>" + linkText + "</CODE></B></A>";

                System.out.println(
                    BYELLOW + s.replace("\n", "\\n") +  RESET +
                    BRED + "\n    ===>\n" + RESET +
                    BGREEN + newS + RESET
                );

                if (Q.YN("Replace this one?"))
                {
                    newFileStr = links.replaceFirst("\n\n" + newS + "\n\n");
                    links.reset(newFileStr);
                }
            }


            // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
            // Replace the leading  "     * "
            // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***

            newFileStr = JDSTARS.matcher(newFileStr).replaceAll("");


            // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
            // Save the file
            // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***

            System.out.println(
                newFileStr +
                "\n************************************************************\n"
            );

            if (Q.YN("Save this File?"))
            {
                FileRW.writeFile(newFileStr, fileName);
                System.out.println("Saved File: " + BYELLOW + fileName + RESET);
            }           
        }
    }
}