Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Index and/or TOC #16

Open
manticore-projects opened this issue Apr 15, 2022 · 0 comments
Open

Add Index and/or TOC #16

manticore-projects opened this issue Apr 15, 2022 · 0 comments

Comments

@manticore-projects
Copy link

manticore-projects commented Apr 15, 2022

Greetings.

Please, would it be possible to add an Index and/or TOC to the HTML output in order to navigate in very long HTML files with a lot of productions.

Example: http://manticore-projects.com/JSQLFormatter/syntax.html#

I have post-added this by parsing the XHTML file via XPATH. It works somehow, but yields in an extra step and the heavy JSOUP/SAXON dependencies.

    private static String stripTrailing(String s, String suffix) {
        if (s.endsWith(suffix))
            return s.substring(0, s.length() - suffix.length());
        else
            return s;
    }

    public static void insertTOC(File file) throws IOException {
        System.setProperty(W3CDom.XPathFactoryProperty, "net.sf.saxon.xpath.XPathFactoryImpl");

        Document doc = Jsoup.parse(file, "UTF-8", "", Parser.xmlParser());
        Elements elements = doc.selectXpath("//*[local-name()='a' and not(@href) and @name]");

        ArrayList<String> tocEntries = new ArrayList<>();
        TreeSet<String> indexEntries = new TreeSet<>();

        for (Element link : elements) {
            String key = stripTrailing(link.text(), ":");
            tocEntries.add(key);
            indexEntries.add(key);
        }

        Element tocElement = doc.body().prependElement("H1");
        tocElement.text("Table of Content:");
        tocElement.attr("style", "font-size: 14px; font-weight:bold");

        Element pElement = tocElement.appendElement("p");
        pElement.attr("style", "font-size: 11px; font-weight:normal");
        for (String s : tocEntries) {
            pElement.appendElement("a").attr("href", "#" + s).text(s);
            pElement.appendText(" ");
        }

        Element indexElement = doc.body().prependElement("H1");
        indexElement.text("Index:");
        indexElement.attr("style", "font-size: 14px; font-weight:bold");

        pElement = indexElement.appendElement("p");
        pElement.attr("style", "font-size: 11px; font-weight:normal");
        for (String s : indexEntries) {
            pElement.appendElement("a").attr("href", "#" + s).text(s);
            pElement.appendText(" ");
        }

        FileUtils.writeStringToFile(file, doc.outerHtml(), StandardCharsets.UTF_8);
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant