package org.ambraproject.wombat.freemarker;

import com.google.common.base.CharMatcher;
import com.google.common.base.Splitter;
import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * Applies standard formatting for abbreviations of authors' given names. Abbreviates names to initials, with hyphens
 * (or other dash characters) between names preserved as hyphens between initials.
 */
public class AbbreviatedNameDirective implements TemplateDirectiveModel {

  @Override
  public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body)
      throws TemplateException, IOException {
    StringWriter givenNames = new StringWriter();
    body.render(givenNames);
    String abbreviation = abbreviate(givenNames.toString());
    env.getOut().write(abbreviation);
  }

  private static final Splitter WHITESPACE_SPLITTER = Splitter.on(CharMatcher.WHITESPACE);
  private static final Pattern GIVEN_NAME_PATTERN = Pattern.compile(".*\\p{Pd}\\p{L}.*");
  private static final Pattern DASH_PATTERN = Pattern.compile("\\p{Pd}");

  private static String abbreviate(String rawGivenNameString) {
    Iterable<String> givenNames = WHITESPACE_SPLITTER.split(rawGivenNameString);
    StringBuilder abbreviation = new StringBuilder();
    for (String givenName : givenNames) {
      if (givenName.length() > 0) {
        if (GIVEN_NAME_PATTERN.matcher(givenName).matches()) {
          // Handle names with dash
          String[] sarr = DASH_PATTERN.split(givenName);
          for (int i = 0; i < sarr.length; i++) {
            if (i > 0) {
              abbreviation.append('-');
            }

            if (sarr[i].length() > 0) {
              abbreviation.append(sarr[i].charAt(0));
            }
          }
        } else {
          abbreviation.append(givenName.charAt(0));
        }
      }
    }

    return abbreviation.toString();
  }

}

@@ -1230,121 +1230,45 @@
 nal 
-Pattern GIVEN_NAME_PA
+Splitter DASH_SPLI
 TTER
-N
  = 
-Pattern.compile(%22.*%5C%5Cp%7BPd%7D%5C%5Cp%7BL%7D.*%22);%0A  private static final Pattern DASH_PATTERN = 
+Splitter.on(
 Patt
@@ -1289,16 +1289,17 @@
 %5Cp%7BPd%7D%22)
+)
 ;%0A%0A  pri
@@ -1544,168 +1544,42 @@
     
-if (givenName.length() %3E 0) %7B%0A        if (GIVEN_NAME_PATTERN.matcher(givenName).matches()) %7B%0A          // Handle names with dash%0A          String%5B%5D sarr
+Iterable%3CString%3E nameParts
  = DASH_
 PATT
@@ -1578,15 +1578,16 @@
 ASH_
-PA
+SPLI
 TTER
-N
 .spl
@@ -1611,213 +1611,75 @@
     
-    
 for (
-int i = 0; i %3C sarr.length; i++) %7B%0A            if (i %3E 0) %7B%0A              abbreviation.append('-');%0A            %7D%0A%0A            if (sarr%5Bi%5D.length() %3E 0) %7B%0A              abbreviation.append(sarr%5Bi%5D
+String namePart : nameParts) %7B%0A        char initial = namePart
 .cha
@@ -1676,33 +1676,32 @@
 mePart.charAt(0)
-)
 ;%0A            %7D%0A
@@ -1698,104 +1698,37 @@
     
-    %7D%0A          %7D%0A        %7D else %7B%0A          abbreviation.append(givenName.charAt(0));%0A        %7D
+abbreviation.append(initial);
 %0A   

Simplify AbbreviatedNameDirective.java