001/*
002 * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.  Oracle designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Oracle in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
022 * or visit www.oracle.com if you need additional information or have any
023 * questions.
024 */
025
026package org.jdrupes.mdoclet.internal.doclets.formats.html;
027
028import org.jdrupes.mdoclet.internal.doclets.formats.html.Navigation.PageMode;
029import org.jdrupes.mdoclet.internal.doclets.formats.html.markup.BodyContents;
030import org.jdrupes.mdoclet.internal.doclets.formats.html.markup.ContentBuilder;
031import org.jdrupes.mdoclet.internal.doclets.formats.html.markup.HtmlAttr;
032import org.jdrupes.mdoclet.internal.doclets.formats.html.markup.HtmlId;
033import org.jdrupes.mdoclet.internal.doclets.formats.html.markup.HtmlStyle;
034import org.jdrupes.mdoclet.internal.doclets.formats.html.markup.HtmlTree;
035import org.jdrupes.mdoclet.internal.doclets.formats.html.markup.TagName;
036import org.jdrupes.mdoclet.internal.doclets.formats.html.markup.Text;
037import org.jdrupes.mdoclet.internal.doclets.toolkit.Content;
038import org.jdrupes.mdoclet.internal.doclets.toolkit.util.DocFileIOException;
039import org.jdrupes.mdoclet.internal.doclets.toolkit.util.DocPath;
040import org.jdrupes.mdoclet.internal.doclets.toolkit.util.DocPaths;
041
042/**
043 * Generates the search landing page for the generated API documentation.
044 */
045public class SearchWriter extends HtmlDocletWriter {
046
047    /**
048     * Constructor to construct SearchWriter object.
049     * @param configuration the configuration
050     * @param filename file to be generated
051     */
052    public SearchWriter(HtmlConfiguration configuration, DocPath filename) {
053        super(configuration, filename);
054    }
055
056    /**
057     * Constructs the SearchWriter object and then use it to generate the search
058     * file. The name of the generated file is "search.html". The search file
059     * will get generated if and only if "-noindex" is not used on the command line.
060     *
061     * @param configuration the configuration
062     * @throws DocFileIOException if there is a problem while generating the documentation
063     */
064    public static void generate(HtmlConfiguration configuration)
065            throws DocFileIOException {
066        DocPath filename = DocPaths.SEARCH_PAGE;
067        SearchWriter searchWriter = new SearchWriter(configuration, filename);
068        searchWriter.generateSearchFile();
069    }
070
071    /**
072     * Generates the search file contents.
073     *
074     * @throws DocFileIOException if there is a problem while generating the documentation
075     */
076    protected void generateSearchFile() throws DocFileIOException {
077        String title = resources.getText("doclet.Window_Search_title");
078        HtmlTree body = getBody(getWindowTitle(title));
079        ContentBuilder searchFileContent = new ContentBuilder();
080        addSearchFileContents(searchFileContent);
081        body.add(new BodyContents()
082            .setHeader(getHeader(PageMode.SEARCH))
083            .addMainContent(searchFileContent)
084            .setFooter(getFooter()));
085        printHtmlDocument(null, "search", body);
086    }
087
088    /**
089     * Adds the search file contents to the content tree.
090     */
091    protected void addSearchFileContents(Content contentTree) {
092
093        String copyText = resources.getText("doclet.Copy_to_clipboard");
094        String copiedText = resources.getText("doclet.Copied_to_clipboard");
095        String copyUrlText = resources.getText("doclet.Copy_url_to_clipboard");
096        Content helpSection = Text.EMPTY;
097        // Suppress link to help page if no help page is generated or a custom
098        // help page is used.
099        HtmlOptions options = configuration.getOptions();
100        if (!options.noHelp() && options.helpFile().isEmpty()) {
101            Content helpLink = HtmlTree.A("help-doc.html#search",
102                contents.getContent("doclet.search.help_page_link"));
103            helpSection = HtmlTree.P(
104                contents.getContent("doclet.search.help_page_info", helpLink));
105        }
106
107        contentTree
108            .add(HtmlTree.HEADING(Headings.PAGE_TITLE_HEADING, HtmlStyle.title,
109                contents.getContent("doclet.search.main_heading")))
110            .add(HtmlTree
111                .DIV(HtmlTree.INPUT("text", HtmlId.of("page-search-input"))
112                    .put(HtmlAttr.PLACEHOLDER,
113                        resources.getText("doclet.search_placeholder")))
114                .add(HtmlTree.INPUT("reset", HtmlId.of("page-search-reset"))
115                    .put(HtmlAttr.VALUE,
116                        resources.getText("doclet.search_reset"))
117                    .put(HtmlAttr.STYLE, "margin: 6px;"))
118                .add(HtmlTree.DETAILS(HtmlStyle.pageSearchDetails)
119                    .add(HtmlTree
120                        .SUMMARY(contents.getContent("doclet.search.show_more"))
121                        .setId(HtmlId.of("page-search-expand")))))
122            .add(HtmlTree.DIV(HtmlStyle.pageSearchInfo, helpSection)
123                .add(HtmlTree
124                    .P(contents.getContent("doclet.search.keyboard_info")))
125                .add(HtmlTree
126                    .P(contents.getContent("doclet.search.browser_info")))
127                .add(HtmlTree.SPAN(Text.of("link"))
128                    .setId(HtmlId.of("page-search-link")))
129                .add(new HtmlTree(TagName.BUTTON)
130                    .add(new HtmlTree(TagName.IMG)
131                        .put(HtmlAttr.SRC,
132                            pathToRoot.resolve(DocPaths.CLIPBOARD_SVG)
133                                .getPath())
134                        .put(HtmlAttr.ALT, copyUrlText))
135                    .add(HtmlTree.SPAN(Text.of(copyText))
136                        .put(HtmlAttr.DATA_COPIED, copiedText))
137                    .addStyle(HtmlStyle.copy)
138                    .put(HtmlAttr.ARIA_LABEL, copyUrlText)
139                    .setId(HtmlId.of("page-search-copy")))
140                .add(HtmlTree
141                    .P(HtmlTree.INPUT("checkbox", HtmlId.of("search-redirect")))
142                    .add(HtmlTree.LABEL("search-redirect",
143                        contents.getContent("doclet.search.redirect")))))
144            .add(new HtmlTree(TagName.P)
145                .setId(HtmlId.of("page-search-notify"))
146                .add(contents.getContent("doclet.search.loading")))
147            .add(HtmlTree.DIV(new HtmlTree(TagName.DIV)
148                .setId(HtmlId.of("result-container"))
149                .addUnchecked(Text.EMPTY))
150                .setId(HtmlId.of("result-section"))
151                .put(HtmlAttr.STYLE, "display: none;")
152                .add(HtmlTree.SCRIPT(
153                    pathToRoot.resolve(DocPaths.SEARCH_PAGE_JS).getPath())));
154    }
155}