001/* 002 * Copyright (c) 1997, 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.markup; 027 028import org.jdrupes.mdoclet.internal.doclets.toolkit.Content; 029import org.jdrupes.mdoclet.internal.doclets.toolkit.util.DocLink; 030import org.jdrupes.mdoclet.internal.doclets.toolkit.util.DocPath; 031 032/** 033 * Factory for HTML A elements: links (with a {@code href} attribute). 034 */ 035public class Links { 036 037 private final DocPath file; 038 039 /** 040 * Creates a {@code Links} object for a specific file. 041 * Links to other files will be made relative to this file where possible. 042 * 043 * @param file the file 044 */ 045 public Links(DocPath file) { 046 this.file = file; 047 } 048 049 /** 050 * Creates a link of the form {@code <a href="#id">label</a>}. 051 * 052 * @param id the position of the link in the file 053 * @param label the content for the link 054 * @return the link 055 */ 056 public Content createLink(HtmlId id, Content label) { 057 DocLink l = DocLink.fragment(id.name()); 058 return createLink(l, label, ""); 059 } 060 061 /** 062 * Creates a link of the form {@code <a href="#id">label</a>} if {@code link} 063 * is {@code true}, or else just returns {@code label}. 064 * 065 * @param id the position of the link in the file 066 * @param label the content for the link 067 * @param link whether to create a link or just return the label 068 * @return the link or just the label 069 */ 070 public Content createLink(HtmlId id, Content label, boolean link) { 071 return link ? createLink(id, label) : label; 072 } 073 074 /** 075 * Creates a link of the form {@code <a href="#id" title="title">label</a>}. 076 * 077 * @param id the id to which the link will be created 078 * @param label the content for the link 079 * @param title the title for the link 080 * 081 * @return the link 082 */ 083 public Content createLink(HtmlId id, Content label, String title) { 084 DocLink l = DocLink.fragment(id.name()); 085 return createLink(l, label, title); 086 } 087 088 /** 089 * Creates a link of the form {@code <a href="path">label</a>}. 090 * 091 * @param path the path for the link 092 * @param label the content for the link 093 * @return the link 094 */ 095 public Content createLink(DocPath path, String label) { 096 return createLink(path, Text.of(label), null, ""); 097 } 098 099 /** 100 * Creates a link of the form {@code <a href="path">label</a>}. 101 * 102 * @param path the path for the link 103 * @param label the content for the link 104 * @return the link 105 */ 106 public Content createLink(DocPath path, Content label) { 107 return createLink(path, label, ""); 108 } 109 110 /** 111 * Creates a link of the form {@code <a href="path" title="title">label</a>}. 112 * If {@code style} is not null, it will be added as {@code class="style"} to the link. 113 * 114 * @param path the path for the link 115 * @param label the content for the link 116 * @param style the style for the link, or null 117 * @param title the title for the link 118 * @return the link 119 */ 120 public Content createLink(DocPath path, Content label, HtmlStyle style, 121 String title) { 122 return createLink(new DocLink(path), label, style, title); 123 } 124 125 /** 126 * Creates a link of the form {@code <a href="path" title="title">label</a>}. 127 * 128 * @param path the path for the link 129 * @param label the content for the link 130 * @param title the title for the link 131 * @return the link 132 */ 133 public Content createLink(DocPath path, Content label, String title) { 134 return createLink(new DocLink(path), label, title); 135 } 136 137 /** 138 * Creates a link of the form {@code <a href="link">label</a>}. 139 * 140 * @param link the details for the link 141 * @param label the content for the link 142 * @return the link 143 */ 144 public Content createLink(DocLink link, Content label) { 145 return createLink(link, label, ""); 146 } 147 148 /** 149 * Creates a link of the form {@code <a href="path" title="title">label</a>}. 150 * 151 * @param link the details for the link 152 * @param label the content for the link 153 * @param title the title for the link 154 * @return the link 155 */ 156 public Content createLink(DocLink link, Content label, String title) { 157 var anchor = HtmlTree.A(link.relativizeAgainst(file).toString(), label); 158 if (title != null && title.length() != 0) { 159 anchor.put(HtmlAttr.TITLE, title); 160 } 161 return anchor; 162 } 163 164 /** 165 * Creates a link of the form {@code <a href="link" title="title" >label</a>}. 166 * If {@code style} is not null, it will be added as {@code class="style"} to the link. 167 * 168 * @param link the details for the link 169 * @param label the content for the link 170 * @param style the style for the link, or null 171 * @param title the title for the link 172 * @return the link 173 */ 174 public Content createLink(DocLink link, Content label, HtmlStyle style, 175 String title) { 176 return createLink(link, label, style, title, false); 177 } 178 179 /** 180 * Creates a link of the form {@code <a href="link" title="title">label</a>}. 181 * If {@code style} is not null, it will be added as {@code class="style"} to the link. 182 * 183 * @param link the details for the link 184 * @param label the content for the link 185 * @param style the style for the link, or null 186 * @param title the title for the link 187 * @param isExternal is the link external to the generated documentation 188 * @return the link 189 */ 190 public Content createLink(DocLink link, Content label, HtmlStyle style, 191 String title, boolean isExternal) { 192 var l = HtmlTree.A(link.relativizeAgainst(file).toString(), label); 193 if (style != null) { 194 l.setStyle(style); 195 } 196 if (title != null && title.length() != 0) { 197 l.put(HtmlAttr.TITLE, title); 198 } 199 if (isExternal) { 200 // Use addStyle as external links might have an explicit style set 201 // above as well. 202 l.addStyle(HtmlStyle.externalLink); 203 } 204 return l; 205 } 206 207 /** 208 * Creates an external link. 209 * 210 * @param link the details for the link 211 * @param label the content for the link 212 * @return the link 213 */ 214 public Content createExternalLink(DocLink link, Content label) { 215 return HtmlTree.A(link.relativizeAgainst(file).toString(), label) 216 .setStyle(HtmlStyle.externalLink); 217 } 218}