EmitterUtil.java

  1. package docsite.util;

  2. import java.nio.file.*;
  3. import java.util.regex.*;
  4. import static j2html.TagCreator.*;
  5. import docsite.*;
  6. import j2html.tags.specialized.*;

  7. public final class EmitterUtil {

  8.     private EmitterUtil() { /*avoid instantiation */ }


  9.     private static Pattern faPattern = Pattern.compile("(fa.?):([^:]+)");



  10.     public static ATag internalLink(String title, String url) {
  11.         return a(title).withClass("internal").withHref(url);
  12.     }


  13.     public static ATag internalLinkWithIcon(Path baseDir, String title, String url, String icon, ImageResolver images) {
  14.         return a()
  15.             .withClasses("label internal")
  16.             .withHref(url)
  17.             .with(
  18.                 icon(baseDir, icon,images),
  19.                 span(title)
  20.             );
  21.     }


  22.     public static ATag externalLinkWithIcon(Path baseDir, String title, String url, String icon, ImageResolver images) {
  23.         return a()
  24.             .withClasses("label external")
  25.             .withHref(url)
  26.             .withTarget("_blank")
  27.             .withRel("external noreferrer noopener nofollow")
  28.             .with(
  29.                 icon(baseDir, icon, images),
  30.                 span(title)
  31.             );
  32.     }


  33.     public static ATag externalLinkWithImage(Path baseDir, String title, String url, String icon, ImageResolver images) {
  34.         return a()
  35.             .withClasses("label external")
  36.             .withHref(url)
  37.             .withTarget("_blank")
  38.             .withRel("external noreferrer noopener nofollow")
  39.             .with(
  40.                 image(baseDir, icon, images),
  41.                 span(title)
  42.             );
  43.     }


  44.     public static ITag icon(Path baseDir, String icon, ImageResolver images) {
  45.         if (icon == null || icon.isBlank()) {
  46.             return i().withClass("hidden");
  47.         }
  48.         Matcher faMatcher = faPattern.matcher(icon);
  49.         if (faMatcher.matches()) {
  50.             return i().withClasses(faMatcher.group(1)+" fa-"+faMatcher.group(2));
  51.         }
  52.         if (Files.exists(baseDir.resolve(icon))) {
  53.             return i().withClass("external-icon").withStyle("background-image: url('"+images.imageFile(icon)+"')");
  54.         }
  55.         return i().withClass("external-icon").withStyle("background-image: url('"+icon+"')");
  56.     }


  57.     public static ImgTag image(Path baseDir, String source, ImageResolver images) {
  58.         if (source == null || source.isBlank()) {
  59.             return img().withClass("hidden");
  60.         }
  61.         if (Files.exists(baseDir.resolve(source))) {
  62.             return img().withSrc(images.imageFile(source));
  63.         }
  64.         return img().withSrc(source);
  65.     }


  66.     public static ATag externalLink(String title, String url) {
  67.         return a(title)
  68.             .withClass("external")
  69.             .withHref(url)
  70.             .withTarget("_blank")
  71.             .withRel("external noreferrer noopener nofollow");
  72.     }



  73.     public static String href(String name) {
  74.         return name.strip().toLowerCase().replace(" ", "-");
  75.     }


  76.     public static String href(Section section) {
  77.         if (section.type() == Section.SectionType.embedded) {
  78.             return href(section.name()+"/"+section.siteIndex());
  79.         } else {
  80.             return href(section.name());
  81.         }
  82.     }


  83.     public static String page(String name) {
  84.         return href(name)+ ".html";
  85.     }

  86.     public static String page(String name, SiteLanguage language) {
  87.         return language.isPrimary() ? page(name) : href(name)+ "_"+language.language()+".html";
  88.     }


  89.     public static LinkTag stylesheet(String href) {
  90.         return link().attr("href",href).attr("rel","stylesheet");
  91.     }


  92.     public static String withLanguage(SiteLanguage language, String path) {
  93.         if (language.isPrimary()) {
  94.             return path;
  95.         }
  96.         int extensionPosition = path.lastIndexOf('.');
  97.         if (extensionPosition == -1) {
  98.             return path+"_"+language.language();
  99.         } else {
  100.             return path.substring(0,extensionPosition)+"_"+language.language()+path.substring(extensionPosition);
  101.         }
  102.     }

  103. }